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