~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/blenkernel/intern/sound.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * sound.c (mar-2001 nzc)
3
 
 *
4
 
 * $Id: sound.c 30568 2010-07-21 07:55:53Z nexyon $
 
1
/*
 
2
 * ***** BEGIN GPL LICENSE BLOCK *****
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software Foundation,
 
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
17
 *
 
18
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 
19
 * All rights reserved.
 
20
 *
 
21
 * The Original Code is: all of this file.
 
22
 *
 
23
 * Contributor(s): none yet.
 
24
 *
 
25
 * ***** END GPL LICENSE BLOCK *****
 
26
 */
 
27
 
 
28
/** \file blender/blenkernel/intern/sound.c
 
29
 *  \ingroup bke
5
30
 */
6
31
 
7
32
#include <string.h>
10
35
#include "MEM_guardedalloc.h"
11
36
 
12
37
#include "BLI_blenlib.h"
 
38
#include "BLI_math.h"
13
39
 
14
40
#include "DNA_anim_types.h"
 
41
#include "DNA_object_types.h"
15
42
#include "DNA_scene_types.h"
16
43
#include "DNA_sequence_types.h"
17
44
#include "DNA_packedFile_types.h"
18
45
#include "DNA_screen_types.h"
 
46
#include "DNA_sound_types.h"
 
47
#include "DNA_speaker_types.h"
19
48
 
20
 
#include "AUD_C-API.h"
 
49
#ifdef WITH_AUDASPACE
 
50
#  include "AUD_C-API.h"
 
51
#endif
21
52
 
22
53
#include "BKE_utildefines.h"
23
54
#include "BKE_global.h"
26
57
#include "BKE_context.h"
27
58
#include "BKE_library.h"
28
59
#include "BKE_packedFile.h"
29
 
#include "BKE_fcurve.h"
30
60
#include "BKE_animsys.h"
31
 
 
 
61
#include "BKE_sequencer.h"
 
62
#include "BKE_scene.h"
 
63
 
 
64
// evil quiet NaN definition
 
65
static const int NAN_INT = 0x7FC00000;
 
66
#define NAN_FLT *((float*)(&NAN_INT))
 
67
 
 
68
#ifdef WITH_AUDASPACE
 
69
// evil global ;-)
 
70
static int sound_cfra;
 
71
#endif
 
72
 
 
73
struct bSound* sound_new_file(struct Main *bmain, const char *filename)
 
74
{
 
75
        bSound* sound = NULL;
 
76
 
 
77
        char str[FILE_MAX];
 
78
        char *path;
 
79
 
 
80
        size_t len;
 
81
 
 
82
        BLI_strncpy(str, filename, sizeof(str));
 
83
 
 
84
        path = /*bmain ? bmain->name :*/ G.main->name;
 
85
 
 
86
        BLI_path_abs(str, path);
 
87
 
 
88
        len = strlen(filename);
 
89
        while (len > 0 && filename[len-1] != '/' && filename[len-1] != '\\')
 
90
                len--;
 
91
 
 
92
        sound = alloc_libblock(&bmain->sound, ID_SO, filename+len);
 
93
        BLI_strncpy(sound->name, filename, FILE_MAX);
 
94
// XXX unused currently sound->type = SOUND_TYPE_FILE;
 
95
 
 
96
        sound_load(bmain, sound);
 
97
 
 
98
        if (!sound->playback_handle)
 
99
        {
 
100
                free_libblock(&bmain->sound, sound);
 
101
                sound = NULL;
 
102
        }
 
103
 
 
104
        return sound;
 
105
}
 
106
 
 
107
void sound_free(struct bSound* sound)
 
108
{
 
109
        if (sound->packedfile)
 
110
        {
 
111
                freePackedFile(sound->packedfile);
 
112
                sound->packedfile = NULL;
 
113
        }
 
114
 
 
115
#ifdef WITH_AUDASPACE
 
116
        if (sound->handle)
 
117
        {
 
118
                AUD_unload(sound->handle);
 
119
                sound->handle = NULL;
 
120
                sound->playback_handle = NULL;
 
121
        }
 
122
 
 
123
        if (sound->cache)
 
124
        {
 
125
                AUD_unload(sound->cache);
 
126
                sound->cache = NULL;
 
127
        }
 
128
 
 
129
        sound_free_waveform(sound);
 
130
#endif // WITH_AUDASPACE
 
131
}
 
132
 
 
133
#ifdef WITH_AUDASPACE
32
134
 
33
135
static int force_device = -1;
34
136
 
39
141
        struct Scene* scene;
40
142
 
41
143
        scene = bmain->scene.first;
42
 
        while(scene)
 
144
        while (scene)
43
145
        {
44
 
                if(scene->audio.flag & AUDIO_SYNC)
 
146
                if (scene->audio.flag & AUDIO_SYNC)
45
147
                {
46
 
                        if(mode)
 
148
                        if (mode)
47
149
                                sound_play_scene(scene);
48
150
                        else
49
151
                                sound_stop_scene(scene);
50
 
                        AUD_seek(scene->sound_scene_handle, time);
 
152
                        if (scene->sound_scene_handle)
 
153
                                AUD_seek(scene->sound_scene_handle, time);
51
154
                }
52
155
                scene = scene->id.next;
53
156
        }
54
157
}
55
158
#endif
56
159
 
57
 
int sound_define_from_str(char *str)
 
