-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
232 lines (175 loc) · 5.75 KB
/
main.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
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
/*
ttyplay: plays audio on the serial port.
Take audio from TX and GND on serial connector.
License: GPL
credits:
libsamplerate / libsndfile, http://mega-nerd.com
serial routines copied blindly from Serial Programming HOWTO.
http://www.tldp.org/HOWTO/Serial-Programming-HOWTO
Maxim appnote #1870, for a simple diagram of a Sigma-Delta modulator
hackaday.com
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <sndfile.h>
#include <samplerate.h>
#include <string.h>
#define SOUND_BUFFER_LEN (0.25) // seconds
#define BAUDRATE 115200
#define SERIAL_BAUDRATE B115200
int main(int argc, char **argv) {
SNDFILE *soundfile = NULL;
SF_INFO info;
SNDFILE *soundfile_out = NULL;
SF_INFO info_out;
float integrator=0.0;
float error_signal=0.0;
float out=0.0;
int i,j,k;
int count;
int error;
int ttyfd=-1;
struct termios oldtio,newtio;
unsigned char outbyte;
SRC_STATE *samplerate;
SRC_DATA resample_data;
SRC_STATE *samplerate_out;
SRC_DATA resample_data_out;
info.format=0;
int quiet = 0;
char device[128];
char outfile[1024];
char infile[1024];
char output[1024];
int rate = 48000;
strcpy(device, "/dev/ttyS0");
strcpy(output, device);
int opt;
while ((opt = getopt(argc, argv, "qd:r:w:")) != -1) {
switch(opt) {
case 'd':
strcpy(device, optarg);
strcpy(output, device);
break;
case 'r':
rate = atoi(optarg);
break;
case 'w':
strcpy(outfile, optarg);
strcpy(output, outfile);
break;
case 'q':
quiet = 1;
break;
}
}
if (optind >= argc) {
fprintf(stderr, "Usage:\t%s [-q] [-r sample_rate] [-d device | -w out.wav] in.wav\n", argv[0]);
return 1;
} else strcpy(infile, argv[optind]);
if (!quiet)
fprintf(stderr,"Playing:\t%s\nOutput:\t\t%s\nSample rate:\t%d\n", infile, output, rate);
soundfile=sf_open(infile,SFM_READ,&info);
info_out.samplerate=rate;
info_out.channels=1;
info_out.format=SF_FORMAT_PCM_16|SF_FORMAT_WAV;
if (strlen(outfile) > 1) {
soundfile_out=sf_open(outfile,SFM_WRITE,&info_out);
if ((soundfile==NULL) || (soundfile_out==NULL)) {
fprintf(stderr,"Couldn't open that file. Sorry.\n");
return 1;
}
} else { // we're not writing an output file, try serial I/O
// blocking I/O blocks forever on open(), dunno why.
ttyfd = open(device, O_RDWR|O_NOCTTY|O_NONBLOCK);
if (ttyfd <0) {perror(device); exit(-1); }
tcgetattr(ttyfd,&oldtio); /* save current serial port settings */
bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
cfmakeraw(&newtio);
if (0!=cfsetospeed(&newtio,SERIAL_BAUDRATE)) { perror(device); exit(-1); }
if (0!=tcflush(ttyfd, TCIFLUSH)) { perror(device); exit(-1); }
if (0!=tcsetattr(ttyfd,TCSANOW,&newtio)) { perror(device); exit(-1); }
}
samplerate=src_new(SRC_LINEAR,1,&error);
if (error) {
fprintf(stderr,"Samplerate error. Sorry.\n");
return 1;
}
samplerate_out=src_new(SRC_SINC_FASTEST,1,&error);
if (error) {
fprintf(stderr,"Samplerate error. Sorry.\n");
return 1;
}
resample_data.data_in=(float *)malloc((int)(SOUND_BUFFER_LEN*info.samplerate*info.channels)*sizeof(float));
resample_data.data_out=(float *)malloc(2*(int)(SOUND_BUFFER_LEN*BAUDRATE)*sizeof(float));
resample_data.src_ratio=((float)BAUDRATE)/(float)info.samplerate;
resample_data.end_of_input=0;
resample_data.output_frames=(int)2*BAUDRATE*SOUND_BUFFER_LEN;
resample_data_out.data_in=(float *)malloc(2*BAUDRATE*sizeof(float));
resample_data_out.data_out=(float *)malloc(2*(int)(SOUND_BUFFER_LEN*rate)*sizeof(float));
resample_data_out.src_ratio=((float)rate)/((float)BAUDRATE);
resample_data_out.end_of_input=0;
resample_data_out.output_frames=(int)(2*rate*SOUND_BUFFER_LEN);
k=0;
while ((count=sf_readf_float(soundfile,resample_data.data_in,(int)(SOUND_BUFFER_LEN*info.samplerate)))) {
// make mono
for (j=0; j<count; j++) {
resample_data.data_in[j]=resample_data.data_in[j*info.channels];
for (i=1; i<info.channels; i++) {
resample_data.data_in[j]+=resample_data.data_in[j*info.channels+i];
}
resample_data.data_in[j]/=(float)info.channels;
}
resample_data.input_frames=count;
// following has a bug...
if (count<(int)(SOUND_BUFFER_LEN*info.samplerate)) {
resample_data.end_of_input=1;
resample_data_out.end_of_input=1;
}
src_process(samplerate,&resample_data);
// Now we have our input signal converted to the serial baudrate. All that's left is sigma-delta modulate
// and write to the serial port
outbyte=0;
for (j=0; j<resample_data.output_frames_gen; j++) {
error_signal=resample_data.data_out[j]-out;
integrator+=error_signal;
switch (k%10) {
case 0:
outbyte=0;
out=-1.0; //start bit
break;
case 9:
out=1.0; //stop bit
if (ttyfd>=0) {
while ( 1!=write(ttyfd,&outbyte,1)) { /* What's a spinloop among friends? */ ; }
}
break;
default:
out=(integrator>0) ? 1.0 : -1.0;
if (out>0.0) outbyte |= 1<<((k%10)-1);
}
resample_data_out.data_in[j]=out;
k++;
}
if (soundfile_out) {
resample_data_out.input_frames=resample_data.output_frames_gen;
src_process(samplerate_out,&resample_data_out);
i=sf_write_float(soundfile_out,
resample_data_out.data_out,
resample_data_out.output_frames_gen);
}
}
sf_close(soundfile);
if (soundfile_out) sf_close(soundfile_out);
// restore serial port settings
if (ttyfd>=0) {
tcsetattr(ttyfd,TCSANOW,&oldtio);
}
return 0;
}