~ubuntu-branches/ubuntu/lucid/blender/lucid

« back to all changes in this revision

Viewing changes to source/blender/src/seqaudio.c

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2009-08-06 22:32:19 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090806223219-8z4eej1u8levu4pz
Tags: 2.49a+dfsg-0ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/control: Build-depend on python-2.6 rather than python-2.5.
  - debian/misc/*.desktop: Add Spanish translation to .desktop 
    files.
  - debian/pyversions: 2.6.
  - debian/rules: Clean *.o of source/blender/python/api2_2x/
* New upstream release (LP: #382153).
* Refreshed patches:
  - 01_sanitize_sys.patch
  - 02_tmp_in_HOME
  - 10_use_systemwide_ftgl
  - 70_portability_platform_detection
* Removed patches merged upstream:
  - 30_fix_python_syntax_warning
  - 90_ubuntu_ffmpeg_52_changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  $Id: seqaudio.c 16941 2008-10-06 01:59:11Z campbellbarton $
 
2
 *  $Id: seqaudio.c 20821 2009-06-11 22:21:00Z schlaile $
3
3
 *
4
4
 * ***** BEGIN GPL LICENSE BLOCK *****
5
5
 *
95
95
static int audio_initialised=0;
96
96
static int audio_startframe=0;
97
97
static double audio_starttime = 0.0;
 
98
static Scene * audio_scene = 0; /* we can't use G.scene, since
 
99
                                   Sequence Scene strips can change G.scene
 
100
                                   (and SDL-audio-fill callback can be
 
101
                                   called while we have G.scene changed!)
 
102
                                */
 
103
 
 
104
#define AFRA2TIME(a)           ((((double) audio_scene->r.frs_sec_base) * (a)) / audio_scene->r.frs_sec)
 
105
#define ATIME2FRA(a)           ((((double) audio_scene->r.frs_sec) * (a)) / audio_scene->r.frs_sec_base)
 
106
 
 
107
/* we do currently stereo 16 bit mixing only */
 
108
#define AUDIO_CHANNELS 2
 
109
#define SAMPLE_SIZE (AUDIO_CHANNELS * sizeof(short))
 
110
 
98
111
/////
99
112
//
100
113
/* local protos ------------------- */
140
153
 
141
154
        strcpy(buf, "RIFFlengWAVEfmt fmln01ccRATEbsecBP16dataDLEN");
142
155
        totframe = (EFRA - SFRA + 1);
143
 
        totlen = (int) ( FRA2TIME(totframe) * (float)G.scene->audio.mixrate * 4.0);
 
156
        totlen = (int) ( FRA2TIME(totframe) 
 
157
                         * (float)G.scene->audio.mixrate * SAMPLE_SIZE);
144
158
        printf(" totlen %d\n", totlen+36+8);
145
159
        
146
160
        totlen+= 36;    /* len is filesize-8 in WAV spec, total header is 44 bytes */
150
164
        buf[16] = 0x10; buf[17] = buf[18] = buf[19] = 0; buf[20] = 1; buf[21] = 0;
151
165
        buf[22] = 2; buf[23]= 0;
152
166
        memcpy(buf+24, &G.scene->audio.mixrate, 4);
153
 
        i = G.scene->audio.mixrate * 4;
 
167
        i = G.scene->audio.mixrate * SAMPLE_SIZE;
154
168
        memcpy(buf+28, &i, 4);
155
169
        buf[32] = 4; buf[33] = 0; buf[34] = 16; buf[35] = 0;
156
170
        i = totlen;
183
197
                
184
198
                memset(buf+i, 0, 64);
185
199
                
186
 
                CFRA=(int) ( ((float)(audio_pos-64)/( G.scene->audio.mixrate*4 ))*FPS );
 
200
                CFRA=(int) ( ((float)(audio_pos-64)
 
201
                              / ( G.scene->audio.mixrate*SAMPLE_SIZE ))*FPS );
187
202
                        
