svn merge -r39145:39286 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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);
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 /******************** mixdown operator ********************/
185
186 static int mixdown_exec(bContext *C, wmOperator *op)
187 {
188         char path[FILE_MAX];
189         char filename[FILE_MAX];
190         Scene *scene;
191         Main *bmain;
192
193         int bitrate, accuracy;
194         AUD_DeviceSpecs specs;
195         AUD_Container container;
196         AUD_Codec codec;
197         const char* result;
198
199         RNA_string_get(op->ptr, "filepath", path);
200         bitrate = RNA_int_get(op->ptr, "bitrate") * 1000;
201         accuracy = RNA_int_get(op->ptr, "accuracy");
202         specs.format = RNA_enum_get(op->ptr, "format");
203         container = RNA_enum_get(op->ptr, "container");
204         codec = RNA_enum_get(op->ptr, "codec");
205         scene = CTX_data_scene(C);
206         bmain = CTX_data_main(C);
207         specs.channels = scene->r.ffcodecdata.audio_channels;
208         specs.rate = scene->r.ffcodecdata.audio_mixrate;
209
210         BLI_strncpy(filename, path, sizeof(filename));
211         BLI_path_abs(filename, bmain->name);
212
213         result = AUD_mixdown(scene->sound_scene, SFRA * specs.rate / FPS, (EFRA - SFRA) * specs.rate / FPS,
214                                                  accuracy, filename, specs, container, codec, bitrate);
215
216         if(result)
217         {
218                 BKE_report(op->reports, RPT_ERROR, result);
219                 return OPERATOR_CANCELLED;
220         }
221
222         return OPERATOR_FINISHED;
223 }
224
225 static int mixdown_invoke(bContext *C, wmOperator *op, wmEvent *event)
226 {
227         if(!RNA_property_is_set(op->ptr, "relative_path"))
228                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
229
230         if(RNA_property_is_set(op->ptr, "filepath"))
231                 return mixdown_exec(C, op);
232
233         return WM_operator_filesel(C, op, event);
234 }
235
236 static int mixdown_draw_check_prop(PropertyRNA *prop)
237 {
238         const char *prop_id= RNA_property_identifier(prop);
239         return !(       strcmp(prop_id, "filepath") == 0 ||
240                                 strcmp(prop_id, "directory") == 0 ||
241                                 strcmp(prop_id, "filename") == 0
242         );
243 }
244
245 static void mixdown_draw(bContext *C, wmOperator *op)
246 {
247         static EnumPropertyItem pcm_format_items[] = {
248                 {AUD_FORMAT_U8, "U8", 0, "U8", "8 bit unsigned"},
249                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
250 #ifdef WITH_SNDFILE
251                 {AUD_FORMAT_S24, "S24", 0, "S24", "24 bit signed"},
252 #endif
253                 {AUD_FORMAT_S32, "S32", 0, "S32", "32 bit signed"},
254                 {AUD_FORMAT_FLOAT32, "F32", 0, "F32", "32 bit floating point"},
255                 {AUD_FORMAT_FLOAT64, "F64", 0, "F64", "64 bit floating point"},
256                 {0, NULL, 0, NULL, NULL}};
257
258         static EnumPropertyItem mp3_format_items[] = {
259                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
260                 {AUD_FORMAT_S32, "S32", 0, "S32", "32 bit signed"},
261                 {0, NULL, 0, NULL, NULL}};
262
263         static EnumPropertyItem ac3_format_items[] = {
264                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
265                 {AUD_FORMAT_FLOAT32, "F32", 0, "F32", "32 bit floating point"},
266                 {0, NULL, 0, NULL, NULL}};
267
268 #ifdef WITH_SNDFILE
269         static EnumPropertyItem flac_format_items[] = {
270                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
271                 {AUD_FORMAT_S24, "S24", 0, "S24", "24 bit signed"},
272                 {0, NULL, 0, NULL, NULL}};
273 #endif
274
275         static EnumPropertyItem all_codec_items[] = {
276                 {AUD_CODEC_AAC, "AAC", 0, "AAC", "Advanced Audio Coding"},
277                 {AUD_CODEC_AC3, "AC3", 0, "AC3", "Dolby Digital ATRAC 3"},
278                 {AUD_CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
279                 {AUD_CODEC_MP2, "MP2", 0, "MP2", "MPEG-1 Audio Layer II"},
280                 {AUD_CODEC_MP3, "MP3", 0, "MP3", "MPEG-2 Audio Layer III"},
281                 {AUD_CODEC_PCM, "PCM", 0, "PCM", "Pulse Code Modulation (RAW)"},
282                 {AUD_CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"},
283                 {0, NULL, 0, NULL, NULL}};
284
285         static EnumPropertyItem ogg_codec_items[] = {
286                 {AUD_CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
287                 {AUD_CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"},
288                 {0, NULL, 0, NULL, NULL}};
289
290         uiLayout *layout = op->layout;
291         wmWindowManager *wm= CTX_wm_manager(C);
292         PointerRNA ptr;
293         PropertyRNA *prop_format;
294         PropertyRNA *prop_codec;
295         PropertyRNA *prop_bitrate;
296
297         AUD_Container container = RNA_enum_get(op->ptr, "container");
298         AUD_Codec codec = RNA_enum_get(op->ptr, "codec");
299
300         prop_format = RNA_struct_find_property(op->ptr, "format");
301         prop_codec = RNA_struct_find_property(op->ptr, "codec");
302         prop_bitrate = RNA_struct_find_property(op->ptr, "bitrate");
303
304         RNA_def_property_clear_flag(prop_bitrate, PROP_HIDDEN);
305         RNA_def_property_flag(prop_codec, PROP_HIDDEN);
306         RNA_def_property_flag(prop_format, PROP_HIDDEN);
307
308         switch(container)
309         {
310         case AUD_CONTAINER_AC3:
311                 RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
312                 RNA_def_property_enum_items(prop_format, ac3_format_items);
313                 RNA_def_property_enum_items(prop_codec, all_codec_items);
314                 RNA_enum_set(op->ptr, "codec", AUD_CODEC_AC3);
315                 break;
316         case AUD_CONTAINER_FLAC:
317                 RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
318                 RNA_def_property_enum_items(prop_codec, all_codec_items);
319                 RNA_enum_set(op->ptr, "codec", AUD_CODEC_FLAC);
320 #ifdef WITH_SNDFILE
321                 RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
322                 RNA_def_property_enum_items(prop_format, flac_format_items);
323 #else
324                 RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
325 #endif
326                 break;
327         case AUD_CONTAINER_MATROSKA:
328                 RNA_def_property_clear_flag(prop_codec, PROP_HIDDEN);
329                 RNA_def_property_enum_items(prop_codec, all_codec_items);
330
331                 switch(codec)
332                 {
333                 case AUD_CODEC_AAC:
334                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
335                         break;
336                 case AUD_CODEC_AC3:
337                         RNA_def_property_enum_items(prop_format, ac3_format_items);
338                         RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
339                         break;
340                 case AUD_CODEC_FLAC:
341                         RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
342                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
343                         break;
344                 case AUD_CODEC_MP2:
345                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
346                         break;
347                 case AUD_CODEC_MP3:
348                         RNA_def_property_enum_items(prop_format, mp3_format_items);
349                         RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
350                         break;
351                 case AUD_CODEC_PCM:
352                         RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
353                         RNA_def_property_enum_items(prop_format, pcm_format_items);
354                         RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
355                         break;
356                 case AUD_CODEC_VORBIS:
357                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
358                         break;
359                 default:
360                         break;
361                 }
362
363                 break;
364         case AUD_CONTAINER_MP2:
365                 RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
366                 RNA_enum_set(op->ptr, "codec", AUD_CODEC_MP2);
367                 RNA_def_property_enum_items(prop_codec, all_codec_items);
368                 break;
369         case AUD_CONTAINER_MP3:
370                 RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
371                 RNA_def_property_enum_items(prop_format, mp3_format_items);
372                 RNA_def_property_enum_items(prop_codec, all_codec_items);
373                 RNA_enum_set(op->ptr, "codec", AUD_CODEC_MP3);
374                 break;
375         case AUD_CONTAINER_OGG:
376                 RNA_def_property_clear_flag(prop_codec, PROP_HIDDEN);
377                 RNA_def_property_enum_items(prop_codec, ogg_codec_items);
378                 RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
379                 break;
380         case AUD_CONTAINER_WAV:
381                 RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
382                 RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
383                 RNA_def_property_enum_items(prop_format, pcm_format_items);
384                 RNA_def_property_enum_items(prop_codec, all_codec_items);
385                 RNA_enum_set(op->ptr, "codec", AUD_CODEC_PCM);
386                 break;
387         default:
388                 break;
389         }
390
391         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
392
393         /* main draw call */
394         uiDefAutoButsRNA(layout, &ptr, mixdown_draw_check_prop, '\0');
395 }
396
397 void SOUND_OT_mixdown(wmOperatorType *ot)
398 {
399         static EnumPropertyItem format_items[] = {
400                 {AUD_FORMAT_U8, "U8", 0, "U8", "8 bit unsigned"},
401                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
402                 {AUD_FORMAT_S24, "S24", 0, "S24", "24 bit signed"},
403                 {AUD_FORMAT_S32, "S32", 0, "S32", "32 bit signed"},
404                 {AUD_FORMAT_FLOAT32, "F32", 0, "F32", "32 bit floating point"},
405                 {AUD_FORMAT_FLOAT64, "F64", 0, "F64", "64 bit floating point"},
406                 {0, NULL, 0, NULL, NULL}};
407
408         static EnumPropertyItem container_items[] = {
409 #ifdef WITH_FFMPEG
410                 {AUD_CONTAINER_AC3, "AC3", 0, "ac3", "Dolby Digital ATRAC 3"},
411 #endif
412                 {AUD_CONTAINER_FLAC, "FLAC", 0, "flac", "Free Lossless Audio Codec"},
413 #ifdef WITH_FFMPEG
414                 {AUD_CONTAINER_MATROSKA, "MATROSKA", 0, "mkv", "Matroska"},
415                 {AUD_CONTAINER_MP2, "MP2", 0, "mp2", "MPEG-1 Audio Layer II"},
416                 {AUD_CONTAINER_MP3, "MP3", 0, "mp3", "MPEG-2 Audio Layer III"},
417 #endif
418                 {AUD_CONTAINER_OGG, "OGG", 0, "ogg", "Xiph.Org Ogg Container"},
419                 {AUD_CONTAINER_WAV, "WAV", 0, "wav", "Waveform Audio File Format"},
420                 {0, NULL, 0, NULL, NULL}};
421
422         static EnumPropertyItem codec_items[] = {
423 #ifdef WITH_FFMPEG
424                 {AUD_CODEC_AAC, "AAC", 0, "AAC", "Advanced Audio Coding"},
425                 {AUD_CODEC_AC3, "AC3", 0, "AC3", "Dolby Digital ATRAC 3"},
426 #endif
427                 {AUD_CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
428 #ifdef WITH_FFMPEG
429                 {AUD_CODEC_MP2, "MP2", 0, "MP2", "MPEG-1 Audio Layer II"},
430                 {AUD_CODEC_MP3, "MP3", 0, "MP3", "MPEG-2 Audio Layer III"},
431 #endif
432                 {AUD_CODEC_PCM, "PCM", 0, "PCM", "Pulse Code Modulation (RAW)"},
433                 {AUD_CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"},
434                 {0, NULL, 0, NULL, NULL}};
435
436         /* identifiers */
437         ot->name= "Mixdown";
438         ot->description= "Mixes the scene's audio to a sound file";
439         ot->idname= "SOUND_OT_mixdown";
440
441         /* api callbacks */
442         ot->exec= mixdown_exec;
443         ot->invoke= mixdown_invoke;
444         ot->ui= mixdown_draw;
445
446         /* flags */
447         ot->flag= OPTYPE_REGISTER;
448
449         /* properties */
450         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH);
451         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);
452         RNA_def_enum(ot->srna, "container", container_items, AUD_CONTAINER_FLAC, "Container", "File format");
453         RNA_def_enum(ot->srna, "codec", codec_items, AUD_CODEC_FLAC, "Codec", "Audio Codec");
454         RNA_def_enum(ot->srna, "format", format_items, AUD_FORMAT_S16, "Format", "Sample format");
455         RNA_def_int(ot->srna, "bitrate", 192, 32, 512, "Bitrate", "Bitrate in kbit/s", 32, 512);
456 }
457
458 /* ******************************************************* */
459
460 static int sound_poll(bContext *C)
461 {
462         Editing* ed = CTX_data_scene(C)->ed;
463
464         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
465                 return 0;
466
467         return 1;
468 }
469 /********************* pack operator *********************/
470
471 static int pack_exec(bContext *C, wmOperator *op)
472 {
473         Editing* ed = CTX_data_scene(C)->ed;
474         bSound* sound;
475
476         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
477                 return OPERATOR_CANCELLED;
478
479         sound = ed->act_seq->sound;
480
481         if(!sound || sound->packedfile)
482                 return OPERATOR_CANCELLED;
483
484         sound->packedfile= newPackedFile(op->reports, sound->name);
485         sound_load(CTX_data_main(C), sound);
486
487         return OPERATOR_FINISHED;
488 }
489
490 static void SOUND_OT_pack(wmOperatorType *ot)
491 {
492         /* identifiers */
493         ot->name= "Pack Sound";
494         ot->description= "Pack the sound into the current blend file";
495         ot->idname= "SOUND_OT_pack";
496
497         /* api callbacks */
498         ot->exec= pack_exec;
499         ot->poll= sound_poll;
500
501         /* flags */
502         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
503 }
504
505 /********************* unpack operator *********************/
506
507 static int sound_unpack_exec(bContext *C, wmOperator *op)
508 {
509         int method= RNA_enum_get(op->ptr, "method");
510         bSound* sound= NULL;
511
512         /* find the suppplied image by name */
513         if (RNA_property_is_set(op->ptr, "id")) {
514                 char sndname[MAX_ID_NAME-2];
515                 RNA_string_get(op->ptr, "id", sndname);
516                 sound = BLI_findstring(&CTX_data_main(C)->sound, sndname, offsetof(ID, name) + 2);
517         }
518
519         if(!sound || !sound->packedfile)
520                 return OPERATOR_CANCELLED;
521
522         if(G.fileflags & G_AUTOPACK)
523                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
524
525         unpackSound(CTX_data_main(C), op->reports, sound, method);
526
527         return OPERATOR_FINISHED;
528 }
529
530 static int sound_unpack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
531 {
532         Editing* ed = CTX_data_scene(C)->ed;
533         bSound* sound;
534
535         if(RNA_property_is_set(op->ptr, "id"))
536                 return sound_unpack_exec(C, op);
537
538         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
539                 return OPERATOR_CANCELLED;
540
541         sound = ed->act_seq->sound;
542
543         if(!sound || !sound->packedfile)
544                 return OPERATOR_CANCELLED;
545
546         if(G.fileflags & G_AUTOPACK)
547                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
548
549         unpack_menu(C, "SOUND_OT_unpack", sound->id.name+2, sound->name, "sounds", sound->packedfile);
550
551         return OPERATOR_FINISHED;
552 }
553
554 static void SOUND_OT_unpack(wmOperatorType *ot)
555 {
556         /* identifiers */
557         ot->name= "Unpack Sound";
558         ot->description= "Unpack the sound to the samples filename";
559         ot->idname= "SOUND_OT_unpack";
560
561         /* api callbacks */
562         ot->exec= sound_unpack_exec;
563         ot->invoke= sound_unpack_invoke;
564         ot->poll= sound_poll;
565
566         /* flags */
567         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
568
569         /* properties */
570         RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
571         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 */
572 }
573
574 /* ******************************************************* */
575
576 static int update_animation_flags_exec(bContext *C, wmOperator *UNUSED(op))
577 {
578         Sequence* seq;
579         Scene* scene = CTX_data_scene(C);
580         struct FCurve* fcu;
581         char driven;
582
583         SEQ_BEGIN(scene->ed, seq) {
584                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "volume", 0, &driven);
585                 if(fcu || driven)
586                         seq->flag |= SEQ_AUDIO_VOLUME_ANIMATED;
587                 else
588                         seq->flag &= ~SEQ_AUDIO_VOLUME_ANIMATED;
589
590                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "pitch", 0, &driven);
591                 if(fcu || driven)
592                         seq->flag |= SEQ_AUDIO_PITCH_ANIMATED;
593                 else
594                         seq->flag &= ~SEQ_AUDIO_PITCH_ANIMATED;
595
596                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "pan", 0, &driven);
597                 if(fcu || driven)
598                         seq->flag |= SEQ_AUDIO_PAN_ANIMATED;
599                 else
600                         seq->flag &= ~SEQ_AUDIO_PAN_ANIMATED;
601         }
602         SEQ_END
603
604         fcu = id_data_find_fcurve(&scene->id, scene, &RNA_Scene, "audio_volume", 0, &driven);
605         if(fcu || driven)
606                 scene->audio.flag |= AUDIO_VOLUME_ANIMATED;
607         else
608                 scene->audio.flag &= ~AUDIO_VOLUME_ANIMATED;
609
610         return OPERATOR_FINISHED;
611 }
612
613 void SOUND_OT_update_animation_flags(wmOperatorType *ot)
614 {
615         /* identifiers */
616         ot->name= "Update animation";
617         ot->description= "Update animation flags";
618         ot->idname= "SOUND_OT_update_animation_flags";
619
620         /* api callbacks */
621         ot->exec= update_animation_flags_exec;
622
623         /* flags */
624         ot->flag= OPTYPE_REGISTER;
625 }
626
627 /* ******************************************************* */
628
629 static int bake_animation_exec(bContext *C, wmOperator *UNUSED(op))
630 {
631         Main* bmain = CTX_data_main(C);
632         Scene* scene = CTX_data_scene(C);
633         int oldfra = scene->r.cfra;
634         int cfra;
635
636         update_animation_flags_exec(C, NULL);
637
638         for(cfra = scene->r.sfra; cfra <= scene->r.efra; cfra++)
639         {
640                 scene->r.cfra = cfra;
641                 scene_update_for_newframe(bmain, scene, scene->lay);
642         }
643
644         scene->r.cfra = oldfra;
645         scene_update_for_newframe(bmain, scene, scene->lay);
646
647         return OPERATOR_FINISHED;
648 }
649
650 void SOUND_OT_bake_animation(wmOperatorType *ot)
651 {
652         /* identifiers */
653         ot->name= "Update animation cache";
654         ot->description= "Updates the audio animation cache so that it's up to date";
655         ot->idname= "SOUND_OT_bake_animation";
656
657         /* api callbacks */
658         ot->exec= bake_animation_exec;
659
660         /* flags */
661         ot->flag= OPTYPE_REGISTER;
662 }
663
664
665 /* ******************************************************* */
666
667 void ED_operatortypes_sound(void)
668 {
669         WM_operatortype_append(SOUND_OT_open);
670         WM_operatortype_append(SOUND_OT_mixdown);
671         WM_operatortype_append(SOUND_OT_pack);
672         WM_operatortype_append(SOUND_OT_unpack);
673         WM_operatortype_append(SOUND_OT_update_animation_flags);
674         WM_operatortype_append(SOUND_OT_bake_animation);
675 }