Subversion Repositories Kolibri OS

Rev

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