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