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