I made multitude of fixes based on the comments provided online:
[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 int open_cancel(bContext *UNUSED(C), wmOperator *op)
83 {
84         MEM_freeN(op->customdata);
85         op->customdata= NULL;
86         return OPERATOR_CANCELLED;
87 }
88
89 static void open_init(bContext *C, wmOperator *op)
90 {
91         PropertyPointerRNA *pprop;
92
93         op->customdata= pprop= MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
94         uiIDContextProperty(C, &pprop->ptr, &pprop->prop);
95 }
96
97 #ifdef WITH_AUDASPACE
98 static int open_exec(bContext *C, wmOperator *op)
99 {
100         char path[FILE_MAX];
101         bSound *sound;
102         PropertyPointerRNA *pprop;
103         PointerRNA idptr;
104         AUD_SoundInfo info;
105         Main *bmain = CTX_data_main(C);
106
107         RNA_string_get(op->ptr, "filepath", path);
108         sound = sound_new_file(bmain, path);
109
110         if(!op->customdata)
111                 open_init(C, op);
112
113         if (sound==NULL || sound->playback_handle == NULL) {
114                 if(op->customdata) MEM_freeN(op->customdata);
115                 BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
116                 return OPERATOR_CANCELLED;
117         }
118
119         info = AUD_getInfo(sound->playback_handle);
120
121         if (info.specs.channels == AUD_CHANNELS_INVALID) {
122                 sound_delete(bmain, sound);
123                 if(op->customdata) MEM_freeN(op->customdata);
124                 BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
125                 return OPERATOR_CANCELLED;
126         }
127
128         if(RNA_boolean_get(op->ptr, "mono")) {
129                 sound->flags |= SOUND_FLAGS_MONO;
130                 sound_load(bmain, sound);
131         }
132
133         if (RNA_boolean_get(op->ptr, "cache")) {
134                 sound_cache(sound);
135         }
136
137         /* hook into UI */
138         pprop= op->customdata;
139
140         if(pprop->prop) {
141                 /* when creating new ID blocks, use is already 1, but RNA
142                  * pointer se also increases user, so this compensates it */
143                 sound->id.us--;
144
145                 RNA_id_pointer_create(&sound->id, &idptr);
146                 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
147                 RNA_property_update(C, &pprop->ptr, pprop->prop);
148         }
149
150         if(op->customdata) MEM_freeN(op->customdata);
151         return OPERATOR_FINISHED;
152 }
153
154 #else //WITH_AUDASPACE
155
156 static int open_exec(bContext *UNUSED(C), wmOperator *op)
157 {
158         BKE_report(op->reports, RPT_ERROR, "Compiled without sound support");
159
160         return OPERATOR_CANCELLED;
161 }
162
163 #endif
164
165 static int open_invoke(bContext *C, wmOperator *op, wmEvent *event)
166 {
167         if(!RNA_property_is_set(op->ptr, "relative_path"))
168                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
169
170         if(RNA_property_is_set(op->ptr, "filepath"))
171                 return open_exec(C, op);
172
173         open_init(C, op);
174
175         return WM_operator_filesel(C, op, event);
176 }
177
178 void SOUND_OT_open(wmOperatorType *ot)
179 {
180         /* identifiers */
181         ot->name= "Open Sound";
182         ot->description= "Load a sound file";
183         ot->idname= "SOUND_OT_open";
184
185         /* api callbacks */
186         ot->exec= open_exec;
187         ot->invoke= open_invoke;
188         ot->cancel= open_cancel;
189
190         /* flags */
191         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
192
193         /* properties */
194         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH);
195         RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory.");
196         RNA_def_boolean(ot->srna, "mono", FALSE, "Mono", "Mixdown the sound to mono.");
197 }
198
199 void SOUND_OT_open_mono(wmOperatorType *ot)
200 {
201         /* identifiers */
202         ot->name= "Open Sound Mono";
203         ot->description= "Load a sound file as mono";
204         ot->idname= "SOUND_OT_open_mono";
205
206         /* api callbacks */
207         ot->exec= open_exec;
208         ot->invoke= open_invoke;
209         ot->cancel= open_cancel;
210
211         /* flags */
212         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
213
214         /* properties */
215         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH);
216         RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory.");
217         RNA_def_boolean(ot->srna, "mono", TRUE, "Mono", "Mixdown the sound to mono.");
218 }
219
220 /******************** mixdown operator ********************/
221
222 static int mixdown_exec(bContext *C, wmOperator *op)
223 {
224 #ifdef WITH_AUDASPACE
225         char path[FILE_MAX];
226         char filename[FILE_MAX];
227         Scene *scene;
228         Main *bmain;
229
230         int bitrate, accuracy;
231         AUD_DeviceSpecs specs;
232         AUD_Container container;
233         AUD_Codec codec;
234         const char* result;
235
236         RNA_string_get(op->ptr, "filepath", path);
237         bitrate = RNA_int_get(op->ptr, "bitrate") * 1000;
238         accuracy = RNA_int_get(op->ptr, "accuracy");
239         specs.format = RNA_enum_get(op->ptr, "format");
240         container = RNA_enum_get(op->ptr, "container");
241         codec = RNA_enum_get(op->ptr, "codec");
242         scene = CTX_data_scene(C);
243         bmain = CTX_data_main(C);
244         specs.channels = scene->r.ffcodecdata.audio_channels;
245         specs.rate = scene->r.ffcodecdata.audio_mixrate;
246
247         BLI_strncpy(filename, path, sizeof(filename));
248         BLI_path_abs(filename, bmain->name);
249
250         result = AUD_mixdown(scene->sound_scene, SFRA * specs.rate / FPS, (EFRA - SFRA) * specs.rate / FPS,
251                                                  accuracy, filename, specs, container, codec, bitrate);
252
253         if(result)
254         {
255                 BKE_report(op->reports, RPT_ERROR, result);
256                 return OPERATOR_CANCELLED;
257         }
258 #else // WITH_AUDASPACE
259         (void)C;
260         (void)op;
261 #endif // WITH_AUDASPACE
262         return OPERATOR_FINISHED;
263 }
264
265 static int mixdown_invoke(bContext *C, wmOperator *op, wmEvent *event)
266 {
267         if(!RNA_property_is_set(op->ptr, "relative_path"))
268                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
269
270         if(RNA_property_is_set(op->ptr, "filepath"))
271                 return mixdown_exec(C, op);
272
273         return WM_operator_filesel(C, op, event);
274 }
275
276 static int mixdown_draw_check_prop(PropertyRNA *prop)
277 {
278         const char *prop_id= RNA_property_identifier(prop);
279         return !(       strcmp(prop_id, "filepath") == 0 ||
280                                 strcmp(prop_id, "directory") == 0 ||
281                                 strcmp(prop_id, "filename") == 0
282         );
283 }
284
285 #ifdef WITH_AUDASPACE
286 static void mixdown_draw(bContext *C, wmOperator *op)
287 {
288         static EnumPropertyItem pcm_format_items[] = {
289                 {AUD_FORMAT_U8, "U8", 0, "U8", "8 bit unsigned"},
290                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
291 #ifdef WITH_SNDFILE
292                 {AUD_FORMAT_S24, "S24", 0, "S24", "24 bit signed"},
293 #endif
294                 {AUD_FORMAT_S32, "S32", 0, "S32", "32 bit signed"},
295                 {AUD_FORMAT_FLOAT32, "F32", 0, "F32", "32 bit floating point"},
296                 {AUD_FORMAT_FLOAT64, "F64", 0, "F64", "64 bit floating point"},
297                 {0, NULL, 0, NULL, NULL}};
298
299         static EnumPropertyItem mp3_format_items[] = {
300                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
301                 {AUD_FORMAT_S32, "S32", 0, "S32", "32 bit signed"},
302                 {0, NULL, 0, NULL, NULL}};
303
304         static EnumPropertyItem ac3_format_items[] = {
305                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
306                 {AUD_FORMAT_FLOAT32, "F32", 0, "F32", "32 bit floating point"},
307                 {0, NULL, 0, NULL, NULL}};
308
309 #ifdef WITH_SNDFILE
310         static EnumPropertyItem flac_format_items[] = {
311                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
312                 {AUD_FORMAT_S24, "S24", 0, "S24", "24 bit signed"},
313                 {0, NULL, 0, NULL, NULL}};
314 #endif
315
316         static EnumPropertyItem all_codec_items[] = {
317                 {AUD_CODEC_AAC, "AAC", 0, "AAC", "Advanced Audio Coding"},
318                 {AUD_CODEC_AC3, "AC3", 0, "AC3", "Dolby Digital ATRAC 3"},
319                 {AUD_CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
320                 {AUD_CODEC_MP2, "MP2", 0, "MP2", "MPEG-1 Audio Layer II"},
321                 {AUD_CODEC_MP3, "MP3", 0, "MP3", "MPEG-2 Audio Layer III"},
322                 {AUD_CODEC_PCM, "PCM", 0, "PCM", "Pulse Code Modulation (RAW)"},
323                 {AUD_CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"},
324                 {0, NULL, 0, NULL, NULL}};
325
326         static EnumPropertyItem ogg_codec_items[] = {
327                 {AUD_CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
328                 {AUD_CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"},
329                 {0, NULL, 0, NULL, NULL}};
330
331         uiLayout *layout = op->layout;
332         wmWindowManager *wm= CTX_wm_manager(C);
333         PointerRNA ptr;
334         PropertyRNA *prop_format;
335         PropertyRNA *prop_codec;
336         PropertyRNA *prop_bitrate;
337
338         AUD_Container container = RNA_enum_get(op->ptr, "container");
339         AUD_Codec codec = RNA_enum_get(op->ptr, "codec");
340
341         prop_format = RNA_struct_find_property(op->ptr, "format");
342         prop_codec = RNA_struct_find_property(op->ptr, "codec");
343         prop_bitrate = RNA_struct_find_property(op->ptr, "bitrate");
344
345         RNA_def_property_clear_flag(prop_bitrate, PROP_HIDDEN);
346         RNA_def_property_flag(prop_codec, PROP_HIDDEN);
347         RNA_def_property_flag(prop_format, PROP_HIDDEN);
348
349         switch(container)
350         {
351         case AUD_CONTAINER_AC3:
352                 RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
353                 RNA_def_property_enum_items(prop_format, ac3_format_items);
354                 RNA_def_property_enum_items(prop_codec, all_codec_items);
355                 RNA_enum_set(op->ptr, "codec", AUD_CODEC_AC3);
356                 break;
357         case AUD_CONTAINER_FLAC:
358                 RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
359                 RNA_def_property_enum_items(prop_codec, all_codec_items);
360                 RNA_enum_set(op->ptr, "codec", AUD_CODEC_FLAC);
361 #ifdef WITH_SNDFILE
362                 RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
363                 RNA_def_property_enum_items(prop_format, flac_format_items);
364 #else
365                 RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
366 #endif
367                 break;
368         case AUD_CONTAINER_MATROSKA:
369                 RNA_def_property_clear_flag(prop_codec, PROP_HIDDEN);
370                 RNA_def_property_enum_items(prop_codec, all_codec_items);
371
372                 switch(codec)
373                 {
374                 case AUD_CODEC_AAC:
375                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
376                         break;
377                 case AUD_CODEC_AC3:
378                         RNA_def_property_enum_items(prop_format, ac3_format_items);
379                         RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
380                         break;
381                 case AUD_CODEC_FLAC:
382                         RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
383                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
384                         break;
385                 case AUD_CODEC_MP2:
386                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
387                         break;
388                 case AUD_CODEC_MP3:
389                         RNA_def_property_enum_items(prop_format, mp3_format_items);
390                         RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
391                         break;
392                 case AUD_CODEC_PCM:
393                         RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
394                         RNA_def_property_enum_items(prop_format, pcm_format_items);
395                         RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
396                         break;
397                 case AUD_CODEC_VORBIS:
398                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
399                         break;
400                 default:
401                         break;
402                 }
403
404                 break;
405         case AUD_CONTAINER_MP2:
406                 RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
407                 RNA_enum_set(op->ptr, "codec", AUD_CODEC_MP2);
408                 RNA_def_property_enum_items(prop_codec, all_codec_items);
409                 break;
410         case AUD_CONTAINER_MP3:
411                 RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
412                 RNA_def_property_enum_items(prop_format, mp3_format_items);
413                 RNA_def_property_enum_items(prop_codec, all_codec_items);
414                 RNA_enum_set(op->ptr, "codec", AUD_CODEC_MP3);
415                 break;
416         case AUD_CONTAINER_OGG:
417                 RNA_def_property_clear_flag(prop_codec, PROP_HIDDEN);
418                 RNA_def_property_enum_items(prop_codec, ogg_codec_items);
419                 RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
420                 break;
421         case AUD_CONTAINER_WAV:
422                 RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
423                 RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
424                 RNA_def_property_enum_items(prop_format, pcm_format_items);
425                 RNA_def_property_enum_items(prop_codec, all_codec_items);
426                 RNA_enum_set(op->ptr, "codec", AUD_CODEC_PCM);
427                 break;
428         default:
429                 break;
430         }
431
432         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
433
434         /* main draw call */
435         uiDefAutoButsRNA(layout, &ptr, mixdown_draw_check_prop, '\0');
436 }
437 #endif // WITH_AUDASPACE
438
439 void SOUND_OT_mixdown(wmOperatorType *ot)
440 {
441 #ifdef WITH_AUDASPACE
442         static EnumPropertyItem format_items[] = {
443                 {AUD_FORMAT_U8, "U8", 0, "U8", "8 bit unsigned"},
444                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
445                 {AUD_FORMAT_S24, "S24", 0, "S24", "24 bit signed"},
446                 {AUD_FORMAT_S32, "S32", 0, "S32", "32 bit signed"},
447                 {AUD_FORMAT_FLOAT32, "F32", 0, "F32", "32 bit floating point"},
448                 {AUD_FORMAT_FLOAT64, "F64", 0, "F64", "64 bit floating point"},
449                 {0, NULL, 0, NULL, NULL}};
450
451         static EnumPropertyItem container_items[] = {
452 #ifdef WITH_FFMPEG
453                 {AUD_CONTAINER_AC3, "AC3", 0, "ac3", "Dolby Digital ATRAC 3"},
454 #endif
455                 {AUD_CONTAINER_FLAC, "FLAC", 0, "flac", "Free Lossless Audio Codec"},
456 #ifdef WITH_FFMPEG
457                 {AUD_CONTAINER_MATROSKA, "MATROSKA", 0, "mkv", "Matroska"},
458                 {AUD_CONTAINER_MP2, "MP2", 0, "mp2", "MPEG-1 Audio Layer II"},
459                 {AUD_CONTAINER_MP3, "MP3", 0, "mp3", "MPEG-2 Audio Layer III"},
460 #endif
461                 {AUD_CONTAINER_OGG, "OGG", 0, "ogg", "Xiph.Org Ogg Container"},
462                 {AUD_CONTAINER_WAV, "WAV", 0, "wav", "Waveform Audio File Format"},
463                 {0, NULL, 0, NULL, NULL}};
464
465         static EnumPropertyItem codec_items[] = {
466 #ifdef WITH_FFMPEG
467                 {AUD_CODEC_AAC, "AAC", 0, "AAC", "Advanced Audio Coding"},
468                 {AUD_CODEC_AC3, "AC3", 0, "AC3", "Dolby Digital ATRAC 3"},
469 #endif
470                 {AUD_CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
471 #ifdef WITH_FFMPEG
472                 {AUD_CODEC_MP2, "MP2", 0, "MP2", "MPEG-1 Audio Layer II"},
473                 {AUD_CODEC_MP3, "MP3", 0, "MP3", "MPEG-2 Audio Layer III"},
474 #endif
475                 {AUD_CODEC_PCM, "PCM", 0, "PCM", "Pulse Code Modulation (RAW)"},
476                 {AUD_CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"},
477                 {0, NULL, 0, NULL, NULL}};
478
479 #endif // WITH_AUDASPACE
480
481         /* identifiers */
482         ot->name= "Mixdown";
483         ot->description= "Mixes the scene's audio to a sound file";
484         ot->idname= "SOUND_OT_mixdown";
485
486         /* api callbacks */
487         ot->exec= mixdown_exec;
488         ot->invoke= mixdown_invoke;
489
490 #ifdef WITH_AUDASPACE
491         ot->ui= mixdown_draw;
492 #endif
493         /* flags */
494         ot->flag= OPTYPE_REGISTER;
495
496         /* properties */
497         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH);
498 #ifdef WITH_AUDASPACE
499         RNA_def_int(ot->srna, "accuracy", 1024, 1, 16777216, "Accuracy", "Sample accuracy. Important for animation data. The lower the value, the more accurate.", 1, 16777216);
500         RNA_def_enum(ot->srna, "container", container_items, AUD_CONTAINER_FLAC, "Container", "File format");
501         RNA_def_enum(ot->srna, "codec", codec_items, AUD_CODEC_FLAC, "Codec", "Audio Codec");
502         RNA_def_enum(ot->srna, "format", format_items, AUD_FORMAT_S16, "Format", "Sample format");
503         RNA_def_int(ot->srna, "bitrate", 192, 32, 512, "Bitrate", "Bitrate in kbit/s", 32, 512);
504 #endif // WITH_AUDASPACE
505 }
506
507 /* ******************************************************* */
508
509 static int sound_poll(bContext *C)
510 {
511         Editing* ed = CTX_data_scene(C)->ed;
512
513         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
514                 return 0;
515
516         return 1;
517 }
518 /********************* pack operator *********************/
519
520 static int pack_exec(bContext *C, wmOperator *op)
521 {
522         Editing* ed = CTX_data_scene(C)->ed;
523         bSound* sound;
524
525         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
526                 return OPERATOR_CANCELLED;
527
528         sound = ed->act_seq->sound;
529
530         if(!sound || sound->packedfile)
531                 return OPERATOR_CANCELLED;
532
533         sound->packedfile= newPackedFile(op->reports, sound->name);
534         sound_load(CTX_data_main(C), sound);
535
536         return OPERATOR_FINISHED;
537 }
538
539 static void SOUND_OT_pack(wmOperatorType *ot)
540 {
541         /* identifiers */
542         ot->name= "Pack Sound";
543         ot->description= "Pack the sound into the current blend file";
544         ot->idname= "SOUND_OT_pack";
545
546         /* api callbacks */
547         ot->exec= pack_exec;
548         ot->poll= sound_poll;
549
550         /* flags */
551         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
552 }
553
554 /********************* unpack operator *********************/
555
556 static int sound_unpack_exec(bContext *C, wmOperator *op)
557 {
558         int method= RNA_enum_get(op->ptr, "method");
559         bSound* sound= NULL;
560
561         /* find the suppplied image by name */
562         if (RNA_property_is_set(op->ptr, "id")) {
563                 char sndname[MAX_ID_NAME-2];
564                 RNA_string_get(op->ptr, "id", sndname);
565                 sound = BLI_findstring(&CTX_data_main(C)->sound, sndname, offsetof(ID, name) + 2);
566         }
567
568         if(!sound || !sound->packedfile)
569                 return OPERATOR_CANCELLED;
570
571         if(G.fileflags & G_AUTOPACK)
572                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
573
574         unpackSound(CTX_data_main(C), op->reports, sound, method);
575
576         return OPERATOR_FINISHED;
577 }
578
579 static int sound_unpack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
580 {
581         Editing* ed = CTX_data_scene(C)->ed;
582         bSound* sound;
583
584         if(RNA_property_is_set(op->ptr, "id"))
585                 return sound_unpack_exec(C, op);
586
587         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
588                 return OPERATOR_CANCELLED;
589
590         sound = ed->act_seq->sound;
591
592         if(!sound || !sound->packedfile)
593                 return OPERATOR_CANCELLED;
594
595         if(G.fileflags & G_AUTOPACK)
596                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
597
598         unpack_menu(C, "SOUND_OT_unpack", sound->id.name+2, sound->name, "sounds", sound->packedfile);
599
600         return OPERATOR_FINISHED;
601 }
602
603 static void SOUND_OT_unpack(wmOperatorType *ot)
604 {
605         /* identifiers */
606         ot->name= "Unpack Sound";
607         ot->description= "Unpack the sound to the samples filename";
608         ot->idname= "SOUND_OT_unpack";
609
610         /* api callbacks */
611         ot->exec= sound_unpack_exec;
612         ot->invoke= sound_unpack_invoke;
613         ot->poll= sound_poll;
614
615         /* flags */
616         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
617
618         /* properties */
619         RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
620         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 */
621 }
622
623 /* ******************************************************* */
624
625 static int update_animation_flags_exec(bContext *C, wmOperator *UNUSED(op))
626 {
627         Sequence* seq;
628         Scene* scene = CTX_data_scene(C);
629         struct FCurve* fcu;
630         char driven;
631
632         SEQ_BEGIN(scene->ed, seq) {
633                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "volume", 0, &driven);
634                 if(fcu || driven)
635                         seq->flag |= SEQ_AUDIO_VOLUME_ANIMATED;
636                 else
637                         seq->flag &= ~SEQ_AUDIO_VOLUME_ANIMATED;
638
639                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "pitch", 0, &driven);
640                 if(fcu || driven)
641                         seq->flag |= SEQ_AUDIO_PITCH_ANIMATED;
642                 else
643                         seq->flag &= ~SEQ_AUDIO_PITCH_ANIMATED;
644
645                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "pan", 0, &driven);
646                 if(fcu || driven)
647                         seq->flag |= SEQ_AUDIO_PAN_ANIMATED;
648                 else
649                         seq->flag &= ~SEQ_AUDIO_PAN_ANIMATED;
650         }
651         SEQ_END
652
653         fcu = id_data_find_fcurve(&scene->id, scene, &RNA_Scene, "audio_volume", 0, &driven);
654         if(fcu || driven)
655                 scene->audio.flag |= AUDIO_VOLUME_ANIMATED;
656         else
657                 scene->audio.flag &= ~AUDIO_VOLUME_ANIMATED;
658
659         return OPERATOR_FINISHED;
660 }
661
662 void SOUND_OT_update_animation_flags(wmOperatorType *ot)
663 {
664         /*
665           This operator is needed to set a correct state of the sound animation
666           System. Unfortunately there's no really correct place to call the exec
667           function, that's why I made it an operator that's only visible in the
668           search menu. Apart from that the bake animation operator calls it too.
669         */
670
671         /* identifiers */
672         ot->name= "Update animation";
673         ot->description= "Update animation flags";
674         ot->idname= "SOUND_OT_update_animation_flags";
675
676         /* api callbacks */
677         ot->exec= update_animation_flags_exec;
678
679         /* flags */
680         ot->flag= OPTYPE_REGISTER;
681 }
682
683 /* ******************************************************* */
684
685 static int bake_animation_exec(bContext *C, wmOperator *UNUSED(op))
686 {
687         Main* bmain = CTX_data_main(C);
688         Scene* scene = CTX_data_scene(C);
689         int oldfra = scene->r.cfra;
690         int cfra;
691
692         update_animation_flags_exec(C, NULL);
693
694         for(cfra = scene->r.sfra > 0 ? scene->r.sfra - 1 : 0; cfra <= scene->r.efra + 1; cfra++)
695         {
696                 scene->r.cfra = cfra;
697                 scene_update_for_newframe(bmain, scene, scene->lay);
698         }
699
700         scene->r.cfra = oldfra;
701         scene_update_for_newframe(bmain, scene, scene->lay);
702
703         return OPERATOR_FINISHED;
704 }
705
706 void SOUND_OT_bake_animation(wmOperatorType *ot)
707 {
708         /* identifiers */
709         ot->name= "Update animation cache";
710         ot->description= "Updates the audio animation cache so that it's up to date";
711         ot->idname= "SOUND_OT_bake_animation";
712
713         /* api callbacks */
714         ot->exec= bake_animation_exec;
715
716         /* flags */
717         ot->flag= OPTYPE_REGISTER;
718 }
719
720
721 /* ******************************************************* */
722
723 void ED_operatortypes_sound(void)
724 {
725         WM_operatortype_append(SOUND_OT_open);
726         WM_operatortype_append(SOUND_OT_open_mono);
727         WM_operatortype_append(SOUND_OT_mixdown);
728         WM_operatortype_append(SOUND_OT_pack);
729         WM_operatortype_append(SOUND_OT_unpack);
730         WM_operatortype_append(SOUND_OT_update_animation_flags);
731         WM_operatortype_append(SOUND_OT_bake_animation);
732 }