160
int sound_define_from_str(const char *str)
58
161
{
59
162
        if (BLI_strcaseeq(str, "NULL"))
60
163
                return AUD_NULL_DEVICE;
73
176
        force_device = device;
74
177
}
75
178
 
76
 
void sound_init_once()
 
179
void sound_init_once(void)
77
180
{
78
181
        AUD_initOnce();
79
182
}
89
192
        specs.format = U.audioformat;
90
193
        specs.rate = U.audiorate;
91
194
 
92
 
        if(force_device >= 0)
 
195
        if (force_device >= 0)
93
196
                device = force_device;
94
197
 
95
 
        if(buffersize < 128)
 
198
        if (buffersize < 128)
96
199
                buffersize = AUD_DEFAULT_BUFFER_SIZE;
97
200
 
98
 
        if(specs.rate < AUD_RATE_8000)
 
201
        if (specs.rate < AUD_RATE_8000)
99
202
                specs.rate = AUD_RATE_44100;
100
203
 
101
 
        if(specs.format <= AUD_FORMAT_INVALID)
 
204
        if (specs.format <= AUD_FORMAT_INVALID)
102
205
                specs.format = AUD_FORMAT_S16;
103
206
 
104
 
        if(specs.channels <= AUD_CHANNELS_INVALID)
 
207
        if (specs.channels <= AUD_CHANNELS_INVALID)
105
208
                specs.channels = AUD_CHANNELS_STEREO;
106
209
 
107
 
        if(!AUD_init(device, specs, buffersize))
 
210
        if (!AUD_init(device, specs, buffersize))
108
211
                AUD_init(AUD_NULL_DEVICE, specs, buffersize);
109
 
                
 
212
 
 
213
        sound_init_main(bmain);
 
214
}
 
215
 
 
216
void sound_init_main(struct Main *bmain)
 
217
{
110
218
#ifdef WITH_JACK
111
219
        AUD_setSyncCallback(sound_sync_callback, bmain);
 
220
#else
 
221
        (void)bmain; /* unused */
112
222
#endif
113
223
}
114
224
 
115
 
void sound_exit()
 
225
void sound_exit(void)
116
226
{
117
227
        AUD_exit();
118
228
}
119
229
 
120
 
struct bSound* sound_new_file(struct Main *bmain, char* filename)
121
 
{
122
 
        bSound* sound = NULL;
123
 
 
124
 
        char str[FILE_MAX];
125
 
        char *path;
126
 
 
127
 
        int len;
128
 
 
129
 
        strcpy(str, filename);
130
 
 
131
 
        path = /*bmain ? bmain->name :*/ G.sce;
132
 
 
133
 
        BLI_path_abs(str, path);
134
 
 
135
 
        len = strlen(filename);
136
 
        while(len > 0 && filename[len-1] != '/' && filename[len-1] != '\\')
137
 
                len--;
138
 
 
139
 
        sound = alloc_libblock(&bmain->sound, ID_SO, filename+len);
140
 
        BLI_strncpy(sound->name, filename, FILE_MAX);
141
 
// XXX unused currently sound->type = SOUND_TYPE_FILE;
142
 
 
143
 
        sound_load(bmain, sound);
144
 
 
145
 
        if(!sound->playback_handle)
146
 
        {
147
 
                free_libblock(&bmain->sound, sound);
148
 
                sound = NULL;
149
 
        }
150
 
 
151
 
        return sound;
152
 
}
153
 
 
154
230
// XXX unused currently
155
231
#if 0
156
 
struct bSound* sound_new_buffer(struct bContext *C, struct bSound *source)
 
232
struct bSound* sound_new_buffer(struct Main *bmain, struct bSound *source)
157
233
{
158
234
        bSound* sound = NULL;
159
235
 
160
 
        char name[25];
 
236
        char name[MAX_ID_NAME+5];
161
237
        strcpy(name, "buf_");
162
238
        strcpy(name + 4, source->id.name);
163
239
 
164
 
        sound = alloc_libblock(&CTX_data_main(C)->sound, ID_SO, name);
 
240
        sound = alloc_libblock(&bmain->sound, ID_SO, name);
165
241
 
166
242
        sound->child_sound = source;
167
243
        sound->type = SOUND_TYPE_BUFFER;
168
244
 
169
 
        sound_load(CTX_data_main(C), sound);
 
245
        sound_load(bmain, sound);
170
246
 
171
 
        if(!sound->playback_handle)
 
247
        if (!sound->playback_handle)
172
248
        {
173
 
                free_libblock(&CTX_data_main(C)->sound, sound);
 
249
                free_libblock(&bmain->sound, sound);
174
250
                sound = NULL;
175
251
        }
176
252
 
177
253
        return sound;
178
254
}
179
255
 
180
 
struct bSound* sound_new_limiter(struct bContext *C, struct bSound *source, float start, float end)
 
256
struct bSound* sound_new_limiter(struct Main *bmain, struct bSound *source, float start, float end)
181
257
{
182
258
        bSound* sound = NULL;
183
259
 
184
 
        char name[25];
 
260
        char name[MAX_ID_NAME+5];
185
261
        strcpy(name, "lim_");
186
262
        strcpy(name + 4, source->id.name);
187
263
 
188
 
        sound = alloc_libblock(&CTX_data_main(C)->sound, ID_SO, name);
 
264
        sound = alloc_libblock(&bmain->sound, ID_SO, name);
189
265
 
190
266
        sound->child_sound = source;
191
267
        sound->start = start;
192
268
        sound->end = end;
193
269
        sound->type = SOUND_TYPE_LIMITER;
194
270
 
195
 
        sound_load(CTX_data_main(C), sound);
 
271
        sound_load(bmain, sound);
196
272
 
197
 
        if(!sound->playback_handle)
 
273
        if (!sound->playback_handle)
198
274
        {
199
 
                free_libblock(&CTX_data_main(C)->sound, sound);
 
275
                free_libblock(&bmain->sound, sound);
200
276
                sound = NULL;
201
277
        }
202
278
 
204
280
}
205
281
#endif
206
282
 
