1 /** \file blender/blenkernel/intern/sound.c
5 * sound.c (mar-2001 nzc)
13 #include "MEM_guardedalloc.h"
15 #include "BLI_blenlib.h"
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"
24 #include "AUD_C-API.h"
26 #include "BKE_utildefines.h"
27 #include "BKE_global.h"
29 #include "BKE_sound.h"
30 #include "BKE_context.h"
31 #include "BKE_library.h"
32 #include "BKE_packedFile.h"
33 #include "BKE_fcurve.h"
34 #include "BKE_animsys.h"
37 static int force_device = -1;
40 static void sound_sync_callback(void* data, int mode, float time)
42 struct Main* bmain = (struct Main*)data;
45 scene = bmain->scene.first;
48 if(scene->audio.flag & AUDIO_SYNC)
51 sound_play_scene(scene);
53 sound_stop_scene(scene);
54 if(scene->sound_scene_handle)
55 AUD_seek(scene->sound_scene_handle, time);
57 scene = scene->id.next;
62 int sound_define_from_str(const char *str)
64 if (BLI_strcaseeq(str, "NULL"))
65 return AUD_NULL_DEVICE;
66 if (BLI_strcaseeq(str, "SDL"))
67 return AUD_SDL_DEVICE;
68 if (BLI_strcaseeq(str, "OPENAL"))
69 return AUD_OPENAL_DEVICE;
70 if (BLI_strcaseeq(str, "JACK"))
71 return AUD_JACK_DEVICE;
76 void sound_force_device(int device)
78 force_device = device;
81 void sound_init_once(void)
86 void sound_init(struct Main *bmain)
88 AUD_DeviceSpecs specs;
89 int device, buffersize;
91 device = U.audiodevice;
92 buffersize = U.mixbufsize;
93 specs.channels = U.audiochannels;
94 specs.format = U.audioformat;
95 specs.rate = U.audiorate;
98 device = force_device;
101 buffersize = AUD_DEFAULT_BUFFER_SIZE;
103 if(specs.rate < AUD_RATE_8000)
104 specs.rate = AUD_RATE_44100;
106 if(specs.format <= AUD_FORMAT_INVALID)
107 specs.format = AUD_FORMAT_S16;
109 if(specs.channels <= AUD_CHANNELS_INVALID)
110 specs.channels = AUD_CHANNELS_STEREO;
112 if(!AUD_init(device, specs, buffersize))
113 AUD_init(AUD_NULL_DEVICE, specs, buffersize);
116 AUD_setSyncCallback(sound_sync_callback, bmain);
118 (void)bmain; /* unused */
122 void sound_exit(void)
127 struct bSound* sound_new_file(struct Main *bmain, const char *filename)
129 bSound* sound = NULL;
136 strcpy(str, filename);
138 path = /*bmain ? bmain->name :*/ G.main->name;
140 BLI_path_abs(str, path);
142 len = strlen(filename);
143 while(len > 0 && filename[len-1] != '/' && filename[len-1] != '\\')
146 sound = alloc_libblock(&bmain->sound, ID_SO, filename+len);
147 BLI_strncpy(sound->name, filename, FILE_MAX);
148 // XXX unused currently sound->type = SOUND_TYPE_FILE;
150 sound_load(bmain, sound);
152 if(!sound->playback_handle)
154 free_libblock(&bmain->sound, sound);
161 // XXX unused currently
163 struct bSound* sound_new_buffer(struct bContext *C, struct bSound *source)
165 bSound* sound = NULL;
168 strcpy(name, "buf_");
169 strcpy(name + 4, source->id.name);
171 sound = alloc_libblock(&CTX_data_main(C)->sound, ID_SO, name);
173 sound->child_sound = source;
174 sound->type = SOUND_TYPE_BUFFER;
176 sound_load(CTX_data_main(C), sound);
178 if(!sound->playback_handle)
180 free_libblock(&CTX_data_main(C)->sound, sound);
187 struct bSound* sound_new_limiter(struct bContext *C, struct bSound *source, float start, float end)
189 bSound* sound = NULL;
192 strcpy(name, "lim_");
193 strcpy(name + 4, source->id.name);
195 sound = alloc_libblock(&CTX_data_main(C)->sound, ID_SO, name);
197 sound->child_sound = source;
198 sound->start = start;
200 sound->type = SOUND_TYPE_LIMITER;
202 sound_load(CTX_data_main(C), sound);
204 if(!sound->playback_handle)
206 free_libblock(&CTX_data_main(C)->sound, sound);
214 void sound_delete(struct bContext *C, struct bSound* sound)
220 free_libblock(&CTX_data_main(C)->sound, sound);
224 void sound_cache(struct bSound* sound, int ignore)
226 if(sound->cache && !ignore)
227 AUD_unload(sound->cache);
229 sound->cache = AUD_bufferSound(sound->handle);
230 sound->playback_handle = sound->cache;
233 void sound_delete_cache(struct bSound* sound)
237 AUD_unload(sound->cache);
239 sound->playback_handle = sound->handle;
243 void sound_load(struct Main *bmain, struct bSound* sound)
249 AUD_unload(sound->handle);
250 sound->handle = NULL;
251 sound->playback_handle = NULL;
254 // XXX unused currently
258 case SOUND_TYPE_FILE:
261 char fullpath[FILE_MAX];
265 PackedFile* pf = sound->packedfile;
267 /* dont modify soundact->sound->name, only change a copy */
268 BLI_strncpy(fullpath, sound->name, sizeof(fullpath));
271 path = sound->id.lib->filepath;
275 BLI_path_abs(fullpath, path);
277 /* but we need a packed file then */
279 sound->handle = AUD_loadBuffer((unsigned char*) pf->data, pf->size);
280 /* or else load it from disk */
282 sound->handle = AUD_load(fullpath);
284 // XXX unused currently
288 case SOUND_TYPE_BUFFER:
289 if(sound->child_sound && sound->child_sound->handle)
290 sound->handle = AUD_bufferSound(sound->child_sound->handle);
292 case SOUND_TYPE_LIMITER:
293 if(sound->child_sound && sound->child_sound->handle)
294 sound->handle = AUD_limitSound(sound->child_sound, sound->start, sound->end);
299 sound->playback_handle = sound->cache;
301 sound->playback_handle = sound->handle;
305 void sound_free(struct bSound* sound)
307 if (sound->packedfile)
309 freePackedFile(sound->packedfile);
310 sound->packedfile = NULL;
315 AUD_unload(sound->handle);
316 sound->handle = NULL;
317 sound->playback_handle = NULL;
322 AUD_unload(sound->cache);
326 static float sound_get_volume(Scene* scene, Sequence* sequence, float time)
328 AnimData *adt= BKE_animdata_from_id(&scene->id);
332 /* NOTE: this manually constructed path needs to be used here to avoid problems with RNA crashes */
333 sprintf(buf, "sequence_editor.sequences_all[\"%s\"].volume", sequence->name+2);
334 if (adt && adt->action && adt->action->curves.first)
335 fcu= list_find_fcurve(&adt->action->curves, buf, 0);
338 return evaluate_fcurve(fcu, time * (float)FPS);
340 return sequence->volume;
343 AUD_Device* sound_mixdown(struct Scene *scene, AUD_DeviceSpecs specs, int start, float volume)
345 AUD_Device* mixdown = AUD_openReadDevice(specs);
347 AUD_setDeviceVolume(mixdown, volume);
349 AUD_freeChannel(AUD_playDevice(mixdown, scene->sound_scene, start / FPS));
354 void sound_create_scene(struct Scene *scene)
356 scene->sound_scene = AUD_createSequencer(scene->audio.flag & AUDIO_MUTE, scene, (AUD_volumeFunction)&sound_get_volume);
357 scene->sound_scene_handle = NULL;
358 scene->sound_scrub_handle = NULL;
361 void sound_destroy_scene(struct Scene *scene)
363 if(scene->sound_scene_handle)
364 AUD_stop(scene->sound_scene_handle);
365 if(scene->sound_scrub_handle)
366 AUD_stop(scene->sound_scrub_handle);
367 if(scene->sound_scene)
368 AUD_destroySequencer(scene->sound_scene);
371 void sound_mute_scene(struct Scene *scene, int muted)
373 if(scene->sound_scene)
374 AUD_setSequencerMuted(scene->sound_scene, muted);
377 void* sound_scene_add_scene_sound(struct Scene *scene, struct Sequence* sequence, int startframe, int endframe, int frameskip)
379 if(scene != sequence->scene)
380 return AUD_addSequencer(scene->sound_scene, &(sequence->scene->sound_scene), startframe / FPS, endframe / FPS, frameskip / FPS, sequence);
384 void* sound_add_scene_sound(struct Scene *scene, struct Sequence* sequence, int startframe, int endframe, int frameskip)
386 return AUD_addSequencer(scene->sound_scene, &(sequence->sound->playback_handle), startframe / FPS, endframe / FPS, frameskip / FPS, sequence);
389 void sound_remove_scene_sound(struct Scene *scene, void* handle)
391 AUD_removeSequencer(scene->sound_scene, handle);
394 void sound_mute_scene_sound(struct Scene *scene, void* handle, char mute)
396 AUD_muteSequencer(scene->sound_scene, handle, mute);
399 void sound_move_scene_sound(struct Scene *scene, void* handle, int startframe, int endframe, int frameskip)
401 AUD_moveSequencer(scene->sound_scene, handle, startframe / FPS, endframe / FPS, frameskip / FPS);
404 static void sound_start_play_scene(struct Scene *scene)
406 if(scene->sound_scene_handle)
407 AUD_stop(scene->sound_scene_handle);
408 if((scene->sound_scene_handle = AUD_play(scene->sound_scene, 1)))
409 AUD_setLoop(scene->sound_scene_handle, -1);
412 void sound_play_scene(struct Scene *scene)
417 status = scene->sound_scene_handle ? AUD_getStatus(scene->sound_scene_handle) : AUD_STATUS_INVALID;
419 if(status == AUD_STATUS_INVALID)
420 sound_start_play_scene(scene);
422 if(!scene->sound_scene_handle)
428 if(status != AUD_STATUS_PLAYING)
430 AUD_seek(scene->sound_scene_handle, CFRA / FPS);
431 AUD_resume(scene->sound_scene_handle);
434 if(scene->audio.flag & AUDIO_SYNC)
440 void sound_stop_scene(struct Scene *scene)
442 if(scene->sound_scene_handle)
444 AUD_pause(scene->sound_scene_handle);
446 if(scene->audio.flag & AUDIO_SYNC)
451 void sound_seek_scene(struct bContext *C)
453 struct Scene *scene = CTX_data_scene(C);
458 status = scene->sound_scene_handle ? AUD_getStatus(scene->sound_scene_handle) : AUD_STATUS_INVALID;
460 if(status == AUD_STATUS_INVALID)
462 sound_start_play_scene(scene);
464 if(!scene->sound_scene_handle)
470 AUD_pause(scene->sound_scene_handle);
473 if(scene->audio.flag & AUDIO_SCRUB && !CTX_wm_screen(C)->animtimer)
475 if(scene->audio.flag & AUDIO_SYNC)
477 AUD_seek(scene->sound_scene_handle, CFRA / FPS);
478 AUD_seekSequencer(scene->sound_scene_handle, CFRA / FPS);
481 AUD_seek(scene->sound_scene_handle, CFRA / FPS);
482 AUD_resume(scene->sound_scene_handle);
483 if(scene->sound_scrub_handle && AUD_getStatus(scene->sound_scrub_handle) != AUD_STATUS_INVALID)
484 AUD_seek(scene->sound_scrub_handle, 0);
487 if(scene->sound_scrub_handle)
488 AUD_stop(scene->sound_scrub_handle);
489 scene->sound_scrub_handle = AUD_pauseAfter(scene->sound_scene_handle, 1 / FPS);
494 if(scene->audio.flag & AUDIO_SYNC)
495 AUD_seekSequencer(scene->sound_scene_handle, CFRA / FPS);
498 if(status == AUD_STATUS_PLAYING)
499 AUD_seek(scene->sound_scene_handle, CFRA / FPS);
506 float sound_sync_scene(struct Scene *scene)
508 if(scene->sound_scene_handle)
510 if(scene->audio.flag & AUDIO_SYNC)
511 return AUD_getSequencerPosition(scene->sound_scene_handle);
513 return AUD_getPosition(scene->sound_scene_handle);
518 int sound_scene_playing(struct Scene *scene)
520 if(scene->audio.flag & AUDIO_SYNC)
521 return AUD_doesPlayback();
526 int sound_read_sound_buffer(struct bSound* sound, float* buffer, int length, float start, float end)
528 AUD_Sound* limiter = AUD_limitSound(sound->cache, start, end);
529 return AUD_readSound(limiter, buffer, length);
533 int sound_get_channels(struct bSound* sound)
537 info = AUD_getInfo(sound->playback_handle);
539 return info.specs.channels;
542 void* sound_get_factory(void* sound)
544 return ((struct bSound*) sound)->playback_handle;