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