207
 
void sound_delete(struct bContext *C, struct bSound* sound)
 
283
void sound_delete(struct Main *bmain, struct bSound* sound)
208
284
{
209
 
        if(sound)
 
285
        if (sound)
210
286
        {
211
287
                sound_free(sound);
212
288
 
213
 
                free_libblock(&CTX_data_main(C)->sound, sound);
 
289
                free_libblock(&bmain->sound, sound);
214
290
        }
215
291
}
216
292
 
217
 
void sound_cache(struct bSound* sound, int ignore)
 
293
void sound_cache(struct bSound* sound)
218
294
{
219
 
        if(sound->cache && !ignore)
 
295
        sound->flags |= SOUND_FLAGS_CACHING;
 
296
        if (sound->cache)
220
297
                AUD_unload(sound->cache);
221
298
 
222
299
        sound->cache = AUD_bufferSound(sound->handle);
223
 
        sound->playback_handle = sound->cache;
 
300
        if (sound->cache)
 
301
                sound->playback_handle = sound->cache;
 
302
        else
 
303
                sound->playback_handle = sound->handle;
 
304
}
 
305
 
 
306
void sound_cache_notifying(struct Main* main, struct bSound* sound)
 
307
{
 
308
        sound_cache(sound);
 
309
        sound_update_sequencer(main, sound);
224
310
}
225
311
 
226
312
void sound_delete_cache(struct bSound* sound)
227
313
{
228
 
        if(sound->cache)
 
314
        sound->flags &= ~SOUND_FLAGS_CACHING;
 
315
        if (sound->cache)
229
316
        {
230
317
                AUD_unload(sound->cache);
231
318
                sound->cache = NULL;
235
322
 
236
323
void sound_load(struct Main *bmain, struct bSound* sound)
237
324
{
238
 
        if(sound)
 
325
        if (sound)
239
326
        {
240
 
                if(sound->handle)
 
327
                if (sound->cache)
 
328
                {
 
329
                        AUD_unload(sound->cache);
 
330
                        sound->cache = NULL;
 
331
                }
 
332
 
 
333
                if (sound->handle)
241
334
                {
242
335
                        AUD_unload(sound->handle);
243
336
                        sound->handle = NULL;
244
337
                        sound->playback_handle = NULL;
245
338
                }
246
339
 
 
340
                sound_free_waveform(sound);
 
341
 
247
342
// XXX unused currently
248
343
#if 0
249
344
                switch(sound->type)
252
347
#endif
253
348
                {
254
349
                        char fullpath[FILE_MAX];
255
 
                        char *path;
256
350
 
257
351
                        /* load sound */
258
352
                        PackedFile* pf = sound->packedfile;
259
353
 
260
 
                        /* dont modify soundact->sound->name, only change a copy */
 
354
                        /* don't modify soundact->sound->name, only change a copy */
261
355
                        BLI_strncpy(fullpath, sound->name, sizeof(fullpath));
262
 
 
263
 
                        if(sound->id.lib)
264
 
                                path = sound->id.lib->filepath;
265
 
                        else
266
 
                                path = /*bmain ? bmain->name :*/ G.sce;
267
 
 
268
 
                        BLI_path_abs(fullpath, path);
 
356
                        BLI_path_abs(fullpath, ID_BLEND_PATH(bmain, &sound->id));
269
357
 
270
358
                        /* but we need a packed file then */
271
359
                        if (pf)
273
361
                        /* or else load it from disk */
274
362
                        else
275
363
                                sound->handle = AUD_load(fullpath);
276
 
                } // XXX
 
364
                }
277
365
// XXX unused currently
278
366
#if 0
279
367
                        break;
280
368
                }
281
369
                case SOUND_TYPE_BUFFER:
282
 
                        if(sound->child_sound && sound->child_sound->handle)
 
370
                        if (sound->child_sound && sound->child_sound->handle)
283
371
                                sound->handle = AUD_bufferSound(sound->child_sound->handle);
284
372
                        break;
285
373
                case SOUND_TYPE_LIMITER:
286
 
                        if(sound->child_sound && sound->child_sound->handle)
 
374
                        if (sound->child_sound && sound->child_sound->handle)
287
375
                                sound->handle = AUD_limitSound(sound->child_sound, sound->start, sound->end);
288
376
                        break;
289
377
                }
290
378
#endif
291
 
                if(sound->cache)
 
379
                if (sound->flags & SOUND_FLAGS_MONO)
 
380
                {
 
381
                        void* handle = AUD_monoSound(sound->handle);
 
382
                        AUD_unload(sound->handle);
 
383
                        sound->handle = handle;
 
384
                }
 
385
 
 
386
                if (sound->flags & SOUND_FLAGS_CACHING)
 
387
                {
 
388
                        sound->cache = AUD_bufferSound(sound->handle);
 
389
                }
 
390
 
 
391
                if (sound->cache)
292
392
                        sound->playback_handle = sound->cache;
293
393
                else
294
394
                        sound->playback_handle = sound->handle;
295
 
        }
