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