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