d5de3f8101b97f79385bbd0734995c2f6f2d1283
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32
33 #include "DNA_packedFile_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_sound_types.h"
37 #include "DNA_sequence_types.h"
38 #include "DNA_windowmanager_types.h"
39
40 #include "BKE_context.h"
41 #include "BKE_global.h"
42 #include "BKE_main.h"
43 #include "BKE_report.h"
44 #include "BKE_packedFile.h"
45 #include "BKE_sound.h"
46 #include "BKE_utildefines.h"
47
48 #include "BLI_blenlib.h"
49
50 #include "ED_sound.h"
51
52 #include "RNA_access.h"
53 #include "RNA_define.h"
54 #include "RNA_enum_types.h"
55
56 #include "UI_interface.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "AUD_C-API.h"
62
63 #include "sound_intern.h"
64
65 /******************** open sound operator ********************/
66
67 static int open_exec(bContext *C, wmOperator *op)
68 {
69         char path[FILE_MAX];
70         bSound *sound;
71         AUD_SoundInfo info;
72
73         RNA_string_get(op->ptr, "path", path);
74
75         sound = sound_new_file(CTX_data_main(C), path);
76
77         if (sound==NULL || sound->handle == NULL) {
78                 BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
79                 return OPERATOR_CANCELLED;
80         }
81
82         info = AUD_getInfo(sound->handle);
83
84         if (info.specs.channels == AUD_CHANNELS_INVALID) {
85                 sound_delete(C, sound);
86                 BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
87                 return OPERATOR_CANCELLED;
88         }
89
90         if (RNA_boolean_get(op->ptr, "cache")) {
91                 sound_cache(sound, 0);
92         }
93
94         return OPERATOR_FINISHED;
95 }
96
97 static int open_invoke(bContext *C, wmOperator *op, wmEvent *event)
98 {
99         return WM_operator_filesel(C, op, event);
100 }
101
102 void SOUND_OT_open(wmOperatorType *ot)
103 {
104         /* identifiers */
105         ot->name= "Open Sound";
106         ot->description= "Load a sound file.";
107         ot->idname= "SOUND_OT_open";
108
109         /* api callbacks */
110         ot->exec= open_exec;
111         ot->invoke= open_invoke;
112
113         /* flags */
114         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
115
116         /* properties */
117         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPEN);
118         RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory.");
119 }
120
121 /* ******************************************************* */
122
123 static int sound_poll(bContext *C)
124 {
125         Editing* ed = CTX_data_scene(C)->ed;
126
127         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
128                 return 0;
129
130         return 1;
131 }
132 /********************* pack operator *********************/
133
134 static int pack_exec(bContext *C, wmOperator *op)
135 {
136         Editing* ed = CTX_data_scene(C)->ed;
137         bSound* sound;
138
139         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
140                 return OPERATOR_CANCELLED;
141
142         sound = ed->act_seq->sound;
143
144         if(!sound || sound->packedfile)
145                 return OPERATOR_CANCELLED;
146
147         sound->packedfile= newPackedFile(op->reports, sound->name);
148         sound_load(CTX_data_main(C), sound);
149
150         return OPERATOR_FINISHED;
151 }
152
153 void SOUND_OT_pack(wmOperatorType *ot)
154 {
155         /* identifiers */
156         ot->name= "Pack Sound";
157         ot->description= "Pack the sound into the current blend file.";
158         ot->idname= "SOUND_OT_pack";
159
160         /* api callbacks */
161         ot->exec= pack_exec;
162         ot->poll= sound_poll;
163
164         /* flags */
165         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
166 }
167
168 /********************* unpack operator *********************/
169
170 // XXX this function is in image_ops.c too, exactly the same, should be moved to a generally accessible position
171 static void unpack_menu(bContext *C, char *opname, char *abs_name, char *folder, PackedFile *pf)
172 {
173         uiPopupMenu *pup;
174         uiLayout *layout;
175         char line[FILE_MAX + 100];
176         char local_name[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
177
178         strcpy(local_name, abs_name);
179         BLI_splitdirstring(local_name, fi);
180         sprintf(local_name, "//%s/%s", folder, fi);
181
182         pup= uiPupMenuBegin(C, "Unpack file", 0);
183         layout= uiPupMenuLayout(pup);
184
185         uiItemEnumO(layout, "Remove Pack", 0, opname, "method", PF_REMOVE);
186
187         if(strcmp(abs_name, local_name)) {
188                 switch(checkPackedFile(local_name, pf)) {
189                         case PF_NOFILE:
190                                 sprintf(line, "Create %s", local_name);
191                                 uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_LOCAL);
192                                 break;
193                         case PF_EQUAL:
194                                 sprintf(line, "Use %s (identical)", local_name);
195                                 uiItemEnumO(layout, line, 0, opname, "method", PF_USE_LOCAL);
196                                 break;
197                         case PF_DIFFERS:
198                                 sprintf(line, "Use %s (differs)", local_name);
199                                 uiItemEnumO(layout, line, 0, opname, "method", PF_USE_LOCAL);
200                                 sprintf(line, "Overwrite %s", local_name);
201                                 uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_LOCAL);
202                                 break;
203                 }
204         }
205
206         switch(checkPackedFile(abs_name, pf)) {
207                 case PF_NOFILE:
208                         sprintf(line, "Create %s", abs_name);
209                         uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_ORIGINAL);
210                         break;
211                 case PF_EQUAL:
212                         sprintf(line, "Use %s (identical)", abs_name);
213                         uiItemEnumO(layout, line, 0, opname, "method", PF_USE_ORIGINAL);
214                         break;
215                 case PF_DIFFERS:
216                         sprintf(line, "Use %s (differs)", local_name);
217                         uiItemEnumO(layout, line, 0, opname, "method", PF_USE_ORIGINAL);
218                         sprintf(line, "Overwrite %s", local_name);
219                         uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_ORIGINAL);
220                         break;
221         }
222
223         uiPupMenuEnd(C, pup);
224 }
225
226 static int unpack_exec(bContext *C, wmOperator *op)
227 {
228         int method= RNA_enum_get(op->ptr, "method");
229         Editing* ed = CTX_data_scene(C)->ed;
230         bSound* sound;
231
232         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
233                 return OPERATOR_CANCELLED;
234
235         sound = ed->act_seq->sound;
236
237         if(!sound || !sound->packedfile)
238                 return OPERATOR_CANCELLED;
239
240         if(G.fileflags & G_AUTOPACK)
241                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
242
243         unpackSound(op->reports, sound, method);
244
245         return OPERATOR_FINISHED;
246 }
247
248 static int unpack_invoke(bContext *C, wmOperator *op, wmEvent *event)
249 {
250         Editing* ed = CTX_data_scene(C)->ed;
251         bSound* sound;
252
253         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
254                 return OPERATOR_CANCELLED;
255
256         sound = ed->act_seq->sound;
257
258         if(!sound || !sound->packedfile)
259                 return OPERATOR_CANCELLED;
260
261         if(G.fileflags & G_AUTOPACK)
262                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
263
264         unpack_menu(C, "SOUND_OT_unpack", sound->name, "audio", sound->packedfile);
265
266         return OPERATOR_FINISHED;
267 }
268
269 void SOUND_OT_unpack(wmOperatorType *ot)
270 {
271         /* identifiers */
272         ot->name= "Unpack Sound";
273         ot->description= "Unpack the sound to the samples filename.";
274         ot->idname= "SOUND_OT_unpack";
275
276         /* api callbacks */
277         ot->exec= unpack_exec;
278         ot->invoke= unpack_invoke;
279         ot->poll= sound_poll;
280
281         /* flags */
282         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
283
284         /* properties */
285         RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
286 }
287
288 /* ******************************************************* */
289
290 void ED_operatortypes_sound(void)
291 {
292         WM_operatortype_append(SOUND_OT_open);
293         WM_operatortype_append(SOUND_OT_pack);
294         WM_operatortype_append(SOUND_OT_unpack);
295 }