296
 
}
297
 
 
298
 
void sound_free(struct bSound* sound)
299
 
{
300
 
        if (sound->packedfile)
301
 
        {
302
 
                freePackedFile(sound->packedfile);
303
 
                sound->packedfile = NULL;
304
 
        }
305
 
 
306
 
        if(sound->handle)
307
 
        {
308
 
                AUD_unload(sound->handle);
309
 
                sound->handle = NULL;
310
 
                sound->playback_handle = NULL;
311
 
        }
312
 
}
313
 
 
314
 
static float sound_get_volume(Scene* scene, Sequence* sequence, float time)
315
 
{
316
 
        AnimData *adt= BKE_animdata_from_id(&scene->id);
317
 
        FCurve *fcu = NULL;
318
 
        char buf[64];
319
 
        
320
 
        /* NOTE: this manually constructed path needs to be used here to avoid problems with RNA crashes */
321
 
        sprintf(buf, "sequence_editor.sequences_all[\"%s\"].volume", sequence->name+2);
322
 
        if (adt && adt->action && adt->action->curves.first)
323
 
                fcu= list_find_fcurve(&adt->action->curves, buf, 0);
324
 
        
325
 
        if(fcu)
326
 
                return evaluate_fcurve(fcu, time * FPS);
327
 
        else
328
 
                return sequence->volume;
 
395
 
 
396
                sound_update_sequencer(bmain, sound);
 
397
        }
329
398
}
330
399
 
331
400
AUD_Device* sound_mixdown(struct Scene *scene, AUD_DeviceSpecs specs, int start, float volume)
332
401
{
333
 
        AUD_Device* mixdown = AUD_openReadDevice(specs);
334
 
 
335
 
        AUD_setDeviceVolume(mixdown, volume);
336
 
 
337
 
        AUD_playDevice(mixdown, scene->sound_scene, start / FPS);
338
 
 
339
 
        return mixdown;
 
402
        return AUD_openMixdownDevice(specs, scene->sound_scene, volume, start / FPS);
340
403
}
341
404
 
342
405
void sound_create_scene(struct Scene *scene)
343
406
{
344
 
        scene->sound_scene = AUD_createSequencer(scene, (AUD_volumeFunction)&sound_get_volume);
 
407
        scene->sound_scene = AUD_createSequencer(FPS, scene->audio.flag & AUDIO_MUTE);
 
408
        AUD_updateSequencerData(scene->sound_scene, scene->audio.speed_of_sound,
 
409
                                                        scene->audio.doppler_factor, scene->audio.distance_model);
 
410
        scene->sound_scene_handle = NULL;
 
411
        scene->sound_scrub_handle = NULL;
 
412
        scene->speaker_handles = NULL;
345
413
}
346
414
 
347
415
void sound_destroy_scene(struct Scene *scene)
348
416
{
349
 
        if(scene->sound_scene_handle)
 
417
        if (scene->sound_scene_handle)
350
418
                AUD_stop(scene->sound_scene_handle);
351
 
        if(scene->sound_scene)
 
419
        if (scene->sound_scrub_handle)
 
420
                AUD_stop(scene->sound_scrub_handle);
 
421
        if (scene->sound_scene)
352
422
                AUD_destroySequencer(scene->sound_scene);
 
423
        if (scene->speaker_handles)
 
424
                AUD_destroySet(scene->speaker_handles);
 
425
}
 
426
 
 
427
void sound_mute_scene(struct Scene *scene, int muted)
 
428
{
 
429
        if (scene->sound_scene)
 
430
                AUD_setSequencerMuted(scene->sound_scene, muted);
 
431
}
 
432
 
 
433
void sound_update_fps(struct Scene *scene)
 
434
{
 
435
        if (scene->sound_scene)
 
436
                AUD_setSequencerFPS(scene->sound_scene, FPS);
 
437
}
 
438
 
 
439
void sound_update_scene_listener(struct Scene *scene)
 
440
{
 
441
        AUD_updateSequencerData(scene->sound_scene, scene->audio.speed_of_sound,
 
442
                                                        scene->audio.doppler_factor, scene->audio.distance_model);
353
443
}
354
444
 
355
445
void* sound_scene_add_scene_sound(struct Scene *scene, struct Sequence* sequence, int startframe, int endframe, int frameskip)
356
446
{
357
 
        if(scene != sequence->scene)
358
 
                return AUD_addSequencer(scene->sound_scene, &(sequence->scene->sound_scene), startframe / FPS, endframe / FPS, frameskip / FPS, sequence);
 
447
        if (scene != sequence->scene)
 
448
                return AUD_addSequence(scene->sound_scene, sequence->scene->sound_scene, startframe / FPS, endframe / FPS, frameskip / FPS);
359
449
        return NULL;
360
450
}
361
451
 
 
452
void* sound_scene_add_scene_sound_defaults(struct Scene *scene, struct Sequence* sequence)
 
453
{
 
454
        return sound_scene_add_scene_sound(scene, sequence,
 
455
                                           sequence->startdisp, sequence->enddisp,
 
456
                                           sequence->startofs + sequence->anim_startofs);
 
457
}
 
458
 