188
203
                audio_fill(buf+i, NULL, 64);
189
204
                if (G.order == B_ENDIAN) {
217
232
#ifndef DISABLE_SDL
218
233
        for (i = 0; i < len; i += 64) {
219
234
                CFRA = (int) ( ((float)(audio_pos-64)
220
 
                                /( G.scene->audio.mixrate*4 ))
 
235
                                /( audio_scene->audio.mixrate * SAMPLE_SIZE ))
221
236
                               * FPS );
222
237
 
223
238
                audio_fill(mixdown + i, NULL, 
229
244
}
230
245
 
231
246
 
232
 
static void audio_levels(uint8_t *buf, int len, float db, float facf, float pan)
 
247
static void audio_levels(uint8_t *buf, int len, float db, 
 
248
                         float facf_start, float facf_end, float pan)
233
249
{
234
250
        int i;
 
251
        double m = (facf_end - facf_start) / len;
235
252
        float facl, facr, fac;
236
253
        signed short *sample;
237
254
        
238
255
        if (pan>=0) { facr = 1.0; facl = 1.0-pan; }
239
256
               else { facr = pan+1.0; facl = 1.0; }
240
257
        
241
 
        fac = pow(10.0, ((-(db+G.scene->audio.main))/20.0)) / facf;
242
 
        facl /= fac;
243
 
        facr /= fac;
244
 
        
245
 
        for (i=0; i<len; i+=4) {
 
258
        fac = pow(10.0, ((-(db+audio_scene->audio.main))/20.0));
 
259
 
 
260
        for (i = 0; i < len; i += SAMPLE_SIZE) {
 
261
                float facf = facf_start + ((double) i) * m;
 
262
                float f_l = facl / (fac / facf);
 
263
                float f_r = facr / (fac / facf);
 
264
 
246
265
                sample = (signed short*)(buf+i);
247
 
                sample[0] = (short) ((float)sample[0] * facl);
248
 
                sample[1] = (short) ((float)sample[1] * facr);
 
266
                sample[0] = (short) ((float)sample[0] * f_l);
 
267
                sample[1] = (short) ((float)sample[1] * f_r);
249
268
        }
250
269
}
251
270
 
263
282
                return;
264
283
        }
265
284
        ratio = (float)G.scene->audio.mixrate / (float)sound->sample->rate;
266
 
        sound->streamlen = (int) ( (float)sound->sample->len * ratio * 2.0/((float)sound->sample->channels) );
 
285
        sound->streamlen = (int) ( (float)sound->sample->len * ratio 
 
286
                                   * AUDIO_CHANNELS 
 
287
                                   / ((float)sound->sample->channels) );
267
288
        sound->stream = malloc((int) ((float)sound->streamlen * 1.05));
268
289
        if (sound->sample->rate == G.scene->audio.mixrate) {
269
 
                if (sound->sample->channels == 2) {
270
 
                        memcpy(sound->stream, sound->sample->data, sound->streamlen);
 
290
                if (sound->sample->channels == AUDIO_CHANNELS) {
 
291
                        memcpy(sound->stream, 
 
292
                               sound->sample->data, sound->streamlen);
271
293
                        return;
272
 
                } else {
 
294
                } else if (sound->sample->channels == 1) {
273
295
                        for (source = (signed short*)(sound->sample->data),
274
296
                             dest = (signed short*)(sound->stream),
275
297
                                 i=0;
276
 
                                 i<sound->streamlen/4;
277
 
                                 dest += 2, source++, i++) dest[0] = dest[1] = source[0];
 
298
                                 i<sound->streamlen/SAMPLE_SIZE;
 
299
                             dest += 2, source++, i++) {
 
300
                                int j;
 
301
                                for (j = 0; j < AUDIO_CHANNELS; j++) {
 
302
                                        dest[j] = source[0];
 
303
                                }
 
304
                        }
 
305
                        return;
 
306
                } else {
 
307
                        fprintf(stderr, "audio_makestream: "
 
308
                                "FIXME: can't handle number of channels %d\n",
 
309
                                sound->sample->channels);
278
310
                        return;
279
311
                }
280
312
        }
281
313
        if (sound->sample->channels == 1) {
282
 
                for (dest=(signed short*)(sound->stream), i=0, source=(signed short*)(sound->sample->data); 
283
 
                     i<(sound->streamlen/4); dest+=2, i++)
284
 
                        dest[0] = dest[1] = source[(int)((float)i/ratio)];
 
314
                for (dest = (signed short*)(sound->stream), i=0, 
 
315
                             source = (signed short*)(sound->sample->data); 
 
316
                     i<(sound->streamlen/SAMPLE_SIZE); 
 
317
                     dest += AUDIO_CHANNELS, i++) {
 
318
                        int j;
 
319
                        int s = source[(int)((float)i/ratio)];
 
320
                        for (j = 0; j < AUDIO_CHANNELS; j++) {
 
321
                                dest[j] = s;
 
322
                        }
 
323
                }
285
324
        }
286
325
        else if (sound->sample->channels == 2) {
287
 
                for (dest=(signed short*)(sound->stream), i=0, source=(signed short*)(sound->sample->data); 
288
 
                     i<(sound->streamlen/2); dest+=2, i+=2) {
 
326
                for (dest=(signed short*)(sound->stream), i=0, 
 
327
                             source = (signed short*)(sound->sample->data); 
 
328
                     i<(sound->streamlen / 2); dest += AUDIO_CHANNELS, i+=2) {
289
329
                        dest[1] = source[(int)((float)i/ratio)];
290
 
                        dest[0] = source[(int)((float)i/ratio)+1];                      
 
330
                        dest[0] = source[(int)((float)i/ratio)+1];
291
331
                }
292
332
        }       
293
333
}
294
334
 
295
335
#ifndef DISABLE_SDL
 
336
 
 
337
static int fra2curpos(Sequence * seq, int cfra)
 
338
{
 
339
        return (int)( (AFRA2TIME(((double) cfra) -
 
340
                                 ((double) seq->start) +
 
341
                                 ((double) 
 
342
                                  seq->anim_startofs))
 
343
                       * ((float)audio_scene
 
344
                          ->audio.mixrate)
 
345
                       * SAMPLE_SIZE));
 
346
}
 
347
 
 
348
static int curpos2fra(Sequence * seq, int curpos)
 
349
{
 
350
        return ((int) floor(
 
351
                        ATIME2FRA(
 
352
                                ((double) curpos) / SAMPLE_SIZE 
 
353
                                /audio_scene->audio.mixrate)))
 
354
                - seq->anim_startofs + seq->start;
 
355
}
 
356
 
 
357
static int get_curpos(Sequence * seq, int cfra)
 
358
{
 
359
        return audio_pos + 
 
360
                (((int)((FRA2TIME(((double) cfra) 
 
361
                                 - ((double) audio_scene->r.cfra)
 
362
                                 - ((double) seq->start) 
 
363
                                 + ((double) seq->anim_startofs))
 
364
                        * ((float)audio_scene->audio.mixrate)
 
365
                        * SAMPLE_SIZE )))
 
366
                 & (~(SAMPLE_SIZE - 1))); /* has to be sample aligned! */
 
367
}
 
368
 
 
369
static void do_audio_seq_ipo(Sequence * seq, int len, float * facf_start,
 
370
                             float * facf_end, int cfra)
 
371
{
 
372
        int seq_curpos = get_curpos(seq, cfra);
 
373
        int cfra_start = curpos2fra(seq, seq_curpos);
 
374
        int cfra_end = cfra_start + 1;
 
375
        int ipo_curpos_start = fra2curpos(seq, curpos2fra(seq, seq_curpos));
 
376
        int ipo_curpos_end = fra2curpos(seq, cfra_end);
 
377
        double ipo_facf_start;
 
378
        double ipo_facf_end;
 
379
        double m;
 
380
 
 
381
        do_seq_ipo(seq, cfra_start);
 
382
        ipo_facf_start = seq->facf0;
 
383
 
 
384
        do_seq_ipo(seq, cfra_end);
 
385
        ipo_facf_end = seq->facf0;
 
386
 
 
387
        m = (ipo_facf_end- ipo_facf_start)/(ipo_curpos_end - ipo_curpos_start);
 
388
        
 
389
        *facf_start = ipo_facf_start + (seq_curpos - ipo_curpos_start) * m;
 
390
        *facf_end = ipo_facf_start + (seq_curpos + len-ipo_curpos_start) * m;
 
391
}
 
392
 
 
393
#endif
 
394
 
 
395
#ifndef DISABLE_SDL
296
396
static void audio_fill_ram_sound(Sequence *seq, void * mixdown, 
297
 
                                 uint8_t * sstream, int len)
 
397
                                 uint8_t * sstream, int len,
 
398
                                 int cfra)
