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