362
459
void* sound_add_scene_sound(struct Scene *scene, struct Sequence* sequence, int startframe, int endframe, int frameskip)
363
460
{
364
 
        return AUD_addSequencer(scene->sound_scene, &(sequence->sound->playback_handle), startframe / FPS, endframe / FPS, frameskip / FPS, sequence);
 
461
        void* handle = AUD_addSequence(scene->sound_scene, sequence->sound->playback_handle, startframe / FPS, endframe / FPS, frameskip / FPS);
 
462
        AUD_muteSequence(handle, (sequence->flag & SEQ_MUTE) != 0);
 
463
        AUD_setSequenceAnimData(handle, AUD_AP_VOLUME, CFRA, &sequence->volume, 0);
 
464
        AUD_setSequenceAnimData(handle, AUD_AP_PITCH, CFRA, &sequence->pitch, 0);
 
465
        AUD_setSequenceAnimData(handle, AUD_AP_PANNING, CFRA, &sequence->pan, 0);
 
466
        return handle;
 
467
}
 
468
 
 
469
void* sound_add_scene_sound_defaults(struct Scene *scene, struct Sequence* sequence)
 
470
{
 
471
        return sound_add_scene_sound(scene, sequence,
 
472
                                     sequence->startdisp, sequence->enddisp,
 
473
                                     sequence->startofs + sequence->anim_startofs);
365
474
}
366
475
 
367
476
void sound_remove_scene_sound(struct Scene *scene, void* handle)
368
477
{
369
 
        AUD_removeSequencer(scene->sound_scene, handle);
 
478
        AUD_removeSequence(scene->sound_scene, handle);
370
479
}
371
480
 
372
 
void sound_mute_scene_sound(struct Scene *scene, void* handle, char mute)
 
481
void sound_mute_scene_sound(void* handle, char mute)
373
482
{
374
 
        AUD_muteSequencer(scene->sound_scene, handle, mute);
 
483
        AUD_muteSequence(handle, mute);
375
484
}
376
485
 
377
486
void sound_move_scene_sound(struct Scene *scene, void* handle, int startframe, int endframe, int frameskip)
378
487
{
379
 
        AUD_moveSequencer(scene->sound_scene, handle, startframe / FPS, endframe / FPS, frameskip / FPS);
380
 
}
381
 
 
382
 
void sound_start_play_scene(struct Scene *scene)
383
 
{
384
 
        AUD_Sound* sound;
385
 
        sound = AUD_loopSound(scene->sound_scene);
386
 
        scene->sound_scene_handle = AUD_play(sound, 1);
387
 
        AUD_unload(sound);
 
488
        AUD_moveSequence(handle, startframe / FPS, endframe / FPS, frameskip / FPS);
 
489
}
 
490
 
 
491
void sound_move_scene_sound_defaults(struct Scene *scene, struct Sequence* sequence)
 
492
{
 
493
        if (sequence->scene_sound) {
 
494
                sound_move_scene_sound(scene, sequence->scene_sound,
 
495
                                       sequence->startdisp, sequence->enddisp,
 
496
                                       sequence->startofs + sequence->anim_startofs);
 
497
        }
 
498
}
 
499
 
 
500
void sound_update_scene_sound(void* handle, struct bSound* sound)
 
501
{
 
502
        AUD_updateSequenceSound(handle, sound->playback_handle);
 
503
}
 
504
 
 
505
void sound_set_cfra(int cfra)
 
506
{
 
507
        sound_cfra = cfra;
 
508
}
 
509
 
 
510
void sound_set_scene_volume(struct Scene *scene, float volume)
 
511
{
 
512
        AUD_setSequencerAnimData(scene->sound_scene, AUD_AP_VOLUME, CFRA, &volume, (scene->audio.flag & AUDIO_VOLUME_ANIMATED) != 0);
 
513
}
 
514
 
 
515
void sound_set_scene_sound_volume(void* handle, float volume, char animated)
 
516
{
 
517
        AUD_setSequenceAnimData(handle, AUD_AP_VOLUME, sound_cfra, &volume, animated);
 
518
}
 
519
 
 
520
void sound_set_scene_sound_pitch(void* handle, float pitch, char animated)
 
521
{
 
522
        AUD_setSequenceAnimData(handle, AUD_AP_PITCH, sound_cfra, &pitch, animated);
 
523
}
 
524
 
 
525
void sound_set_scene_sound_pan(void* handle, float pan, char animated)
 
526
{
 
527
        AUD_setSequenceAnimData(handle, AUD_AP_PANNING, sound_cfra, &pan, animated);
 
528
}
 
529
 
 
530
void sound_update_sequencer(struct Main* main, struct bSound* sound)
 
531
{
 
532
        struct Scene* scene;
 
533
 
 
534
        for (scene = main->scene.first; scene; scene = scene->id.next) {
 
535
                seq_update_sound(scene, sound);
 
536
        }
 
537
}
 
538
 
 
539
static void sound_start_play_scene(struct Scene *scene)
 
540
{
 
541
        if (scene->sound_scene_handle)
 
542
                AUD_stop(scene->sound_scene_handle);
 
543
 
 
544
        AUD_setSequencerDeviceSpecs(scene->sound_scene);
 
545
 
 
546
        if ((scene->sound_scene_handle = AUD_play(scene->sound_scene, 1)))
 
547
                AUD_setLoop(scene->sound_scene_handle, -1);
388
548
}
389
549
 
390
550
void sound_play_scene(struct Scene *scene)
392
552
        AUD_Status status;
393
553
        AUD_lock();
394
554
 
395
 
        status = AUD_getStatus(scene->sound_scene_handle);
 
555
        status = scene->sound_scene_handle ? AUD_getStatus(scene->sound_scene_handle) : AUD_STATUS_INVALID;
396
556
 
397
 
        if(status == AUD_STATUS_INVALID)
 