298
399
{
299
400
        uint8_t* cvtbuf;
300
401
        bSound* sound;
301
 
        float facf;
 
402
        float facf_start;
 
403
        float facf_end;
 
404
        int seq_curpos = get_curpos(seq, cfra);
 
405
 
 
406
        /* catch corner case at the beginning of strip */
 
407
        if (seq_curpos < 0 && (seq_curpos + len > 0)) {
 
408
                seq_curpos *= -1;
 
409
                len -= seq_curpos;
 
410
                sstream += seq_curpos;
 
411
                seq_curpos = 0;
 
412
        }
302
413
 
303
414
        sound = seq->sound;
304
415
        audio_makestream(sound);
305
 
        if ((seq->curpos<sound->streamlen -len) && (seq->curpos>=0) &&
306
 
            (seq->startdisp <= CFRA) && ((seq->enddisp) > CFRA))
 
416
        if ((seq_curpos < sound->streamlen -len) && (seq_curpos >= 0) &&
 
417
            (seq->startdisp <= cfra) && ((seq->enddisp) > cfra))
307
418
        {
308
419
                if(seq->ipo && seq->ipo->curve.first) {
309
 
                        do_seq_ipo(seq, CFRA);
310
 
                        facf = seq->facf0;
 
420
                        do_audio_seq_ipo(seq, len, &facf_start, &facf_end,
 
421
                                         cfra);
311
422
                } else {
312
 
                        facf = 1.0;
 
423
                        facf_start = 1.0;
 
424
                        facf_end = 1.0;
313
425
                }
314
426
                cvtbuf = malloc(len);                                   
315
 
                memcpy(cvtbuf, ((uint8_t*)sound->stream)+(seq->curpos & (~3)), len);
316
 
                audio_levels(cvtbuf, len, seq->level, facf, seq->pan);
 
427
                memcpy(cvtbuf, ((uint8_t*)sound->stream)+(seq_curpos), len);
 
428
                audio_levels(cvtbuf, len, seq->level, facf_start, facf_end, 
 
429
                             seq->pan);
317
430
                if (!mixdown) {
318
431
                        SDL_MixAudio(sstream, cvtbuf, len, SDL_MIX_MAXVOLUME);
319
432
                } else {
321
434
                }
322
435
                free(cvtbuf);
323
436
        }
324
 
        seq->curpos += len;
325
437
}
326
438
#endif
327
439
 
