Progetto

Generale

Profilo

Wiki » meta-ser.c

Programma in C per estrarre i metadati dal flusso mp3 - Andrea Belloni, 05-12-2014 14:36

 
1
#include <stdio.h>
2
#include <regex.h>
3
#include <string.h>
4
#include <unistd.h>  /* UNIX standard function definitions */
5
#include <fcntl.h>   /* File control definitions */
6
#include <errno.h>   /* Error number definitions */
7
#include <termios.h> /* POSIX terminal control definitions */
8

    
9
#define BUFF_SIZE 4096
10

    
11
/*
12
 * 'open_port()' - Open serial port 1.
13
 *
14
 * Returns the file descriptor on success or -1 on error.
15
 */
16

    
17
int open_port(void)
18
{
19
  int fd; /* File descriptor for the port */
20

    
21
  fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
22
  if (fd == -1)
23
  {
24
   /*
25
    * Could not open the port.
26
    */
27

    
28
    perror("open_port: Unable to open /dev/ttyS0 - ");
29
  }
30
  else
31
    fcntl(fd, F_SETFL, 0);
32

    
33
  return (fd);
34
}
35

    
36
int main (void)
37
{
38
  regex_t regexST, regexnm, regexgn, regexmi, regexbr, regexrn;
39
  unsigned char buffer[BUFF_SIZE];
40
  unsigned char line[256];
41
  unsigned char *meta[2];
42
  unsigned char lcd[100];
43
  unsigned char icyok = 0;
44
  int count, done = 0;
45
  int reti, br, metaint = 0;
46
  int i, m, n;
47
  int fds, len;
48
  int bsize = BUFF_SIZE;
49
  char *cp;
50

    
51
  fds = open_port();
52

    
53
  usleep(100000);
54

    
55
  sprintf(lcd, "NM24                    ");
56
  count = write(fds, lcd, 24);
57
  if (count < 0)
58
    fputs("Error: write on ttyS0 failed!\n", stderr);
59

    
60
  usleep(100000);
61

    
62
  sprintf(lcd, "ST24                    ");
63
  count = write(fds, lcd, 24);
64
  if (count < 0)
65
    fputs("Error: write on ttyS0 failed!\n", stderr);
66

    
67
  usleep(100000);
68

    
69
  reti = regcomp(&regexST, "StreamTitle", 0);
70
  reti = regcomp(&regexnm, "^icy-name", 0);
71
  reti = regcomp(&regexgn, "^icy-genre", 0);
72
  reti = regcomp(&regexmi, "^icy-metaint", 0);
73
  reti = regcomp(&regexbr, "^icy-br", 0);
74
  reti = regcomp(&regexrn, "^\r\n$", 0);
75

    
76
  cp = fgets(line, 256, stdin);
77
  while (cp != NULL && !done) {
78
    reti = regexec(&regexnm, line, 0, NULL, 0);
79
    if (!reti) {
80
      //fputs(line, stderr);
81
      meta[0] = strtok(line, ":");
82
      meta[1] = strtok(NULL, "\r");
83
      fprintf(stderr, "name=%s\n", meta[1]);
84

    
85
      len = strlen(meta[1]);
86
      if (len > 95) {
87
        len = 95;
88
      }
89
      snprintf(lcd, 100, "NM%02i%s", len + 4, meta[1]);
90
      count = write(fds, lcd, len + 4);
91
      if (count < 0)
92
        fputs("Error: write on ttyS0 failed!\n", stderr);
93

    
94
      usleep(1000);
95
    }
96

    
97
    reti = regexec(&regexgn, line, 0, NULL, 0);
98
    if (!reti) {
99
      //fputs(line, stderr);
100
      meta[0] = strtok(line, ":");
101
      meta[1] = strtok(NULL, "\r");
102
      fprintf(stderr, "genre=%s\n", meta[1]);
103
    }
104

    
105
    reti = regexec(&regexmi, line, 0, NULL, 0);
106
    if (!reti) {
107
      //fputs(line, stderr);
108
      meta[0] = strtok(line, ":");
109
      meta[1] = strtok(NULL, "\r");
110
      metaint = atoi(meta[1]);
111
      fprintf(stderr, "metaint=%i\n", metaint);
112
    }
113

    
114
    reti = regexec(&regexbr, line, 0, NULL, 0);
115
    if (!reti) {
116
      //fputs(line, stderr);
117
      meta[0] = strtok(line, ":");
118
      meta[1] = strtok(NULL, "\r");
119
      br = atoi(meta[1]);
120
      fprintf(stderr, "br=%i\n", br);
121
    }
122

    
123
    reti = regexec(&regexrn, line, 0, NULL, 0);
124
    if (!reti) {
125
      done = 1;
126
      fputs("\n", stderr);
127
    }
128

    
129
    if (!done) {
130
      cp = fgets(line, 256, stdin);
131
    }
132
  }
133

    
134
  if (metaint == 0) {
135
    n = 4;
136
  } else if (metaint > 0) {
137
    if ((metaint % bsize) != 0) {
138
      bsize = 4000;
139
      while ((metaint % bsize) != 0) {
140
        bsize -= 1000;
141
        if (bsize == 0) {
142
          fprintf(stderr, "Error: metaint is not multiple of bsize\n");
143
          return 1;
144
        }
145
      }
146
    }
147
    n = metaint / bsize;
148
  } else {
149
    fprintf(stderr, "Error: metaint less then 0\n");
150
  }
151

    
152
  while (!feof(stdin)) {
153
    for (i = 0; i < n; i++)
154
    {
155
      count = fread(buffer, 1, bsize, stdin);
156
      fwrite(buffer, 1, count, stdout);
157
    }
158

    
159
    if (metaint > 0) {
160
      count = fread(buffer, 1, 1, stdin);
161
      m = buffer[0];
162

    
163
      if (m != 0) {
164
        count = fread(buffer, 1, m * 16, stdin);
165
        //fwrite(buffer, 1, m * 16, stderr);
166
        //fputs("\n", stderr);
167
        reti = regexec(&regexST, buffer, 0, NULL, 0);
168
        if (!reti) {
169
          meta[0] = strtok(buffer, "'");
170
          meta[1] = strtok(NULL, "'");
171
          fputs("StreamTitle=", stderr);
172
          fputs(meta[1], stderr);
173
          fputs("\n", stderr);
174

    
175
          len = strlen(meta[1]);
176
          if (len > 95) {
177
            len = 95;
178
          }
179
          snprintf(lcd, 100, "ST%02i%s", len + 4, meta[1]);
180
          count = write(fds, lcd, len + 4);
181
          if (count < 0)
182
            fputs("Error: write on ttyS0 failed!\n", stderr);
183

    
184
          usleep(1000);
185
        }
186
      }
187
    }
188
  }
189

    
190
  return 0;
191
}
(6-6/6)