Subversion Repositories Kolibri OS

Rev

Rev 2693 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2693 Rev 3068
Line 12... Line 12...
12
 
12
 
Line 13... Line 13...
13
astream_t astream;
13
astream_t astream;
-
 
14
 
-
 
15
extern uint8_t *decoder_buffer;
-
 
16
int resampler_size;
14
 
17
volatile int sound_level_0;
15
extern uint8_t *decoder_buffer;
18
volatile int sound_level_1;
-
 
19
 
-
 
20
volatile enum player_state player_state;
Line 16... Line 21...
16
 
21
volatile enum player_state decoder_state;
Line 17... Line 22...
17
extern volatile enum player_state player_state;
22
volatile enum player_state sound_state;
Line 44... Line 49...
44
    };
49
    };
45
 
50
 
Line 46... Line 51...
46
    mutex_unlock(&driver_lock);
51
    mutex_unlock(&driver_lock);
Line 47... Line 52...
47
 
52
 
Line 48... Line 53...
48
    printf("sound version 0x%x\n", version);
53
//    printf("sound version 0x%x\n", version);
49
 
54
 
50
    if( (SOUND_VERSION>(version&0xFFFF)) ||
55
    if( (SOUND_VERSION>(version&0xFFFF)) ||
51
        (SOUND_VERSION<(version >> 16)))
56
        (SOUND_VERSION<(version >> 16)))
Line 65... Line 70...
65
    printf(errstr);
70
    printf(errstr);
66
    return 0;
71
    return 0;
67
};
72
};
Line -... Line 73...
-
 
73
 
-
 
74
void set_audio_volume(int left, int right)
-
 
75
{
-
 
76
    SetVolume(hBuff, left, right);
-
 
77
};
68
 
78
 
69
static uint64_t samples_lost;
79
static uint64_t samples_lost;
70
static double  audio_delta;
80
static double  audio_delta;
Line 87... Line 97...
87
    int         len;
97
    int         len;
88
    int         data_size=0;
98
    int         data_size=0;
Line 89... Line 99...
89
 
99
 
90
    if( astream.count > AVCODEC_MAX_AUDIO_FRAME_SIZE*7)
100
    if( astream.count > AVCODEC_MAX_AUDIO_FRAME_SIZE*7)
Line 91... Line 101...
91
        return 1;
101
        return -1;
92
 
102
 
Line 93... Line 103...
93
    if( get_packet(qa, &pkt) == 0 )
103
    if( get_packet(qa, &pkt) == 0 )
Line 159... Line 169...
159
        offset = evnt.offset;
169
        offset = evnt.offset;
Line 160... Line 170...
160
 
170
 
161
        mutex_lock(&astream.lock);
171
        mutex_lock(&astream.lock);
-
 
172
        {
-
 
173
            if(astream.count < buffsize)
-
 
174
            {
-
 
175
                memset(astream.buffer+astream.count,
-
 
176
                       0, buffsize-astream.count);
-
 
177
                astream.count = buffsize;
-
 
178
            };
162
        {
179
 
163
            SetBuffer(hbuff, astream.buffer, offset, buffsize);
180
            SetBuffer(hbuff, astream.buffer, offset, buffsize);
-
 
181
            samples_written+= buffsize/4;
164
            samples_written+= buffsize/4;
182
 
165
            astream.count -= buffsize;
183
            astream.count -= buffsize;
166
            if(astream.count)
184
            if(astream.count)
167
                memcpy(astream.buffer, astream.buffer+buffsize, astream.count);
185
                memcpy(astream.buffer, astream.buffer+buffsize, astream.count);
168
            mutex_unlock(&astream.lock);
186
            mutex_unlock(&astream.lock);
Line 172... Line 190...
172
#endif
190
#endif
173
 
191
 
Line 174... Line 192...
174
};
192
};
Line 175... Line -...
175
 
-
 
176
 
193
 
177
int audio_thread(void *param)
194
int audio_thread(void *param)
178
{
195
{
Line 179... Line 196...
179
    SND_EVENT evnt;
196
    SND_EVENT evnt;
Line 190... Line 207...
190
        errstr = "Cannot create sound buffer\n\r";
207
        errstr = "Cannot create sound buffer\n\r";
191
        goto exit_whith_error;
208
        goto exit_whith_error;
192
    };
209
    };
193
 
210
 
Line 194... Line 211...
194
    SetVolume(hBuff,-1000,-1000);
211
    SetVolume(hBuff,-1875,-1875);
Line 195... Line 212...
195
 
212
 
196
    if((err = GetBufferSize(hBuff, &buffsize)) != 0)