328
440
#ifndef DISABLE_SDL
329
441
static void audio_fill_hd_sound(Sequence *seq, 
330
442
                                void * mixdown, uint8_t * sstream, 
331
 
                                int len)
 
443
                                int len, int cfra)
332
444
{
333
445
        uint8_t* cvtbuf;
334
 
        float facf;
335
 
 
336
 
        if ((seq->curpos >= 0) &&
337
 
            (seq->startdisp <= CFRA) && ((seq->enddisp) > CFRA))
 
446
        float facf_start;
 
447
        float facf_end;
 
448
        int seq_curpos = get_curpos(seq, cfra);
 
449
 
 
450
        /* catch corner case at the beginning of strip */
 
451
        if (seq_curpos < 0 && (seq_curpos + len > 0)) {
 
452
                seq_curpos *= -1;
 
453
                len -= seq_curpos;
 
454
                sstream += seq_curpos;
 
455
                seq_curpos = 0;
 
456
        }
 
457
 
 
458
        if ((seq_curpos >= 0) &&
 
459
            (seq->startdisp <= cfra) && ((seq->enddisp) > cfra))
338
460
        {
339
461
                if(seq->ipo && seq->ipo->curve.first) {
340
 
                        do_seq_ipo(seq, CFRA);
341
 
                        facf = seq->facf0; 
 
462
                        do_audio_seq_ipo(seq, len, &facf_start, &facf_end,
 
463
                                         cfra);
342
464
                } else {
343
 
                        facf = 1.0;
 
465
                        facf_start = 1.0;
 
466
                        facf_end = 1.0;
344
467
                }
345
468
                cvtbuf = malloc(len);
346
469
                
347
470
                sound_hdaudio_extract(seq->hdaudio, (short*) cvtbuf,
348
 
                                      seq->curpos / 4,
349
 
                                      G.scene->audio.mixrate,
350
 
                                      2,
351
 
                                      len / 4);
352
 
                audio_levels(cvtbuf, len, seq->level, facf, seq->pan);
 
471
                                      seq_curpos / SAMPLE_SIZE,
 
472
                                      audio_scene->audio.mixrate,
 
473
                                      AUDIO_CHANNELS,
 
474
                                      len / SAMPLE_SIZE);
 
475
                audio_levels(cvtbuf, len, seq->level, facf_start, facf_end,
 
476
                             seq->pan);
353
477
                if (!mixdown) {
354
478
                        SDL_MixAudio(sstream, 
355
479
                                     cvtbuf, len, SDL_MIX_MAXVOLUME);
359
483
                }
360
484
                free(cvtbuf);
361
485
        }