557
        if (status == AUD_STATUS_INVALID)
398
558
                sound_start_play_scene(scene);
399
559
 
400
 
        AUD_setLoop(scene->sound_scene_handle, -1, -1);
 
560
        if (!scene->sound_scene_handle)
 
561
        {
 
562
                AUD_unlock();
 
563
                return;
 
564
        }
401
565
 
402
 
        if(status != AUD_STATUS_PLAYING)
 
566
        if (status != AUD_STATUS_PLAYING)
403
567
        {
404
568
                AUD_seek(scene->sound_scene_handle, CFRA / FPS);
405
569
                AUD_resume(scene->sound_scene_handle);
406
570
        }
407
571
 
408
 
        if(scene->audio.flag & AUDIO_SYNC)
 
572
        if (scene->audio.flag & AUDIO_SYNC)
409
573
                AUD_startPlayback();
410
574
 
411
575
        AUD_unlock();
413
577
 
414
578
void sound_stop_scene(struct Scene *scene)
415
579
{
416
 
        AUD_pause(scene->sound_scene_handle);
 
580
        if (scene->sound_scene_handle)
 
581
        {
 
582
                AUD_pause(scene->sound_scene_handle);
417
583
 
418
 
        if(scene->audio.flag & AUDIO_SYNC)
419
 
                AUD_stopPlayback();
 
584
                if (scene->audio.flag & AUDIO_SYNC)
 
585
                        AUD_stopPlayback();
 
586
        }
420
587
}
421
588
 
422
 
void sound_seek_scene(struct bContext *C)
 