213
    if((err = GetBufferSize(hBuff, &buffsize)) != 0)
197
    {
214
    {
198
        errstr = "Cannot get buffer size\n\r";
215
        errstr = "Cannot get buffer size\n\r";
199
        goto exit_whith_error;
216
        goto exit_whith_error;
Line 200... Line 217...
200
    };
217
    };
Line 201... Line 218...
201
 
218
 
Line 202... Line -...
202
    buffsize = buffsize/2;
-
 
203
 
-
 
204
    samples = buffsize/4;
-
 
205
 
-
 
206
    while( (astream.count < buffsize*2) &&
-
 
207
               (player_state != CLOSED) )
-
 
208
        yield();
-
 
209
 
-
 
210
    mutex_lock(&astream.lock);
-
 
211
    {
-
 
212
        SetBuffer(hBuff, astream.buffer, 0, buffsize);
-
 
213
        samples_written+= buffsize/4;
-
 
214
        astream.count -= buffsize;
-
 
215
        if(astream.count)
-
 
216
            memcpy(astream.buffer, astream.buffer+buffsize, astream.count);
219
    resampler_size = buffsize = buffsize/2;
217
        mutex_unlock(&astream.lock);
220
 
218
    };
221
    samples = buffsize/4;
219
 
222
 
220
    while( player_state != CLOSED)
223
    while( player_state != CLOSED)
Line 221... Line -...
221
    {
-
 
222
        uint32_t  offset;
-
 
223
        double    event_stamp, wait_stamp;
-
 
224
        int       too_late = 0;
224
    {
225
 
225
        uint32_t  offset;
226
        if((player_state == PAUSE) ||
-
 
227
           (player_state == PLAY_INIT) )
-
 
228
        {
-
 
229
            if( active )
-
 
230
            {
-
 
231
                StopBuffer(hBuff);
-
 
232
                active = 0;
-
 
233
            };
-
 
234
            delay(1);
226
        double    event_stamp, wait_stamp;
235
            continue;
-
 
236
        }
-
 
237
        else if(player_state == REWIND)
-
 
238
        {
-
 
239
            if( active )
-
 
240
            {
-
 
241
                StopBuffer(hBuff);
-
 
242
                active = 0;
-
 
243
            };
-
 
244
            mutex_lock(&astream.lock);
-
 
245
            astream.count = 0;
-
 
246
            mutex_unlock(&astream.lock);
-
 
247
            delay(1);
-
 
248
            continue;
-
 
249
        }
-
 
Line -... Line 227...
-
 
227
        int       too_late = 0;
250
        else if(player_state == PAUSE_2_PLAY)
228
 
251
        {
229
        switch(sound_state)
252
//            SetTimeBase(hBuff, audio_base);
230
        {
-
 
231
            case PREPARE:
253
            GetTimeStamp(hBuff, &last_time_stamp);
232
 
254
//            printf("last_time_stamp %f\n", last_time_stamp);
233
                mutex_lock(&astream.lock);
255
            
-
 
-
 
234
                    if(astream.count < buffsize*2)
256
            if((err = PlayBuffer(hBuff, 0)) !=0 )
235
                    {
257
            {
236
                        memset(astream.buffer+astream.count,
258
                errstr = "Cannot play buffer\n\r";
-
 
259
                goto exit_whith_error;
-
 
260
            };
237
                               0, buffsize*2-astream.count);
261
            active = 1;
-
 
262
            sync_audio(hBuff, buffsize);
238
                        astream.count = buffsize*2;
263
            player_state = PLAY;
239
                    };
264
            printf("render: set audio latency to %f\n", audio_delta);
-
 
Line 265... Line 240...
265
        }
240
 
-
 
241
                    SetBuffer(hBuff, astream.buffer, 0, buffsize*2);
-
 
242
                    astream.count -= buffsize*2;
266
        else if(player_state == REWIND_2_PLAY)
243
                    if(astream.count)
267
        {
244
                        memcpy(astream.buffer, astream.buffer+buffsize*2, astream.count);
Line 268... Line 245...
268
            while( (astream.count < buffsize*2) &&
245
                mutex_unlock(&astream.lock);
269
                   (player_state != CLOSED) )
246
 
270
            yield();
247
                SetTimeBase(hBuff, audio_base);
271
 
248
 
272
            SetTimeBase(hBuff, audio_base);
249
            case PAUSE_2_PLAY:
273
            GetTimeStamp(hBuff, &last_time_stamp);
250
                GetTimeStamp(hBuff, &last_time_stamp);
274
            printf("last audio time stamp %f\n", last_time_stamp);
251
//                printf("last audio time stamp %f\n", last_time_stamp);
275
            
252
 
276
            if((err = PlayBuffer(hBuff, 0)) !=0 )
253
                if((err = PlayBuffer(hBuff, 0)) !=0 )
277
            {
-
 
Line -... Line 254...
-
 
254
                {
-
 
255
                    errstr = "Cannot play buffer\n\r";
-
 
256
                    goto exit_whith_error;
278
                errstr = "Cannot play buffer\n\r";
257
                };
Line 279... Line 258...
279
                goto exit_whith_error;
258
                active = 1;
280
            };
259
                sync_audio(hBuff, buffsize);
281
            active = 1;
260
                sound_state = PLAY;
Line 298... Line 277...
298
                    evnt.stream, hBuff);
277
                            evnt.stream, hBuff);
299
            continue;
278
                    continue;
300
        };
279
                };
