2.5 Audio:
[blender.git] / source / blender / blenkernel / intern / sound.c
1 /**
2  * sound.c (mar-2001 nzc)
3  *
4  * $Id$
5  */
6
7 #include <string.h>
8 #include <stdlib.h>
9
10 #include "MEM_guardedalloc.h"
11
12 #include "BLI_blenlib.h"
13
14 #include "DNA_anim_types.h"
15 #include "DNA_scene_types.h"
16 #include "DNA_sequence_types.h"
17 #include "DNA_sound_types.h"
18 #include "DNA_packedFile_types.h"
19 #include "DNA_screen_types.h"
20 #include "DNA_userdef_types.h"
21
22 #include "AUD_C-API.h"
23
24 #include "BKE_utildefines.h"
25 #include "BKE_global.h"
26 #include "BKE_main.h"
27 #include "BKE_sound.h"
28 #include "BKE_context.h"
29 #include "BKE_library.h"
30 #include "BKE_packedFile.h"
31 #include "BKE_fcurve.h"
32 #include "BKE_animsys.h"
33
34 #include "RNA_access.h"
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 static int force_device = -1;
41
42 static void sound_sync_callback(void* data, int mode, float time)
43 {
44         struct Main* main = (struct Main*)data;
45         struct Scene* scene;
46
47         scene = main->scene.first;
48         while(scene)
49         {
50                 if(scene->audio.flag & AUDIO_SYNC)
51                 {
52                         if(mode)
53                                 sound_play_scene(scene);
54                         else
55                                 sound_stop_scene(scene);
56                         AUD_seek(scene->sound_scene_handle, time);
57                 }
58                 scene = scene->id.next;
59         }
60 }
61
62 int sound_define_from_str(char *str)
63 {
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;
72
73         return -1;
74 }
75
76 void sound_force_device(int device)
77 {
78         force_device = device;
79 }
80
81 void sound_init(struct Main *main)
82 {
83         AUD_DeviceSpecs specs;
84         int device, buffersize;
85
86         device = U.audiodevice;
87         buffersize = U.mixbufsize;
88         specs.channels = U.audiochannels;
89         specs.format = U.audioformat;
90         specs.rate = U.audiorate;
91
92         if(force_device >= 0)
93                 device = force_device;
94
95         if(buffersize < 128)
96                 buffersize = AUD_DEFAULT_BUFFER_SIZE;
97
98         if(specs.rate < AUD_RATE_8000)
99                 specs.rate = AUD_RATE_44100;
100
101         if(specs.format <= AUD_FORMAT_INVALID)
102                 specs.format = AUD_FORMAT_S16;
103
104         if(specs.channels <= AUD_CHANNELS_INVALID)
105                 specs.channels = AUD_CHANNELS_STEREO;
106
107         if(!AUD_init(device, specs, buffersize))
108                 AUD_init(AUD_NULL_DEVICE, specs, buffersize);
109         AUD_setSyncCallback(sound_sync_callback, main);
110 }
111
112 void sound_exit()
113 {
114         AUD_exit();
115 }
116
117 struct bSound* sound_new_file(struct Main *main, char* filename)
118 {
119         bSound* sound = NULL;
120
121         char str[FILE_MAX];
122         int len;
123
124         strcpy(str, filename);
125         BLI_convertstringcode(str, main->name);
126
127         len = strlen(filename);
128         while(len > 0 && filename[len-1] != '/' && filename[len-1] != '\\')
129                 len--;
130
131         sound = alloc_libblock(&main->sound, ID_SO, filename+len);
132         strcpy(sound->name, filename);
133 // XXX unused currently sound->type = SOUND_TYPE_FILE;
134
135         sound_load(main, sound);
136
137         if(!sound->playback_handle)
138         {
139                 free_libblock(&main->sound, sound);
140                 sound = NULL;
141         }
142
143         return sound;
144 }
145
146 // XXX unused currently
147 #if 0
148 struct bSound* sound_new_buffer(struct bContext *C, struct bSound *source)
149 {
150         bSound* sound = NULL;
151
152         char name[25];
153         strcpy(name, "buf_");
154         strcpy(name + 4, source->id.name);
155
156         sound = alloc_libblock(&CTX_data_main(C)->sound, ID_SO, name);
157
158         sound->child_sound = source;
159         sound->type = SOUND_TYPE_BUFFER;
160
161         sound_load(CTX_data_main(C), sound);
162
163         if(!sound->playback_handle)
164         {
165                 free_libblock(&CTX_data_main(C)->sound, sound);
166                 sound = NULL;
167         }
168
169         return sound;
170 }
171
172 struct bSound* sound_new_limiter(struct bContext *C, struct bSound *source, float start, float end)
173 {
174         bSound* sound = NULL;
175
176         char name[25];
177         strcpy(name, "lim_");
178         strcpy(name + 4, source->id.name);
179
180         sound = alloc_libblock(&CTX_data_main(C)->sound, ID_SO, name);
181
182         sound->child_sound = source;
183         sound->start = start;
184         sound->end = end;
185         sound->type = SOUND_TYPE_LIMITER;
186
187         sound_load(CTX_data_main(C), sound);
188
189         if(!sound->playback_handle)
190         {
191                 free_libblock(&CTX_data_main(C)->sound, sound);
192                 sound = NULL;
193         }
194
195         return sound;
196 }
197 #endif
198
199 void sound_delete(struct bContext *C, struct bSound* sound)
200 {
201         if(sound)
202         {
203                 sound_free(sound);
204
205                 free_libblock(&CTX_data_main(C)->sound, sound);
206         }
207 }
208
209 void sound_cache(struct bSound* sound, int ignore)
210 {
211         if(sound->cache && !ignore)
212                 AUD_unload(sound->cache);
213
214         sound->cache = AUD_bufferSound(sound->handle);
215         sound->playback_handle = sound->cache;
216 }
217
218 void sound_delete_cache(struct bSound* sound)
219 {
220         if(sound->cache)
221         {
222                 AUD_unload(sound->cache);
223                 sound->cache = NULL;
224                 sound->playback_handle = sound->handle;
225         }
226 }
227
228 void sound_load(struct Main *main, struct bSound* sound)
229 {
230         if(sound)
231         {
232                 if(sound->handle)
233                 {
234                         AUD_unload(sound->handle);
235                         sound->handle = NULL;
236                         sound->playback_handle = NULL;
237                 }
238
239 // XXX unused currently
240 #if 0
241                 switch(sound->type)
242                 {
243                 case SOUND_TYPE_FILE:
244 #endif
245                 {
246                         char fullpath[FILE_MAX];
247                         char *path;
248
249                         /* load sound */
250                         PackedFile* pf = sound->packedfile;
251
252                         /* dont modify soundact->sound->name, only change a copy */
253                         BLI_strncpy(fullpath, sound->name, sizeof(fullpath));
254
255                         if(sound->id.lib)
256                                 path = sound->id.lib->filename;
257                         else
258                                 path = main ? main->name : G.sce;
259
260                         BLI_convertstringcode(fullpath, path);
261
262                         /* but we need a packed file then */
263                         if (pf)
264                                 sound->handle = AUD_loadBuffer((unsigned char*) pf->data, pf->size);
265                         /* or else load it from disk */
266                         else
267                                 sound->handle = AUD_load(fullpath);
268                 } // XXX
269 // XXX unused currently
270 #if 0
271                         break;
272                 }
273                 case SOUND_TYPE_BUFFER:
274                         if(sound->child_sound && sound->child_sound->handle)
275                                 sound->handle = AUD_bufferSound(sound->child_sound->handle);
276                         break;
277                 case SOUND_TYPE_LIMITER:
278                         if(sound->child_sound && sound->child_sound->handle)
279                                 sound->handle = AUD_limitSound(sound->child_sound, sound->start, sound->end);
280                         break;
281                 }
282 #endif
283                 if(sound->cache)
284                         sound->playback_handle = sound->cache;
285                 else
286                         sound->playback_handle = sound->handle;
287         }
288 }
289
290 void sound_free(struct bSound* sound)
291 {
292         if (sound->packedfile)
293         {
294                 freePackedFile(sound->packedfile);
295                 sound->packedfile = NULL;
296         }
297
298         if(sound->handle)
299         {
300                 AUD_unload(sound->handle);
301                 sound->handle = NULL;
302                 sound->playback_handle = NULL;
303         }
304 }
305
306 static float sound_get_volume(Scene* scene, Sequence* sequence, float time)
307 {
308         AnimData *adt= BKE_animdata_from_id(&scene->id);
309         FCurve *fcu = NULL;
310         char buf[64];
311         
312         /* NOTE: this manually constructed path needs to be used here to avoid problems with RNA crashes */
313         sprintf(buf, "sequence_editor.sequences_all[\"%s\"].volume", sequence->name+2);
314         if (adt && adt->action && adt->action->curves.first)
315                 fcu= list_find_fcurve(&adt->action->curves, buf, 0);
316         
317         if(fcu)
318                 return evaluate_fcurve(fcu, time * FPS);
319         else
320                 return sequence->volume;
321 }
322
323 AUD_Device* sound_mixdown(struct Scene *scene, AUD_DeviceSpecs specs, int start, float volume)
324 {
325         AUD_Device* mixdown = AUD_openReadDevice(specs);
326
327         AUD_setDeviceVolume(mixdown, volume);
328
329         AUD_playDevice(mixdown, scene->sound_scene, start / FPS);
330
331         return mixdown;
332 }
333
334 void sound_create_scene(struct Scene *scene)
335 {
336         scene->sound_scene = AUD_createSequencer(scene, (AUD_volumeFunction)&sound_get_volume);
337 }
338
339 void sound_destroy_scene(struct Scene *scene)
340 {
341         if(scene->sound_scene_handle)
342                 AUD_stop(scene->sound_scene_handle);
343         if(scene->sound_scene)
344                 AUD_destroySequencer(scene->sound_scene);
345 }
346
347 void* sound_add_scene_sound(struct Scene *scene, struct Sequence* sequence, int startframe, int endframe, int frameskip)
348 {
349         return AUD_addSequencer(scene->sound_scene, &(sequence->sound->playback_handle), startframe / FPS, endframe / FPS, frameskip / FPS, sequence);
350 }
351
352 void sound_remove_scene_sound(struct Scene *scene, void* handle)
353 {
354         AUD_removeSequencer(scene->sound_scene, handle);
355 }
356
357 void sound_mute_scene_sound(struct Scene *scene, void* handle, char mute)
358 {
359         AUD_muteSequencer(scene->sound_scene, handle, mute);
360 }
361
362 void sound_move_scene_sound(struct Scene *scene, void* handle, int startframe, int endframe, int frameskip)
363 {
364         AUD_moveSequencer(scene->sound_scene, handle, startframe / FPS, endframe / FPS, frameskip / FPS);
365 }
366
367 void sound_start_play_scene(struct Scene *scene)
368 {
369         AUD_Sound* sound;
370         sound = AUD_loopSound(scene->sound_scene);
371         scene->sound_scene_handle = AUD_play(sound, 1);
372         AUD_unload(sound);
373 }
374
375 void sound_play_scene(struct Scene *scene)
376 {
377         AUD_Status status;
378         AUD_lock();
379
380         status = AUD_getStatus(scene->sound_scene_handle);
381
382         if(status == AUD_STATUS_INVALID)
383                 sound_start_play_scene(scene);
384
385         AUD_setLoop(scene->sound_scene_handle, -1, -1);
386
387         if(status != AUD_STATUS_PLAYING)
388         {
389                 AUD_seek(scene->sound_scene_handle, CFRA / FPS);
390                 AUD_resume(scene->sound_scene_handle);
391         }
392
393         if(scene->audio.flag & AUDIO_SYNC)
394                 AUD_startPlayback();
395
396         AUD_unlock();
397 }
398
399 void sound_stop_scene(struct Scene *scene)
400 {
401         AUD_pause(scene->sound_scene_handle);
402
403         if(scene->audio.flag & AUDIO_SYNC)
404                 AUD_stopPlayback();
405 }
406
407 void sound_seek_scene(struct bContext *C)
408 {
409         struct Scene *scene = CTX_data_scene(C);
410         AUD_Status status;
411
412         AUD_lock();
413
414         status = AUD_getStatus(scene->sound_scene_handle);
415
416         if(status == AUD_STATUS_INVALID)
417         {
418                 sound_start_play_scene(scene);
419                 AUD_pause(scene->sound_scene_handle);
420         }
421
422         if(scene->audio.flag & AUDIO_SCRUB && !CTX_wm_screen(C)->animtimer)
423         {
424                 AUD_setLoop(scene->sound_scene_handle, -1, 1 / FPS);
425                 if(scene->audio.flag & AUDIO_SYNC)
426                         AUD_seekSequencer(scene->sound_scene_handle, CFRA / FPS);
427                 else
428                         AUD_seek(scene->sound_scene_handle, CFRA / FPS);
429                 AUD_resume(scene->sound_scene_handle);
430         }
431         else
432         {
433                 if(scene->audio.flag & AUDIO_SYNC)
434                         AUD_seekSequencer(scene->sound_scene_handle, CFRA / FPS);
435                 else
436                 {
437                         if(status == AUD_STATUS_PLAYING)
438                                 AUD_seek(scene->sound_scene_handle, CFRA / FPS);
439                 }
440         }
441
442         AUD_unlock();
443 }
444
445 float sound_sync_scene(struct Scene *scene)
446 {
447         if(scene->audio.flag & AUDIO_SYNC)
448                 return AUD_getSequencerPosition(scene->sound_scene_handle);
449         else
450                 return AUD_getPosition(scene->sound_scene_handle);
451 }
452
453 int sound_scene_playing(struct Scene *scene)
454 {
455         if(scene->audio.flag & AUDIO_SYNC)
456                 return AUD_doesPlayback();
457         else
458                 return -1;
459 }
460
461 int sound_read_sound_buffer(struct bSound* sound, float* buffer, int length)
462 {
463         return AUD_readSound(sound->cache, buffer, length);
464 }