362
 
        seq->curpos += len;
363
 
}
364
 
#endif
365
 
 
366
 
#ifndef DISABLE_SDL
367
 
static void audio_fill_seq(Sequence * seq, void * mixdown,
368
 
                           uint8_t *sstream, int len, int advance_only)
 
486
}
 
487
#endif
 
488
 
 
489
#ifndef DISABLE_SDL
 
490
static void audio_fill_seq(Sequence * seq, void * mixdown,
 
491
                           uint8_t *sstream, int len, int cfra);
 
492
 
 
493
static void audio_fill_scene_strip(Sequence * seq, void * mixdown,
 
494
                                   uint8_t *sstream, int len, int cfra)
 
495
{
 
496
        Editing *ed;
 
497
 
 
498
        /* prevent eternal loop */
 
499
        seq->scene->r.scemode |= R_RECURS_PROTECTION;
 
500
 
 
501
        ed = seq->scene->ed;
 
502
 
 
503
        if (ed) {
 
504
                int sce_cfra = seq->sfra + seq->anim_startofs
 
505
                        + cfra - seq->startdisp;
 
506
 
 
507
                audio_fill_seq(ed->seqbasep->first,
 
508
                               mixdown,
 
509
                               sstream, len, sce_cfra);
 
510
        }
 
511
        
 
512
        /* restore */
 
513
        seq->scene->r.scemode &= ~R_RECURS_PROTECTION;
 
514
}
 
515
#endif
 
516
 
 
517
#ifndef DISABLE_SDL
 
518
static void audio_fill_seq(Sequence * seq, void * mixdown,
 
519
                           uint8_t *sstream, int len, int cfra)