301
 
280
 
Line 302... Line -...
302
        GetTimeStamp(hBuff, &event_stamp);
-
 
303
 
-
 
304
        offset = evnt.offset;
281
                offset = evnt.offset;
Line 305... Line 282...
305
 
282
 
306
        while( (astream.count < buffsize) &&
283
                mutex_lock(&astream.lock);
307
               (player_state != CLOSED) )
284
                if(astream.count < buffsize)
308
        {
-
 
309
            yield();
-
 
310
            GetTimeStamp(hBuff, &wait_stamp);
285
                {
311
            if( (wait_stamp - event_stamp) >
286
                    memset(astream.buffer+astream.count,
312
                 samples*1500/sample_rate )
-
 
313
            {
287
                           0, buffsize-astream.count);
314
                samples_lost+= samples;
-
 
315
                audio_delta = (double)samples_lost*1000/sample_rate;
-
 
316
//                printf("audio delta %f\n", audio_delta);
-
 
317
                too_late = 1;
-
 
318
                break;
-
 
319
            }
288
                    astream.count = buffsize;
Line 320... Line 289...
320
        };
289
                };
-
 
290
 
321
 
291
                SetBuffer(hBuff, astream.buffer, offset, buffsize);
322
        if((too_late == 1) || (player_state == CLOSED))
292
 
-
 
293
                {
-
 
294
                    double  val = 0;
323
        {
295
                    int16_t *src = (int16_t*)astream.buffer;
-
 
296
                    int samples = buffsize/2;
-
 
297
                    int i;
-
 
298
 
-
 
299
                    for(i = 0, val = 0; i < samples/2; i++, src++)
-
 
300
                        if(val < abs(*src))
-
 
301
                            val= abs(*src); // * *src;
-
 
302
 
-
 
303
                    sound_level_0 = val; //sqrt(val / (samples/2));
-
 
304
 
-
 
305
                    for(i = 0, val = 0; i < samples/2; i++, src++)
-
 
306
                        if(val < abs(*src))
-
 
307
                            val= abs(*src); // * *src;
-
 
308
 
-
 
309
                    sound_level_1 = val; //sqrt(val / (samples/2));
324
            too_late = 0;
310
 
Line 325... Line -...
325
            continue;
-
 
326
        };
-
 
327
 
311
 //                   printf("%d\n", sound_level);
-
 
312
                };
328
        mutex_lock(&astream.lock);
313
 
329
        SetBuffer(hBuff, astream.buffer, offset, buffsize);
314
                samples_written+= buffsize/4;
330
        samples_written+= buffsize/4;
315
 
331
        astream.count -= buffsize;
316
                astream.count -= buffsize;
-
 
317
                if(astream.count)
-
 
318
                    memcpy(astream.buffer, astream.buffer+buffsize, astream.count);
-
 
319
                mutex_unlock(&astream.lock);
-
 
320
                break;
-
 
321
 
-
 
322
            case PLAY_2_STOP:
-
 
323
                if( active )
-
 
324
                {
-
 
325
                    ResetBuffer(hBuff, SND_RESET_ALL);
-
 
326
                    audio_base = -1.0;
-
 
327
                    active = 0;
-
 
328
                }
-
 
329
                sound_state = STOP;
-
 
330
                break;
-
 
331
 
-
 
332
            case PLAY_2_PAUSE:
-
 
333
                if( active )
-
 
334
                {
-
 
335
                    StopBuffer(hBuff);
-
 
336
                };
-
 
337
                sound_state = PAUSE;
-
 
338
 
-
 
339
            case PAUSE:
332
        if(astream.count)
340
            case STOP:
Line 333... Line 341...
333
            memcpy(astream.buffer, astream.buffer+buffsize, astream.count);
341
                delay(1);
334
        mutex_unlock(&astream.lock);
342
        };