890fc114e68faa0dfb122143f230294f01500886
[blender-staging.git] / source / blender / blenkernel / intern / sound.c
1 /** \file blender/blenkernel/intern/sound.c
2  *  \ingroup bke
3  */
4 /**
5  * sound.c (mar-2001 nzc)
6  *
7  * $Id$
8  */
9
10 #include <string.h>
11 #include <stdlib.h>
12
13 #include "MEM_guardedalloc.h"
14
15 #include "BLI_blenlib.h"
16
17 #include "DNA_anim_types.h"
18 #include "DNA_scene_types.h"
19 #include "DNA_sequence_types.h"
20 #include "DNA_packedFile_types.h"
21 #include "DNA_screen_types.h"
22 #include "DNA_sound_types.h"
23
24 #ifdef WITH_AUDASPACE
25 #  include "AUD_C-API.h"
26 #endif
27
28 #include "BKE_utildefines.h"
29 #include "BKE_global.h"
30 #include "BKE_main.h"
31 #include "BKE_sound.h"
32 #include "BKE_context.h"
33 #include "BKE_library.h"
34 #include "BKE_packedFile.h"
35 #include "BKE_animsys.h"
36 #include "BKE_sequencer.h"
37
38 // evil global ;-)
39 static int sound_cfra;
40
41 struct bSound* sound_new_file(struct Main *bmain, const char *filename)
42 {
43         bSound* sound = NULL;
44
45         char str[FILE_MAX];
46         char *path;
47
48         int len;
49
50         strcpy(str, filename);
51
52         path = /*bmain ? bmain->name :*/ G.main->name;
53
54         BLI_path_abs(str, path);
55
56         len = strlen(filename);
57         while(len > 0 && filename[len-1] != '/' && filename[len-1] != '\\')
58                 len--;
59
60         sound = alloc_libblock(&bmain->sound, ID_SO, filename+len);
61         BLI_strncpy(sound->name, filename, FILE_MAX);
62 // XXX unused currently sound->type = SOUND_TYPE_FILE;
63
64         sound_load(bmain, sound);
65
66         if(!sound->playback_handle)
67         {
68                 free_libblock(&bmain->sound, sound);
69                 sound = NULL;
70         }
71
72         return sound;
73 }
74
75 void sound_free(struct bSound* sound)
76 {
77         if (sound->packedfile)
78         {
79                 freePackedFile(sound->packedfile);
80                 sound->packedfile = NULL;
81         }
82
83 #ifdef WITH_AUDASPACE
84         if(sound->handle)
85         {
86                 AUD_unload(sound->handle);
87                 sound->handle = NULL;
88                 sound->playback_handle = NULL;
89         }
90
91         if(sound->cache)
92         {
93                 AUD_unload(sound->cache);
94                 sound->cache = NULL;
95         }
96 #endif // WITH_AUDASPACE
97 }
98
99 #ifdef WITH_AUDASPACE
100
101 static int force_device = -1;
102
103 #ifdef WITH_JACK
104 static void sound_sync_callback(void* data, int mode, float time)
105 {
106         struct Main* bmain = (struct Main*)data;
107         struct Scene* scene;
108
109         scene = bmain->scene.first;
110         while(scene)
111         {
112                 if(scene->audio.flag & AUDIO_SYNC)
113                 {
114                         if(mode)
115                                 sound_play_scene(scene);
116                         else
117                                 sound_stop_scene(scene);
118                         if(scene->sound_scene_handle)
119                                 AUD_seek(scene->sound_scene_handle, time);
120                 }
121                 scene = scene->id.next;
122         }
123 }
124 #endif
125
126 int sound_define_from_str(const char *str)
127 {
128         if (BLI_strcaseeq(str, "NULL"))
129                 return AUD_NULL_DEVICE;
130         if (BLI_strcaseeq(str, "SDL"))
131                 return AUD_SDL_DEVICE;
132         if (BLI_strcaseeq(str, "OPENAL"))
133                 return AUD_OPENAL_DEVICE;
134         if (BLI_strcaseeq(str, "JACK"))
135                 return AUD_JACK_DEVICE;
136
137         return -1;
138 }
139
140 void sound_force_device(int device)
141 {
142         force_device = device;
143 }
144
145 void sound_init_once(void)
146 {
147         AUD_initOnce();
148 }
149
150 void sound_init(struct Main *bmain)
151 {
152         AUD_DeviceSpecs specs;
153         int device, buffersize;
154
155         device = U.audiodevice;
156         buffersize = U.mixbufsize;
157         specs.channels = U.audiochannels;
158         specs.format = U.audioformat;
159         specs.rate = U.audiorate;
160
161         if(force_device >= 0)
162                 device = force_device;
163
164         if(buffersize < 128)
165                 buffersize = AUD_DEFAULT_BUFFER_SIZE;
166
167         if(specs.rate < AUD_RATE_8000)
168                 specs.rate = AUD_RATE_44100;
169
170         if(specs.format <= AUD_FORMAT_INVALID)
171                 specs.format = AUD_FORMAT_S16;
172
173         if(specs.channels <= AUD_CHANNELS_INVALID)
174                 specs.channels = AUD_CHANNELS_STEREO;
175
176         if(!AUD_init(device, specs, buffersize))
177                 AUD_init(AUD_NULL_DEVICE, specs, buffersize);
178                 
179 #ifdef WITH_JACK
180         AUD_setSyncCallback(sound_sync_callback, bmain);
181 #else
182         (void)bmain; /* unused */
183 #endif
184 }
185
186 void sound_exit(void)
187 {
188         AUD_exit();
189 }
190
191 // XXX unused currently
192 #if 0
193 struct bSound* sound_new_buffer(struct bContext *C, struct bSound *source)
194 {
195         bSound* sound = NULL;
196
197         char name[25];
198         strcpy(name, "buf_");
199         strcpy(name + 4, source->id.name);
200
201         sound = alloc_libblock(&CTX_data_main(C)->sound, ID_SO, name);
202
203         sound->child_sound = source;
204         sound->type = SOUND_TYPE_BUFFER;
205
206         sound_load(CTX_data_main(C), sound);
207
208         if(!sound->playback_handle)
209         {
210                 free_libblock(&CTX_data_main(C)->sound, sound);
211                 sound = NULL;
212         }
213
214         return sound;
215 }
216
217 struct bSound* sound_new_limiter(struct bContext *C, struct bSound *source, float start, float end)
218 {
219         bSound* sound = NULL;
220
221         char name[25];
222         strcpy(name, "lim_");
223         strcpy(name + 4, source->id.name);
224
225         sound = alloc_libblock(&CTX_data_main(C)->sound, ID_SO, name);
226
227         sound->child_sound = source;
228         sound->start = start;
229         sound->end = end;
230         sound->type = SOUND_TYPE_LIMITER;
231
232         sound_load(CTX_data_main(C), sound);
233
234         if(!sound->playback_handle)
235         {
236                 free_libblock(&CTX_data_main(C)->sound, sound);
237                 sound = NULL;
238         }
239
240         return sound;
241 }
242 #endif
243
244 void sound_delete(struct bContext *C, struct bSound* sound)
245 {
246         if(sound)
247         {
248                 sound_free(sound);
249
250                 free_libblock(&CTX_data_main(C)->sound, sound);
251         }
252 }
253
254 void sound_cache(struct bSound* sound)
255 {
256         sound->flags |= SOUND_FLAGS_CACHING;
257         if(sound->cache)
258                 AUD_unload(sound->cache);
259
260         sound->cache = AUD_bufferSound(sound->handle);
261         sound->playback_handle = sound->cache;
262 }
263
264 void sound_cache_notifying(struct Main* main, struct bSound* sound)
265 {
266         sound_cache(sound);
267         sound_update_sequencer(main, sound);
268 }
269
270 void sound_delete_cache(struct bSound* sound)
271 {
272         sound->flags &= ~SOUND_FLAGS_CACHING;
273         if(sound->cache)
274         {
275                 AUD_unload(sound->cache);
276                 sound->cache = NULL;
277                 sound->playback_handle = sound->handle;
278         }
279 }
280
281 void sound_load(struct Main *bmain, struct bSound* sound)
282 {
283         if(sound)
284         {
285                 if(sound->cache)
286                 {
287                         AUD_unload(sound->cache);
288                         sound->cache = NULL;
289                 }
290
291                 if(sound->handle)
292                 {
293                         AUD_unload(sound->handle);
294                         sound->handle = NULL;
295                         sound->playback_handle = NULL;
296                 }
297
298 // XXX unused currently
299 #if 0
300                 switch(sound->type)
301                 {
302                 case SOUND_TYPE_FILE:
303 #endif
304                 {
305                         char fullpath[FILE_MAX];
306                         char *path;
307
308                         /* load sound */
309                         PackedFile* pf = sound->packedfile;
310
311                         /* dont modify soundact->sound->name, only change a copy */
312                         BLI_strncpy(fullpath, sound->name, sizeof(fullpath));
313
314                         if(sound->id.lib)
315                                 path = sound->id.lib->filepath;
316                         else
317                                 path = bmain->name;
318
319                         BLI_path_abs(fullpath, path);
320
321                         /* but we need a packed file then */
322                         if (pf)
323                                 sound->handle = AUD_loadBuffer((unsigned char*) pf->data, pf->size);
324                         /* or else load it from disk */
325                         else
326                                 sound->handle = AUD_load(fullpath);
327                 }
328 // XXX unused currently
329 #if 0
330                         break;
331                 }
332                 case SOUND_TYPE_BUFFER:
333                         if(sound->child_sound && sound->child_sound->handle)
334                                 sound->handle = AUD_bufferSound(sound->child_sound->handle);
335                         break;
336                 case SOUND_TYPE_LIMITER:
337                         if(sound->child_sound && sound->child_sound->handle)
338                                 sound->handle = AUD_limitSound(sound->child_sound, sound->start, sound->end);
339                         break;
340                 }
341 #endif
342                 if(sound->flags & SOUND_FLAGS_CACHING)
343                 {
344                         sound->cache = AUD_bufferSound(sound->handle);
345                 }
346
347                 if(sound->cache)
348                         sound->playback_handle = sound->cache;
349                 else
350                         sound->playback_handle = sound->handle;
351
352                 sound_update_sequencer(bmain, sound);
353         }
354 }
355
356 AUD_Device* sound_mixdown(struct Scene *scene, AUD_DeviceSpecs specs, int start, float volume)
357 {
358         AUD_Device* mixdown = AUD_openReadDevice(specs);
359
360         AUD_setDeviceVolume(mixdown, volume);
361
362         AUD_setSequencerSpecs(scene->sound_scene, specs.specs);
363         AUD_freeHandle(AUD_playDevice(mixdown, scene->sound_scene, start / FPS));
364
365         return mixdown;
366 }
367
368 void sound_create_scene(struct Scene *scene)
369 {
370         scene->sound_scene = AUD_createSequencer(FPS, scene->audio.flag & AUDIO_MUTE);
371         AUD_updateSequencerData(scene->sound_scene, scene->audio.speed_of_sound,
372                                                         scene->audio.doppler_factor, scene->audio.distance_model);
373         scene->sound_scene_handle = NULL;
374         scene->sound_scrub_handle = NULL;
375         scene->speaker_handles = NULL;
376 }
377
378 void sound_destroy_scene(struct Scene *scene)
379 {
380         if(scene->sound_scene_handle)
381                 AUD_stop(scene->sound_scene_handle);
382         if(scene->sound_scrub_handle)
383                 AUD_stop(scene->sound_scrub_handle);
384         if(scene->sound_scene)
385                 AUD_destroySequencer(scene->sound_scene);
386         if(scene->speaker_handles)
387                 AUD_destroySet(scene->speaker_handles);
388 }
389
390 void sound_mute_scene(struct Scene *scene, int muted)
391 {
392         if(scene->sound_scene)
393                 AUD_setSequencerMuted(scene->sound_scene, muted);
394 }
395
396 void sound_update_fps(struct Scene *scene)
397 {
398         if(scene->sound_scene)
399                 AUD_setSequencerFPS(scene->sound_scene, FPS);
400 }
401
402 void sound_update_scene_listener(struct Scene *scene)
403 {
404         AUD_updateSequencerData(scene->sound_scene, scene->audio.speed_of_sound,
405                                                         scene->audio.doppler_factor, scene->audio.distance_model);
406 }
407
408 void* sound_scene_add_scene_sound(struct Scene *scene, struct Sequence* sequence, int startframe, int endframe, int frameskip)
409 {
410         if(scene != sequence->scene)
411                 return AUD_addSequence(scene->sound_scene, sequence->scene->sound_scene, startframe / FPS, endframe / FPS, frameskip / FPS);
412         return NULL;
413 }
414
415 void* sound_add_scene_sound(struct Scene *scene, struct Sequence* sequence, int startframe, int endframe, int frameskip)
416 {
417         void* handle = AUD_addSequence(scene->sound_scene, sequence->sound->playback_handle, startframe / FPS, endframe / FPS, frameskip / FPS);
418         AUD_muteSequence(handle, (sequence->flag & SEQ_MUTE) != 0);
419         AUD_setSequenceAnimData(handle, AUD_AP_VOLUME, CFRA, &sequence->volume, 0);
420         AUD_setSequenceAnimData(handle, AUD_AP_PITCH, CFRA, &sequence->pitch, 0);
421         AUD_setSequenceAnimData(handle, AUD_AP_PANNING, CFRA, &sequence->pan, 0);
422         return handle;
423 }
424
425 void sound_remove_scene_sound(struct Scene *scene, void* handle)
426 {
427         AUD_removeSequence(scene->sound_scene, handle);
428 }
429
430 void sound_mute_scene_sound(void* handle, char mute)
431 {
432         AUD_muteSequence(handle, mute);
433 }
434
435 void sound_move_scene_sound(struct Scene *scene, void* handle, int startframe, int endframe, int frameskip)
436 {
437         AUD_moveSequence(handle, startframe / FPS, endframe / FPS, frameskip / FPS);
438 }
439
440 void sound_update_scene_sound(void* handle, struct bSound* sound)
441 {
442         AUD_updateSequenceSound(handle, sound->playback_handle);
443 }
444
445 void sound_set_cfra(int cfra)
446 {
447         sound_cfra = cfra;
448 }
449
450 void sound_set_scene_volume(struct Scene *scene, float volume)
451 {
452         AUD_setSequencerAnimData(scene->sound_scene, AUD_AP_VOLUME, CFRA, &volume, (scene->audio.flag & AUDIO_VOLUME_ANIMATED) != 0);
453 }
454
455 void sound_set_scene_sound_volume(void* handle, float volume, char animated)
456 {
457         AUD_setSequenceAnimData(handle, AUD_AP_VOLUME, sound_cfra, &volume, animated);
458 }
459
460 void sound_set_scene_sound_pitch(void* handle, float pitch, char animated)
461 {
462         AUD_setSequenceAnimData(handle, AUD_AP_PITCH, sound_cfra, &pitch, animated);
463 }
464
465 void sound_set_scene_sound_pan(void* handle, float pan, char animated)
466 {
467         AUD_setSequenceAnimData(handle, AUD_AP_PANNING, sound_cfra, &pan, animated);
468 }
469
470 void sound_update_sequencer(struct Main* main, struct bSound* sound)
471 {
472         struct Scene* scene;
473
474         for(scene = main->scene.first; scene; scene = scene->id.next)
475                 seq_update_sound(scene, sound);
476 }
477
478 static void sound_start_play_scene(struct Scene *scene)
479 {
480         if(scene->sound_scene_handle)
481                 AUD_stop(scene->sound_scene_handle);
482
483         AUD_setSequencerDeviceSpecs(scene->sound_scene);
484
485         if((scene->sound_scene_handle = AUD_play(scene->sound_scene, 1)))
486                 AUD_setLoop(scene->sound_scene_handle, -1);
487 }
488
489 void sound_play_scene(struct Scene *scene)
490 {
491         AUD_Status status;
492         AUD_lock();
493
494         status = scene->sound_scene_handle ? AUD_getStatus(scene->sound_scene_handle) : AUD_STATUS_INVALID;
495
496         if(status == AUD_STATUS_INVALID)
497                 sound_start_play_scene(scene);
498
499         if(!scene->sound_scene_handle)
500         {
501                 AUD_unlock();
502                 return;
503         }
504
505         if(status != AUD_STATUS_PLAYING)
506         {
507                 AUD_seek(scene->sound_scene_handle, CFRA / FPS);
508                 AUD_resume(scene->sound_scene_handle);
509         }
510
511         if(scene->audio.flag & AUDIO_SYNC)
512                 AUD_startPlayback();
513
514         AUD_unlock();
515 }
516
517 void sound_stop_scene(struct Scene *scene)
518 {
519         if(scene->sound_scene_handle)
520         {
521                 AUD_pause(scene->sound_scene_handle);
522
523                 if(scene->audio.flag & AUDIO_SYNC)
524                         AUD_stopPlayback();
525         }
526 }
527
528 void sound_seek_scene(struct bContext *C)
529 {
530         struct Scene *scene = CTX_data_scene(C);
531         AUD_Status status;
532
533         AUD_lock();
534
535         status = scene->sound_scene_handle ? AUD_getStatus(scene->sound_scene_handle) : AUD_STATUS_INVALID;
536
537         if(status == AUD_STATUS_INVALID)
538         {
539                 sound_start_play_scene(scene);
540
541                 if(!scene->sound_scene_handle)
542                 {
543                         AUD_unlock();
544                         return;
545                 }
546
547                 AUD_pause(scene->sound_scene_handle);
548         }
549
550         if(scene->audio.flag & AUDIO_SCRUB && !CTX_wm_screen(C)->animtimer)
551         {
552                 if(scene->audio.flag & AUDIO_SYNC)
553                 {
554                         AUD_seek(scene->sound_scene_handle, CFRA / FPS);
555                         AUD_seekSequencer(scene->sound_scene_handle, CFRA / FPS);
556                 }
557                 else
558                         AUD_seek(scene->sound_scene_handle, CFRA / FPS);
559                 AUD_resume(scene->sound_scene_handle);
560                 if(scene->sound_scrub_handle && AUD_getStatus(scene->sound_scrub_handle) != AUD_STATUS_INVALID)
561                         AUD_seek(scene->sound_scrub_handle, 0);
562                 else
563                 {
564                         if(scene->sound_scrub_handle)
565                                 AUD_stop(scene->sound_scrub_handle);
566                         scene->sound_scrub_handle = AUD_pauseAfter(scene->sound_scene_handle, 1 / FPS);
567                 }
568         }
569         else
570         {
571                 if(scene->audio.flag & AUDIO_SYNC)
572                         AUD_seekSequencer(scene->sound_scene_handle, CFRA / FPS);
573                 else
574                 {
575                         if(status == AUD_STATUS_PLAYING)
576                                 AUD_seek(scene->sound_scene_handle, CFRA / FPS);
577                 }
578         }
579
580         AUD_unlock();
581 }
582
583 float sound_sync_scene(struct Scene *scene)
584 {
585         if(scene->sound_scene_handle)
586         {
587                 if(scene->audio.flag & AUDIO_SYNC)
588                         return AUD_getSequencerPosition(scene->sound_scene_handle);
589                 else
590                         return AUD_getPosition(scene->sound_scene_handle);
591         }
592         return 0.0f;
593 }
594
595 int sound_scene_playing(struct Scene *scene)
596 {
597         if(scene->audio.flag & AUDIO_SYNC)
598                 return AUD_doesPlayback();
599         else
600                 return -1;
601 }
602
603 int sound_read_sound_buffer(struct bSound* sound, float* buffer, int length, float start, float end)
604 {
605         AUD_Sound* limiter = AUD_limitSound(sound->cache, start, end);
606         int ret= AUD_readSound(limiter, buffer, length);
607         AUD_unload(limiter);
608         return ret;
609 }
610
611 int sound_get_channels(struct bSound* sound)
612 {
613         AUD_SoundInfo info;
614
615         info = AUD_getInfo(sound->playback_handle);
616
617         return info.specs.channels;
618 }
619
620 void* sound_get_factory(void* sound)
621 {
622         return ((struct bSound*) sound)->playback_handle;
623 }
624
625 #else // WITH_AUDASPACE
626
627 #include "BLI_utildefines.h"
628
629 int sound_define_from_str(const char *UNUSED(str)) { return -1;}
630 void sound_force_device(int UNUSED(device)) {}
631 void sound_init_once(void) {}
632 void sound_init(struct Main *UNUSED(bmain)) {}
633 void sound_exit(void) {}
634 void sound_cache(struct bSound* UNUSED(sound)) { }
635 void sound_delete_cache(struct bSound* UNUSED(sound)) {}
636 void sound_load(struct Main *UNUSED(bmain), struct bSound* UNUSED(sound)) {}
637 void sound_create_scene(struct Scene *UNUSED(scene)) {}
638 void sound_destroy_scene(struct Scene *UNUSED(scene)) {}
639 void sound_mute_scene(struct Scene *UNUSED(scene), int UNUSED(muted)) {}
640 void* sound_scene_add_scene_sound(struct Scene *UNUSED(scene), struct Sequence* UNUSED(sequence), int UNUSED(startframe), int UNUSED(endframe), int UNUSED(frameskip)) { return NULL; }
641 void* sound_add_scene_sound(struct Scene *UNUSED(scene), struct Sequence* UNUSED(sequence), int UNUSED(startframe), int UNUSED(endframe), int UNUSED(frameskip)) { return NULL; }
642 void sound_remove_scene_sound(struct Scene *UNUSED(scene), void* UNUSED(handle)) {}
643 void sound_mute_scene_sound(void* UNUSED(handle), char UNUSED(mute)) {}
644 void sound_move_scene_sound(struct Scene *UNUSED(scene), void* UNUSED(handle), int UNUSED(startframe), int UNUSED(endframe), int UNUSED(frameskip)) {}
645 static void sound_start_play_scene(struct Scene *UNUSED(scene)) {}
646 void sound_play_scene(struct Scene *UNUSED(scene)) {}
647 void sound_stop_scene(struct Scene *UNUSED(scene)) {}
648 void sound_seek_scene(struct bContext *UNUSED(C)) {}
649 float sound_sync_scene(struct Scene *UNUSED(scene)) { return 0.0f; }
650 int sound_scene_playing(struct Scene *UNUSED(scene)) { return -1; }
651 int sound_read_sound_buffer(struct bSound* UNUSED(sound), float* UNUSED(buffer), int UNUSED(length), float UNUSED(start), float UNUSED(end)) { return 0; }
652 int sound_get_channels(struct bSound* UNUSED(sound)) { return 1; }
653
654 #endif // WITH_AUDASPACE