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