b81839f80fbae9f3bfe4fa5f5beb73e017a721fa
[blender.git] / source / blender / blenkernel / intern / sound.c
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
30  */
31
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_threads.h"
40
41 #include "DNA_anim_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_sequence_types.h"
45 #include "DNA_packedFile_types.h"
46 #include "DNA_screen_types.h"
47 #include "DNA_sound_types.h"
48 #include "DNA_speaker_types.h"
49
50 #ifdef WITH_AUDASPACE
51 #  include AUD_SOUND_H
52 #  include AUD_SEQUENCE_H
53 #  include AUD_HANDLE_H
54 #  include AUD_SPECIAL_H
55 #  ifdef WITH_SYSTEM_AUDASPACE
56 #    include "../../../intern/audaspace/intern/AUD_Set.h"
57 #  endif
58 #endif
59
60 #include "BKE_global.h"
61 #include "BKE_main.h"
62 #include "BKE_sound.h"
63 #include "BKE_library.h"
64 #include "BKE_packedFile.h"
65 #include "BKE_sequencer.h"
66 #include "BKE_scene.h"
67
68 #ifdef WITH_AUDASPACE
69 /* evil global ;-) */
70 static int sound_cfra;
71 #endif
72
73 bSound *BKE_sound_new_file(struct Main *bmain, const char *filename)
74 {
75         bSound *sound;
76
77         char str[FILE_MAX];
78         const 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 = BKE_libblock_alloc(bmain, ID_SO, filename + len);
93         BLI_strncpy(sound->name, filename, FILE_MAX);
94         /* sound->type = SOUND_TYPE_FILE; */ /* XXX unused currently */
95
96         BKE_sound_load(bmain, sound);
97
98         return sound;
99 }
100
101 void BKE_sound_free(bSound *sound)
102 {
103         if (sound->packedfile) {
104                 freePackedFile(sound->packedfile);
105                 sound->packedfile = NULL;
106         }
107
108 #ifdef WITH_AUDASPACE
109         if (sound->handle) {
110                 AUD_Sound_free(sound->handle);
111                 sound->handle = NULL;
112                 sound->playback_handle = NULL;
113         }
114
115         if (sound->cache) {
116                 AUD_Sound_free(sound->cache);
117                 sound->cache = NULL;
118         }
119
120         BKE_sound_free_waveform(sound);
121         
122         if (sound->spinlock) {
123                 BLI_spin_end(sound->spinlock);
124                 MEM_freeN(sound->spinlock);
125                 sound->spinlock = NULL;
126         }
127         
128 #endif  /* WITH_AUDASPACE */
129 }
130
131 #ifdef WITH_AUDASPACE
132
133 static int force_device = -1;
134
135 #ifdef WITH_JACK
136 static void sound_sync_callback(void *data, int mode, float time)
137 {
138         struct Main *bmain = (struct Main *)data;
139         struct Scene *scene;
140
141         scene = bmain->scene.first;
142         while (scene) {
143                 if (scene->audio.flag & AUDIO_SYNC) {
144                         if (mode)
145                                 BKE_sound_play_scene(scene);
146                         else
147                                 BKE_sound_stop_scene(scene);
148                         if (scene->playback_handle)
149                                 AUD_Handle_setPosition(scene->playback_handle, time);
150                 }
151                 scene = scene->id.next;
152         }
153 }
154 #endif
155
156 int BKE_sound_define_from_str(const char *str)
157 {
158         if (BLI_strcaseeq(str, "NULL"))
159                 return 0;
160         if (BLI_strcaseeq(str, "SDL"))
161                 return 1;
162         if (BLI_strcaseeq(str, "OPENAL"))
163                 return 2;
164         if (BLI_strcaseeq(str, "JACK"))
165                 return 3;
166
167         return -1;
168 }
169
170 void BKE_sound_force_device(int device)
171 {
172         force_device = device;
173 }
174
175 void BKE_sound_init_once(void)
176 {
177         AUD_initOnce();
178         atexit(BKE_sound_exit_once);
179 }
180
181 static AUD_Device* sound_device;
182
183 void* BKE_sound_get_device(void)
184 {
185         return sound_device;
186 }
187
188 void BKE_sound_init(struct Main *bmain)
189 {
190         AUD_DeviceSpecs specs;
191         int device, buffersize;
192         const char* device_name;
193
194         device = U.audiodevice;
195         buffersize = U.mixbufsize;
196         specs.channels = U.audiochannels;
197         specs.format = U.audioformat;
198         specs.rate = U.audiorate;
199
200         if (force_device >= 0)
201                 device = force_device;
202
203         switch(device)
204         {
205         case 1:
206                 device_name = "SDL";
207                 break;
208         case 2:
209                 device_name = "OpenAL";
210                 break;
211         case 3:
212                 device_name = "Jack";
213                 break;
214         default:
215                 device_name = "Null";
216                 break;
217         }
218
219         if (buffersize < 128)
220                 buffersize = 1024;
221
222         if (specs.rate < AUD_RATE_8000)
223                 specs.rate = AUD_RATE_44100;
224
225         if (specs.format <= AUD_FORMAT_INVALID)
226                 specs.format = AUD_FORMAT_S16;
227
228         if (specs.channels <= AUD_CHANNELS_INVALID)
229                 specs.channels = AUD_CHANNELS_STEREO;
230
231         if (!(sound_device = AUD_init(device_name, specs, buffersize, "Blender")))
232                 sound_device = AUD_init("Null", specs, buffersize, "Blender");
233
234         BKE_sound_init_main(bmain);
235 }
236
237 void BKE_sound_init_main(struct Main *bmain)
238 {
239 #ifdef WITH_JACK
240         AUD_setSynchronizerCallback(sound_sync_callback, bmain);
241 #else
242         (void)bmain; /* unused */
243 #endif
244 }
245
246 void BKE_sound_exit(void)
247 {
248         AUD_exit(sound_device);
249         sound_device = NULL;
250 }
251
252 void BKE_sound_exit_once(void)
253 {
254         AUD_exit(sound_device);
255         sound_device = NULL;
256         AUD_exitOnce();
257 }
258
259 /* XXX unused currently */
260 #if 0
261 bSound *BKE_sound_new_buffer(struct Main *bmain, bSound *source)
262 {
263         bSound *sound = NULL;
264
265         char name[MAX_ID_NAME + 5];
266         strcpy(name, "buf_");
267         strcpy(name + 4, source->id.name);
268
269         sound = BKE_libblock_alloc(bmain, ID_SO, name);
270
271         sound->child_sound = source;
272         sound->type = SOUND_TYPE_BUFFER;
273
274         sound_load(bmain, sound);
275
276         return sound;
277 }
278
279 bSound *BKE_sound_new_limiter(struct Main *bmain, bSound *source, float start, float end)
280 {
281         bSound *sound = NULL;
282
283         char name[MAX_ID_NAME + 5];
284         strcpy(name, "lim_");
285         strcpy(name + 4, source->id.name);
286
287         sound = BKE_libblock_alloc(bmain, ID_SO, name);
288
289         sound->child_sound = source;
290         sound->start = start;
291         sound->end = end;
292         sound->type = SOUND_TYPE_LIMITER;
293
294         sound_load(bmain, sound);
295
296         return sound;
297 }
298 #endif
299
300 void BKE_sound_delete(struct Main *bmain, bSound *sound)
301 {
302         if (sound) {
303                 BKE_sound_free(sound);
304
305                 BKE_libblock_free(bmain, sound);
306         }
307 }
308
309 void BKE_sound_cache(bSound *sound)
310 {
311         sound->flags |= SOUND_FLAGS_CACHING;
312         if (sound->cache)
313                 AUD_Sound_free(sound->cache);
314
315         sound->cache = AUD_Sound_cache(sound->handle);
316         if (sound->cache)
317                 sound->playback_handle = sound->cache;
318         else
319                 sound->playback_handle = sound->handle;
320 }
321
322 void BKE_sound_delete_cache(bSound *sound)
323 {
324         sound->flags &= ~SOUND_FLAGS_CACHING;
325         if (sound->cache) {
326                 AUD_Sound_free(sound->cache);
327                 sound->cache = NULL;
328                 sound->playback_handle = sound->handle;
329         }
330 }
331
332 void BKE_sound_load(struct Main *bmain, bSound *sound)
333 {
334         if (sound) {
335                 if (sound->cache) {
336                         AUD_Sound_free(sound->cache);
337                         sound->cache = NULL;
338                 }
339
340                 if (sound->handle) {
341                         AUD_Sound_free(sound->handle);
342                         sound->handle = NULL;
343                         sound->playback_handle = NULL;
344                 }
345
346                 BKE_sound_free_waveform(sound);
347
348 /* XXX unused currently */
349 #if 0
350                 switch (sound->type)
351                 {
352                         case SOUND_TYPE_FILE:
353 #endif
354                 {
355                         char fullpath[FILE_MAX];
356
357                         /* load sound */
358                         PackedFile *pf = sound->packedfile;
359
360                         /* don't modify soundact->sound->name, only change a copy */
361                         BLI_strncpy(fullpath, sound->name, sizeof(fullpath));
362                         BLI_path_abs(fullpath, ID_BLEND_PATH(bmain, &sound->id));
363
364                         /* but we need a packed file then */
365                         if (pf)
366                                 sound->handle = AUD_Sound_bufferFile((unsigned char *) pf->data, pf->size);
367                         /* or else load it from disk */
368                         else
369                                 sound->handle = AUD_Sound_file(fullpath);
370                 }
371 /* XXX unused currently */
372 #if 0
373                         break;
374                 }
375                 case SOUND_TYPE_BUFFER:
376                         if (sound->child_sound && sound->child_sound->handle)
377                                 sound->handle = AUD_bufferSound(sound->child_sound->handle);
378                         break;
379                 case SOUND_TYPE_LIMITER:
380                         if (sound->child_sound && sound->child_sound->handle)
381                                 sound->handle = AUD_limitSound(sound->child_sound, sound->start, sound->end);
382                         break;
383         }
384 #endif
385                 if (sound->flags & SOUND_FLAGS_MONO) {
386                         void *handle = AUD_Sound_rechannel(sound->handle, AUD_CHANNELS_MONO);
387                         AUD_Sound_free(sound->handle);
388                         sound->handle = handle;
389                 }
390
391                 if (sound->flags & SOUND_FLAGS_CACHING) {
392                         sound->cache = AUD_Sound_cache(sound->handle);
393                 }
394
395                 if (sound->cache)
396                         sound->playback_handle = sound->cache;
397                 else
398                         sound->playback_handle = sound->handle;
399
400                 BKE_sound_update_sequencer(bmain, sound);
401         }
402 }
403
404 AUD_Device *BKE_sound_mixdown(struct Scene *scene, AUD_DeviceSpecs specs, int start, float volume)
405 {
406         return AUD_openMixdownDevice(specs, scene->sound_scene, volume, start / FPS);
407 }
408
409 void BKE_sound_create_scene(struct Scene *scene)
410 {
411         /* should be done in version patch, but this gets called before */
412         if (scene->r.frs_sec_base == 0)
413                 scene->r.frs_sec_base = 1;
414
415         scene->sound_scene = AUD_Sequence_create(FPS, scene->audio.flag & AUDIO_MUTE);
416         AUD_Sequence_setSpeedOfSound(scene->sound_scene, scene->audio.speed_of_sound);
417         AUD_Sequence_setDopplerFactor(scene->sound_scene, scene->audio.doppler_factor);
418         AUD_Sequence_setDistanceModel(scene->sound_scene, scene->audio.distance_model);
419         scene->playback_handle = NULL;
420         scene->sound_scrub_handle = NULL;
421         scene->speaker_handles = NULL;
422 }
423
424 void BKE_sound_destroy_scene(struct Scene *scene)
425 {
426         if (scene->playback_handle)
427                 AUD_Handle_stop(scene->playback_handle);
428         if (scene->sound_scrub_handle)
429                 AUD_Handle_stop(scene->sound_scrub_handle);
430         if (scene->sound_scene)
431                 AUD_Sequence_free(scene->sound_scene);
432         if (scene->speaker_handles)
433                 AUD_destroySet(scene->speaker_handles);
434 }
435
436 void BKE_sound_mute_scene(struct Scene *scene, int muted)
437 {
438         if (scene->sound_scene)
439                 AUD_Sequence_setMuted(scene->sound_scene, muted);
440 }
441
442 void BKE_sound_update_fps(struct Scene *scene)
443 {
444         if (scene->sound_scene)
445                 AUD_Sequence_setFPS(scene->sound_scene, FPS);
446
447         BKE_sequencer_refresh_sound_length(scene);
448 }
449
450 void BKE_sound_update_scene_listener(struct Scene *scene)
451 {
452         AUD_Sequence_setSpeedOfSound(scene->sound_scene, scene->audio.speed_of_sound);
453         AUD_Sequence_setDopplerFactor(scene->sound_scene, scene->audio.doppler_factor);
454         AUD_Sequence_setDistanceModel(scene->sound_scene, scene->audio.distance_model);
455 }
456
457 void *BKE_sound_scene_add_scene_sound(struct Scene *scene, struct Sequence *sequence,
458                                   int startframe, int endframe, int frameskip)
459 {
460         if (scene != sequence->scene) {
461                 const double fps = FPS;
462                 return AUD_Sequence_add(scene->sound_scene, sequence->scene->sound_scene,
463                                        startframe / fps, endframe / fps, frameskip / fps);
464         }
465         return NULL;
466 }
467
468 void *BKE_sound_scene_add_scene_sound_defaults(struct Scene *scene, struct Sequence *sequence)
469 {
470         return BKE_sound_scene_add_scene_sound(scene, sequence,
471                                            sequence->startdisp, sequence->enddisp,
472                                            sequence->startofs + sequence->anim_startofs);
473 }
474
475 void *BKE_sound_add_scene_sound(struct Scene *scene, struct Sequence *sequence, int startframe, int endframe, int frameskip)
476 {
477         const double fps = FPS;
478         void *handle = AUD_Sequence_add(scene->sound_scene, sequence->sound->playback_handle,
479                                        startframe / fps, endframe / fps, frameskip / fps);
480         AUD_SequenceEntry_setMuted(handle, (sequence->flag & SEQ_MUTE) != 0);
481         AUD_SequenceEntry_setAnimationData(handle, AUD_AP_VOLUME, CFRA, &sequence->volume, 0);
482         AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PITCH, CFRA, &sequence->pitch, 0);
483         AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PANNING, CFRA, &sequence->pan, 0);
484         return handle;
485 }
486
487 void *BKE_sound_add_scene_sound_defaults(struct Scene *scene, struct Sequence *sequence)
488 {
489         return BKE_sound_add_scene_sound(scene, sequence,
490                                      sequence->startdisp, sequence->enddisp,
491                                      sequence->startofs + sequence->anim_startofs);
492 }
493
494 void BKE_sound_remove_scene_sound(struct Scene *scene, void *handle)
495 {
496         AUD_Sequence_remove(scene->sound_scene, handle);
497 }
498
499 void BKE_sound_mute_scene_sound(void *handle, char mute)
500 {
501         AUD_SequenceEntry_setMuted(handle, mute);
502 }
503
504 void BKE_sound_move_scene_sound(struct Scene *scene, void *handle, int startframe, int endframe, int frameskip)
505 {
506         const double fps = FPS;
507         AUD_SequenceEntry_move(handle, startframe / fps, endframe / fps, frameskip / fps);
508 }
509
510 void BKE_sound_move_scene_sound_defaults(struct Scene *scene, struct Sequence *sequence)
511 {
512         if (sequence->scene_sound) {
513                 BKE_sound_move_scene_sound(scene, sequence->scene_sound,
514                                        sequence->startdisp, sequence->enddisp,
515                                        sequence->startofs + sequence->anim_startofs);
516         }
517 }
518
519 void BKE_sound_update_scene_sound(void *handle, bSound *sound)
520 {
521         AUD_SequenceEntry_setSound(handle, sound->playback_handle);
522 }
523
524 void BKE_sound_set_cfra(int cfra)
525 {
526         sound_cfra = cfra;
527 }
528
529 void BKE_sound_set_scene_volume(struct Scene *scene, float volume)
530 {
531         AUD_Sequence_setAnimationData(scene->sound_scene, AUD_AP_VOLUME, CFRA, &volume,
532                                  (scene->audio.flag & AUDIO_VOLUME_ANIMATED) != 0);
533 }
534
535 void BKE_sound_set_scene_sound_volume(void *handle, float volume, char animated)
536 {
537         AUD_SequenceEntry_setAnimationData(handle, AUD_AP_VOLUME, sound_cfra, &volume, animated);
538 }
539
540 void BKE_sound_set_scene_sound_pitch(void *handle, float pitch, char animated)
541 {
542         AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PITCH, sound_cfra, &pitch, animated);
543 }
544
545 void BKE_sound_set_scene_sound_pan(void *handle, float pan, char animated)
546 {
547         AUD_SequenceEntry_setAnimationData(handle, AUD_AP_PANNING, sound_cfra, &pan, animated);
548 }
549
550 void BKE_sound_update_sequencer(struct Main *main, bSound *sound)
551 {
552         struct Scene *scene;
553
554         for (scene = main->scene.first; scene; scene = scene->id.next) {
555                 BKE_sequencer_update_sound(scene, sound);
556         }
557 }
558
559 static void sound_start_play_scene(struct Scene *scene)
560 {
561         AUD_Specs specs;
562
563         if (scene->playback_handle)
564                 AUD_Handle_stop(scene->playback_handle);
565
566         specs.channels = AUD_Device_getChannels(sound_device);
567         specs.rate = AUD_Device_getRate(sound_device);
568
569         AUD_Sequence_setSpecs(scene->sound_scene, specs);
570
571         if ((scene->playback_handle = AUD_Device_play(sound_device, scene->sound_scene, 1)))
572                 AUD_Handle_setLoopCount(scene->playback_handle, -1);
573 }
574
575 void BKE_sound_play_scene(struct Scene *scene)
576 {
577         AUD_Status status;
578         const float cur_time = (float)((double)CFRA / FPS);
579
580         AUD_Device_lock(sound_device);
581
582         status = scene->playback_handle ? AUD_Handle_getStatus(scene->playback_handle) : AUD_STATUS_INVALID;
583
584         if (status == AUD_STATUS_INVALID) {
585                 sound_start_play_scene(scene);
586
587                 if (!scene->playback_handle) {
588                         AUD_Device_unlock(sound_device);
589                         return;
590                 }
591         }
592
593         if (status != AUD_STATUS_PLAYING) {
594                 AUD_Handle_setPosition(scene->playback_handle, cur_time);
595                 AUD_Handle_resume(scene->playback_handle);
596         }
597
598         if (scene->audio.flag & AUDIO_SYNC)
599                 AUD_playSynchronizer();
600
601         AUD_Device_unlock(sound_device);
602 }
603
604 void BKE_sound_stop_scene(struct Scene *scene)
605 {
606         if (scene->playback_handle) {
607                 AUD_Handle_pause(scene->playback_handle);
608
609                 if (scene->audio.flag & AUDIO_SYNC)
610                         AUD_stopSynchronizer();
611         }
612 }
613
614 void BKE_sound_seek_scene(struct Main *bmain, struct Scene *scene)
615 {
616         AUD_Status status;
617         bScreen *screen;
618         int animation_playing;
619
620         const float one_frame = (float)(1.0 / FPS);
621         const float cur_time = (float)((double)CFRA / FPS);
622
623         AUD_Device_lock(sound_device);
624
625         status = scene->playback_handle ? AUD_Handle_getStatus(scene->playback_handle) : AUD_STATUS_INVALID;
626
627         if (status == AUD_STATUS_INVALID) {
628                 sound_start_play_scene(scene);
629
630                 if (!scene->playback_handle) {
631                         AUD_Device_unlock(sound_device);
632                         return;
633                 }
634
635                 AUD_Handle_pause(scene->playback_handle);
636         }
637
638         animation_playing = 0;
639         for (screen = bmain->screen.first; screen; screen = screen->id.next) {
640                 if (screen->animtimer) {
641                         animation_playing = 1;
642                         break;
643                 }
644         }
645
646         if (scene->audio.flag & AUDIO_SCRUB && !animation_playing) {
647                 AUD_Handle_setPosition(scene->playback_handle, cur_time);
648                 if (scene->audio.flag & AUDIO_SYNC) {
649                         AUD_seekSynchronizer(scene->playback_handle, cur_time);
650                 }
651                 AUD_Handle_resume(scene->playback_handle);
652                 if (scene->sound_scrub_handle && AUD_Handle_getStatus(scene->sound_scrub_handle) != AUD_STATUS_INVALID) {
653                         AUD_Handle_setPosition(scene->sound_scrub_handle, 0);
654                 }
655                 else {
656                         if (scene->sound_scrub_handle) {
657                                 AUD_Handle_stop(scene->sound_scrub_handle);
658                         }
659                         scene->sound_scrub_handle = AUD_pauseAfter(scene->playback_handle, one_frame);
660                 }
661         }
662         else {
663                 if (scene->audio.flag & AUDIO_SYNC) {
664                         AUD_seekSynchronizer(scene->playback_handle, cur_time);
665                 }
666                 else {
667                         if (status == AUD_STATUS_PLAYING) {
668                                 AUD_Handle_setPosition(scene->playback_handle, cur_time);
669                         }
670                 }
671         }
672
673         AUD_Device_unlock(sound_device);
674 }
675
676 float BKE_sound_sync_scene(struct Scene *scene)
677 {
678         if (scene->playback_handle) {
679                 if (scene->audio.flag & AUDIO_SYNC)
680                         return AUD_getSynchronizerPosition(scene->playback_handle);
681                 else
682                         return AUD_Handle_getPosition(scene->playback_handle);
683         }
684         return NAN_FLT;
685 }
686
687 int BKE_sound_scene_playing(struct Scene *scene)
688 {
689         if (scene->audio.flag & AUDIO_SYNC)
690                 return AUD_isSynchronizerPlaying();
691         else
692                 return -1;
693 }
694
695 void BKE_sound_free_waveform(bSound *sound)
696 {
697         SoundWaveform *waveform = sound->waveform;
698         if (waveform) {
699                 if (waveform->data) {
700                         MEM_freeN(waveform->data);
701                 }
702                 MEM_freeN(waveform);
703         }
704
705         sound->waveform = NULL;
706 }
707
708 void BKE_sound_read_waveform(bSound *sound, short *stop)
709 {
710         AUD_SoundInfo info = AUD_getInfo(sound->playback_handle);
711         SoundWaveform *waveform = MEM_mallocN(sizeof(SoundWaveform), "SoundWaveform");
712
713         if (info.length > 0) {
714                 int length = info.length * SOUND_WAVE_SAMPLES_PER_SECOND;
715                 
716                 waveform->data = MEM_mallocN(length * sizeof(float) * 3, "SoundWaveform.samples");
717                 waveform->length = AUD_readSound(sound->playback_handle, waveform->data, length, SOUND_WAVE_SAMPLES_PER_SECOND, stop);
718         }
719         else {
720                 /* Create an empty waveform here if the sound couldn't be
721                  * read. This indicates that reading the waveform is "done",
722                  * whereas just setting sound->waveform to NULL causes other
723                  * code to think the waveform still needs to be created. */
724                 waveform->data = NULL;
725                 waveform->length = 0;
726         }
727
728         if (*stop) {
729                 if (waveform->data) {
730                         MEM_freeN(waveform->data);
731                 }
732                 MEM_freeN(waveform);
733                 BLI_spin_lock(sound->spinlock);
734                 sound->flags &= ~SOUND_FLAGS_WAVEFORM_LOADING;
735                 BLI_spin_unlock(sound->spinlock);
736                 return;
737         }
738                 
739         BKE_sound_free_waveform(sound);
740         
741         BLI_spin_lock(sound->spinlock);
742         sound->waveform = waveform;
743         sound->flags &= ~SOUND_FLAGS_WAVEFORM_LOADING;
744         BLI_spin_unlock(sound->spinlock);
745 }
746
747 void BKE_sound_update_scene(Main *bmain, struct Scene *scene)
748 {
749         Object *ob;
750         Base *base;
751         NlaTrack *track;
752         NlaStrip *strip;
753         Speaker *speaker;
754         Scene *sce_it;
755
756         void *new_set = AUD_createSet();
757         void *handle;
758         float quat[4];
759
760         /* cheap test to skip looping over all objects (no speakers is a common case) */
761         if (!BLI_listbase_is_empty(&bmain->speaker)) {
762                 for (SETLOOPER(scene, sce_it, base)) {
763                         ob = base->object;
764                         if ((ob->type != OB_SPEAKER) || !ob->adt) {
765                                 continue;
766                         }
767                         for (track = ob->adt->nla_tracks.first; track; track = track->next) {
768                                 for (strip = track->strips.first; strip; strip = strip->next) {
769                                         if (strip->type != NLASTRIP_TYPE_SOUND) {
770                                                 continue;
771                                         }
772                                         speaker = (Speaker *)ob->data;
773
774                                         if (AUD_removeSet(scene->speaker_handles, strip->speaker_handle)) {
775                                                 if (speaker->sound) {
776                                                         AUD_SequenceEntry_move(strip->speaker_handle, (double)strip->start / FPS, FLT_MAX, 0);
777                                                 }
778                                                 else {
779                                                         AUD_Sequence_remove(scene->sound_scene, strip->speaker_handle);
780                                                         strip->speaker_handle = NULL;
781                                                 }
782                                         }
783                                         else {
784                                                 if (speaker->sound) {
785                                                         strip->speaker_handle = AUD_Sequence_add(scene->sound_scene,
786                                                                                                 speaker->sound->playback_handle,
787                                                                                                 (double)strip->start / FPS, FLT_MAX, 0);
788                                                         AUD_SequenceEntry_setRelative(strip->speaker_handle, 0);
789                                                 }
790                                         }
791
792                                         if (strip->speaker_handle) {
793                                                 const bool mute = ((strip->flag & NLASTRIP_FLAG_MUTED) || (speaker->flag & SPK_MUTED));
794                                                 AUD_addSet(new_set, strip->speaker_handle);
795                                                 AUD_SequenceEntry_setVolumeMaximum(strip->speaker_handle, speaker->volume_max);
796                                                 AUD_SequenceEntry_setVolumeMinimum(strip->speaker_handle, speaker->volume_min);
797                                                 AUD_SequenceEntry_setDistanceMaximum(strip->speaker_handle, speaker->distance_max);
798                                                 AUD_SequenceEntry_setDistanceReference(strip->speaker_handle, speaker->distance_reference);
799                                                 AUD_SequenceEntry_setAttenuation(strip->speaker_handle, speaker->attenuation);
800                                                 AUD_SequenceEntry_setConeAngleOuter(strip->speaker_handle, speaker->cone_angle_outer);
801                                                 AUD_SequenceEntry_setConeAngleInner(strip->speaker_handle, speaker->cone_angle_inner);
802                                                 AUD_SequenceEntry_setConeVolumeOuter(strip->speaker_handle, speaker->cone_volume_outer);
803
804                                                 mat4_to_quat(quat, ob->obmat);
805                                                 AUD_SequenceEntry_setAnimationData(strip->speaker_handle, AUD_AP_LOCATION, CFRA, ob->obmat[3], 1);
806                                                 AUD_SequenceEntry_setAnimationData(strip->speaker_handle, AUD_AP_ORIENTATION, CFRA, quat, 1);
807                                                 AUD_SequenceEntry_setAnimationData(strip->speaker_handle, AUD_AP_VOLUME, CFRA, &speaker->volume, 1);
808                                                 AUD_SequenceEntry_setAnimationData(strip->speaker_handle, AUD_AP_PITCH, CFRA, &speaker->pitch, 1);
809                                                 AUD_SequenceEntry_setSound(strip->speaker_handle, speaker->sound->playback_handle);
810                                                 AUD_SequenceEntry_setMuted(strip->speaker_handle, mute);
811                                         }
812                                 }
813                         }
814                 }
815         }
816
817         while ((handle = AUD_getSet(scene->speaker_handles))) {
818                 AUD_Sequence_remove(scene->sound_scene, handle);
819         }
820
821         if (scene->camera) {
822                 mat4_to_quat(quat, scene->camera->obmat);
823                 AUD_Sequence_setAnimationData(scene->sound_scene, AUD_AP_LOCATION, CFRA, scene->camera->obmat[3], 1);
824                 AUD_Sequence_setAnimationData(scene->sound_scene, AUD_AP_ORIENTATION, CFRA, quat, 1);
825         }
826
827         AUD_destroySet(scene->speaker_handles);
828         scene->speaker_handles = new_set;
829 }
830
831 void *BKE_sound_get_factory(void *sound)
832 {
833         return ((bSound *)sound)->playback_handle;
834 }
835
836 /* stupid wrapper because AUD_C-API.h includes Python.h which makesrna doesn't like */
837 float BKE_sound_get_length(bSound *sound)
838 {
839         AUD_SoundInfo info = AUD_getInfo(sound->playback_handle);
840
841         return info.length;
842 }
843
844 bool BKE_sound_is_jack_supported(void)
845 {
846 #ifdef WITH_SYSTEM_AUDASPACE
847         return 1;
848 #else
849         return (bool)AUD_isJackSupported();
850 #endif
851 }
852
853 #else  /* WITH_AUDASPACE */
854
855 #include "BLI_utildefines.h"
856
857 int BKE_sound_define_from_str(const char *UNUSED(str)) { return -1; }
858 void BKE_sound_force_device(int UNUSED(device)) {}
859 void BKE_sound_init_once(void) {}
860 void BKE_sound_init(struct Main *UNUSED(bmain)) {}
861 void BKE_sound_exit(void) {}
862 void BKE_sound_exit_once(void) {}
863 void BKE_sound_cache(struct bSound *UNUSED(sound)) {}
864 void BKE_sound_delete_cache(struct bSound *UNUSED(sound)) {}
865 void BKE_sound_load(struct Main *UNUSED(bmain), struct bSound *UNUSED(sound)) {}
866 void BKE_sound_create_scene(struct Scene *UNUSED(scene)) {}
867 void BKE_sound_destroy_scene(struct Scene *UNUSED(scene)) {}
868 void BKE_sound_mute_scene(struct Scene *UNUSED(scene), int UNUSED(muted)) {}
869 void *BKE_sound_scene_add_scene_sound(struct Scene *UNUSED(scene), struct Sequence *UNUSED(sequence),
870                                       int UNUSED(startframe), int UNUSED(endframe), int UNUSED(frameskip)) { return NULL; }
871 void *BKE_sound_scene_add_scene_sound_defaults(struct Scene *UNUSED(scene),
872                                               struct Sequence *UNUSED(sequence)) { return NULL; }
873 void *BKE_sound_add_scene_sound(struct Scene *UNUSED(scene), struct Sequence *UNUSED(sequence), int UNUSED(startframe),
874                                 int UNUSED(endframe), int UNUSED(frameskip)) { return NULL; }
875 void *BKE_sound_add_scene_sound_defaults(struct Scene *UNUSED(scene), struct Sequence *UNUSED(sequence)) { return NULL; }
876 void BKE_sound_remove_scene_sound(struct Scene *UNUSED(scene), void *UNUSED(handle)) {}
877 void BKE_sound_mute_scene_sound(void *UNUSED(handle), char UNUSED(mute)) {}
878 void BKE_sound_move_scene_sound(struct Scene *UNUSED(scene), void *UNUSED(handle), int UNUSED(startframe),
879                                 int UNUSED(endframe), int UNUSED(frameskip)) {}
880 void BKE_sound_move_scene_sound_defaults(struct Scene *UNUSED(scene), struct Sequence *UNUSED(sequence)) {}
881 void BKE_sound_play_scene(struct Scene *UNUSED(scene)) {}
882 void BKE_sound_stop_scene(struct Scene *UNUSED(scene)) {}
883 void BKE_sound_seek_scene(struct Main *UNUSED(bmain), struct Scene *UNUSED(scene)) {}
884 float BKE_sound_sync_scene(struct Scene *UNUSED(scene)) { return NAN_FLT; }
885 int BKE_sound_scene_playing(struct Scene *UNUSED(scene)) { return -1; }
886 void BKE_sound_read_waveform(struct bSound *sound, short *stop) { UNUSED_VARS(sound, stop); }
887 void BKE_sound_init_main(struct Main *UNUSED(bmain)) {}
888 void BKE_sound_set_cfra(int UNUSED(cfra)) {}
889 void BKE_sound_update_sequencer(struct Main *UNUSED(main), struct bSound *UNUSED(sound)) {}
890 void BKE_sound_update_scene(struct Main *UNUSED(bmain), struct Scene *UNUSED(scene)) {}
891 void BKE_sound_update_scene_sound(void *UNUSED(handle), struct bSound *UNUSED(sound)) {}
892 void BKE_sound_update_scene_listener(struct Scene *UNUSED(scene)) {}
893 void BKE_sound_update_fps(struct Scene *UNUSED(scene)) {}
894 void BKE_sound_set_scene_sound_volume(void *UNUSED(handle), float UNUSED(volume), char UNUSED(animated)) {}
895 void BKE_sound_set_scene_sound_pan(void *UNUSED(handle), float UNUSED(pan), char UNUSED(animated)) {}
896 void BKE_sound_set_scene_volume(struct Scene *UNUSED(scene), float UNUSED(volume)) {}
897 void BKE_sound_set_scene_sound_pitch(void *UNUSED(handle), float UNUSED(pitch), char UNUSED(animated)) {}
898 float BKE_sound_get_length(struct bSound *UNUSED(sound)) { return 0; }
899 bool BKE_sound_is_jack_supported(void) { return false; }
900 #endif  /* WITH_AUDASPACE */