-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaudio.c
238 lines (225 loc) · 4.71 KB
/
audio.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
233
234
235
236
237
238
/*
3psk - 3-pole Phase Shift Keying
Copyright (c) Edward Cree, 2012
Licensed under the GNU GPL v3+
audio: AUDio Input/Output subsystem
*/
#include "audio.h"
#include <math.h> // for floor()
int init_audiorx(audiobuf *a, unsigned int audiobuflen, unsigned int sdlbuflen, FILE *wav)
{
SDL_AudioSpec expected, result;
a->audiobuflen=audiobuflen;
if(wav)
{
#ifdef WAVLIB
a->wav=wav;
int e=read_wh44(wav, &a->wavhdr);
if(e)
{
fprintf(stderr, "Failed to read WAV header\n");
return(e);
}
#else /* !WAVLIB */
fprintf(stderr, "WAV support (--rx) not compiled in!\n");
return(1);
#endif
}
else
{
a->wav=NULL;
}
if(SDL_InitAudioIn()<0)
{
fprintf(stderr, "Failed to initialise SDL_audioin: %s\n", SDL_GetError());
return(1);
}
expected.format=AUDIO_S16;
expected.silence=0;
expected.freq=sample_rate;
expected.channels=1;
expected.samples=sdlbuflen;
expected.callback=rxaudio;
expected.userdata=a;
a->buf=malloc(a->audiobuflen*sizeof(int16_t));
if(!a->buf)
{
perror("malloc");
return(1);
}
for(unsigned int i=0;i<a->audiobuflen;i++)
a->buf[i]=0;
a->rp=a->wp=0;
if(wav)
{
#ifdef WAVLIB
a->srate=a->wavhdr.sample_rate;
#endif
}
else
{
a->srate=sample_rate; // should use result.freq, but SDL_audioin doesn't populate it
}
if(SDL_OpenAudioIn(&expected,&result)<0)
{
fprintf(stderr, "Can't open audio input: %s\n", SDL_GetError());
return(1);
}
if(result.format!=expected.format)
{
fprintf(stderr, "Warning, got the wrong audio format; strange things may happen\n");
fprintf(stderr, "\tRequested %04x, got %04x\n", expected.format, result.format);
#ifndef WINDOWS
fprintf(stderr, "\t(We may be ok; SDL_audioin reports bogus values)\n");
#endif
}
if(result.freq!=expected.freq)
{
fprintf(stderr, "Warning, got the wrong sample rate; strange things may happen\n");
fprintf(stderr, "\tRequested %uHz, got %uHz\n", expected.freq, result.freq);
#ifndef WINDOWS
fprintf(stderr, "\t(We may be ok; SDL_audioin reports bogus values)\n");
#endif
}
SDL_PauseAudioIn(0);
return(0);
}
void rxaudio(void *udata, Uint8 *stream, int len)
{
audiobuf *a=udata;
Sint16 *sData=(Sint16 *)stream;
for(int i=0;i<len>>1;i++)
{
unsigned int newwp=(a->wp+1)%a->audiobuflen;
while(a->rp==newwp)
{
a->underrun=false;
SLEEP;
}
a->buf[newwp]=sData[i];
a->wp=newwp;
}
}
void stop_audiorx(audiobuf *a)
{
a->rp=-1;
SDL_CloseAudioIn();
SDL_QuitAudioIn();
}
int rxsample(audiobuf *a, int16_t *samp)
{
if(a->wav)
{
#ifdef WAVLIB
if(feof(a->wav))
{
fclose(a->wav);
a->wav=NULL;
}
else
{
double v=read_sample(a->wavhdr, a->wav);
int16_t i=floor((v-0.5)*65535);
if(samp) *samp=i;
return(0);
}
#else
fprintf(stderr, "a->wav was set for some reason but no WAVLIB, so unsetting\n");
a->wav=NULL;
#endif
}
unsigned int newrp=(a->rp+1)%a->audiobuflen;
if(a->wp==newrp)
{
a->underrun=true;
return(1);
}
if(samp)
*samp=a->buf[newrp];
a->rp=newrp;
return(0);
}
int init_audiotx(audiobuf *a, unsigned int audiobuflen, unsigned int sdlbuflen)
{
if(SDL_InitSubSystem(SDL_INIT_AUDIO)<0)
{
fprintf(stderr, "Failed to initialise SDL_audio: %s\n", SDL_GetError());
return(1);
}
a->audiobuflen=audiobuflen;
SDL_AudioSpec expected;
expected.format=AUDIO_S16;
expected.silence=0;
expected.freq=sample_rate;
expected.channels=1;
expected.samples=sdlbuflen;
expected.callback=txaudio;
expected.userdata=a;
a->buf=malloc(a->audiobuflen*sizeof(int16_t));
if(!a->buf)
{
perror("malloc");
return(1);
}
for(unsigned int i=0;i<a->audiobuflen;i++)
a->buf[i]=0;
a->rp=a->wp=0;
SDL_AudioSpec result;
if(SDL_OpenAudio(&expected, &result)<0)
{
fprintf(stderr, "Failed to open audio: %s\n", SDL_GetError());
return(1);
}
if(result.format!=expected.format)
{
fprintf(stderr, "Failed to get the right audio format\n");
return(1);
}
if(result.freq!=expected.freq)
{
fprintf(stderr, "Warning, got the wrong sample rate; strange things may happen\n");
fprintf(stderr, "\tRequested %uHz, got %uHz\n", expected.freq, result.freq);
}
a->srate=result.freq;
SDL_PauseAudio(0);
return(0);
}
void txaudio(void *udata, Uint8 *stream, int len)
{
audiobuf *a=udata;
Sint16 *sData=(Sint16 *)stream;
for(int i=0;i<len>>1;i++)
{
unsigned int newrp=(a->rp+1)%a->audiobuflen;
while(a->wp==newrp)
{
a->underrun=true;
SLEEP;
}
sData[i]=a->buf[newrp];
a->rp=newrp;
}
}
void stop_audiotx(audiobuf *a)
{
a->wp=-1;
SDL_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
void txsample(audiobuf *a, int16_t samp)
{
unsigned int newwp=(a->wp+1)%a->audiobuflen;
while(a->rp==newwp)
{
a->underrun=false;
SLEEP;
}
a->buf[newwp]=samp;
a->wp=newwp;
}
bool cantx(audiobuf *a)
{
bool can=a->rp!=((a->wp+1)%a->audiobuflen);
if(!can) a->underrun=false;
return(can);
}