Cleanup: trailing whitespace (comment blocks)
[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_packedFile_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_space_types.h"
45 #include "DNA_sequence_types.h"
46 #include "DNA_sound_types.h"
47 #include "DNA_userdef_types.h"
48
49 #include "BKE_context.h"
50 #include "BKE_fcurve.h"
51 #include "BKE_global.h"
52 #include "BKE_main.h"
53 #include "BKE_report.h"
54 #include "BKE_library.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_SPECIAL_H
71 #endif
72
73 #include "ED_sound.h"
74 #include "ED_util.h"
75
76
77 /******************** open sound operator ********************/
78
79 static void sound_open_cancel(bContext *UNUSED(C), wmOperator *op)
80 {
81         MEM_freeN(op->customdata);
82         op->customdata = NULL;
83 }
84
85 static void sound_open_init(bContext *C, wmOperator *op)
86 {
87         PropertyPointerRNA *pprop;
88
89         op->customdata = pprop = MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
90         UI_context_active_but_prop_get_templateID(C, &pprop->ptr, &pprop->prop);
91 }
92
93 #ifdef WITH_AUDASPACE
94 static int sound_open_exec(bContext *C, wmOperator *op)
95 {
96         char path[FILE_MAX];
97         bSound *sound;
98         PropertyPointerRNA *pprop;
99         PointerRNA idptr;
100         AUD_SoundInfo info;
101         Main *bmain = CTX_data_main(C);
102
103         RNA_string_get(op->ptr, "filepath", path);
104         sound = BKE_sound_new_file(bmain, path);
105
106         if (!op->customdata)
107                 sound_open_init(C, op);
108
109         if (sound->playback_handle == NULL) {
110                 if (op->customdata) MEM_freeN(op->customdata);
111                 BKE_libblock_free(bmain, sound);
112                 BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
113                 return OPERATOR_CANCELLED;
114         }
115
116         info = AUD_getInfo(sound->playback_handle);
117
118         if (info.specs.channels == AUD_CHANNELS_INVALID) {
119                 BKE_libblock_free(bmain, sound);
120                 if (op->customdata) MEM_freeN(op->customdata);
121                 BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
122                 return OPERATOR_CANCELLED;
123         }
124
125         if (RNA_boolean_get(op->ptr, "mono")) {
126                 sound->flags |= SOUND_FLAGS_MONO;
127                 BKE_sound_load(bmain, sound);
128         }
129
130         if (RNA_boolean_get(op->ptr, "cache")) {
131                 BKE_sound_cache(sound);
132         }
133
134         /* hook into UI */
135         pprop = op->customdata;
136
137         if (pprop->prop) {
138                 /* when creating new ID blocks, use is already 1, but RNA
139                  * pointer use also increases user, so this compensates it */
140                 id_us_min(&sound->id);
141
142                 RNA_id_pointer_create(&sound->id, &idptr);
143                 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
144                 RNA_property_update(C, &pprop->ptr, pprop->prop);
145         }
146
147         MEM_freeN(op->customdata);
148         return OPERATOR_FINISHED;
149 }
150
151 #else //WITH_AUDASPACE
152
153 static int sound_open_exec(bContext *UNUSED(C), wmOperator *op)
154 {
155         BKE_report(op->reports, RPT_ERROR, "Compiled without sound support");
156
157         return OPERATOR_CANCELLED;
158 }
159
160 #endif
161
162 static int sound_open_invoke(bContext *C, wmOperator *op, const wmEvent *event)
163 {
164         if (RNA_struct_property_is_set(op->ptr, "filepath"))
165                 return sound_open_exec(C, op);
166
167         sound_open_init(C, op);
168
169         return WM_operator_filesel(C, op, event);
170 }
171
172 static void SOUND_OT_open(wmOperatorType *ot)
173 {
174         /* identifiers */
175         ot->name = "Open Sound";
176         ot->description = "Load a sound file";
177         ot->idname = "SOUND_OT_open";
178
179         /* api callbacks */
180         ot->exec = sound_open_exec;
181         ot->invoke = sound_open_invoke;
182         ot->cancel = sound_open_cancel;
183
184         /* flags */
185         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
186
187         /* properties */
188         WM_operator_properties_filesel(
189                 ot, FILE_TYPE_FOLDER | FILE_TYPE_SOUND | FILE_TYPE_MOVIE, FILE_SPECIAL, FILE_OPENFILE,
190                 WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
191         RNA_def_boolean(ot->srna, "cache", false, "Cache", "Cache the sound in memory");
192         RNA_def_boolean(ot->srna, "mono", false, "Mono", "Merge all the sound's channels into one");
193 }
194
195 static void SOUND_OT_open_mono(wmOperatorType *ot)
196 {
197         /* identifiers */
198         ot->name = "Open Sound Mono";
199         ot->description = "Load a sound file as mono";
200         ot->idname = "SOUND_OT_open_mono";
201
202         /* api callbacks */
203         ot->exec = sound_open_exec;
204         ot->invoke = sound_open_invoke;
205         ot->cancel = sound_open_cancel;
206
207         /* flags */
208         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
209
210         /* properties */
211         WM_operator_properties_filesel(
212                 ot, FILE_TYPE_FOLDER | FILE_TYPE_SOUND | FILE_TYPE_MOVIE, FILE_SPECIAL, FILE_OPENFILE,
213                 WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
214         RNA_def_boolean(ot->srna, "cache", false, "Cache", "Cache the sound in memory");
215         RNA_def_boolean(ot->srna, "mono", true, "Mono", "Mixdown the sound to mono");
216 }
217
218 /* ******************************************************* */
219
220 static void sound_update_animation_flags(Scene *scene);
221
222 static int sound_update_animation_flags_cb(Sequence *seq, void *user_data)
223 {
224         struct FCurve *fcu;
225         Scene *scene = (Scene *)user_data;
226         bool driven;
227
228         fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "volume", 0, &driven);
229         if (fcu || driven)
230                 seq->flag |= SEQ_AUDIO_VOLUME_ANIMATED;
231         else
232                 seq->flag &= ~SEQ_AUDIO_VOLUME_ANIMATED;
233
234         fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "pitch", 0, &driven);
235         if (fcu || driven)
236                 seq->flag |= SEQ_AUDIO_PITCH_ANIMATED;
237         else
238                 seq->flag &= ~SEQ_AUDIO_PITCH_ANIMATED;
239
240         fcu = id_data_find_fcurve(&scene->id, seq, &RNA_Sequence, "pan", 0, &driven);
241         if (fcu || driven)
242                 seq->flag |= SEQ_AUDIO_PAN_ANIMATED;
243         else
244                 seq->flag &= ~SEQ_AUDIO_PAN_ANIMATED;
245
246         if (seq->type == SEQ_TYPE_SCENE) {
247                 /* TODO(sergey): For now we do manual recursion into the scene strips,
248                  * but perhaps it should be covered by recursive_apply?
249                  */
250                 sound_update_animation_flags(seq->scene);
251         }
252
253         return 0;
254 }
255
256 static void sound_update_animation_flags(Scene *scene)
257 {
258         struct FCurve *fcu;
259         bool driven;
260         Sequence *seq;
261
262         if (scene->id.tag & LIB_TAG_DOIT) {
263                 return;
264         }
265         scene->id.tag |= LIB_TAG_DOIT;
266
267         SEQ_BEGIN(scene->ed, seq)
268         {
269                 BKE_sequencer_recursive_apply(seq, sound_update_animation_flags_cb, scene);
270         }
271         SEQ_END
272
273         fcu = id_data_find_fcurve(&scene->id, scene, &RNA_Scene, "audio_volume", 0, &driven);
274         if (fcu || driven)
275                 scene->audio.flag |= AUDIO_VOLUME_ANIMATED;
276         else
277                 scene->audio.flag &= ~AUDIO_VOLUME_ANIMATED;
278 }
279
280 static int sound_update_animation_flags_exec(bContext *C, wmOperator *UNUSED(op))
281 {
282         BKE_main_id_tag_idcode(CTX_data_main(C), ID_SCE, LIB_TAG_DOIT, false);
283         sound_update_animation_flags(CTX_data_scene(C));
284         return OPERATOR_FINISHED;
285 }
286
287 static void SOUND_OT_update_animation_flags(wmOperatorType *ot)
288 {
289         /*
290          * This operator is needed to set a correct state of the sound animation
291          * System. Unfortunately there's no really correct place to call the exec
292          * function, that's why I made it an operator that's only visible in the
293          * search menu. Apart from that the bake animation operator calls it too.
294          */
295
296         /* identifiers */
297         ot->name = "Update Animation";
298         ot->description = "Update animation flags";
299         ot->idname = "SOUND_OT_update_animation_flags";
300
301         /* api callbacks */
302         ot->exec = sound_update_animation_flags_exec;
303
304         /* flags */
305         ot->flag = OPTYPE_REGISTER;
306 }
307
308 /* ******************************************************* */
309
310 static int sound_bake_animation_exec(bContext *C, wmOperator *UNUSED(op))
311 {
312         Main *bmain = CTX_data_main(C);
313         Scene *scene = CTX_data_scene(C);
314         int oldfra = scene->r.cfra;
315         int cfra;
316
317         sound_update_animation_flags_exec(C, NULL);
318
319         for (cfra = (scene->r.sfra > 0) ? (scene->r.sfra - 1) : 0; cfra <= scene->r.efra + 1; cfra++) {
320                 scene->r.cfra = cfra;
321                 BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, scene->lay);
322         }
323
324         scene->r.cfra = oldfra;
325         BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, scene->lay);
326
327         return OPERATOR_FINISHED;
328 }
329
330 static void SOUND_OT_bake_animation(wmOperatorType *ot)
331 {
332         /* identifiers */
333         ot->name = "Update Animation Cache";
334         ot->description = "Update the audio animation cache";
335         ot->idname = "SOUND_OT_bake_animation";
336
337         /* api callbacks */
338         ot->exec = sound_bake_animation_exec;
339
340         /* flags */
341         ot->flag = OPTYPE_REGISTER;
342 }
343
344
345 /******************** mixdown operator ********************/
346
347 static int sound_mixdown_exec(bContext *C, wmOperator *op)
348 {
349 #ifdef WITH_AUDASPACE
350         char path[FILE_MAX];
351         char filename[FILE_MAX];
352         Scene *scene;
353         Main *bmain;
354         int split;
355
356         int bitrate, accuracy;
357         AUD_DeviceSpecs specs;
358         AUD_Container container;
359         AUD_Codec codec;
360         const char *result;
361
362         sound_bake_animation_exec(C, op);
363
364         RNA_string_get(op->ptr, "filepath", path);
365         bitrate = RNA_int_get(op->ptr, "bitrate") * 1000;
366         accuracy = RNA_int_get(op->ptr, "accuracy");
367         specs.format = RNA_enum_get(op->ptr, "format");
368         container = RNA_enum_get(op->ptr, "container");
369         codec = RNA_enum_get(op->ptr, "codec");
370         split = RNA_boolean_get(op->ptr, "split_channels");
371         scene = CTX_data_scene(C);
372         bmain = CTX_data_main(C);
373         specs.channels = scene->r.ffcodecdata.audio_channels;
374         specs.rate = scene->r.ffcodecdata.audio_mixrate;
375
376         BLI_strncpy(filename, path, sizeof(filename));
377         BLI_path_abs(filename, bmain->name);
378
379         if (split)
380                 result = AUD_mixdown_per_channel(scene->sound_scene, SFRA * specs.rate / FPS, (EFRA - SFRA + 1) * specs.rate / FPS,
381                                                  accuracy, filename, specs, container, codec, bitrate);
382         else
383                 result = AUD_mixdown(scene->sound_scene, SFRA * specs.rate / FPS, (EFRA - SFRA + 1) * specs.rate / FPS,
384                                      accuracy, filename, specs, container, codec, bitrate);
385
386         BKE_sound_reset_scene_specs(scene);
387
388         if (result) {
389                 BKE_report(op->reports, RPT_ERROR, result);
390                 return OPERATOR_CANCELLED;
391         }
392 #else // WITH_AUDASPACE
393         (void)C;
394         (void)op;
395 #endif // WITH_AUDASPACE
396         return OPERATOR_FINISHED;
397 }
398
399 #ifdef WITH_AUDASPACE
400 static const EnumPropertyItem container_items[] = {
401 #ifdef WITH_FFMPEG
402         {AUD_CONTAINER_AC3, "AC3", 0, "ac3", "Dolby Digital ATRAC 3"},
403 #endif
404         {AUD_CONTAINER_FLAC, "FLAC", 0, "flac", "Free Lossless Audio Codec"},
405 #ifdef WITH_FFMPEG
406         {AUD_CONTAINER_MATROSKA, "MATROSKA", 0, "mkv", "Matroska"},
407         {AUD_CONTAINER_MP2, "MP2", 0, "mp2", "MPEG-1 Audio Layer II"},
408         {AUD_CONTAINER_MP3, "MP3", 0, "mp3", "MPEG-2 Audio Layer III"},
409 #endif
410         {AUD_CONTAINER_OGG, "OGG", 0, "ogg", "Xiph.Org Ogg Container"},
411         {AUD_CONTAINER_WAV, "WAV", 0, "wav", "Waveform Audio File Format"},
412         {0, NULL, 0, NULL, NULL}
413 };
414
415 static const char *snd_ext_sound[] = {
416         ".ac3",
417         ".flac",
418         ".mkv",
419         ".mp2",
420         ".mp3",
421         ".ogg",
422         ".wav",
423         NULL
424 };
425
426 static bool sound_mixdown_check(bContext *UNUSED(C), wmOperator *op)
427 {
428         AUD_Container container = RNA_enum_get(op->ptr, "container");
429
430         const char *extension = NULL;
431
432         const EnumPropertyItem *item = container_items;
433         while (item->identifier != NULL) {
434                 if (item->value == container) {
435                         const char **ext = snd_ext_sound;
436                         while (*ext != NULL) {
437                                 if (STREQ(*ext + 1, item->name)) {
438                                         extension = *ext;
439                                         break;
440                                 }
441
442                                 ext++;
443                         }
444                 }
445                 item++;
446         }
447
448         if (extension) {
449                 PropertyRNA *prop;
450                 char filepath[FILE_MAX];
451
452                 int check;
453
454                 prop = RNA_struct_find_property(op->ptr, "filepath");
455                 RNA_property_string_get(op->ptr, prop, filepath);
456
457                 if (BLI_testextensie_array(filepath, snd_ext_sound))
458                         check = BLI_replace_extension(filepath, FILE_MAX, extension);
459                 else
460                         check = BLI_ensure_extension(filepath, FILE_MAX, extension);
461
462                 if (!check)
463                         return check;
464
465                 RNA_property_string_set(op->ptr, prop, filepath);
466                 return true;
467         }
468
469         return false;
470 }
471
472 #endif // WITH_AUDASPACE
473
474 static int sound_mixdown_invoke(bContext *C, wmOperator *op, const wmEvent *event)
475 {
476         if (RNA_struct_property_is_set(op->ptr, "filepath"))
477                 return sound_mixdown_exec(C, op);
478
479         return WM_operator_filesel(C, op, event);
480 }
481
482 #ifdef WITH_AUDASPACE
483
484 static bool sound_mixdown_draw_check_prop(PointerRNA *UNUSED(ptr), PropertyRNA *prop)
485 {
486         const char *prop_id = RNA_property_identifier(prop);
487         return !(STREQ(prop_id, "filepath") ||
488                  STREQ(prop_id, "directory") ||
489                  STREQ(prop_id, "filename"));
490 }
491
492 static void sound_mixdown_draw(bContext *C, wmOperator *op)
493 {
494         static const EnumPropertyItem pcm_format_items[] = {
495                 {AUD_FORMAT_U8, "U8", 0, "U8", "8 bit unsigned"},
496                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
497 #ifdef WITH_SNDFILE
498                 {AUD_FORMAT_S24, "S24", 0, "S24", "24 bit signed"},
499 #endif
500                 {AUD_FORMAT_S32, "S32", 0, "S32", "32 bit signed"},
501                 {AUD_FORMAT_FLOAT32, "F32", 0, "F32", "32 bit floating point"},
502                 {AUD_FORMAT_FLOAT64, "F64", 0, "F64", "64 bit floating point"},
503                 {0, NULL, 0, NULL, NULL}
504         };
505
506         static const EnumPropertyItem mp3_format_items[] = {
507                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
508                 {AUD_FORMAT_S32, "S32", 0, "S32", "32 bit signed"},
509                 {0, NULL, 0, NULL, NULL}
510         };
511
512 #ifdef WITH_SNDFILE
513         static const EnumPropertyItem flac_format_items[] = {
514                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
515                 {AUD_FORMAT_S24, "S24", 0, "S24", "24 bit signed"},
516                 {0, NULL, 0, NULL, NULL}
517         };
518 #endif
519
520         static const EnumPropertyItem all_codec_items[] = {
521                 {AUD_CODEC_AAC, "AAC", 0, "AAC", "Advanced Audio Coding"},
522                 {AUD_CODEC_AC3, "AC3", 0, "AC3", "Dolby Digital ATRAC 3"},
523                 {AUD_CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
524                 {AUD_CODEC_MP2, "MP2", 0, "MP2", "MPEG-1 Audio Layer II"},
525                 {AUD_CODEC_MP3, "MP3", 0, "MP3", "MPEG-2 Audio Layer III"},
526                 {AUD_CODEC_PCM, "PCM", 0, "PCM", "Pulse Code Modulation (RAW)"},
527                 {AUD_CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"},
528                 {0, NULL, 0, NULL, NULL}
529         };
530
531         static const EnumPropertyItem ogg_codec_items[] = {
532                 {AUD_CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
533                 {AUD_CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"},
534                 {0, NULL, 0, NULL, NULL}
535         };
536
537         uiLayout *layout = op->layout;
538         wmWindowManager *wm = CTX_wm_manager(C);
539         PointerRNA ptr;
540         PropertyRNA *prop_format;
541         PropertyRNA *prop_codec;
542         PropertyRNA *prop_bitrate;
543
544         AUD_Container container = RNA_enum_get(op->ptr, "container");
545         AUD_Codec codec = RNA_enum_get(op->ptr, "codec");
546
547         prop_format = RNA_struct_find_property(op->ptr, "format");
548         prop_codec = RNA_struct_find_property(op->ptr, "codec");
549         prop_bitrate = RNA_struct_find_property(op->ptr, "bitrate");
550
551         RNA_def_property_clear_flag(prop_bitrate, PROP_HIDDEN);
552         RNA_def_property_flag(prop_codec, PROP_HIDDEN);
553         RNA_def_property_flag(prop_format, PROP_HIDDEN);
554
555         switch (container) {
556                 case AUD_CONTAINER_AC3:
557                         RNA_def_property_enum_items(prop_codec, all_codec_items);
558                         RNA_enum_set(op->ptr, "codec", AUD_CODEC_AC3);
559                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_FLOAT32);
560                         break;
561                 case AUD_CONTAINER_FLAC:
562                         RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
563                         RNA_def_property_enum_items(prop_codec, all_codec_items);
564                         RNA_enum_set(op->ptr, "codec", AUD_CODEC_FLAC);
565 #ifdef WITH_SNDFILE
566                         RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
567                         RNA_def_property_enum_items(prop_format, flac_format_items);
568 #else
569                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
570 #endif
571                         break;
572                 case AUD_CONTAINER_MATROSKA:
573                         RNA_def_property_clear_flag(prop_codec, PROP_HIDDEN);
574                         RNA_def_property_enum_items(prop_codec, all_codec_items);
575
576                         switch (codec) {
577                                 case AUD_CODEC_AAC:
578                                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
579                                         break;
580                                 case AUD_CODEC_AC3:
581                                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_FLOAT32);
582                                         break;
583                                 case AUD_CODEC_FLAC:
584                                         RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
585                                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
586                                         break;
587                                 case AUD_CODEC_MP2:
588                                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
589                                         break;
590                                 case AUD_CODEC_MP3:
591                                         RNA_def_property_enum_items(prop_format, mp3_format_items);
592                                         RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
593                                         break;
594                                 case AUD_CODEC_PCM:
595                                         RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
596                                         RNA_def_property_enum_items(prop_format, pcm_format_items);
597                                         RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
598                                         break;
599                                 case AUD_CODEC_VORBIS:
600                                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
601                                         break;
602                                 default:
603                                         break;
604                         }
605
606                         break;
607                 case AUD_CONTAINER_MP2:
608                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
609                         RNA_enum_set(op->ptr, "codec", AUD_CODEC_MP2);
610                         RNA_def_property_enum_items(prop_codec, all_codec_items);
611                         break;
612                 case AUD_CONTAINER_MP3:
613                         RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
614                         RNA_def_property_enum_items(prop_format, mp3_format_items);
615                         RNA_def_property_enum_items(prop_codec, all_codec_items);
616                         RNA_enum_set(op->ptr, "codec", AUD_CODEC_MP3);
617                         break;
618                 case AUD_CONTAINER_OGG:
619                         RNA_def_property_clear_flag(prop_codec, PROP_HIDDEN);
620                         RNA_def_property_enum_items(prop_codec, ogg_codec_items);
621                         RNA_enum_set(op->ptr, "format", AUD_FORMAT_S16);
622                         break;
623                 case AUD_CONTAINER_WAV:
624                         RNA_def_property_flag(prop_bitrate, PROP_HIDDEN);
625                         RNA_def_property_clear_flag(prop_format, PROP_HIDDEN);
626                         RNA_def_property_enum_items(prop_format, pcm_format_items);
627                         RNA_def_property_enum_items(prop_codec, all_codec_items);
628                         RNA_enum_set(op->ptr, "codec", AUD_CODEC_PCM);
629                         break;
630                 default:
631                         break;
632         }
633
634         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
635
636         /* main draw call */
637         uiDefAutoButsRNA(layout, &ptr, sound_mixdown_draw_check_prop, '\0');
638 }
639 #endif // WITH_AUDASPACE
640
641 static void SOUND_OT_mixdown(wmOperatorType *ot)
642 {
643 #ifdef WITH_AUDASPACE
644         static const EnumPropertyItem format_items[] = {
645                 {AUD_FORMAT_U8, "U8", 0, "U8", "8 bit unsigned"},
646                 {AUD_FORMAT_S16, "S16", 0, "S16", "16 bit signed"},
647                 {AUD_FORMAT_S24, "S24", 0, "S24", "24 bit signed"},
648                 {AUD_FORMAT_S32, "S32", 0, "S32", "32 bit signed"},
649                 {AUD_FORMAT_FLOAT32, "F32", 0, "F32", "32 bit floating point"},
650                 {AUD_FORMAT_FLOAT64, "F64", 0, "F64", "64 bit floating point"},
651                 {0, NULL, 0, NULL, NULL}
652         };
653
654         static const EnumPropertyItem codec_items[] = {
655 #ifdef WITH_FFMPEG
656                 {AUD_CODEC_AAC, "AAC", 0, "AAC", "Advanced Audio Coding"},
657                 {AUD_CODEC_AC3, "AC3", 0, "AC3", "Dolby Digital ATRAC 3"},
658 #endif
659                 {AUD_CODEC_FLAC, "FLAC", 0, "FLAC", "Free Lossless Audio Codec"},
660 #ifdef WITH_FFMPEG
661                 {AUD_CODEC_MP2, "MP2", 0, "MP2", "MPEG-1 Audio Layer II"},
662                 {AUD_CODEC_MP3, "MP3", 0, "MP3", "MPEG-2 Audio Layer III"},
663 #endif
664                 {AUD_CODEC_PCM, "PCM", 0, "PCM", "Pulse Code Modulation (RAW)"},
665                 {AUD_CODEC_VORBIS, "VORBIS", 0, "Vorbis", "Xiph.Org Vorbis Codec"},
666                 {0, NULL, 0, NULL, NULL}
667         };
668
669 #endif // WITH_AUDASPACE
670
671         /* identifiers */
672         ot->name = "Mixdown";
673         ot->description = "Mix the scene's audio to a sound file";
674         ot->idname = "SOUND_OT_mixdown";
675
676         /* api callbacks */
677         ot->exec = sound_mixdown_exec;
678         ot->invoke = sound_mixdown_invoke;
679
680 #ifdef WITH_AUDASPACE
681         ot->check = sound_mixdown_check;
682         ot->ui = sound_mixdown_draw;
683 #endif
684         /* flags */
685         ot->flag = OPTYPE_REGISTER;
686
687         /* properties */
688         WM_operator_properties_filesel(
689                 ot, FILE_TYPE_FOLDER | FILE_TYPE_SOUND, FILE_SPECIAL, FILE_SAVE,
690                 WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
691 #ifdef WITH_AUDASPACE
692         RNA_def_int(ot->srna, "accuracy", 1024, 1, 16777216, "Accuracy",
693                     "Sample accuracy, important for animation data (the lower the value, the more accurate)",
694                     1, 16777216);
695         RNA_def_enum(ot->srna, "container", container_items, AUD_CONTAINER_FLAC, "Container", "File format");
696         RNA_def_enum(ot->srna, "codec", codec_items, AUD_CODEC_FLAC, "Codec", "Audio Codec");
697         RNA_def_enum(ot->srna, "format", format_items, AUD_FORMAT_S16, "Format", "Sample format");
698         RNA_def_int(ot->srna, "bitrate", 192, 32, 512, "Bitrate", "Bitrate in kbit/s", 32, 512);
699         RNA_def_boolean(ot->srna, "split_channels", 0, "Split channels", "Each channel will be rendered into a mono file");
700 #endif // WITH_AUDASPACE
701 }
702
703 /* ******************************************************* */
704
705 static int sound_poll(bContext *C)
706 {
707         Editing *ed = CTX_data_scene(C)->ed;
708
709         if (!ed || !ed->act_seq || ed->act_seq->type != SEQ_TYPE_SOUND_RAM)
710                 return 0;
711
712         return 1;
713 }
714 /********************* pack operator *********************/
715
716 static int sound_pack_exec(bContext *C, wmOperator *op)
717 {
718         Main *bmain = CTX_data_main(C);
719         Editing *ed = CTX_data_scene(C)->ed;
720         bSound *sound;
721
722         if (!ed || !ed->act_seq || ed->act_seq->type != SEQ_TYPE_SOUND_RAM)
723                 return OPERATOR_CANCELLED;
724
725         sound = ed->act_seq->sound;
726
727         if (!sound || sound->packedfile)
728                 return OPERATOR_CANCELLED;
729
730         sound->packedfile = newPackedFile(op->reports, sound->name, ID_BLEND_PATH(bmain, &sound->id));
731         BKE_sound_load(bmain, sound);
732
733         return OPERATOR_FINISHED;
734 }
735
736 static void SOUND_OT_pack(wmOperatorType *ot)
737 {
738         /* identifiers */
739         ot->name = "Pack Sound";
740         ot->description = "Pack the sound into the current blend file";
741         ot->idname = "SOUND_OT_pack";
742
743         /* api callbacks */
744         ot->exec = sound_pack_exec;
745         ot->poll = sound_poll;
746
747         /* flags */
748         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
749 }
750
751 /********************* unpack operator *********************/
752
753 static int sound_unpack_exec(bContext *C, wmOperator *op)
754 {
755         Main *bmain = CTX_data_main(C);
756         int method = RNA_enum_get(op->ptr, "method");
757         bSound *sound = NULL;
758
759         /* find the suppplied image by name */
760         if (RNA_struct_property_is_set(op->ptr, "id")) {
761                 char sndname[MAX_ID_NAME - 2];
762                 RNA_string_get(op->ptr, "id", sndname);
763                 sound = BLI_findstring(&bmain->sound, sndname, offsetof(ID, name) + 2);
764         }
765
766         if (!sound || !sound->packedfile)
767                 return OPERATOR_CANCELLED;
768
769         if (G.fileflags & G_AUTOPACK)
770                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save");
771
772         unpackSound(bmain, op->reports, sound, method);
773
774         return OPERATOR_FINISHED;
775 }
776
777 static int sound_unpack_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
778 {
779         Editing *ed = CTX_data_scene(C)->ed;
780         bSound *sound;
781
782         if (RNA_struct_property_is_set(op->ptr, "id"))
783                 return sound_unpack_exec(C, op);
784
785         if (!ed || !ed->act_seq || ed->act_seq->type != SEQ_TYPE_SOUND_RAM)
786                 return OPERATOR_CANCELLED;
787
788         sound = ed->act_seq->sound;
789
790         if (!sound || !sound->packedfile)
791                 return OPERATOR_CANCELLED;
792
793         if (G.fileflags & G_AUTOPACK)
794                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save");
795
796         unpack_menu(C, "SOUND_OT_unpack", sound->id.name + 2, sound->name, "sounds", sound->packedfile);
797
798         return OPERATOR_FINISHED;
799 }
800
801 static void SOUND_OT_unpack(wmOperatorType *ot)
802 {
803         /* identifiers */
804         ot->name = "Unpack Sound";
805         ot->description = "Unpack the sound to the samples filename";
806         ot->idname = "SOUND_OT_unpack";
807
808         /* api callbacks */
809         ot->exec = sound_unpack_exec;
810         ot->invoke = sound_unpack_invoke;
811         ot->poll = sound_poll;
812
813         /* flags */
814         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
815
816         /* properties */
817         RNA_def_enum(ot->srna, "method", rna_enum_unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack");
818         RNA_def_string(ot->srna, "id", NULL, MAX_ID_NAME - 2, "Sound Name", "Sound data-block name to unpack"); /* XXX, weark!, will fail with library, name collisions */
819 }
820
821 /* ******************************************************* */
822
823 void ED_operatortypes_sound(void)
824 {
825         WM_operatortype_append(SOUND_OT_open);
826         WM_operatortype_append(SOUND_OT_open_mono);
827         WM_operatortype_append(SOUND_OT_mixdown);
828         WM_operatortype_append(SOUND_OT_pack);
829         WM_operatortype_append(SOUND_OT_unpack);
830         WM_operatortype_append(SOUND_OT_update_animation_flags);
831         WM_operatortype_append(SOUND_OT_bake_animation);
832 }