2 * sound.c (mar-2001 nzc)
10 #include "MEM_guardedalloc.h"
12 #include "BLI_blenlib.h"
14 #include "DNA_anim_types.h"
15 #include "DNA_scene_types.h"
16 #include "DNA_sequence_types.h"
17 #include "DNA_packedFile_types.h"
18 #include "DNA_screen_types.h"
20 #include "AUD_C-API.h"
22 #include "BKE_utildefines.h"
23 #include "BKE_global.h"
25 #include "BKE_sound.h"
26 #include "BKE_context.h"
27 #include "BKE_library.h"
28 #include "BKE_packedFile.h"
29 #include "BKE_fcurve.h"
30 #include "BKE_animsys.h"
33 static int force_device = -1;
36 static void sound_sync_callback(void* data, int mode, float time)
38 struct Main* bmain = (struct Main*)data;
41 scene = bmain->scene.first;
44 if(scene->audio.flag & AUDIO_SYNC)
47 sound_play_scene(scene);
49 sound_stop_scene(scene);
50 AUD_seek(scene->sound_scene_handle, time);
52 scene = scene->id.next;
57 int sound_define_from_str(char *str)
59 if (BLI_strcaseeq(str, "NULL"))
60 return AUD_NULL_DEVICE;
61 if (BLI_strcaseeq(str, "SDL"))
62 return AUD_SDL_DEVICE;
63 if (BLI_strcaseeq(str, "OPENAL"))
64 return AUD_OPENAL_DEVICE;
65 if (BLI_strcaseeq(str, "JACK"))
66 return AUD_JACK_DEVICE;
71 void sound_force_device(int device)
73 force_device = device;
76 void sound_init_once()
81 void sound_init(struct Main *bmain)
83 AUD_DeviceSpecs specs;
84 int device, buffersize;
86 device = U.audiodevice;
87 buffersize = U.mixbufsize;
88 specs.channels = U.audiochannels;
89 specs.format = U.audioformat;
90 specs.rate = U.audiorate;
93 device = force_device;
96 buffersize = AUD_DEFAULT_BUFFER_SIZE;
98 if(specs.rate < AUD_RATE_8000)
99 specs.rate = AUD_RATE_44100;
101 if(specs.format <= AUD_FORMAT_INVALID)
102 specs.format = AUD_FORMAT_S16;
104 if(specs.channels <= AUD_CHANNELS_INVALID)
105 specs.channels = AUD_CHANNELS_STEREO;
107 if(!AUD_init(device, specs, buffersize))
108 AUD_init(AUD_NULL_DEVICE, specs, buffersize);
111 AUD_setSyncCallback(sound_sync_callback, bmain);
120 struct bSound* sound_new_file(struct Main *bmain, char* filename)
122 bSound* sound = NULL;
129 strcpy(str, filename);
131 path = /*bmain ? bmain->name :*/ G.sce;
133 BLI_path_abs(str, path);
135 len = strlen(filename);
136 while(len > 0 && filename[len-1] != '/' && filename[len-1] != '\\')
139 sound = alloc_libblock(&bmain->sound, ID_SO, filename+len);
140 BLI_strncpy(sound->name, filename, FILE_MAX);
141 // XXX unused currently sound->type = SOUND_TYPE_FILE;
143 sound_load(bmain, sound);
145 if(!sound->playback_handle)
147 free_libblock(&bmain->sound, sound);
154 // XXX unused currently
156 struct bSound* sound_new_buffer(struct bContext *C, struct bSound *source)
158 bSound* sound = NULL;
161 strcpy(name, "buf_");
162 strcpy(name + 4, source->id.name);
164 sound = alloc_libblock(&CTX_data_main(C)->sound, ID_SO, name);
166 sound->child_sound = source;
167 sound->type = SOUND_TYPE_BUFFER;
169 sound_load(CTX_data_main(C), sound);
171 if(!sound->playback_handle)
173 free_libblock(&CTX_data_main(C)->sound, sound);
180 struct bSound* sound_new_limiter(struct bContext *C, struct bSound *source, float start, float end)
182 bSound* sound = NULL;
185 strcpy(name, "lim_");
186 strcpy(name + 4, source->id.name);
188 sound = alloc_libblock(&CTX_data_main(C)->sound, ID_SO, name);
190 sound->child_sound = source;
191 sound->start = start;
193 sound->type = SOUND_TYPE_LIMITER;
195 sound_load(CTX_data_main(C), sound);
197 if(!sound->playback_handle)
199 free_libblock(&CTX_data_main(C)->sound, sound);
207 void sound_delete(struct bContext *C, struct bSound* sound)
213 free_libblock(&CTX_data_main(C)->sound, sound);
217 void sound_cache(struct bSound* sound, int ignore)
219 if(sound->cache && !ignore)
220 AUD_unload(sound->cache);
222 sound->cache = AUD_bufferSound(sound->handle);
223 sound->playback_handle = sound->cache;
226 void sound_delete_cache(struct bSound* sound)
230 AUD_unload(sound->cache);
232 sound->playback_handle = sound->handle;
236 void sound_load(struct Main *bmain, struct bSound* sound)
242 AUD_unload(sound->handle);
243 sound->handle = NULL;
244 sound->playback_handle = NULL;
247 // XXX unused currently
251 case SOUND_TYPE_FILE:
254 char fullpath[FILE_MAX];
258 PackedFile* pf = sound->packedfile;
260 /* dont modify soundact->sound->name, only change a copy */
261 BLI_strncpy(fullpath, sound->name, sizeof(fullpath));
264 path = sound->id.lib->filepath;
266 path = /*bmain ? bmain->name :*/ G.sce;
268 BLI_path_abs(fullpath, path);
270 /* but we need a packed file then */
272 sound->handle = AUD_loadBuffer((unsigned char*) pf->data, pf->size);
273 /* or else load it from disk */
275 sound->handle = AUD_load(fullpath);
277 // XXX unused currently
281 case SOUND_TYPE_BUFFER:
282 if(sound->child_sound && sound->child_sound->handle)
283 sound->handle = AUD_bufferSound(sound->child_sound->handle);
285 case SOUND_TYPE_LIMITER:
286 if(sound->child_sound && sound->child_sound->handle)
287 sound->handle = AUD_limitSound(sound->child_sound, sound->start, sound->end);
292 sound->playback_handle = sound->cache;
294 sound->playback_handle = sound->handle;
298 void sound_free(struct bSound* sound)
300 if (sound->packedfile)
302 freePackedFile(sound->packedfile);
303 sound->packedfile = NULL;
308 AUD_unload(sound->handle);
309 sound->handle = NULL;
310 sound->playback_handle = NULL;
314 static float sound_get_volume(Scene* scene, Sequence* sequence, float time)
316 AnimData *adt= BKE_animdata_from_id(&scene->id);
320 /* NOTE: this manually constructed path needs to be used here to avoid problems with RNA crashes */
321 sprintf(buf, "sequence_editor.sequences_all[\"%s\"].volume", sequence->name+2);
322 if (adt && adt->action && adt->action->curves.first)
323 fcu= list_find_fcurve(&adt->action->curves, buf, 0);
326 return evaluate_fcurve(fcu, time * FPS);
328 return sequence->volume;
331 AUD_Device* sound_mixdown(struct Scene *scene, AUD_DeviceSpecs specs, int start, float volume)
333 AUD_Device* mixdown = AUD_openReadDevice(specs);
335 AUD_setDeviceVolume(mixdown, volume);
337 AUD_playDevice(mixdown, scene->sound_scene, start / FPS);
342 void sound_create_scene(struct Scene *scene)
344 scene->sound_scene = AUD_createSequencer(scene, (AUD_volumeFunction)&sound_get_volume);
347 void sound_destroy_scene(struct Scene *scene)
349 if(scene->sound_scene_handle)
350 AUD_stop(scene->sound_scene_handle);
351 if(scene->sound_scene)
352 AUD_destroySequencer(scene->sound_scene);
355 void* sound_scene_add_scene_sound(struct Scene *scene, struct Sequence* sequence, int startframe, int endframe, int frameskip)
357 if(scene != sequence->scene)
358 return AUD_addSequencer(scene->sound_scene, &(sequence->scene->sound_scene), startframe / FPS, endframe / FPS, frameskip / FPS, sequence);
362 void* sound_add_scene_sound(struct Scene *scene, struct Sequence* sequence, int startframe, int endframe, int frameskip)
364 return AUD_addSequencer(scene->sound_scene, &(sequence->sound->playback_handle), startframe / FPS, endframe / FPS, frameskip / FPS, sequence);
367 void sound_remove_scene_sound(struct Scene *scene, void* handle)
369 AUD_removeSequencer(scene->sound_scene, handle);
372 void sound_mute_scene_sound(struct Scene *scene, void* handle, char mute)
374 AUD_muteSequencer(scene->sound_scene, handle, mute);
377 void sound_move_scene_sound(struct Scene *scene, void* handle, int startframe, int endframe, int frameskip)
379 AUD_moveSequencer(scene->sound_scene, handle, startframe / FPS, endframe / FPS, frameskip / FPS);
382 void sound_start_play_scene(struct Scene *scene)
385 sound = AUD_loopSound(scene->sound_scene);
386 scene->sound_scene_handle = AUD_play(sound, 1);
390 void sound_play_scene(struct Scene *scene)
395 status = AUD_getStatus(scene->sound_scene_handle);
397 if(status == AUD_STATUS_INVALID)
398 sound_start_play_scene(scene);
400 AUD_setLoop(scene->sound_scene_handle, -1, -1);
402 if(status != AUD_STATUS_PLAYING)
404 AUD_seek(scene->sound_scene_handle, CFRA / FPS);
405 AUD_resume(scene->sound_scene_handle);
408 if(scene->audio.flag & AUDIO_SYNC)
414 void sound_stop_scene(struct Scene *scene)
416 AUD_pause(scene->sound_scene_handle);
418 if(scene->audio.flag & AUDIO_SYNC)
422 void sound_seek_scene(struct bContext *C)
424 struct Scene *scene = CTX_data_scene(C);
429 status = AUD_getStatus(scene->sound_scene_handle);
431 if(status == AUD_STATUS_INVALID)
433 sound_start_play_scene(scene);
434 AUD_pause(scene->sound_scene_handle);
437 if(scene->audio.flag & AUDIO_SCRUB && !CTX_wm_screen(C)->animtimer)
439 AUD_setLoop(scene->sound_scene_handle, -1, 1 / FPS);
440 if(scene->audio.flag & AUDIO_SYNC)
441 AUD_seekSequencer(scene->sound_scene_handle, CFRA / FPS);
443 AUD_seek(scene->sound_scene_handle, CFRA / FPS);
444 AUD_resume(scene->sound_scene_handle);
448 if(scene->audio.flag & AUDIO_SYNC)
449 AUD_seekSequencer(scene->sound_scene_handle, CFRA / FPS);
452 if(status == AUD_STATUS_PLAYING)
453 AUD_seek(scene->sound_scene_handle, CFRA / FPS);
460 float sound_sync_scene(struct Scene *scene)
462 if(scene->audio.flag & AUDIO_SYNC)
463 return AUD_getSequencerPosition(scene->sound_scene_handle);
465 return AUD_getPosition(scene->sound_scene_handle);
468 int sound_scene_playing(struct Scene *scene)
470 if(scene->audio.flag & AUDIO_SYNC)
471 return AUD_doesPlayback();
476 int sound_read_sound_buffer(struct bSound* sound, float* buffer, int length, float start, float end)
478 AUD_Sound* limiter = AUD_limitSound(sound->cache, start, end);
479 return AUD_readSound(limiter, buffer, length);