Subversion Repositories Kolibri OS

Rev

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