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