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