forked from windytan/redsea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownmix.c
45 lines (35 loc) · 997 Bytes
/
downmix.c
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
/* downmix.c -- part of redsea RDS decoder (c) OH2-250
*
* 19 kHz downconverter
* PCM is 48k 16bit single-channel little-endian signed-integer
*
* Creates heterodynes of the RDS signal; result needs to be downpass filtered
*
* */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#define BUFLEN 2048
int main() {
unsigned short bufptr = 0;
double freq = 2 * M_PI * (19000.0 / 48000.0); // radians per sample
double phase = -M_PI;
short int pcm;
short int outbuf[BUFLEN];
/* Read PCM data from stdin */
while (read(0, &pcm, 2)) {
/* Downmix */
outbuf[bufptr] = pcm * cos(phase) + .5;
/* Advance local oscillator phase */
phase += freq;
if (phase >= M_PI) phase -= 2 * M_PI;
/* Output PCM when buffer is full */
if (++bufptr == BUFLEN) {
if (!write(1, &outbuf, 2 * BUFLEN)) return (EXIT_FAILURE);
fflush(stdout);
bufptr = 0;
}
}
return (EXIT_SUCCESS);
}