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