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