8744ec5dfd64c336fed03a8cfff0f9743b4ba751
[blender.git] / source / blender / editors / sound / sound_ops.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/sound/sound_ops.c
30  *  \ingroup edsnd
31  */
32
33
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <stddef.h>
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.h"
43
44 #include "DNA_anim_types.h"
45 #include "DNA_packedFile_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_space_types.h"
48 #include "DNA_sequence_types.h"
49 #include "DNA_sound_types.h"
50 #include "DNA_userdef_types.h"
51
52 #include "BKE_context.h"
53 #include "BKE_fcurve.h"
54 #include "BKE_global.h"
55 #include "BKE_main.h"
56 #include "BKE_report.h"
57 #include "BKE_packedFile.h"
58 #include "BKE_scene.h"
59 #include "BKE_sound.h"
60 #include "BKE_sequencer.h"
61
62 #include "RNA_access.h"
63 #include "RNA_define.h"
64 #include "RNA_enum_types.h"
65
66 #include "UI_interface.h"
67
68 #include "WM_api.h"
69 #include "WM_types.h"
70
71 #ifdef WITH_AUDASPACE
72 #  include "AUD_C-API.h"
73 #endif
74
75 #include "ED_sound.h"
76 #include "ED_util.h"
77
78 #include "sound_intern.h"
79
80 /******************** open sound operator ********************/
81
82 static void open_init(bContext *C, wmOperator *op)
83 {
84         PropertyPointerRNA *pprop;
85         
86         op->customdata= pprop= MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
87         uiIDContextProperty(C, &pprop->ptr, &pprop->prop);
88 }
89
90 #ifdef WITH_AUDASPACE
91 static int open_exec(bContext *C, wmOperator *op)
92 {
93         char path[FILE_MAX];
94         bSound *sound;
95         PropertyPointerRNA *pprop;
96         PointerRNA idptr;
97         AUD_SoundInfo info;
98
99         RNA_string_get(op->ptr, "filepath", path);
100         sound = sound_new_file(CTX_data_main(C), path);
101
102         if(!op->customdata)
103                 open_init(C, op);
104         
105         if (sound==NULL || sound->playback_handle == NULL) {
106                 if(op->customdata) MEM_freeN(op->customdata);
107                 BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
108                 return OPERATOR_CANCELLED;
109         }
110
111         info = AUD_getInfo(sound->playback_handle);
112
113         if (info.specs.channels == AUD_CHANNELS_INVALID) {
114                 sound_delete(C, sound);
115                 if(op->customdata) MEM_freeN(op->customdata);
116                 BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
117                 return OPERATOR_CANCELLED;
118         }
119
120         if (RNA_boolean_get(op->ptr, "cache")) {
121                 sound_cache(sound, 0);
122         }
123         
124         /* hook into UI */
125         pprop= op->customdata;
126         
127         if(pprop->prop) {
128                 /* when creating new ID blocks, use is already 1, but RNA
129                  * pointer se also increases user, so this compensates it */
130                 sound->id.us--;
131                 
132                 RNA_id_pointer_create(&sound->id, &idptr);
133                 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
134                 RNA_property_update(C, &pprop->ptr, pprop->prop);
135         }
136
137         if(op->customdata) MEM_freeN(op->customdata);
138         return OPERATOR_FINISHED;
139 }
140
141 #else //WITH_AUDASPACE
142
143 static int open_exec(bContext *UNUSED(C), wmOperator *op)
144 {
145         BKE_report(op->reports, RPT_ERROR, "Compiled without sound support");
146
147         return OPERATOR_CANCELLED;
148 }
149
150 #endif
151
152 static int open_invoke(bContext *C, wmOperator *op, wmEvent *event)
153 {
154         if(!RNA_property_is_set(op->ptr, "relative_path"))
155                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
156         
157         if(RNA_property_is_set(op->ptr, "filepath"))
158                 return open_exec(C, op);
159         
160         open_init(C, op);
161         
162         return WM_operator_filesel(C, op, event);
163 }
164
165 void SOUND_OT_open(wmOperatorType *ot)
166 {
167         /* identifiers */
168         ot->name= "Open Sound";
169         ot->description= "Load a sound file";
170         ot->idname= "SOUND_OT_open";
171
172         /* api callbacks */
173         ot->exec= open_exec;
174         ot->invoke= open_invoke;
175
176         /* flags */
177         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
178
179         /* properties */
180         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH);
181         RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory.");
182 }
183
184 /* ******************************************************* */
185
186 static int sound_poll(bContext *C)
187 {
188         Editing* ed = CTX_data_scene(C)->ed;
189
190         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
191                 return 0;
192
193         return 1;
194 }
195 /********************* pack operator *********************/
196
197 static int pack_exec(bContext *C, wmOperator *op)
198 {
199         Editing* ed = CTX_data_scene(C)->ed;
200         bSound* sound;
201
202         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
203                 return OPERATOR_CANCELLED;
204
205         sound = ed->act_seq->sound;
206
207         if(!sound || sound->packedfile)
208                 return OPERATOR_CANCELLED;
209
210         sound->packedfile= newPackedFile(op->reports, sound->name);
211         sound_load(CTX_data_main(C), sound);
212
213         return OPERATOR_FINISHED;
214 }
215
216 static void SOUND_OT_pack(wmOperatorType *ot)
217 {
218         /* identifiers */
219         ot->name= "Pack Sound";
220         ot->description= "Pack the sound into the current blend file";
221         ot->idname= "SOUND_OT_pack";
222
223         /* api callbacks */
224         ot->exec= pack_exec;
225         ot->poll= sound_poll;
226
227         /* flags */
228         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
229 }
230
231 /********************* unpack operator *********************/
232
233 static int sound_unpack_exec(bContext *C, wmOperator *op)
234 {
235         int method= RNA_enum_get(op->ptr, "method");
236         bSound* sound= NULL;
237
238         /* find the suppplied image by name */
239         if (RNA_property_is_set(op->ptr, "id")) {
240                 char sndname[MAX_ID_NAME-2];
241                 RNA_string_get(op->ptr, "id", sndname);
242                 sound = BLI_findstring(&CTX_data_main(C)->sound, sndname, offsetof(ID, name) + 2);
243         }
244
245         if(!sound || !sound->packedfile)
246                 return OPERATOR_CANCELLED;
247
248         if(G.fileflags & G_AUTOPACK)
249                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
250
251         unpackSound(CTX_data_main(C), op->reports, sound, method);
252
253         return OPERATOR_FINISHED;
254 }
255
256 static int sound_unpack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
257 {
258         Editing* ed = CTX_data_scene(C)->ed;
259         bSound* sound;
260
261         if(RNA_property_is_set(op->ptr, "id"))
262                 return sound_unpack_exec(C, op);
263
264         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
265                 return OPERATOR_CANCELLED;
266
267         sound = ed->act_seq->sound;
268
269         if(!sound || !sound->packedfile)
270                 return OPERATOR_CANCELLED;
271
272         if(G.fileflags & G_AUTOPACK)
273                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
274
275         unpack_menu(C, "SOUND_OT_unpack", sound->id.name+2, sound->name, "sounds", sound->packedfile);
276
277         return OPERATOR_FINISHED;
278 }
279
280 static void SOUND_OT_unpack(wmOperatorType *ot)
281 {
282         /* identifiers */
283         ot->name= "Unpack Sound";
284         ot->description= "Unpack the sound to the samples filename";
285         ot->idname= "SOUND_OT_unpack";
286
287         /* api callbacks */
288         ot->exec= sound_unpack_exec;
289         ot->invoke= sound_unpack_invoke;
290         ot->poll= sound_poll;
291
292         /* flags */
293         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
294
295         /* properties */
296         RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
297         RNA_def_string(ot->srna, "id", "", MAX_ID_NAME-2, "Sound Name", "Sound datablock name to unpack."); /* XXX, weark!, will fail with library, name collisions */
298 }
299
300 /* ******************************************************* */
301
302 static int update_animation_flags_exec(bContext *C, wmOperator *UNUSED(op))
303 {
304         Sequence* seq;
305         Scene* scene = CTX_data_scene(C);
306         struct FCurve* fcu;
307         char driven;
308
309         SEQ_BEGIN(scene->ed, seq) {
310                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "volume", 0, &driven);
311                 if(fcu || driven)
312                         seq->flag |= SEQ_AUDIO_VOLUME_ANIMATED;
313                 else
314                         seq->flag &= ~SEQ_AUDIO_VOLUME_ANIMATED;
315
316                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "pitch", 0, &driven);
317                 if(fcu || driven)
318                         seq->flag |= SEQ_AUDIO_PITCH_ANIMATED;
319                 else
320                         seq->flag &= ~SEQ_AUDIO_PITCH_ANIMATED;
321
322                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "pan", 0, &driven);
323                 if(fcu || driven)
324                         seq->flag |= SEQ_AUDIO_PAN_ANIMATED;
325                 else
326                         seq->flag &= ~SEQ_AUDIO_PAN_ANIMATED;
327         }
328         SEQ_END
329
330         fcu = id_data_find_fcurve(&scene->id, scene, &RNA_Scene, "audio_volume", 0, &driven);
331         if(fcu || driven)
332                 scene->audio.flag |= AUDIO_VOLUME_ANIMATED;
333         else
334                 scene->audio.flag &= ~AUDIO_VOLUME_ANIMATED;
335
336         return OPERATOR_FINISHED;
337 }
338
339 void SOUND_OT_update_animation_flags(wmOperatorType *ot)
340 {
341         /* identifiers */
342         ot->name= "Update animation";
343         ot->description= "Update animation flags";
344         ot->idname= "SOUND_OT_update_animation_flags";
345
346         /* api callbacks */
347         ot->exec= update_animation_flags_exec;
348
349         /* flags */
350         ot->flag= OPTYPE_REGISTER;
351 }
352
353 /* ******************************************************* */
354
355 static int bake_animation_exec(bContext *C, wmOperator *UNUSED(op))
356 {
357         Main* bmain = CTX_data_main(C);
358         Scene* scene = CTX_data_scene(C);
359         int oldfra = scene->r.cfra;
360         int cfra;
361
362         update_animation_flags_exec(C, NULL);
363
364         for(cfra = scene->r.sfra; cfra <= scene->r.efra; cfra++)
365         {
366                 scene->r.cfra = cfra;
367                 scene_update_for_newframe(bmain, scene, scene->lay);
368         }
369
370         scene->r.cfra = oldfra;
371         scene_update_for_newframe(bmain, scene, scene->lay);
372
373         return OPERATOR_FINISHED;
374 }
375
376 void SOUND_OT_bake_animation(wmOperatorType *ot)
377 {
378         /* identifiers */
379         ot->name= "Update animation cache";
380         ot->description= "Updates the audio animation cache so that it's up to date";
381         ot->idname= "SOUND_OT_bake_animation";
382
383         /* api callbacks */
384         ot->exec= bake_animation_exec;
385
386         /* flags */
387         ot->flag= OPTYPE_REGISTER;
388 }
389
390
391 /* ******************************************************* */
392
393 void ED_operatortypes_sound(void)
394 {
395         WM_operatortype_append(SOUND_OT_open);
396         WM_operatortype_append(SOUND_OT_pack);
397         WM_operatortype_append(SOUND_OT_unpack);
398         WM_operatortype_append(SOUND_OT_update_animation_flags);
399         WM_operatortype_append(SOUND_OT_bake_animation);
400 }