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