-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaudio.c
More file actions
616 lines (498 loc) · 16.4 KB
/
audio.c
File metadata and controls
616 lines (498 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#include <fcntl.h>
#include <getopt.h>
#include <sndfile.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "audio.h"
#include "libmsr.h"
/********** function wrappers **********/
/* allocate memory with out of memory checking
[size] allocate size bytes
returns pointer to allocated memory */
void *msr_malloc(size_t size)
{
void *ptr;
ptr = malloc(size);
if (ptr == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(EXIT_FAILURE);
}
return ptr;
}
/* reallocate memory with out of memory checking
[ptr] memory to reallocate
[size] allocate size bytes
returns pointer to reallocated memory */
void *msr_realloc(void *ptr, size_t size)
{
void *nptr;
nptr = realloc(ptr, size);
if (nptr == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(EXIT_FAILURE);
}
return nptr;
}
/* copy a string with out of memory checking
[string] string to copy
returns newly allocated copy of string */
char *msr_strdup(char *string)
{
char *ptr;
ptr = msr_malloc(strlen(string) + 1);
strcpy(ptr, string);
return ptr;
}
/* read with error checking
[fd] file descriptor to read from
[buf] buffer
[count] bytes to read
returns bytes read */
ssize_t msr_read(int fd, void *buf, size_t count)
{
int retval;
retval = read(fd, buf, count);
if (retval == -1) {
perror("read()");
exit(EXIT_FAILURE);
}
return retval;
}
/********** end function wrappers **********/
/********** string functions **********/
/* returns a pointer to the reversed string
[string] string to reverse
returns newly allocated reversed string */
char *msr_reverse_string(char *string)
{
char *rstring;
int i, string_len;
string_len = strlen(string); /* record string length */
/* allocate memory for rstring */
rstring = msr_malloc(string_len + 1);
for (i = 0; i < string_len; i++) /* reverse string and store in rstring */
rstring[i] = string[string_len - i - 1];
rstring[string_len] = '\0'; /* terminate rstring */
return rstring; /* return rstring */
}
/********** end string functions **********/
/********** parsing functions **********/
/* parse ABA format raw bits and return a pointer to the decoded string
[bitstring] string to decode
returns decoded string */
char *msr_parse_ABA(char *bitstring)
{
char *decoded_string, *lrc_start, *start_decode, *string;
char lrc[] = {1, 1, 0, 1, 0}; /* initial condition is LRC of the start
sentinel */
int asciichr, charcnt = 0, i, j;
/* make a copy of bitstring and store it in string */
string = msr_strdup(bitstring);
/* look for start sentinel */
if ((start_decode = strstr(string, "11010")) == NULL) {
free(string); /* free string memory */
return NULL; /* could not find start sentinel */
}
/* set start_decode to first bit (start of first byte) after start
sentinel */
start_decode += 5;
/* look for end sentinel */
if ((lrc_start = strstr(string, "11111")) == NULL) {
free(string); /* free string memory */
return NULL; /* could not find end sentinel */
}
/* must be a multiple of 5 */
while ((strlen(start_decode) - strlen(lrc_start)) % 5) /* search again */
if ((lrc_start = strstr(++lrc_start, "11111")) == NULL) {
free(string); /* free string memory */
return NULL; /* could not find end sentinel */
}
lrc_start[0] = '\0'; /* terminate start_decode at end sentinel */
lrc_start += 5; /* set the pointer to the LRC */
if (lrc_start[5] != '\0') /* terminate LRC if not already */
lrc_start[5] = '\0';
/* allocate memory for decoded_string */
decoded_string = msr_malloc((strlen(start_decode) / 5) + 3);
decoded_string[charcnt++] = ';'; /* add start sentinel */
/* decode each set of bits, check parity, check LRC, and add to
decoded_string */
while (strlen(start_decode)) {
for (i = 0, j = 0; i < 4; i++) /* check parity */
if (start_decode[i] == '1')
j++;
if (((j % 2) && start_decode[4] == '1') ||
(!(j % 2) && start_decode[4] == '0')) {
free(string); /* free string memory */
free(decoded_string); /* free decoded_string memory */
return NULL; /* failed parity check */
}
asciichr = 48; /* generate ascii value from bits */
asciichr += start_decode[0] == '1' ? 1 : 0;
asciichr += start_decode[1] == '1' ? 2 : 0;
asciichr += start_decode[2] == '1' ? 4 : 0;
asciichr += start_decode[3] == '1' ? 8 : 0;
decoded_string[charcnt++] = asciichr; /* add character to decoded_string */
for (i = 0; i < 4; i++) /* calculate LRC */
lrc[i] = lrc[i] ^ (start_decode[i] == '1') ? 1 : 0;
start_decode += 5; /* increment start_decode to next byte */
}
decoded_string[charcnt++] = '?'; /* add end sentinel */
decoded_string[charcnt] = '\0'; /* terminate decoded_string */
for (i = 0; i < 4; i++) /* calculate CRC of end sentinel */
lrc[i] = lrc[i] ^ 1;
for (i = 0, j = 0; i < 4; i++) /* set LRC parity bit */
if (lrc[i])
j++;
if (!(j % 2))
lrc[4] = 1;
else
lrc[4] = 0;
for (i = 0; i < 5; i++) /* check CRC */
if ((lrc[i] && lrc_start[i] == '0') ||
(!lrc[i] && lrc_start[i] == '1')) {
free(string); /* free string memory */
free(decoded_string); /* free decoded_string memory */
return NULL; /* failed CRC check */
}
free(string); /* free string memory */
return decoded_string;
}
/* parse IATA format raw bits and return a pointer to the decoded string
[bitstring] string to decode
returns decoded string */
char *msr_parse_IATA(char *bitstring)
{
char *decoded_string, *lrc_start, *start_decode, *string;
char lrc[] = {1, 0, 1, 0, 0, 0, 1}; /* initial condition is LRC of the start
sentinel */
int asciichr, charcnt = 0, i, j;
/* make a copy of bitstring and store it in string */
string = msr_strdup(bitstring);
/* look for start sentinel */
if ((start_decode = strstr(string, "1010001")) == NULL) {
free(string); /* free string memory */
return NULL; /* could not find start sentinel */
}
/* set start_decode to first bit (start of first byte) after start
sentinel */
start_decode += 7;
/* look for end sentinel */
if ((lrc_start = strstr(string, "1111100")) == NULL) {
free(string); /* free string memory */
return NULL; /* could not find end sentinel */
}
/* must be a multiple of 7 */
while ((strlen(start_decode) - strlen(lrc_start)) % 7)
/* search again */
if ((lrc_start = strstr(++lrc_start, "1111100")) == NULL) {
free(string); /* free string memory */
return NULL; /* could not find end sentinel */
}
lrc_start[0] = '\0'; /* terminate start_decode at end sentinel */
lrc_start += 7; /* set the pointer to the LRC */
if (lrc_start[7] != '\0') /* terminate LRC if not already */
lrc_start[7] = '\0';
/* allocate memory for decoded_string */
decoded_string = msr_malloc((strlen(start_decode) / 7) + 3);
decoded_string[charcnt++] = '%'; /* add start sentinel */
/* decode each set of bits, check parity, check LRC, and add to
decoded_string */
while (strlen(start_decode)) {
for (i = 0, j = 0; i < 6; i++) /* check parity */
if (start_decode[i] == '1')
j++;
if (((j % 2) && start_decode[6] == '1') ||
(!(j % 2) && start_decode[6] == '0')) {
free(string); /* free string memory */
free(decoded_string); /* free decoded_string memory */
return NULL; /* failed parity check */
}
asciichr = 32; /* generate ascii value from bits */
asciichr += start_decode[0] == '1' ? 1 : 0;
asciichr += start_decode[1] == '1' ? 2 : 0;
asciichr += start_decode[2] == '1' ? 4 : 0;
asciichr += start_decode[3] == '1' ? 8 : 0;
asciichr += start_decode[4] == '1' ? 16 : 0;
asciichr += start_decode[5] == '1' ? 32 : 0;
decoded_string[charcnt++] = asciichr; /* add character to decoded_string */
for (i = 0; i < 6; i++) /* calculate LRC */
lrc[i] = lrc[i] ^ (start_decode[i] == '1') ? 1 : 0;
start_decode += 7; /* increment start_decode to next byte */
}
decoded_string[charcnt++] = '?'; /* add end sentinel */
decoded_string[charcnt] = '\0'; /* terminate decoded_string */
for (i = 0; i < 5; i++) /* calculate CRC of end sentinel */
lrc[i] = lrc[i] ^ 1;
lrc[5] = lrc[5] ^ 0;
for (i = 0, j = 0; i < 6; i++) /* set LRC parity bit */
if (lrc[i])
j++;
if (!(j % 2))
lrc[6] = 1;
else
lrc[6] = 0;
for (i = 0; i < 7; i++) /* check CRC */
if ((lrc[i] && lrc_start[i] == '0') ||
(!lrc[i] && lrc_start[i] == '1')) {
free(string); /* free string memory */
free(decoded_string); /* free decoded_string memory */
return NULL; /* failed CRC check */
}
free(string); /* free string memory */
return decoded_string;
}
/********** end parsing functions **********/
/********** dsp functions **********/
/* sets the device parameters
[fd] file descriptor to set ioctls on
[verbose] prints verbose messages if true
returns sample rate */
int msr_dsp_init(int fd)
{
int ch, fmt, sr;
/* set audio format */
fmt = AFMT_S16_LE;
if (ioctl(fd, SNDCTL_DSP_SETFMT, &fmt) == -1) {
perror("SNDCTL_DSP_SETFMT");
exit(EXIT_FAILURE);
}
if (fmt != AFMT_S16_LE) {
fprintf(stderr, "*** Error: Device does not support AFMT_S16_LE\n");
exit(EXIT_FAILURE);
}
/* set audio channels */
ch = 0;
if (ioctl(fd, SNDCTL_DSP_STEREO, &ch) == -1) {
perror("SNDCTL_DSP_STEREO");
exit(EXIT_FAILURE);
}
if (ch != 0) {
fprintf(stderr, "*** Error: Device does not support monaural recording\n");
exit(EXIT_FAILURE);
}
/* set sample rate */
sr = SAMPLE_RATE;
if (ioctl(fd, SNDCTL_DSP_SPEED, &sr) == -1) {
perror("SNDCTL_DSP_SPEED");
exit(EXIT_FAILURE);
}
if (sr != SAMPLE_RATE)
fprintf(stderr, "*** Warning: Highest supported sample rate is %d\n", sr);
return sr;
}
/* prints the maximum dsp level to aid in setting the silence threshold
[fd] file descriptor to read from
[sample_rate] sample rate of device */
void msr_print_max_level(int fd, int sample_rate)
{
int i;
short int buf, last = 0;
printf("Terminating after %d seconds...\n", MAX_TERM);
for (i = 0; i < sample_rate * MAX_TERM; i++) {
/* read from fd */
msr_read(fd, &buf, sizeof (short int));
/* take absolute value */
if (buf < 0)
buf = -buf;
/* print if highest level */
if (buf > last) {
printf("Maximum level: %d\r", buf);
fflush(stdout);
last = buf;
}
}
printf("\n");
}
/* finds the maximum value in sample
** global **
[sample] sample
[sample_size] number of frames in sample */
short int msr_evaluate_max(void)
{
int i;
short int max = 0;
for (i = 0; i < audio_sample_size; i++) {
if (audio_sample[i] > max)
max = audio_sample[i];
}
return max;
}
/* pauses until the dsp level is above the silence threshold
[fd] file descriptor to read from
[silence_thres] silence threshold */
void msr_silence_pause(int fd, int silence_thres)
{
short int buf = 0;
/* loop while silent */
while (buf < silence_thres) {
/* read from fd */
msr_read(fd, &buf, sizeof (short int));
/* absolute value */
if (buf < 0)
buf = -buf;
}
}
/* gets a sample, terminating when the input goes below the silence threshold
[fd] file descriptor to read from
[sample_rate] sample rate of device
[silence_thres] silence threshold
** global **
[sample] sample
[sample_size] number of frames in sample */
void msr_get_dsp(int fd, int sample_rate, int silence_thres)
{
int count = 0, eos = 0, i;
short buf;
audio_sample_size = 0;
/* wait for sample */
msr_silence_pause(fd, silence_thres);
while (!eos) {
/* fill buffer */
audio_sample = msr_realloc(audio_sample, sizeof (short int) * (BUF_SIZE * (count + 1)));
for (i = 0; i < BUF_SIZE; i++) {
msr_read(fd, &buf, sizeof (short int));
audio_sample[i + (count * BUF_SIZE)] = buf;
}
count++;
audio_sample_size = count * BUF_SIZE;
/* check for silence */
eos = 1;
if (audio_sample_size > (sample_rate * END_LENGTH) / 1000) {
for (i = 0; i < (sample_rate * END_LENGTH) / 1000; i++) {
buf = audio_sample[(count * BUF_SIZE) - i - 1];
if (buf < 0)
buf = -buf;
if (buf > silence_thres)
eos = 0;
}
} else
eos = 0;
}
}
/********** end dsp functions **********/
/********** begin sndfile functions **********/
/* open the file
[fd] file to open
[verbose] verbosity flag
** global **
[sample_size] number of frames in the file */
SNDFILE *msr_sndfile_init(int fd)
{
SNDFILE *sndfile;
SF_INFO sfinfo;
/* clear sfinfo structure */
memset(&sfinfo, 0, sizeof(sfinfo));
/* set sndfile from file descriptor */
sndfile = sf_open_fd(fd, SFM_READ, &sfinfo, 0);
if (sndfile == NULL) {
fprintf(stderr, "*** Error: sf_open_fd() failed\n");
exit(EXIT_FAILURE);
}
/* this was if(verbose) but might be useful...*/
/* print some statistics
fprintf(stderr, "*** Input file format:\n"
" Frames: %i\n"
" Sample Rate: %i\n"
" Channels: %i\n"
" Format: 0x%08x\n"
" Sections: %i\n"
" Seekable: %i\n",
(int)sfinfo.frames, sfinfo.samplerate, sfinfo.channels,
sfinfo.format, sfinfo.sections, sfinfo.seekable);
}
*/
/* ensure that the file is monaural */
if (sfinfo.channels != 1) {
fprintf(stderr, "*** Error: Only monaural files are supported\n");
exit(EXIT_FAILURE);
}
/* set sample size */
audio_sample_size = sfinfo.frames;
return sndfile;
}
/* read in data from libsndfile
[sndfile] SNDFILE pointer from sf_open() or sf_open_fd()
** global **
[sample] sample
[sample_size] number of frames in sample */
void msr_get_sndfile(SNDFILE *sndfile)
{
sf_count_t count;
/* allocate memory for sample */
audio_sample = msr_malloc(sizeof(short int) * audio_sample_size);
/* read in sample */
count = sf_read_short(sndfile, audio_sample, audio_sample_size);
if (count != audio_sample_size) {
fprintf(stderr, "*** Warning: expected %i frames, read %i.\n",
audio_sample_size, (int)count);
audio_sample_size = count;
}
}
/********** end sndfile functions **********/
/* decodes aiken biphase and prints binary
[freq_thres] frequency threshold
** global **
[sample] sample
[sample_size] number of frames in sample */
void msr_decode_aiken_biphase(int freq_thres, int silence_thres)
{
int i = 0, peak = 0, ppeak = 0;
int *peaks = NULL, peaks_size = 0;
int zerobl;
/* absolute value */
for (i = 0; i < audio_sample_size; i++)
if (audio_sample[i] < 0)
audio_sample[i] = -audio_sample[i];
/* store peak differences */
i = 0;
while (i < audio_sample_size) {
/* old peak value */
ppeak = peak;
/* find peaks */
while (i < audio_sample_size && audio_sample[i] <= silence_thres)
i++;
peak = 0;
while (i < audio_sample_size && audio_sample[i] > silence_thres) {
if (audio_sample[i] > audio_sample[peak])
peak = i;
i++;
}
if (peak - ppeak > 0) {
peaks = msr_realloc(peaks, sizeof(int) * (peaks_size + 1));
peaks[peaks_size] = peak - ppeak;
peaks_size++;
}
}
/* decode aiken biphase allowing for
frequency deviation based on freq_thres */
/* ignore first two peaks and last peak */
if (peaks_size < 2) {
fprintf(stderr, "*** Error: No data detected\n");
exit(EXIT_FAILURE);
}
zerobl = peaks[2];
for (i = 2; i < peaks_size - 1; i++) {
if (peaks[i] < ((zerobl / 2) + (freq_thres * (zerobl / 2) / 100)) &&
peaks[i] > ((zerobl / 2) - (freq_thres * (zerobl / 2) / 100))) {
if (peaks[i + 1] < ((zerobl / 2) + (freq_thres * (zerobl / 2) / 100)) &&
peaks[i + 1] > ((zerobl / 2) - (freq_thres * (zerobl / 2) / 100))) {
printf("1");
zerobl = peaks[i] * 2;
i++;
}
} else if (peaks[i] < (zerobl + (freq_thres * zerobl / 100)) &&
peaks[i] > (zerobl - (freq_thres * zerobl / 100))) {
printf("0");
#ifndef DISABLE_VC
zerobl = peaks[i];
#endif
}
}
printf("\n");
}