589
void sound_seek_scene(struct Main *bmain, struct Scene *scene)
423
590
{
424
 
        struct Scene *scene = CTX_data_scene(C);
425
591
        AUD_Status status;
 
592
        bScreen *screen;
 
593
        int animation_playing;
426
594
 
427
595
        AUD_lock();
428
596
 
429
 
        status = AUD_getStatus(scene->sound_scene_handle);
 
597
        status = scene->sound_scene_handle ? AUD_getStatus(scene->sound_scene_handle) : AUD_STATUS_INVALID;
430
598
 
431
 
        if(status == AUD_STATUS_INVALID)
 
599
        if (status == AUD_STATUS_INVALID)
432
600
        {
433
601
                sound_start_play_scene(scene);
 
602
 
 
603
                if (!scene->sound_scene_handle)
 
604
                {
 
605
                        AUD_unlock();
 
606
                        return;
 
607
                }
 
608
 
434
609
                AUD_pause(scene->sound_scene_handle);
435
610
        }
436
611
 
437
 
        if(scene->audio.flag & AUDIO_SCRUB && !CTX_wm_screen(C)->animtimer)
 
612
        animation_playing = 0;
 
613
        for (screen=bmain->screen.first; screen; screen=screen->id.next) {
 
614
                if (screen->animtimer) {
 
615
                        animation_playing = 1;
 
616
                }
 
617
        }
 
618
 
 
619
        if (scene->audio.flag & AUDIO_SCRUB && !animation_playing)
438
620
        {
439
 
                AUD_setLoop(scene->sound_scene_handle, -1, 1 / FPS);
440
 
                if(scene->audio.flag & AUDIO_SYNC)
 
621
                if (scene->audio.flag & AUDIO_SYNC)
 
622
                {
 
623
                        AUD_seek(scene->sound_scene_handle, CFRA / FPS);
441
624
                        AUD_seekSequencer(scene->sound_scene_handle, CFRA / FPS);
 
625
                }
442
626
                else
443
627
                        AUD_seek(scene->sound_scene_handle, CFRA / FPS);
444
628
                AUD_resume(scene->sound_scene_handle);
 
629
                if (scene->sound_scrub_handle && AUD_getStatus(scene->sound_scrub_handle) != AUD_STATUS_INVALID)
 
630
                        AUD_seek(scene->sound_scrub_handle, 0);
 
631
                else {
 
632
                        if (scene->sound_scrub_handle)
 
633
                                AUD_stop(scene->sound_scrub_handle);
 
634
                        scene->sound_scrub_handle = AUD_pauseAfter(scene->sound_scene_handle, 1 / FPS);
 
635
                }
445
636
        }
446
 
        else
447
 
        {
448
 
                if(scene->audio.flag & AUDIO_SYNC)
 
637
        else {
 
638
                if (scene->audio.flag & AUDIO_SYNC)
449
639
                        AUD_seekSequencer(scene->sound_scene_handle, CFRA / FPS);
450
 
                else
451
 
                {
452
 
                        if(status == AUD_STATUS_PLAYING)
 
640
                else {
 
641
                        if (status == AUD_STATUS_PLAYING)
453
642
                                AUD_seek(scene->sound_scene_handle, CFRA / FPS);
454
643
                }
455
644
        }
459
648
 
460
649
float sound_sync_scene(struct Scene *scene)
461
650
{
462
 
        if(scene->audio.flag & AUDIO_SYNC)
463
 
                return AUD_getSequencerPosition(scene->sound_scene_handle);
464
 
        else
465
 
                return AUD_getPosition(scene->sound_scene_handle);
 
651
        if (scene->sound_scene_handle)
 
652
        {
 
653
                if (scene->audio.flag & AUDIO_SYNC)
 
654
                        return AUD_getSequencerPosition(scene->sound_scene_handle);
 
655
                else
 
656
                        return AUD_getPosition(scene->sound_scene_handle);
 
657
        }
 
658
        return NAN_FLT;
466
659
}
467
660
 
468
661
int sound_scene_playing(struct Scene *scene)
469
662
{
470
 
        if(scene->audio.flag & AUDIO_SYNC)
 
663
        if (scene->audio.flag & AUDIO_SYNC)
471
664
                return AUD_doesPlayback();
472
665
        else
473
666
                return -1;
474
667
}
475
668
 
476
 
int sound_read_sound_buffer(struct bSound* sound, float* buffer, int length, float start, float end)
477
 
{
478
 
        AUD_Sound* limiter = AUD_limitSound(sound->cache, start, end);
479
 
        return AUD_readSound(limiter, buffer, length);
480
 
        AUD_unload(limiter);
481
 
}
 
669
void sound_free_waveform(struct bSound* sound)
 
670
{
 
671
        if (sound->waveform)
 
672
        {
 
673
                MEM_freeN(((SoundWaveform*)sound->waveform)->data);
 
674
                MEM_freeN(sound->waveform);
 
675
        }
 
676
 
 
677
        sound->waveform = NULL;
 
678
}
 
679
 
 
680
void sound_read_waveform(struct bSound* sound)
 
681
{
 
682
        AUD_SoundInfo info;
 
683
 
 
684
        info = AUD_getInfo(sound->playback_handle);
 
685
 
 
686
        if (info.length > 0)
 
687
        {
 
688
                SoundWaveform* waveform = MEM_mallocN(sizeof(SoundWaveform), "SoundWaveform");
 
689
                int length = info.length * SOUND_WAVE_SAMPLES_PER_SECOND;
 
690
 
 
691
                waveform->data = MEM_mallocN(length * sizeof(float) * 3, "SoundWaveform.samples");
 
692
                waveform->length = AUD_readSound(sound->playback_handle, waveform->data, length, SOUND_WAVE_SAMPLES_PER_SECOND);
 
693
 
 
694
                sound_free_waveform(sound);
 
695
                sound->waveform = waveform;
 
696
        }
 
697
}
 
698
 
 
699
void sound_update_scene(struct Scene* scene)
 
700
{
 
701
        Object* ob;
 
702
        Base* base;
 
703
        NlaTrack* track;
 
704
        NlaStrip* strip;
 
705
        Speaker* speaker;
 
706
        Scene* sce_it;
 
707
 
 
708
        void* new_set = AUD_createSet();
 
709
        void* handle;
 
710
        float quat[4];
 
711
 
 
712
        for (SETLOOPER(scene, sce_it, base))
 
713
        {
 
714
                ob = base->object;
 
715
                if (ob->type == OB_SPEAKER)
 
716
                {
 
717
                        if (ob->adt)
 
718
                        {
 
719
                                for (track = ob->adt->nla_tracks.first; track; track = track->next)
 
720
                                {
 
721
                                        for (strip = track->strips.first; strip; strip = strip->next)
 
722
                                        {
 
723
                                                if (strip->type == NLASTRIP_TYPE_SOUND)
 
724
                                                {
 
725
                                                        speaker = (Speaker*)ob->data;
 
726
 
 
727
                                                        if (AUD_removeSet(scene->speaker_handles, strip->speaker_handle))
 
728
                                                        {
 
729
                                                                if (speaker->sound)
 
730
                                                                        AUD_moveSequence(strip->speaker_handle, strip->start / FPS, -1, 0);
 
731
                                                                else {
 
732
                                                                        AUD_removeSequence(scene->sound_scene, strip->speaker_handle);
 
733
                                                                        strip->speaker_handle = NULL;
 
734
                                                                }
 
735
                                                        }
 
736
                                                        else {
 
737
                                                                if (speaker->sound)
 
738
                                                                {
 
739
                                                                        strip->speaker_handle = AUD_addSequence(scene->sound_scene, speaker->sound->playback_handle, strip->start / FPS, -1, 0);
 
740
                                                                        AUD_setRelativeSequence(strip->speaker_handle, 0);
 
741
                                                                }
 
742
                                                        }
 
743
 
 
744
                                                        if (strip->speaker_handle)
 
745
                                                        {
 
746
                                                                AUD_addSet(new_set, strip->speaker_handle);
 
747
                                                                AUD_updateSequenceData(strip->speaker_handle, speaker->volume_max,
 
748
                                                                                                           speaker->volume_min, speaker->distance_max,
 
749
                                                                                                           speaker->distance_reference, speaker->attenuation,
 
750
                                                                                                           speaker->cone_angle_outer, speaker->cone_angle_inner,
 
751
                                                                                                           speaker->cone_volume_outer);
 
752
 
 
753
                                                                mat4_to_quat(quat, ob->obmat);
 
754
                                                                AUD_setSequenceAnimData(strip->speaker_handle, AUD_AP_LOCATION, CFRA, ob->obmat[3], 1);
 
755
                                                                AUD_setSequenceAnimData(strip->speaker_handle, AUD_AP_ORIENTATION, CFRA, quat, 1);
 
756
                                                                AUD_setSequenceAnimData(strip->speaker_handle, AUD_AP_VOLUME, CFRA, &speaker->volume, 1);
 
757
                                                                AUD_setSequenceAnimData(strip->speaker_handle, AUD_AP_PITCH, CFRA, &speaker->pitch, 1);
 
758
                                                                AUD_updateSequenceSound(strip->speaker_handle, speaker->sound->playback_handle);
 
759
                                                                AUD_muteSequence(strip->speaker_handle, ((strip->flag & NLASTRIP_FLAG_MUTED) != 0) || ((speaker->flag & SPK_MUTED) != 0));
 
760
                                                        }
 
761
                                                }
 
762
                                        }
 
763
                                }
 
764
                        }
 
765
                }
 
766
        }
 
767
 
 
768
        while ((handle = AUD_getSet(scene->speaker_handles)))
 
769
        {
 
770
                AUD_removeSequence(scene->sound_scene, handle);
 
771
        }
 
772
 
 
773
        if (scene->camera)
 
774
        {
 
775
                mat4_to_quat(quat, scene->camera->obmat);
 
776
                AUD_setSequencerAnimData(scene->sound_scene, AUD_AP_LOCATION, CFRA, scene->camera->obmat[3], 1);
 
777
                AUD_setSequencerAnimData(scene->sound_scene, AUD_AP_ORIENTATION, CFRA, quat, 1);
 
778
        }
 
779
 
 
780
        AUD_destroySet(scene->speaker_handles);
 
781
        scene->speaker_handles = new_set;
 
782
}
 
783
 
 
784
void* sound_get_factory(void* sound)
 
785
{
 
786
        return ((struct bSound*) sound)->playback_handle;
 
787
}
 
788
 
 
789
#else // WITH_AUDASPACE
 
790
 
 
791
#include "BLI_utildefines.h"
 
792
 
 
793
int sound_define_from_str(const char *UNUSED(str)) { return -1;}
 
794
void sound_force_device(int UNUSED(device)) {}
 
795
void sound_init_once(void) {}
 
796
void sound_init(struct Main *UNUSED(bmain)) {}
 
797
void sound_exit(void) {}
 
798
void sound_cache(struct bSound* UNUSED(sound)) { }
 
799
void sound_delete_cache(struct bSound* UNUSED(sound)) {}
 
800
void sound_load(struct Main *UNUSED(bmain), struct bSound* UNUSED(sound)) {}
 
801
void sound_create_scene(struct Scene *UNUSED(scene)) {}
 
802
void sound_destroy_scene(struct Scene *UNUSED(scene)) {}
 
803
void sound_mute_scene(struct Scene *UNUSED(scene), int UNUSED(muted)) {}
 
804
void* sound_scene_add_scene_sound(struct Scene *UNUSED(scene), struct Sequence* UNUSED(sequence), int UNUSED(startframe), int UNUSED(endframe), int UNUSED(frameskip)) { return NULL; }
 
805
void* sound_scene_add_scene_sound_defaults(struct Scene *UNUSED(scene), struct Sequence* UNUSED(sequence)) { return NULL; }
 
806
void* sound_add_scene_sound(struct Scene *UNUSED(scene), struct Sequence* UNUSED(sequence), int UNUSED(startframe), int UNUSED(endframe), int UNUSED(frameskip)) { return NULL; }
 
807
void* sound_add_scene_sound_defaults(struct Scene *UNUSED(scene), struct Sequence* UNUSED(sequence)) { return NULL; }
 
808
void sound_remove_scene_sound(struct Scene *UNUSED(scene), void* UNUSED(handle)) {}
 
809
void sound_mute_scene_sound(void* UNUSED(handle), char UNUSED(mute)) {}
 
810
void sound_move_scene_sound(struct Scene *UNUSED(scene), void* UNUSED(handle), int UNUSED(startframe), int UNUSED(endframe), int UNUSED(frameskip)) {}
 
811
void sound_move_scene_sound_defaults(struct Scene *UNUSED(scene), struct Sequence *UNUSED(sequence)) {}
 
812
void sound_play_scene(struct Scene *UNUSED(scene)) {}
 
813
void sound_stop_scene(struct Scene *UNUSED(scene)) {}
 
814
void sound_seek_scene(struct Main *UNUSED(bmain), struct Scene *UNUSED(scene)) {}
 
815
float sound_sync_scene(struct Scene *UNUSED(scene)) { return NAN_FLT; }
 
816
int sound_scene_playing(struct Scene *UNUSED(scene)) { return -1; }
 
817
int sound_read_sound_buffer(struct bSound* UNUSED(sound), float* UNUSED(buffer), int UNUSED(length), float UNUSED(start), float UNUSED(end)) { return 0; }
 
818
void sound_read_waveform(struct bSound* sound) { (void)sound; }
 
819
void sound_init_main(struct Main *bmain) { (void)bmain; }
 
820
void sound_set_cfra(int cfra) { (void)cfra; }
 
821
void sound_update_sequencer(struct Main* main, struct bSound* sound) { (void)main; (void)sound; }
 
822
void sound_update_scene(struct Scene* scene) { (void)scene; }
 
823
void sound_update_scene_sound(void* handle, struct bSound* sound) { (void)handle; (void)sound; }
 
824
void sound_update_scene_listener(struct Scene *scene) { (void)scene; }
 
825
void sound_update_fps(struct Scene *scene) { (void)scene; }
 
826
void sound_set_scene_sound_volume(void* handle, float volume, char animated) { (void)handle; (void)volume; (void)animated; }
 
827
void sound_set_scene_sound_pan(void* handle, float pan, char animated) { (void)handle; (void)pan; (void)animated; }
 
828
void sound_set_scene_volume(struct Scene *scene, float volume) { (void)scene; (void)volume; }
 
829
void sound_set_scene_sound_pitch(void* handle, float pitch, char animated) { (void)handle; (void)pitch; (void)animated; }
 
830
 
 
831
#endif // WITH_AUDASPACE