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