Rev 5603 | Rev 6117 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4438 | Serge | 1 | |
2 | #include |
||
6106 | serge | 3 | #include |
4 | #include |
||
4438 | Serge | 5 | #include |
6 | #include |
||
6106 | serge | 7 | #include |
8 | |||
4438 | Serge | 9 | |
5021 | Serge | 10 | #include "sound.h" |
4438 | Serge | 11 | #include "fplay.h" |
12 | |||
13 | |||
14 | |||
15 | |||
16 | |||
17 | int resampler_size; |
||
18 | volatile int sound_level_0; |
||
19 | volatile int sound_level_1; |
||
20 | |||
21 | |||
22 | volatile enum player_state decoder_state; |
||
23 | volatile enum player_state sound_state; |
||
24 | |||
25 | |||
5603 | serge | 26 | |
4438 | Serge | 27 | |
28 | |||
29 | |||
30 | int sample_rate; |
||
31 | |||
32 | |||
33 | double audio_base = -1.0; |
||
34 | |||
35 | |||
36 | |||
37 | |||
38 | { |
||
39 | int err; |
||
40 | int version =-1; |
||
41 | char *errstr; |
||
42 | |||
43 | |||
44 | { |
||
45 | errstr = "Sound service not installed\n\r"; |
||
46 | goto exit_whith_error; |
||
47 | }; |
||
48 | |||
49 | |||
50 | (SOUND_VERSION<(version >> 16))) |
||
51 | { |
||
52 | errstr = "Sound service version mismatch\n\r"; |
||
53 | goto exit_whith_error; |
||
54 | } |
||
55 | |||
56 | |||
57 | |||
58 | |||
59 | |||
60 | |||
61 | |||
62 | |||
63 | |||
64 | |||
65 | return 0; |
||
66 | }; |
||
67 | |||
68 | |||
69 | { |
||
70 | SetVolume(hBuff, left, right); |
||
71 | }; |
||
72 | |||
73 | |||
74 | static double audio_delta; |
||
75 | static double last_time_stamp; |
||
76 | |||
77 | |||
78 | |||
79 | { |
||
80 | double tstamp; |
||
81 | |||
82 | |||
83 | return tstamp - audio_delta; |
||
84 | }; |
||
85 | |||
86 | |||
87 | { |
||
88 | static struct SwrContext *swr_ctx; |
||
89 | static int64_t src_layout; |
||
90 | static int src_freq; |
||
91 | static int src_channels; |
||
92 | static enum AVSampleFormat src_fmt = -1; |
||
93 | static AVFrame *aFrame; |
||
94 | |||
95 | |||
96 | AVPacket pkt_tmp; |
||
97 | int64_t dec_channel_layout; |
||
98 | int len, len2; |
||
99 | int got_frame; |
||
100 | int data_size; |
||
101 | |||
102 | |||
103 | return -1; |
||
104 | |||
105 | |||
106 | return 0; |
||
107 | |||
108 | |||
109 | { |
||
110 | if (!(aFrame = avcodec_alloc_frame())) |
||
111 | return -1; |
||
112 | } else |
||
113 | avcodec_get_frame_defaults(aFrame); |
||
114 | |||
115 | |||
116 | |||
117 | |||
118 | { |
||
119 | data_size = 192000; |
||
120 | |||
121 | |||
122 | len = avcodec_decode_audio4(ctx, aFrame, &got_frame, &pkt_tmp); |
||
123 | |||
124 | |||
125 | { |
||
126 | char *samples; |
||
127 | int ch, plane_size; |
||
128 | int planar = av_sample_fmt_is_planar(ctx->sample_fmt); |
||
129 | int data_size = av_samples_get_buffer_size(&plane_size, ctx->channels, |
||
130 | aFrame->nb_samples, |
||
131 | ctx->sample_fmt, 1); |
||
132 | |||
133 | |||
134 | // { |
||
135 | // if (pkt.pts != AV_NOPTS_VALUE) |
||
136 | // audio_base = get_audio_base() * pkt.pts; |
||
137 | // printf("audio base %f\n", audio_base); |
||
138 | // }; |
||
139 | |||
140 | |||
141 | pkt_tmp.size -= len; |
||
142 | |||
143 | |||
144 | (aFrame->channel_layout && aFrame->channels == av_get_channel_layout_nb_channels(aFrame->channel_layout)) ? |
||
145 | aFrame->channel_layout : av_get_default_channel_layout(aFrame->channels); |
||
146 | |||
147 | |||
148 | dec_channel_layout != src_layout || |
||
149 | aFrame->sample_rate != src_freq || |
||
150 | !swr_ctx) |
||
151 | { |
||
152 | swr_free(&swr_ctx); |
||
153 | swr_ctx = swr_alloc_set_opts(NULL, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, |
||
154 | aFrame->sample_rate, dec_channel_layout,aFrame->format, |
||
155 | aFrame->sample_rate, 0, NULL); |
||
156 | if (!swr_ctx || swr_init(swr_ctx) < 0) |
||
157 | { |
||
158 | printf("Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n", |
||
159 | aFrame->sample_rate, av_get_sample_fmt_name(aFrame->format), (int)aFrame->channels, |
||
160 | aFrame->sample_rate, av_get_sample_fmt_name(AV_SAMPLE_FMT_S16), 2); |
||
161 | break; |
||
162 | } |
||
163 | |||
164 | |||
165 | src_channels = aFrame->channels; |
||
166 | src_freq = aFrame->sample_rate; |
||
167 | src_fmt = aFrame->format; |
||
168 | }; |
||
169 | |||
170 | |||
171 | { |
||
172 | const uint8_t **in = (const uint8_t **)aFrame->extended_data; |
||
173 | uint8_t *out[] = {decoder_buffer}; |
||
174 | int out_count = 192000 * 3 / 2 / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16); |
||
175 | len2 = swr_convert(swr_ctx, out, out_count, in, aFrame->nb_samples); |
||
176 | if (len2 < 0) { |
||
177 | printf("swr_convert() failed\n"); |
||
178 | break; |
||
179 | } |
||
180 | if (len2 == out_count) { |
||
181 | printf("warning: audio buffer is probably too small\n"); |
||
182 | swr_init(swr_ctx); |
||
183 | } |
||
184 | data_size = len2 * 2 * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16); |
||
185 | |||
186 | |||
187 | |||
188 | |||
189 | |||
190 | |||
191 | |||
192 | |||
193 | mutex_unlock(&astream.lock); |
||
194 | }; |
||
195 | } |
||
196 | else pkt_tmp.size = 0; |
||
197 | } |
||
198 | av_free_packet(&pkt); |
||
199 | return 1; |
||
200 | }; |
||
201 | |||
202 | |||
203 | |||
204 | { |
||
205 | SND_EVENT evnt; |
||
206 | uint32_t offset; |
||
207 | double time_stamp; |
||
208 | |||
209 | |||
210 | |||
211 | |||
212 | { |
||
213 | GetNotify(&evnt); |
||
214 | |||
215 | |||
216 | { |
||
217 | printf("invalid event code %d\n\r", evnt.code); |
||
218 | continue; |
||
219 | } |
||
220 | |||
221 | |||
222 | { |
||
223 | printf("invalid stream %x hBuff= %x\n\r", |
||
224 | evnt.stream, hbuff); |
||
225 | continue; |
||
226 | } |
||
227 | |||
228 | |||
229 | audio_delta = time_stamp - last_time_stamp; |
||
230 | |||
231 | |||
232 | |||
233 | |||
234 | { |
||
235 | if(astream.count < buffsize) |
||
236 | { |
||
237 | memset(astream.buffer+astream.count, |
||
238 | 0, buffsize-astream.count); |
||
239 | astream.count = buffsize; |
||
240 | }; |
||
241 | |||
242 | |||
243 | samples_written+= buffsize/4; |
||
244 | |||
245 | |||
246 | if(astream.count) |
||
247 | memcpy(astream.buffer, astream.buffer+buffsize, astream.count); |
||
248 | mutex_unlock(&astream.lock); |
||
249 | }; |
||
250 | break; |
||
251 | }; |
||
252 | #endif |
||
253 | |||
254 | |||
255 | |||
256 | |||
257 | { |
||
258 | SND_EVENT evnt; |
||
259 | |||
260 | |||
261 | int samples; |
||
262 | int err; |
||
263 | char *errstr; |
||
264 | int active; |
||
265 | |||
266 | |||
267 | { |
||
268 | errstr = "Cannot create sound buffer\n\r"; |
||
269 | goto exit_whith_error; |
||
270 | }; |
||
271 | |||
272 | |||
5603 | serge | 273 | |
4438 | Serge | 274 | |
275 | { |
||
276 | errstr = "Cannot get buffer size\n\r"; |
||
277 | goto exit_whith_error; |
||
278 | }; |
||
279 | |||
280 | |||
5603 | serge | 281 | |
282 | |||
4438 | Serge | 283 | |
284 | |||
285 | |||
286 | |||
287 | { |
||
288 | uint32_t offset; |
||
289 | double event_stamp, wait_stamp; |
||
290 | int too_late = 0; |
||
291 | |||
292 | |||
293 | { |
||
294 | case PREPARE: |
||
295 | |||
296 | |||
297 | if(astream.count < buffsize*2) |
||
298 | { |
||
299 | memset(astream.buffer+astream.count, |
||
300 | 0, buffsize*2-astream.count); |
||
301 | astream.count = buffsize*2; |
||
302 | }; |
||
303 | |||
304 | |||
305 | astream.count -= buffsize*2; |
||
306 | if(astream.count) |
||
307 | memcpy(astream.buffer, astream.buffer+buffsize*2, astream.count); |
||
308 | mutex_unlock(&astream.lock); |
||
309 | |||
310 | |||
311 | |||
312 | |||
313 | GetTimeStamp(hBuff, &last_time_stamp); |
||
314 | // printf("last audio time stamp %f\n", last_time_stamp); |
||
315 | |||
316 | |||
317 | { |
||
318 | errstr = "Cannot play buffer\n\r"; |
||
319 | goto exit_whith_error; |
||
320 | }; |
||
321 | active = 1; |
||
322 | sync_audio(hBuff, buffsize); |
||
323 | sound_state = PLAY; |
||
324 | // printf("render: set audio latency to %f\n", audio_delta); |
||
325 | |||
326 | |||
327 | |||
328 | |||
329 | GetNotify(&evnt); |
||
330 | |||
331 | |||
332 | { |
||
333 | printf("invalid event code %d\n\r", evnt.code); |
||
334 | continue; |
||
335 | } |
||
336 | |||
337 | |||
338 | { |
||
339 | printf("invalid stream %x hBuff= %x\n\r", |
||
340 | evnt.stream, hBuff); |
||
341 | continue; |
||
342 | }; |
||
343 | |||
344 | |||
345 | |||
346 | |||
347 | if(astream.count < buffsize) |
||
348 | { |
||
349 | memset(astream.buffer+astream.count, |
||
350 | 0, buffsize-astream.count); |
||
351 | astream.count = buffsize; |
||
352 | }; |
||
353 | |||
354 | |||
355 | |||
356 | |||
357 | double val = 0; |
||
358 | int16_t *src = (int16_t*)astream.buffer; |
||
359 | int samples = buffsize/2; |
||
360 | int i; |
||
361 | |||
362 | |||
363 | if(val < abs(*src)) |
||
364 | val= abs(*src); // * *src; |
||
365 | |||
366 | |||
367 | |||
368 | |||
369 | if(val < abs(*src)) |
||
370 | val= abs(*src); // * *src; |
||
371 | |||
372 | |||
373 | |||
374 | |||
375 | }; |
||
376 | |||
377 | |||
378 | |||
379 | |||
380 | if(astream.count) |
||
381 | memcpy(astream.buffer, astream.buffer+buffsize, astream.count); |
||
382 | mutex_unlock(&astream.lock); |
||
383 | break; |
||
384 | |||
385 | |||
386 | if( active ) |
||
387 | { |
||
388 | ResetBuffer(hBuff, SND_RESET_ALL); |
||
389 | audio_base = -1.0; |
||
390 | active = 0; |
||
391 | } |
||
392 | sound_state = STOP; |
||
393 | break; |
||
394 | |||
395 | |||
396 | if( active ) |
||
397 | { |
||
398 | StopBuffer(hBuff); |
||
399 | }; |
||
400 | sound_state = PAUSE; |
||
401 | |||
402 | |||
403 | case STOP: |
||
404 | delay(1); |
||
405 | }; |
||
406 | } |
||
407 | |||
408 | |||
5603 | serge | 409 | |
410 | |||
4438 | Serge | 411 | DestroyBuffer(hBuff); |
412 | |||
413 | |||
414 | |||
415 | |||
416 | |||
417 | |||
418 | return -1; |
||
419 | |||
420 | |||
421 |