3D Audio GSoC:
[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 /******************** 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                 }
360
361                 break;
362         case AUD_CONTAINER_MP2:
363                 RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
364                 RNA_enum_set(op->ptr, "codec", AUD_CODEC_MP2);
365                 RNA_def_property_enum_items(prop_codec, all_codec_items);
366                 break;
367         case AUD_CONTAINER_MP3:
368                 RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
369                 RNA_def_property_enum_items(prop_format, mp3_format_items);
370                 RNA_def_property_enum_items(prop_codec, all_codec_items);
371                 RNA_enum_set(op->ptr, "codec", AUD_CODEC_MP3);
372                 break;
373         case AUD_CONTAINER_OGG:
374                 RNA_def_property_clear_flag(prop_codec, PROP_HIDDEN);
375                 RNA_def_property_enum_items(prop_codec, ogg_codec_items);
376                 RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
377                 break;
378         case AUD_CONTAINER_WAV:
379                 RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
380                 RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
381                 RNA_def_property_enum_items(prop_format, pcm_format_items);
382                 RNA_def_property_enum_items(prop_codec, all_codec_items);
383                 RNA_enum_set(op->ptr, "codec", AUD_CODEC_PCM);
384                 break;
385         }
386
387         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
388
389         /* main draw call */
390         uiDefAutoButsRNA(layout, &ptr, mixdown_draw_check_prop, '\0');
391 }
392
393 void SOUND_OT_mixdown(wmOperatorType *ot)
394 {
395         static EnumPropertyItem format_items[] = {
396                 {AUD_FORMAT_U8, "U8", 0, "U8", "8 bit unsigned"},
397                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
398                 {AUD_FORMAT_S24, "S24", 0, "S24", "24 bit signed"},
399                 {AUD_FORMAT_S32, "S32", 0, "S32", "32 bit signed"},
400                 {AUD_FORMAT_FLOAT32, "F32", 0, "F32", "32 bit floating point"},
401                 {AUD_FORMAT_FLOAT64, "F64", 0, "F64", "64 bit floating point"},
402                 {0, NULL, 0, NULL, NULL}};
403
404         static EnumPropertyItem container_items[] = {
405 #ifdef WITH_FFMPEG
406                 {AUD_CONTAINER_AC3, "AC3", 0, "ac3", "Dolby Digital ATRAC 3"},
407 #endif
408                 {AUD_CONTAINER_FLAC, "FLAC", 0, "flac", "Free Lossless Audio Codec"},
409 #ifdef WITH_FFMPEG
410                 {AUD_CONTAINER_MATROSKA, "MATROSKA", 0, "mkv", "Matroska"},
411                 {AUD_CONTAINER_MP2, "MP2", 0, "mp2", "MPEG-1 Audio Layer II"},
412                 {AUD_CONTAINER_MP3, "MP3", 0, "mp3", "MPEG-2 Audio Layer III"},
413 #endif
414                 {AUD_CONTAINER_OGG, "OGG", 0, "ogg", "Xiph.Org Ogg Container"},
415                 {AUD_CONTAINER_WAV, "WAV", 0, "wav", "Waveform Audio File Format"},
416                 {0, NULL, 0, NULL, NULL}};
417
418         static EnumPropertyItem codec_items[] = {
419 #ifdef WITH_FFMPEG
420                 {AUD_CODEC_AAC, "AAC", 0, "AAC", "Advanced Audio Coding"},
421                 {AUD_CODEC_AC3, "AC3", 0, "AC3", "Dolby Digital ATRAC 3"},
422 #endif
423                 {AUD_CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
424 #ifdef WITH_FFMPEG
425                 {AUD_CODEC_MP2, "MP2", 0, "MP2", "MPEG-1 Audio Layer II"},
426                 {AUD_CODEC_MP3, "MP3", 0, "MP3", "MPEG-2 Audio Layer III"},
427 #endif
428                 {AUD_CODEC_PCM, "PCM", 0, "PCM", "Pulse Code Modulation (RAW)"},
429                 {AUD_CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"},
430                 {0, NULL, 0, NULL, NULL}};
431
432         /* identifiers */
433         ot->name= "Mixdown";
434         ot->description= "Mixes the scene's audio to a sound file";
435         ot->idname= "SOUND_OT_mixdown";
436
437         /* api callbacks */
438         ot->exec= mixdown_exec;
439         ot->invoke= mixdown_invoke;
440         ot->ui= mixdown_draw;
441
442         /* flags */
443         ot->flag= OPTYPE_REGISTER;
444
445         /* properties */
446         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH);
447         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);
448         RNA_def_enum(ot->srna, "container", container_items, AUD_CONTAINER_FLAC, "Container", "File format");
449         RNA_def_enum(ot->srna, "codec", codec_items, AUD_CODEC_FLAC, "Codec", "Audio Codec");
450         RNA_def_enum(ot->srna, "format", format_items, AUD_FORMAT_S16, "Format", "Sample format");
451         RNA_def_int(ot->srna, "bitrate", 192, 32, 512, "Bitrate", "Bitrate in kbit/s", 32, 512);
452 }
453
454 /* ******************************************************* */
455
456 static int sound_poll(bContext *C)
457 {
458         Editing* ed = CTX_data_scene(C)->ed;
459
460         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
461                 return 0;
462
463         return 1;
464 }
465 /********************* pack operator *********************/
466
467 static int pack_exec(bContext *C, wmOperator *op)
468 {
469         Editing* ed = CTX_data_scene(C)->ed;
470         bSound* sound;
471
472         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
473                 return OPERATOR_CANCELLED;
474
475         sound = ed->act_seq->sound;
476
477         if(!sound || sound->packedfile)
478                 return OPERATOR_CANCELLED;
479
480         sound->packedfile= newPackedFile(op->reports, sound->name);
481         sound_load(CTX_data_main(C), sound);
482
483         return OPERATOR_FINISHED;
484 }
485
486 static void SOUND_OT_pack(wmOperatorType *ot)
487 {
488         /* identifiers */
489         ot->name= "Pack Sound";
490         ot->description= "Pack the sound into the current blend file";
491         ot->idname= "SOUND_OT_pack";
492
493         /* api callbacks */
494         ot->exec= pack_exec;
495         ot->poll= sound_poll;
496
497         /* flags */
498         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
499 }
500
501 /********************* unpack operator *********************/
502
503 static int sound_unpack_exec(bContext *C, wmOperator *op)
504 {
505         int method= RNA_enum_get(op->ptr, "method");
506         bSound* sound= NULL;
507
508         /* find the suppplied image by name */
509         if (RNA_property_is_set(op->ptr, "id")) {
510                 char sndname[MAX_ID_NAME-2];
511                 RNA_string_get(op->ptr, "id", sndname);
512                 sound = BLI_findstring(&CTX_data_main(C)->sound, sndname, offsetof(ID, name) + 2);
513         }
514
515         if(!sound || !sound->packedfile)
516                 return OPERATOR_CANCELLED;
517
518         if(G.fileflags & G_AUTOPACK)
519                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
520
521         unpackSound(CTX_data_main(C), op->reports, sound, method);
522
523         return OPERATOR_FINISHED;
524 }
525
526 static int sound_unpack_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
527 {
528         Editing* ed = CTX_data_scene(C)->ed;
529         bSound* sound;
530
531         if(RNA_property_is_set(op->ptr, "id"))
532                 return sound_unpack_exec(C, op);
533
534         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
535                 return OPERATOR_CANCELLED;
536
537         sound = ed->act_seq->sound;
538
539         if(!sound || !sound->packedfile)
540                 return OPERATOR_CANCELLED;
541
542         if(G.fileflags & G_AUTOPACK)
543                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
544
545         unpack_menu(C, "SOUND_OT_unpack", sound->id.name+2, sound->name, "sounds", sound->packedfile);
546
547         return OPERATOR_FINISHED;
548 }
549
550 static void SOUND_OT_unpack(wmOperatorType *ot)
551 {
552         /* identifiers */
553         ot->name= "Unpack Sound";
554         ot->description= "Unpack the sound to the samples filename";
555         ot->idname= "SOUND_OT_unpack";
556
557         /* api callbacks */
558         ot->exec= sound_unpack_exec;
559         ot->invoke= sound_unpack_invoke;
560         ot->poll= sound_poll;
561
562         /* flags */
563         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
564
565         /* properties */
566         RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
567         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 */
568 }
569
570 /* ******************************************************* */
571
572 static int update_animation_flags_exec(bContext *C, wmOperator *UNUSED(op))
573 {
574         Sequence* seq;
575         Scene* scene = CTX_data_scene(C);
576         struct FCurve* fcu;
577         char driven;
578
579         SEQ_BEGIN(scene->ed, seq) {
580                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "volume", 0, &driven);
581                 if(fcu || driven)
582                         seq->flag |= SEQ_AUDIO_VOLUME_ANIMATED;
583                 else
584                         seq->flag &= ~SEQ_AUDIO_VOLUME_ANIMATED;
585
586                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "pitch", 0, &driven);
587                 if(fcu || driven)
588                         seq->flag |= SEQ_AUDIO_PITCH_ANIMATED;
589                 else
590                         seq->flag &= ~SEQ_AUDIO_PITCH_ANIMATED;
591
592                 fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "pan", 0, &driven);
593                 if(fcu || driven)
594                         seq->flag |= SEQ_AUDIO_PAN_ANIMATED;
595                 else
596                         seq->flag &= ~SEQ_AUDIO_PAN_ANIMATED;
597         }
598         SEQ_END
599
600         fcu = id_data_find_fcurve(&scene->id, scene, &RNA_Scene, "audio_volume", 0, &driven);
601         if(fcu || driven)
602                 scene->audio.flag |= AUDIO_VOLUME_ANIMATED;
603         else
604                 scene->audio.flag &= ~AUDIO_VOLUME_ANIMATED;
605
606         return OPERATOR_FINISHED;
607 }
608
609 void SOUND_OT_update_animation_flags(wmOperatorType *ot)
610 {
611         /* identifiers */
612         ot->name= "Update animation";
613         ot->description= "Update animation flags";
614         ot->idname= "SOUND_OT_update_animation_flags";
615
616         /* api callbacks */
617         ot->exec= update_animation_flags_exec;
618
619         /* flags */
620         ot->flag= OPTYPE_REGISTER;
621 }
622
623 /* ******************************************************* */
624
625 static int bake_animation_exec(bContext *C, wmOperator *UNUSED(op))
626 {
627         Main* bmain = CTX_data_main(C);
628         Scene* scene = CTX_data_scene(C);
629         int oldfra = scene->r.cfra;
630         int cfra;
631
632         update_animation_flags_exec(C, NULL);
633
634         for(cfra = scene->r.sfra; cfra <= scene->r.efra; cfra++)
635         {
636                 scene->r.cfra = cfra;
637                 scene_update_for_newframe(bmain, scene, scene->lay);
638         }
639
640         scene->r.cfra = oldfra;
641         scene_update_for_newframe(bmain, scene, scene->lay);
642
643         return OPERATOR_FINISHED;
644 }
645
646 void SOUND_OT_bake_animation(wmOperatorType *ot)
647 {
648         /* identifiers */
649         ot->name= "Update animation cache";
650         ot->description= "Updates the audio animation cache so that it's up to date";
651         ot->idname= "SOUND_OT_bake_animation";
652
653         /* api callbacks */
654         ot->exec= bake_animation_exec;
655
656         /* flags */
657         ot->flag= OPTYPE_REGISTER;
658 }
659
660
661 /* ******************************************************* */
662
663 void ED_operatortypes_sound(void)
664 {
665         WM_operatortype_append(SOUND_OT_open);
666         WM_operatortype_append(SOUND_OT_mixdown);
667         WM_operatortype_append(SOUND_OT_pack);
668         WM_operatortype_append(SOUND_OT_unpack);
669         WM_operatortype_append(SOUND_OT_update_animation_flags);
670         WM_operatortype_append(SOUND_OT_bake_animation);
671 }