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