369
520
{
370
521
        while(seq) {
371
522
                if (seq->type == SEQ_META &&
372
523
                    (!(seq->flag & SEQ_MUTE))) {
373
 
                        if (seq->startdisp <= CFRA && seq->enddisp > CFRA) {
374
 
                                audio_fill_seq(seq->seqbase.first,
375
 
                                               mixdown, sstream, len, 
376
 
                                               advance_only);
377
 
                        } else {
378
 
                                audio_fill_seq(seq->seqbase.first,
379
 
                                               mixdown, sstream, len, 
380
 
                                               1);
 
524
                        if (seq->startdisp <= cfra && seq->enddisp > cfra) {
 
525
                                audio_fill_seq(seq->seqbase.first,
 
526
                                               mixdown, sstream, len, 
 
527
                                               cfra);
 
528
                        }
 
529
                }
 
530
                if (seq->type == SEQ_SCENE 
 
531
                    && (!(seq->flag & SEQ_MUTE))
 
532
                    && seq->scene
 
533
                    && (seq->scene->r.scemode & R_DOSEQ)
 
534
                    && !(seq->scene->r.scemode & R_RECURS_PROTECTION)) {
 
535
                        if (seq->startdisp <= cfra && seq->enddisp > cfra) {
 
536
                                audio_fill_scene_strip(
 
537
                                        seq, mixdown, sstream, len,
 
538
                                        cfra);
381
539
                        }
382
540
                }
383
541
                if ( (seq->type == SEQ_RAM_SOUND) &&
384
542
                     (seq->sound) &&
385
543
                     (!(seq->flag & SEQ_MUTE))) {
386
 
                        if (advance_only) {
387
 
                                seq->curpos += len;
388
 
                        } else {
389
 
                                audio_fill_ram_sound(
390
 
                                        seq, mixdown, sstream, len);
391
 
                        }
 
544
                        audio_fill_ram_sound(seq, mixdown, sstream, len, cfra);
392
545
                }
393
546
                if ( (seq->type == SEQ_HD_SOUND) &&
394
547
                     (!(seq->flag & SEQ_MUTE))) {
395
 
                        if (advance_only) {
396
 
                                seq->curpos += len;
397
 
                        } else {
398
 
                                if (!seq->hdaudio) {
399
 
                                        char name[FILE_MAXDIR+FILE_MAXFILE];
400
 
 
401
 
                                        strncpy(name, seq->strip->dir, 
402
 
                                                FILE_MAXDIR-1);
403
 
                                        strncat(name, 
404
 
                                                seq->strip->stripdata->name, 
405
 
                                                FILE_MAXFILE-1);
406
 
                                        BLI_convertstringcode(name, G.sce);
407
 
                                
408
 
                                        seq->hdaudio= sound_open_hdaudio(name);
409
 
                                }
410
 
                                if (seq->hdaudio) {
411
 
                                        audio_fill_hd_sound(seq, mixdown, 
412
 
                                                            sstream, len);
413
 
                                }
 
548
                        if (!seq->hdaudio) {
 
549
                                char name[FILE_MAXDIR+FILE_MAXFILE];
 
550
                                
 
551
                                BLI_join_dirfile(name, seq->strip->dir, seq->strip->stripdata->name);
 
552
                                BLI_convertstringcode(name, G.sce);
 
553
                                
 
554
                                seq->hdaudio= sound_open_hdaudio(name);
 
555
                        }
 
556
                        if (seq->hdaudio) {
 
557
                                audio_fill_hd_sound(seq, mixdown, sstream, len,
 
558
                                                    cfra);
414
559
                        }
415
560
                }
416
561
                seq = seq->next;
424
569
        Editing *ed;
425
570
        Sequence *seq;
426
571
 
427
 
        ed = G.scene->ed;
428
 
        if((ed) && (!(G.scene->audio.flag & AUDIO_MUTE))) {
 
572
        if (!audio_scene) {
 
573
                return;
 
574
        }
 
575
 
 
576
        ed = audio_scene->ed;
 
577
        if((ed) && (!(audio_scene->audio.flag & AUDIO_MUTE))) {
429
578
                seq = ed->seqbasep->first;
430
 
                audio_fill_seq(seq, mixdown, sstream, len, 0);
 
579
                audio_fill_seq(seq, mixdown, sstream, len, 
 
580
                               audio_scene->r.cfra);
431
581
        }
432
582
       
433
 
        audio_pos += len;    
434
 
        if (audio_scrub) { 
435
 
                audio_scrub--;
436
 
                if (!audio_scrub) {
 
583
        audio_pos += len;
 
584
 
 
585
        if (audio_scrub > 0) { 
 
586
                audio_scrub-= len;
 
587
                if (audio_scrub <= 0) {
437
588
                        audiostream_stop();
438
589
                }
439
590
        }
449
600
 
450
601
        obtained = (SDL_AudioSpec*)MEM_mallocN(sizeof(SDL_AudioSpec), 
451
602
                                               "SDL_AudioSpec");
452
 
 
 
603
        audio_initialised = 0;
453
604
        desired->callback=audio_fill;
454
605
 
455
606
        if ( SDL_OpenAudio(desired, obtained) < 0 ) {
467
618
}
468
619
#endif
469
620
 
470
 
static int audiostream_play_seq(Sequence * seq, uint32_t startframe)
 
621
static int audiostream_play_seq(Sequence * seq, int startframe)
471
622
{
472
623
        char name[FILE_MAXDIR+FILE_MAXFILE];
473
624
        int have_sound = 0;
479
630
                                have_sound = 1;
480
631
                        }
481
632
                }
 
633
                if (seq->type == SEQ_SCENE
 
634
                    && seq->scene
 
635
                    && (seq->scene->r.scemode & R_DOSEQ)
 
636
                    && !(seq->scene->r.scemode & R_RECURS_PROTECTION)) {
 
637
                        Editing *ed;
 
638
 
 
639
                        /* prevent eternal loop */
 
640
                        seq->scene->r.scemode |= R_RECURS_PROTECTION;
 
641
 
 
642
                        ed = seq->scene->ed;
 
643
 
 
644
                        if (ed) {
 
645
                                int sce_cfra = seq->sfra + seq->anim_startofs
 
646
                                        + startframe - seq->startdisp;
 
647
 
 
648
                                if (audiostream_play_seq(ed->seqbasep->first,
 
649
                                                         sce_cfra)) {
 
650
                                        have_sound = 1;
 
651
                                }
 
652
                        }
 
653
        
 
654
                        /* restore */
 
655
                        seq->scene->r.scemode &= ~R_RECURS_PROTECTION;
 
656
                }
482
657
                if ((seq->type == SEQ_RAM_SOUND) && (seq->sound)) {
483
658
                        have_sound = 1;
484
 
                        seq->curpos = (int)( (FRA2TIME(
485
 
                                                      (double) startframe -
486
 
                                                      (double) seq->start +
487
 
                                                      (double) 
488
 
                                                      seq->anim_startofs)
489
 
                                              * ((float)G.scene->audio.mixrate)
490
 
                                              * 4 ));
491
659
                }
492
660
                if ((seq->type == SEQ_HD_SOUND)) {
493
 
                        have_sound = 1;
494
661
                        if (!seq->hdaudio) {
495
662
                                strncpy(name, seq->strip->dir, FILE_MAXDIR-1);
496
663
                                strncat(name, seq->strip->stripdata->name, 
498
665
                                
499
666
                                seq->hdaudio = sound_open_hdaudio(name);
500
667
                        }
501
 
                        seq->curpos = (int)( (FRA2TIME((double) startframe - 
502
 
                                                       (double) seq->start +
503
 
                                                       (double)
504
 
                                                       seq->anim_startofs)
505
 
                                              * ((float)G.scene->audio.mixrate)
506
 
                                              * 4 ));
507
668
                }
508
669
                seq= seq->next;
509
670
        }
510
671
        return have_sound;
511
672
}
512
673
 
513
 
void audiostream_play(uint32_t startframe, uint32_t duration, int mixdown)
 
674
static void audiostream_reset_recurs_protection()
 
675
{
 
676
        Scene * sce = G.main->scene.first;
 
677
 
 
678
        while(sce) {
 
679
                sce->r.scemode &= ~R_RECURS_PROTECTION;
 
680
                sce= sce->id.next;
 
681
        }
 
682
        /* fix for silly case, when people try testing with
 
683
           the same scene ... */
 
684
        audio_scene->r.scemode |= R_RECURS_PROTECTION;
 
685
}
 
686
 
 
687
void audiostream_play(int startframe, uint32_t duration, int mixdown)
514
688
{
515
689
#ifndef DISABLE_SDL
516
690
        static SDL_AudioSpec desired;
517
691
        Editing *ed;
518
692
        int have_sound = 0;
519
693
 
520
 
        ed= G.scene->ed;
 
694
        audio_scene = G.scene;
 
695
 
 
696
        audiostream_reset_recurs_protection();
 
697
 
 
698
        ed= audio_scene->ed;
521
699
        if(ed) {
522
700
                have_sound = 
523
701
                        audiostream_play_seq(ed->seqbasep->first, startframe);
528
706
                sound_init_audio();
529
707
        }
530
708
 
531
 
        if (U.mixbufsize && !audio_initialised && !mixdown) {
532
 
                desired.freq=G.scene->audio.mixrate;
 
709
        if (U.mixbufsize && 
 
710
            (!audio_initialised 
 
711
             || desired.freq != audio_scene->audio.mixrate
 
712
             || desired.samples != U.mixbufsize) 
 
713
            && !mixdown) {
 
714
                desired.freq=audio_scene->audio.mixrate;
533
715
                desired.format=AUDIO_S16SYS;
534
716
                desired.channels=2;
535
717
                desired.samples=U.mixbufsize;
536
718
                desired.userdata=0;
537
719
 
538
 
                if (audio_init(&desired)==0) {
539
 
                        U.mixbufsize = 0;       /* no audio */
540
 
                }
 
720
                audio_init(&desired);
541
721
        }
542
722
 
543
723
        audio_startframe = startframe;
544
724
        audio_pos = ( ((int)( FRA2TIME(startframe)
545
 
                              *(G.scene->audio.mixrate)*4 )) & (~3) );
 
725
                              *(audio_scene->audio.mixrate)*4 )) & (~3) );
546
726
        audio_starttime = PIL_check_seconds_timer();
547
727
 
548
728
        /* if audio already is playing, just reseek, otherwise
549
729
           remember scrub-duration */
550
 
        if (!(audio_playing && !audio_scrub)) {
 
730
        if (!(audio_playing && !(audio_scrub > 0))) {
551
731
                audio_scrub = duration;
552
732
        }
553
733
        if (!mixdown) {
557
737
#endif
558
738
}
559
739
 
560
 
void audiostream_start(uint32_t frame)
 
740
void audiostream_start(int frame)
561
741
{
562
742
        audiostream_play(frame, 0, 0);
563
743
}
564
744
 
565
 
void audiostream_scrub(uint32_t frame)
 
745
void audiostream_scrub(int frame)
566
746
{
567
 
        if (U.mixbufsize) audiostream_play(frame, 4096/U.mixbufsize, 0);
 
747
        audiostream_play(frame, 4096, 0);
568
748
}
569
749
 
570
750
void audiostream_stop(void)
579
759
{
580
760
        int pos;
581
761
 
582
 
        if (U.mixbufsize) {
 
762
        if (audio_initialised && audio_scene) {
583
763
                pos = (int) (((double)(audio_pos-U.mixbufsize)
584
 
                              / ( G.scene->audio.mixrate*4 ))
 
764
                              / ( audio_scene->audio.mixrate*4 ))
585
765
                             * FPS );
586
766
        } else { /* fallback to seconds_timer when no audio available */
587
767
                pos = (int) ((PIL_check_seconds_timer() - audio_starttime)