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