-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oswrapper_audio_enc.h
1784 lines (1487 loc) · 76.1 KB
/
oswrapper_audio_enc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
OSWrapper audio encoder: Encode audio files with the built in OS audio encoders.
Usage:
TODO, see test_oswrapper_audio_enc and test_oswrapper_audio_enc_mod
for examples of how to use this library.
Platform requirements:
- On macOS, link with AudioToolbox
- On Windows, call CoInitialize before using the library,
and link with mf.lib, mfplat.lib, mfreadwrite.lib, and shlwapi.lib
The latest version of this file can be found at
https://github.com/NeRdTheNed/OSWrapper/blob/main/oswrapper_audio_enc.h
*/
#ifndef OSWRAPPER_INCLUDE_OSWRAPPER_AUDIO_ENC_H
#define OSWRAPPER_INCLUDE_OSWRAPPER_AUDIO_ENC_H
#include <stddef.h>
#ifndef OSWRAPPER_AUDIO_ENC_DEF
#ifdef OSWRAPPER_AUDIO_ENC_STATIC
#define OSWRAPPER_AUDIO_ENC_DEF static
#else
#define OSWRAPPER_AUDIO_ENC_DEF extern
#endif
#endif /* OSWRAPPER_AUDIO_ENC_DEF */
/* You can make these functions return actual booleans if you want */
#ifndef OSWRAPPER_AUDIO_ENC_RESULT_TYPE
#define OSWRAPPER_AUDIO_ENC_RESULT_TYPE int
#endif
#ifndef OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS
#define OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS 1
#endif
#ifndef OSWRAPPER_AUDIO_ENC_RESULT_FAILURE
#define OSWRAPPER_AUDIO_ENC_RESULT_FAILURE 0
#endif
/* Stable-ish API */
/* PCM format enum.
Input PCM data must be uncompressed integer or floating point.
ALAW, ULAW, and Apple IMA4 only work on macOS. */
typedef enum {
OSWRAPPER_AUDIO_ENC_PCM_DEFAULT = 0,
OSWRAPPER_AUDIO_ENC_PCM_INTEGER,
OSWRAPPER_AUDIO_ENC_PCM_FLOAT,
OSWRAPPER_AUDIO_ENC_PCM_ALAW,
OSWRAPPER_AUDIO_ENC_PCM_ULAW,
OSWRAPPER_AUDIO_ENC_PCM_APPLE_IMA4
} OSWrapper_audio_enc_pcm_type;
/* PCM endianness type.
Audio must always be little-endian on Windows. */
typedef enum {
OSWRAPPER_AUDIO_ENC_ENDIANNESS_DEFAULT = 0,
OSWRAPPER_AUDIO_ENC_ENDIANNESS_LITTLE,
OSWRAPPER_AUDIO_ENC_ENDIANNESS_BIG
} OSWrapper_audio_enc_pcm_endianness_type;
/* What format the output audio is in.
Platform support for each format:
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WAV: macOS, Windows 7
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_SND: macOS
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC: macOS, Windows 7
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_LC: macOS, Windows 7
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_HE: macOS 10.5, Windows 7
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_HE_V2: macOS 10.5, Windows 7
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_MPEG: Windows 8
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_MP3: Windows 8
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_V8: Windows Vista
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_V9: Windows Vista
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_SPEECH: Windows Vista
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_OPUS: macOS 10.13 (uses CAF as a container format)
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_ILBC: macOS 10.6
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_CAF_PCM: macOS
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_ALAC: macOS, Windows 10
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_FLAC: macOS 10.13, Windows 10
Currently unsupported, may be added in the future:
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_LOSSLESS: Windows Vista
*/
typedef enum {
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_PREFERRED_LOSSLESS = 0,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_PREFERRED_LOSSY = 1,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WAV,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_SND,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_LC,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_HE,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_HE_V2,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_MPEG,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_MP3,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_V8,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_V9,
/* OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_LOSSLESS, */
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_SPEECH,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_OPUS,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_ILBC,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_CAF_PCM,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_ALAC,
OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_FLAC
} OSWrapper_audio_enc_output_type;
typedef struct OSWrapper_audio_enc_format_spec {
unsigned long sample_rate;
unsigned int channel_count;
unsigned int bits_per_channel;
OSWrapper_audio_enc_pcm_type pcm_type;
OSWrapper_audio_enc_pcm_endianness_type pcm_endianness_type;
} OSWrapper_audio_enc_format_spec;
/* The created audio context. */
typedef struct OSWrapper_audio_enc_spec {
void* internal_data;
OSWrapper_audio_enc_format_spec input_data;
OSWrapper_audio_enc_format_spec output_data;
OSWrapper_audio_enc_output_type output_type;
unsigned int bitrate;
} OSWrapper_audio_enc_spec;
/* Call oswrapper_audio_enc_init() before using the library,
and call oswrapper_audio_enc_uninit() after you're done using the library. */
OSWRAPPER_AUDIO_ENC_DEF OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc_init(void);
OSWRAPPER_AUDIO_ENC_DEF OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc_uninit(void);
#ifndef OSWRAPPER_AUDIO_ENC_NO_PATH
/* Finalises and frees resources associated with the given OSWrapper_audio_enc_spec
initialised from oswrapper_audio_enc_make_file_from_path.
Returns 1 on success, or 0 on failure. */
OSWRAPPER_AUDIO_ENC_DEF OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc_finalise_file_context(OSWrapper_audio_enc_spec* audio);
/* Create a sound file encoding context from the given path.
You must set all values on the input_data field in the passed OSWrapper_audio_enc_spec:
- sample_rate: the input PCM sample rate.
- channel_count: the input PCM channel count.
- bits_per_channel: the input PCM bits per channel.
- pcm_type: what format the input PCM data is in (integer PCM, floating point PCM).
- pcm_endianness_type: the endianness of the input PCM data.
Values can be set on the output_data field to hint the wanted output properties,
or be left blank to use default values.
You should also set the output_type field to the wanted output encoding type.
Note that not all encoding types are supported on every platform,
and some may only be supported at certain sample rates / bits per channel ect.
You can use the preferred lossless or lossy enum cases to use a format
that the current platform may support.
Bitrate is only used for lossy formats, and may be treated as a suggestion.
Returns 1 on success, or 0 on failure. */
OSWRAPPER_AUDIO_ENC_DEF OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc_make_file_from_path(const char* path, OSWrapper_audio_enc_spec* audio);
#endif /* OSWRAPPER_AUDIO_ENC_NO_PATH */
/* Encode audio samples from the given buffer.
Returns 1 on success, or 0 on failure. */
OSWRAPPER_AUDIO_ENC_DEF OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc_encode_samples(OSWrapper_audio_enc_spec* audio, short* buffer, size_t frames_to_do);
#ifdef OSWRAPPER_AUDIO_ENC_IMPLEMENTATION
#ifndef OSWRAPPER_AUDIO_ENC_NO_INCLUDE_STDLIB
#include <stdlib.h>
#endif
#ifndef OSWRAPPER_AUDIO_ENC_NO_INCLUDE_STRING
#include <string.h>
#endif
#ifndef OSWRAPPER_AUDIO_ENC_MALLOC
#define OSWRAPPER_AUDIO_ENC_MALLOC(x) malloc(x)
#endif /* OSWRAPPER_AUDIO_ENC_MALLOC */
#ifndef OSWRAPPER_AUDIO_ENC_FREE
#define OSWRAPPER_AUDIO_ENC_FREE(x) free(x)
#endif /* OSWRAPPER_AUDIO_ENC_FREE */
#ifndef OSWRAPPER_AUDIO_ENC_MEMCPY
#define OSWRAPPER_AUDIO_ENC_MEMCPY(x, y, amount) memcpy(x, y, amount)
#endif /* OSWRAPPER_AUDIO_ENC_MEMCPY */
#ifndef OSWRAPPER_AUDIO_ENC_MEMCMP
#define OSWRAPPER_AUDIO_ENC_MEMCMP(ptr1, ptr2, amount) memcmp(ptr1, ptr2, amount)
#endif /* OSWRAPPER_AUDIO_ENC_MEMCMP */
#ifndef OSWRAPPER_AUDIO_ENC_STRLEN
#define OSWRAPPER_AUDIO_ENC_STRLEN(str) strlen(str)
#endif /* OSWRAPPER_AUDIO_ENC_STRLEN */
#ifdef __APPLE__
#include <AvailabilityMacros.h>
#if defined(MAC_OS_X_VERSION_10_4) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
#if !defined(OSWRAPPER_AUDIO_ENC_USE_AUDIOTOOLBOX_IMPL) && !defined(OSWRAPPER_AUDIO_ENC_NO_USE_AUDIOTOOLBOX_IMPL)
#define OSWRAPPER_AUDIO_ENC_USE_AUDIOTOOLBOX_IMPL
#endif /* !defined(OSWRAPPER_AUDIO_ENC_USE_AUDIOTOOLBOX_IMPL) && !defined(OSWRAPPER_AUDIO_ENC_NO_USE_AUDIOTOOLBOX_IMPL) */
#endif /* defined(MAC_OS_X_VERSION_10_4) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4 */
#endif /* __APPLE__ */
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
#if !defined(OSWRAPPER_AUDIO_ENC_USE_WIN_MF_IMPL) && !defined(OSWRAPPER_AUDIO_ENC_NO_USE_WIN_MF_IMPL)
#define OSWRAPPER_AUDIO_ENC_USE_WIN_MF_IMPL
#endif /* !defined(OSWRAPPER_AUDIO_ENC_USE_WIN_MF_IMPL) && !defined(OSWRAPPER_AUDIO_ENC_NO_USE_WIN_MF_IMPL) */
#endif
static OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc__is_pcm_input_format_supported(OSWrapper_audio_enc_pcm_type format) {
switch (format) {
case OSWRAPPER_AUDIO_ENC_PCM_INTEGER:
case OSWRAPPER_AUDIO_ENC_PCM_FLOAT:
return OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS;
default:
return OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
}
}
static OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc__is_format_lossy(OSWrapper_audio_enc_output_type type) {
switch (type) {
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_LC:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_HE:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_HE_V2:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_MPEG:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_MP3:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_V8:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_V9:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_SPEECH:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_OPUS:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_ILBC:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_PREFERRED_LOSSY:
return OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS;
default:
return OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
}
}
static OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc__is_format_uncompressed(OSWrapper_audio_enc_output_type type) {
switch (type) {
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WAV:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_SND:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_CAF_PCM:
return OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS;
default:
return OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
}
}
#ifdef OSWRAPPER_AUDIO_ENC_USE_AUDIOTOOLBOX_IMPL
static OSWrapper_audio_enc_pcm_endianness_type oswrapper_audio_enc__get_endianness_for_format(OSWrapper_audio_enc_output_type type) {
switch (type) {
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WAV:
return OSWRAPPER_AUDIO_ENC_ENDIANNESS_LITTLE;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_SND:
return OSWRAPPER_AUDIO_ENC_ENDIANNESS_BIG;
default:
return OSWRAPPER_AUDIO_ENC_ENDIANNESS_DEFAULT;
}
}
#endif
static void oswrapper_audio_enc__fill_output_from_input(OSWrapper_audio_enc_spec* audio) {
if (audio->input_data.pcm_type == OSWRAPPER_AUDIO_ENC_PCM_DEFAULT) {
audio->input_data.pcm_type = OSWRAPPER_AUDIO_ENC_PCM_INTEGER;
}
if (audio->input_data.pcm_endianness_type == OSWRAPPER_AUDIO_ENC_ENDIANNESS_DEFAULT) {
#if defined(__ppc64__) || defined(__ppc__)
audio->input_data.pcm_endianness_type = OSWRAPPER_AUDIO_ENC_ENDIANNESS_BIG;
#else
audio->input_data.pcm_endianness_type = OSWRAPPER_AUDIO_ENC_ENDIANNESS_LITTLE;
#endif
}
if (audio->output_type == OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_PREFERRED_LOSSLESS) {
audio->output_type = OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WAV;
} else if (audio->output_type == OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_PREFERRED_LOSSY) {
audio->output_type = OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC;
}
#ifdef OSWRAPPER_AUDIO_ENC_USE_AUDIOTOOLBOX_IMPL
if (oswrapper_audio_enc__is_format_lossy(audio->output_type) == OSWRAPPER_AUDIO_ENC_RESULT_FAILURE)
#endif
{
if (audio->output_data.sample_rate == 0) {
audio->output_data.sample_rate = audio->input_data.sample_rate;
}
}
if (audio->output_data.channel_count == 0) {
audio->output_data.channel_count = audio->input_data.channel_count;
}
if (audio->output_data.pcm_type == OSWRAPPER_AUDIO_ENC_PCM_DEFAULT) {
audio->output_data.pcm_type = audio->input_data.pcm_type;
}
if (oswrapper_audio_enc__is_format_uncompressed(audio->output_type) == OSWRAPPER_AUDIO_ENC_RESULT_FAILURE) {
switch (audio->output_data.pcm_type) {
case OSWRAPPER_AUDIO_ENC_PCM_INTEGER:
case OSWRAPPER_AUDIO_ENC_PCM_FLOAT:
break;
default:
audio->output_data.pcm_type = OSWRAPPER_AUDIO_ENC_PCM_INTEGER;
break;
}
}
if (audio->output_data.bits_per_channel == 0) {
audio->output_data.bits_per_channel = audio->input_data.bits_per_channel;
if (oswrapper_audio_enc__is_format_uncompressed(audio->output_type) == OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS) {
switch (audio->output_data.pcm_type) {
case OSWRAPPER_AUDIO_ENC_PCM_INTEGER:
switch (audio->output_data.bits_per_channel) {
case 8:
case 16:
case 24:
case 32:
break;
default:
audio->output_data.bits_per_channel = 16;
break;
}
break;
case OSWRAPPER_AUDIO_ENC_PCM_FLOAT:
switch (audio->output_data.bits_per_channel) {
case 32:
case 64:
break;
default:
audio->output_data.bits_per_channel = 32;
break;
}
break;
case OSWRAPPER_AUDIO_ENC_PCM_ALAW:
case OSWRAPPER_AUDIO_ENC_PCM_ULAW:
audio->output_data.bits_per_channel = 8;
break;
default:
break;
}
}
}
if (audio->output_data.pcm_endianness_type == OSWRAPPER_AUDIO_ENC_ENDIANNESS_DEFAULT) {
#ifdef OSWRAPPER_AUDIO_ENC_USE_AUDIOTOOLBOX_IMPL
audio->output_data.pcm_endianness_type = oswrapper_audio_enc__get_endianness_for_format(audio->output_type);
if (audio->output_data.pcm_endianness_type == OSWRAPPER_AUDIO_ENC_ENDIANNESS_DEFAULT)
#endif
{
#if defined(__ppc64__) || defined(__ppc__)
audio->output_data.pcm_endianness_type = OSWRAPPER_AUDIO_ENC_ENDIANNESS_BIG;
#else
audio->output_data.pcm_endianness_type = OSWRAPPER_AUDIO_ENC_ENDIANNESS_LITTLE;
#endif
}
}
}
#ifdef OSWRAPPER_AUDIO_ENC_USE_AUDIOTOOLBOX_IMPL
/* Start macOS AudioToolbox implementation */
#include <AudioToolbox/AudioConverter.h>
#include <AudioToolbox/AudioFormat.h>
#if !defined(MAC_OS_X_VERSION_10_6) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
#include <CoreServices/CoreServices.h>
#endif
#include <AudioToolbox/ExtendedAudioFile.h>
#include <AvailabilityMacros.h>
#if !defined(MAC_OS_X_VERSION_10_5) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
#ifndef kAudioFormatMPEG4AAC_HE
#define kAudioFormatMPEG4AAC_HE 'aach'
#endif
#ifndef kAudioFormatMPEG4AAC_HE_V2
#define kAudioFormatMPEG4AAC_HE_V2 'aacp'
#endif
#endif
#if (!defined(MAC_OS_X_VERSION_10_6) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6) && !defined(kAudioFormatiLBC)
#define kAudioFormatiLBC 'ilbc'
#endif
#if !defined(MAC_OS_X_VERSION_10_10) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_10
#define OSWRAPPER_AUDIO_ENC__AUDIO_FORMAT_ID_TYPE UInt32
#else
#define OSWRAPPER_AUDIO_ENC__AUDIO_FORMAT_ID_TYPE AudioFormatID
#endif
#if !defined(MAC_OS_X_VERSION_10_13) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_13
#ifndef kAudioFileFLACType
#define kAudioFileFLACType 'flac'
#endif
#ifndef kAudioFormatFLAC
#define kAudioFormatFLAC 'flac'
#endif
#ifndef kAudioFormatOpus
#define kAudioFormatOpus 'opus'
#endif
#endif
typedef struct oswrapper_audio_enc__internal_data_mac {
ExtAudioFileRef audio_file_ext;
} oswrapper_audio_enc__internal_data_mac;
static OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc__is_format_supported_on_macos(OSWrapper_audio_enc_output_type type) {
/* TODO Check macOS version & return results based on supported formats */
switch (type) {
/* case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_LOSSLESS: */
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_MPEG:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_MP3:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_V8:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_V9:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_SPEECH:
return OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
default:
return OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS;
}
}
static OSWRAPPER_AUDIO_ENC__AUDIO_FORMAT_ID_TYPE oswrapper_audio_enc__get_audio_format_id_from_enum(OSWrapper_audio_enc_output_type type, OSWrapper_audio_enc_pcm_type pcm_type) {
switch (type) {
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_LC:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_PREFERRED_LOSSY:
return kAudioFormatMPEG4AAC;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_HE:
return kAudioFormatMPEG4AAC_HE;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_HE_V2:
return kAudioFormatMPEG4AAC_HE_V2;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_ALAC:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_PREFERRED_LOSSLESS:
return kAudioFormatAppleLossless;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_OPUS:
return kAudioFormatOpus;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_FLAC:
return kAudioFormatFLAC;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_ILBC:
return kAudioFormatiLBC;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WAV:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_SND:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_CAF_PCM:
switch (pcm_type) {
case OSWRAPPER_AUDIO_ENC_PCM_ALAW:
return kAudioFormatALaw;
case OSWRAPPER_AUDIO_ENC_PCM_ULAW:
return kAudioFormatULaw;
case OSWRAPPER_AUDIO_ENC_PCM_APPLE_IMA4:
return kAudioFormatAppleIMA4;
default:
break;
}
default:
return kAudioFormatLinearPCM;
}
}
static AudioFileTypeID oswrapper_audio_enc__get_audio_file_type_id_from_enum(OSWrapper_audio_enc_output_type type) {
switch (type) {
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WAV:
return kAudioFileWAVEType;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_FLAC:
return kAudioFileFLACType;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_SND:
return kAudioFileNextType;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_LC:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_HE:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_HE_V2:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_ALAC:
return kAudioFileM4AType;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_PREFERRED_LOSSLESS:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_PREFERRED_LOSSY:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_CAF_PCM:
default:
return kAudioFileCAFType;
}
}
static OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc__create_desc(AudioStreamBasicDescription* desc, OSWRAPPER_AUDIO_ENC__AUDIO_FORMAT_ID_TYPE format_id, OSWrapper_audio_enc_format_spec* audio) {
desc->mFormatID = format_id;
/* This may be set to 0 when creating compressed formats */
desc->mSampleRate = audio->sample_rate;
desc->mChannelsPerFrame = audio->channel_count;
if (format_id == kAudioFormatLinearPCM) {
if (audio->pcm_type == OSWRAPPER_AUDIO_ENC_PCM_FLOAT) {
desc->mFormatFlags = kLinearPCMFormatFlagIsFloat;
} else if (audio->pcm_type == OSWRAPPER_AUDIO_ENC_PCM_INTEGER) {
desc->mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
} else {
/* Unsupported audio format, was not float or integer */
return OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
}
desc->mFormatFlags |= kLinearPCMFormatFlagIsPacked;
if (audio->pcm_endianness_type == OSWRAPPER_AUDIO_ENC_ENDIANNESS_BIG) {
desc->mFormatFlags |= kAudioFormatFlagIsBigEndian;
}
desc->mBitsPerChannel = audio->bits_per_channel;
/* kAudioFormatLinearPCM doesn't use packets */
desc->mFramesPerPacket = 1;
/* Bytes per channel * channels per frame */
desc->mBytesPerFrame = (desc->mBitsPerChannel / 8) * desc->mChannelsPerFrame;
/* Bytes per frame * frames per packet */
desc->mBytesPerPacket = desc->mBytesPerFrame * desc->mFramesPerPacket;
} else {
if (format_id == kAudioFormatAppleLossless || format_id == kAudioFormatFLAC) {
if (audio->pcm_type == OSWRAPPER_AUDIO_ENC_PCM_FLOAT) {
/* Source bit depth can't be represented by any flags */
desc->mFormatFlags = 0;
} else {
switch (audio->bits_per_channel) {
case 16:
desc->mFormatFlags = kAppleLosslessFormatFlag_16BitSourceData;
break;
case 20:
desc->mFormatFlags = kAppleLosslessFormatFlag_20BitSourceData;
break;
case 24:
desc->mFormatFlags = kAppleLosslessFormatFlag_24BitSourceData;
break;
case 32:
desc->mFormatFlags = kAppleLosslessFormatFlag_32BitSourceData;
break;
default:
/* Source bit depth can't be represented by any flags */
desc->mFormatFlags = 0;
break;
}
}
} else {
desc->mFormatFlags = 0;
}
}
UInt32 property_size = sizeof(AudioStreamBasicDescription);
if (AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &property_size, desc)) {
/* Could not create valid ASBD from input properties */
return OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
}
return OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS;
}
static OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc__get_bitrates(AudioConverterRef converter, Float64* minimum, Float64* maximum) {
OSWRAPPER_AUDIO_ENC_RESULT_TYPE return_val;
UInt32 property_size;
OSStatus error = AudioConverterGetPropertyInfo(converter, kAudioConverterApplicableEncodeBitRates, &property_size, NULL);
return_val = OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
if (!error && property_size > 0) {
AudioValueRange* bitrates = (AudioValueRange*) OSWRAPPER_AUDIO_ENC_MALLOC(property_size);
if (bitrates != NULL) {
error = AudioConverterGetProperty(converter, kAudioConverterApplicableEncodeBitRates, &property_size, bitrates);
if (!error) {
size_t bitrates_amount = property_size / sizeof(AudioValueRange);
if (bitrates_amount > 0) {
size_t i;
*minimum = bitrates[0].mMinimum;
*maximum = bitrates[0].mMaximum;
for (i = 1; i < bitrates_amount; i++) {
if (bitrates[i].mMinimum < *minimum) {
*minimum = bitrates[i].mMinimum;
}
if (bitrates[i].mMaximum > *maximum) {
*maximum = bitrates[i].mMaximum;
}
}
return_val = OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS;
}
}
OSWRAPPER_AUDIO_ENC_FREE(bitrates);
}
}
return return_val;
}
OSWRAPPER_AUDIO_ENC_DEF OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc_init(void) {
return OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS;
}
OSWRAPPER_AUDIO_ENC_DEF OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc_uninit(void) {
return OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS;
}
#ifndef OSWRAPPER_AUDIO_ENC_NO_PATH
static OSStatus oswrapper_audio_enc__create_from_path(const char* path, AudioStreamBasicDescription* output_format, ExtAudioFileRef* audio_file, AudioFileTypeID file_type) {
OSStatus error;
CFURLRef path_url;
#if !defined(MAC_OS_X_VERSION_10_5) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
CFURLRef base_path_url;
#endif
path_url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8*) path, OSWRAPPER_AUDIO_ENC_STRLEN(path), false);
#if !defined(MAC_OS_X_VERSION_10_5) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
error = kAudioFileUnspecifiedError;
base_path_url = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, path_url);
if (base_path_url != NULL) {
CFStringRef out_file_name = CFURLCopyLastPathComponent(path_url);
if (out_file_name != NULL) {
FSRef base_path_fsref;
if (CFURLGetFSRef(base_path_url, &base_path_fsref)) {
error = ExtAudioFileCreateNew(&base_path_fsref, out_file_name, file_type, output_format, NULL, audio_file);
}
CFRelease(out_file_name);
}
CFRelease(base_path_url);
}
#else
error = ExtAudioFileCreateWithURL(path_url, file_type, output_format, NULL, 0, audio_file);
#endif
CFRelease(path_url);
return error;
}
OSWRAPPER_AUDIO_ENC_DEF OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc_finalise_file_context(OSWrapper_audio_enc_spec* audio) {
/* TODO Better error handling? */
OSStatus error;
oswrapper_audio_enc__internal_data_mac* internal_data = (oswrapper_audio_enc__internal_data_mac*) audio->internal_data;
error = ExtAudioFileDispose(internal_data->audio_file_ext);
OSWRAPPER_AUDIO_ENC_FREE(audio->internal_data);
return !error ? OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS : OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
}
OSWRAPPER_AUDIO_ENC_DEF OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc_make_file_from_path(const char* path, OSWrapper_audio_enc_spec* audio) {
#ifdef __cplusplus
AudioStreamBasicDescription input_format = { };
#else
AudioStreamBasicDescription input_format = { 0 };
#endif
if (oswrapper_audio_enc__is_format_supported_on_macos(audio->output_type) == OSWRAPPER_AUDIO_ENC_RESULT_FAILURE) {
return OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
}
oswrapper_audio_enc__fill_output_from_input(audio);
if (oswrapper_audio_enc__is_pcm_input_format_supported(audio->input_data.pcm_type) == OSWRAPPER_AUDIO_ENC_RESULT_FAILURE) {
return OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
}
if (oswrapper_audio_enc__create_desc(&input_format, kAudioFormatLinearPCM, &audio->input_data) == OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS) {
#ifdef __cplusplus
AudioStreamBasicDescription output_format = { };
#else
AudioStreamBasicDescription output_format = { 0 };
#endif
if (oswrapper_audio_enc__create_desc(&output_format, oswrapper_audio_enc__get_audio_format_id_from_enum(audio->output_type, audio->output_data.pcm_type), &audio->output_data) == OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS) {
OSStatus error;
ExtAudioFileRef audio_file_ext = NULL;
error = oswrapper_audio_enc__create_from_path(path, &output_format, &audio_file_ext, oswrapper_audio_enc__get_audio_file_type_id_from_enum(audio->output_type));
if (!error) {
error = ExtAudioFileSetProperty(audio_file_ext, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &input_format);
if (!error) {
oswrapper_audio_enc__internal_data_mac* internal_data;
UInt32 property_size;
AudioConverterRef converter = NULL;
property_size = sizeof(AudioConverterRef);
if (oswrapper_audio_enc__is_format_lossy(audio->output_type) == OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS && !ExtAudioFileGetProperty(audio_file_ext, kExtAudioFileProperty_AudioConverter, &property_size, &converter) && converter != NULL) {
UInt32 bitrate = audio->bitrate;
property_size = sizeof(bitrate);
if (audio->bitrate != 0) {
Float64 minimum;
Float64 maximum;
minimum = bitrate;
maximum = bitrate;
/* Set encoding bitrate */
if (oswrapper_audio_enc__get_bitrates(converter, &minimum, &maximum) == OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS) {
/* Clamp bitrate to allowed values */
if (bitrate > (UInt32) maximum) {
bitrate = (UInt32) maximum;
}
if (bitrate < (UInt32) minimum) {
bitrate = (UInt32) minimum;
}
}
/* Failure is mostly harmless */
AudioConverterSetProperty(converter, kAudioConverterEncodeBitRate, property_size, &bitrate);
}
error = AudioConverterGetProperty(converter, kAudioConverterEncodeBitRate, &property_size, &bitrate);
if (!error) {
audio->bitrate = bitrate;
}
if (audio->output_data.sample_rate == 0) {
AudioStreamBasicDescription converter_output_format;
property_size = sizeof(converter_output_format);
error = AudioConverterGetProperty(converter, kAudioConverterCurrentOutputStreamDescription, &property_size, &converter_output_format);
if (!error) {
audio->output_data.sample_rate = converter_output_format.mSampleRate;
}
}
CFArrayRef converter_config = NULL;
ExtAudioFileSetProperty(audio_file_ext, kExtAudioFileProperty_ConverterConfig, sizeof(CFArrayRef), &converter_config);
}
internal_data = (oswrapper_audio_enc__internal_data_mac*) OSWRAPPER_AUDIO_ENC_MALLOC(sizeof(oswrapper_audio_enc__internal_data_mac));
if (internal_data != NULL) {
audio->internal_data = (void*) internal_data;
internal_data->audio_file_ext = audio_file_ext;
return OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS;
}
}
ExtAudioFileDispose(audio_file_ext);
}
}
}
return OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
}
#endif /* OSWRAPPER_AUDIO_ENC_NO_PATH */
OSWRAPPER_AUDIO_ENC_DEF OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc_encode_samples(OSWrapper_audio_enc_spec* audio, short* buffer, size_t frames_to_do) {
AudioBufferList buffer_list;
oswrapper_audio_enc__internal_data_mac* internal_data = (oswrapper_audio_enc__internal_data_mac*) audio->internal_data;
buffer_list.mNumberBuffers = 1;
buffer_list.mBuffers[0].mNumberChannels = audio->input_data.channel_count;
buffer_list.mBuffers[0].mDataByteSize = frames_to_do * ((audio->input_data.bits_per_channel / 8) * audio->input_data.channel_count);
buffer_list.mBuffers[0].mData = buffer;
return !ExtAudioFileWrite(internal_data->audio_file_ext, frames_to_do, &buffer_list) ? OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS : OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
}
/* End macOS AudioToolbox implementation */
#elif defined(OSWRAPPER_AUDIO_ENC_USE_WIN_MF_IMPL)
/* Start Win32 MF implementation */
/* This code assumes that WMF uses little-endian data,
and the code is running on a little-endian platform.
If this isn't the case, please let me know, and I'll try to fix it. */
#ifndef COBJMACROS
#define COBJMACROS
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <cguid.h>
#include <initguid.h>
#include <mfapi.h>
#include <mfidl.h>
#include <mfreadwrite.h>
#ifdef OSWRAPPER_AUDIO_ENC__WINDOWS_WIP_ALAW_ULAW
#include <mmreg.h>
#endif
#include <shlwapi.h>
#include <stdio.h>
#ifdef OSWRAPPER_AUDIO_ENC__WINDOWS_WIP_ALAW_ULAW
DEFINE_MEDIATYPE_GUID(OSWrapperAudioEncFormat_PCM_ALAW, WAVE_FORMAT_ALAW);
DEFINE_MEDIATYPE_GUID(OSWrapperAudioEncFormat_PCM_MULAW, WAVE_FORMAT_MULAW);
#endif
#if WINVER < 0x0A00
DEFINE_MEDIATYPE_GUID(MFAudioFormat_FLAC, 0xF1AC);
DEFINE_MEDIATYPE_GUID(MFAudioFormat_ALAC, 0x6C61);
DEFINE_MEDIATYPE_GUID(MFAudioFormat_Opus, 0x704F);
#endif
/* Using CINTERFACE breaks some headers, so we have to define these ourselves */
#if defined(__cplusplus) && !defined(CINTERFACE) && !defined(OSWRAPPER_AUDIO_ENC_NO_DEFINE_WINMF_C_INTERFACE)
#ifndef IMFAttributes_Release
#define IMFAttributes_Release(attributes) attributes->Release()
#endif
#ifndef IMFAttributes_SetGUID
#define IMFAttributes_SetGUID(attributes, ...) attributes->SetGUID(__VA_ARGS__)
#endif
#ifndef IMFCollection_GetElement
#define IMFCollection_GetElement(collection, ...) collection->GetElement(__VA_ARGS__)
#endif
#ifndef IMFCollection_GetElementCount
#define IMFCollection_GetElementCount(collection, ...) collection->GetElementCount(__VA_ARGS__)
#endif
#ifndef IMFCollection_Release
#define IMFCollection_Release(collection) collection->Release()
#endif
#ifndef IMFMediaBuffer_Lock
#define IMFMediaBuffer_Lock(media_buffer, ...) media_buffer->Lock(__VA_ARGS__)
#endif
#ifndef IMFMediaBuffer_Release
#define IMFMediaBuffer_Release(media_buffer) media_buffer->Release()
#endif
#ifndef IMFMediaBuffer_SetCurrentLength
#define IMFMediaBuffer_SetCurrentLength(media_buffer, ...) media_buffer->SetCurrentLength(__VA_ARGS__)
#endif
#ifndef IMFMediaBuffer_Unlock
#define IMFMediaBuffer_Unlock(media_buffer) media_buffer->Unlock()
#endif
#ifndef IMFMediaType_AddRef
#define IMFMediaType_AddRef(media_type) media_type->AddRef()
#endif
#ifndef IMFMediaType_GetUINT32
#define IMFMediaType_GetUINT32(media_type, ...) media_type->GetUINT32(__VA_ARGS__)
#endif
#ifndef IMFMediaType_Release
#define IMFMediaType_Release(media_type) media_type->Release()
#endif
#ifndef IMFMediaType_SetGUID
#define IMFMediaType_SetGUID(media_type, ...) media_type->SetGUID(__VA_ARGS__)
#endif
#ifndef IMFMediaType_SetUINT32
#define IMFMediaType_SetUINT32(media_type, ...) media_type->SetUINT32(__VA_ARGS__)
#endif
#ifndef IMFSample_AddBuffer
#define IMFSample_AddBuffer(sample, ...) sample->AddBuffer(__VA_ARGS__)
#endif
#ifndef IMFSample_Release
#define IMFSample_Release(sample) sample->Release()
#endif
#ifndef IMFSample_SetSampleDuration
#define IMFSample_SetSampleDuration(sample, ...) sample->SetSampleDuration(__VA_ARGS__)
#endif
#ifndef IMFSample_SetSampleTime
#define IMFSample_SetSampleTime(sample, ...) sample->SetSampleTime(__VA_ARGS__)
#endif
#ifndef IMFSinkWriter_AddStream
#define IMFSinkWriter_AddStream(sink_writer, ...) sink_writer->AddStream(__VA_ARGS__)
#endif
#ifndef IMFSinkWriter_BeginWriting
#define IMFSinkWriter_BeginWriting(sink_writer) sink_writer->BeginWriting()
#endif
#ifndef IMFSinkWriter_Finalize
#define IMFSinkWriter_Finalize(sink_writer) sink_writer->Finalize()
#endif
#ifndef IMFSinkWriter_Release
#define IMFSinkWriter_Release(sink_writer) sink_writer->Release()
#endif
#ifndef IMFSinkWriter_SetInputMediaType
#define IMFSinkWriter_SetInputMediaType(sink_writer, ...) sink_writer->SetInputMediaType(__VA_ARGS__)
#endif
#ifndef IMFSinkWriter_WriteSample
#define IMFSinkWriter_WriteSample(sink_writer, ...) sink_writer->WriteSample(__VA_ARGS__)
#endif
#endif
/* TODO Ugly hack */
#ifndef OSWRAPPER_AUDIO_ENC_PATH_MAX
#define OSWRAPPER_AUDIO_ENC_PATH_MAX MAX_PATH
#endif
#ifdef OSWRAPPER_AUDIO_ENC_MANAGE_COINIT
#include <objbase.h>
#ifndef OSWRAPPER_AUDIO_ENC_COINIT_VALUE
#define OSWRAPPER_AUDIO_ENC_COINIT_VALUE COINIT_MULTITHREADED
#endif
#endif
/* The startup flags for MFStartup */
#ifndef OSWRAPPER_AUDIO_ENC__MF_STARTUP_VAL
#define OSWRAPPER_AUDIO_ENC__MF_STARTUP_VAL MFSTARTUP_LITE
#endif
typedef struct oswrapper_audio_enc__internal_data_win {
IMFSinkWriter* writer;
DWORD audio_stream_index;
LONGLONG output_pos;
} oswrapper_audio_enc__internal_data_win;
static OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc__is_format_supported_on_windows(OSWrapper_audio_enc_output_type type) {
/* TODO Check Windows version & return results based on supported formats */
switch (type) {
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_SND:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_ILBC:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_CAF_PCM:
return OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
default:
return OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS;
}
}
#ifndef OSWRAPPER_AUDIO_ENC__WINDOWS_WIP_ALAW_ULAW
static OSWRAPPER_AUDIO_ENC_RESULT_TYPE oswrapper_audio_enc__is_pcm_output_format_supported_on_windows(OSWrapper_audio_enc_pcm_type format) {
switch (format) {
case OSWRAPPER_AUDIO_ENC_PCM_INTEGER:
case OSWRAPPER_AUDIO_ENC_PCM_FLOAT:
case OSWRAPPER_AUDIO_ENC_PCM_DEFAULT:
return OSWRAPPER_AUDIO_ENC_RESULT_SUCCESS;
default:
return OSWRAPPER_AUDIO_ENC_RESULT_FAILURE;
}
}
#endif
static GUID oswrapper_audio_enc__get_guid_from_enum(OSWrapper_audio_enc_output_type type, OSWrapper_audio_enc_pcm_type pcm_type) {
switch (type) {
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_LC:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_HE:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_AAC_HE_V2:
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_PREFERRED_LOSSY:
return MFAudioFormat_AAC;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_MPEG:
return MFAudioFormat_MPEG;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_MP3:
return MFAudioFormat_MP3;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_V8:
return MFAudioFormat_WMAudioV8;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_V9:
return MFAudioFormat_WMAudioV9;
/* case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_LOSSLESS:
return MFAudioFormat_WMAudio_Lossless; */
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WMA_SPEECH:
return MFAudioFormat_MSP1;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_OPUS:
return MFAudioFormat_Opus;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_ALAC:
return MFAudioFormat_ALAC;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_FLAC:
return MFAudioFormat_FLAC;
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_WAV:
switch (pcm_type) {
case OSWRAPPER_AUDIO_ENC_PCM_FLOAT:
return MFAudioFormat_Float;
#ifdef OSWRAPPER_AUDIO_ENC__WINDOWS_WIP_ALAW_ULAW
case OSWRAPPER_AUDIO_ENC_PCM_ALAW:
return OSWrapperAudioEncFormat_PCM_ALAW;
case OSWRAPPER_AUDIO_ENC_PCM_ULAW:
return OSWrapperAudioEncFormat_PCM_MULAW;
#endif
default:
break;
}
case OSWRAPPER_AUDIO_ENC_OUPUT_FORMAT_PREFERRED_LOSSLESS:
default:
return MFAudioFormat_PCM;
}
}
#define OSWRAPPER_AUDIO_ENC__MAKE_MEDIA_HELPER(X) if (FAILED(X)) { goto cleanup; }