rhino/demo/c/dr_libs/tests/external/miniaudio/tools/audioconverter/audioconverter.c
Go to the documentation of this file.
1 /*
2 USAGE: audioconverter [input file] [output file] [format] [channels] [rate]
3 
4 EXAMPLES:
5  audioconverter my_file.flac my_file.wav
6  audioconverter my_file.flac my_file.wav f32 44100 linear --linear-order 8
7  audioconverter my_file.flac my_file.wav s16 2 44100 speex --speex-quality 10
8 */
9 
10 /*
11 Note about Speex resampling. If you decide to enable the Speex resampler with ENABLE_SPEEX, this program will use licensed third party code. If you compile and
12 redistribute this program you need to include a copy of the license which can be found at https://github.com/xiph/opus-tools/blob/master/COPYING. You can also
13 find a copy of this text in extras/speex_resampler/README.md in the miniaudio repository.
14 */
15 #define _CRT_SECURE_NO_WARNINGS /* For stb_vorbis' usage of fopen() instead of fopen_s(). */
16 
17 #define DR_FLAC_IMPLEMENTATION
18 #include "../../extras/dr_flac.h" /* Enables FLAC decoding. */
19 #define DR_MP3_IMPLEMENTATION
20 #include "../../extras/dr_mp3.h" /* Enables MP3 decoding. */
21 #define DR_WAV_IMPLEMENTATION
22 #include "../../extras/dr_wav.h" /* Enables WAV decoding. */
23 #define STB_VORBIS_HEADER_ONLY
24 #include "../../extras/stb_vorbis.c" /* Enables Vorbis decoding. */
25 
26 /* Enable Speex resampling, but only if requested on the command line at a build time. */
27 #if defined(ENABLE_SPEEX)
28  #define MINIAUDIO_SPEEX_RESAMPLER_IMPLEMENTATION
29  #include "../../extras/speex_resampler/ma_speex_resampler.h"
30 #endif
31 
32 #define MINIAUDIO_IMPLEMENTATION
33 #include "../../miniaudio.h"
34 
35 #include <stdio.h>
36 
38 {
39  printf("USAGE: audioconverter [input file] [output file] [format] [channels] [rate]\n");
40  printf(" [format] is optional and can be one of the following:\n");
41  printf(" u8 8-bit unsigned integer\n");
42  printf(" s16 16-bit signed integer\n");
43  printf(" s24 24-bit signed integer (tightly packed)\n");
44  printf(" s32 32-bit signed integer\n");
45  printf(" f32 32-bit floating point\n");
46  printf(" [channels] is optional and in the range of %d and %d\n", MA_MIN_CHANNELS, MA_MAX_CHANNELS);
47  printf(" [rate] is optional and in the range of %d and %d\n", MA_MIN_SAMPLE_RATE, MA_MAX_SAMPLE_RATE);
48  printf("\n");
49  printf("PARAMETERS:\n");
50  printf(" --linear-order [0..%d]\n", MA_MAX_FILTER_ORDER);
51  printf(" --speex-quality [0..10]\n");
52 }
53 
54 ma_result do_conversion(ma_decoder* pDecoder, ma_encoder* pEncoder, ma_format format, ma_uint32 rate)
55 {
56  ma_result result = MA_SUCCESS;
57 
58  MA_ASSERT(pDecoder != NULL);
59  MA_ASSERT(pEncoder != NULL);
60 
61  /* Now we just read from the decoder, resample, then write to the encoder. */
62  for (;;) {
64  ma_uint64 framesReadThisIteration;
65  ma_uint64 framesToReadThisIteration;
66 
67  framesToReadThisIteration = sizeof(pRawData) / ma_get_bytes_per_frame(pDecoder->internalFormat, pDecoder->internalChannels);
68  framesReadThisIteration = ma_decoder_read_pcm_frames(pDecoder, pRawData, framesToReadThisIteration);
69  if (framesReadThisIteration == 0) {
70  break; /* Reached the end. */
71  }
72 
73  /* At this point we have the raw data from the decoder. We now just need to write it to the encoder. */
74  ma_encoder_write_pcm_frames(pEncoder, pRawData, framesReadThisIteration);
75 
76  /* Get out of the loop if we've reached the end. */
77  if (framesReadThisIteration < framesToReadThisIteration) {
78  break;
79  }
80  }
81 
82  return result;
83 }
84 
85 ma_bool32 is_number(const char* str)
86 {
87  if (str == NULL || str[0] == '\0') {
88  return MA_FALSE;
89  }
90 
91  while (str[0] != '\0') {
92  if (str[0] < '0' || str[0] > '9') {
93  return MA_FALSE;
94  }
95 
96  str += 1;
97  }
98 
99  return MA_TRUE;
100 }
101 
103 {
104  ma_uint32 x;
105 
106  if (!is_number(str)) {
107  return MA_FALSE; /* Not an integer. */
108  }
109 
110  x = (ma_uint32)atoi(str);
111  if (x < lo || x > hi) {
112  return MA_FALSE; /* Out of range. */
113  }
114 
115  if (pValue != NULL) {
116  *pValue = x;
117  }
118 
119  return MA_TRUE;
120 }
121 
122 ma_bool32 try_parse_format(const char* str, ma_format* pValue)
123 {
124  ma_format format;
125 
126  /* */ if (strcmp(str, "u8") == 0) {
127  format = ma_format_u8;
128  } else if (strcmp(str, "s16") == 0) {
129  format = ma_format_s16;
130  } else if (strcmp(str, "s24") == 0) {
131  format = ma_format_s24;
132  } else if (strcmp(str, "s32") == 0) {
133  format = ma_format_s32;
134  } else if (strcmp(str, "f32") == 0) {
135  format = ma_format_f32;
136  } else {
137  return MA_FALSE; /* Not a format. */
138  }
139 
140  if (pValue != NULL) {
141  *pValue = format;
142  }
143 
144  return MA_TRUE;;
145 }
146 
147 ma_bool32 try_parse_channels(const char* str, ma_uint32* pValue)
148 {
150 }
151 
152 ma_bool32 try_parse_sample_rate(const char* str, ma_uint32* pValue)
153 {
155 }
156 
158 {
159  ma_resample_algorithm algorithm;
160 
161  /* */ if (strcmp(str, "linear") == 0) {
162  algorithm = ma_resample_algorithm_linear;
163  } else if (strcmp(str, "speex") == 0) {
164  algorithm = ma_resample_algorithm_speex;
165  } else {
166  return MA_FALSE; /* Not a valid algorithm */
167  }
168 
169  if (pValue != NULL) {
170  *pValue = algorithm;
171  }
172 
173  return MA_TRUE;
174 }
175 
176 int main(int argc, char** argv)
177 {
178  ma_result result;
179  ma_decoder_config decoderConfig;
181  ma_encoder_config encoderConfig;
183  ma_resource_format outputResourceFormat;
184  ma_format format = ma_format_unknown;
185  ma_uint32 channels = 0;
186  ma_uint32 rate = 0;
187  ma_resample_algorithm resampleAlgorithm;
188  ma_uint32 linearOrder = 8;
189  ma_uint32 speexQuality = 3;
190  int iarg;
191  const char* pOutputFilePath;
192 
193  print_usage();
194 
195  /* Print help if requested. */
196  if (argc == 2) {
197  if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
198  print_usage();
199  return 0;
200  }
201  }
202 
203  if (argc < 3) {
204  print_usage();
205  return -1;
206  }
207 
208  /* Default to Speex if it's enabled. */
209 #if defined(ENABLE_SPEEX)
210  resampleAlgorithm = ma_resample_algorithm_speex;
211 #else
212  resampleAlgorithm = ma_resample_algorithm_linear;
213 #endif
214 
215  /*
216  The fourth and fifth arguments can be a format and/or rate specifier. It doesn't matter which order they are in as we can identify them by whether or
217  not it's a number. If it's a number we assume it's a sample rate, otherwise we assume it's a format specifier.
218  */
219  for (iarg = 3; iarg < argc; iarg += 1) {
220  if (strcmp(argv[iarg], "--linear-order") == 0) {
221  iarg += 1;
222  if (iarg >= argc) {
223  break;
224  }
225 
226  if (!try_parse_uint32_in_range(argv[iarg], &linearOrder, 0, 8)) {
227  printf("Expecting a number between 0 and %d for --linear-order.\n", MA_MAX_FILTER_ORDER);
228  return -1;
229  }
230 
231  continue;
232  }
233 
234  if (strcmp(argv[iarg], "--speex-quality") == 0) {
235  iarg += 1;
236  if (iarg >= argc) {
237  break;
238  }
239 
240  if (!try_parse_uint32_in_range(argv[iarg], &speexQuality, 0, 10)) {
241  printf("Expecting a number between 0 and 10 for --speex-quality.\n");
242  return -1;
243  }
244 
245  continue;
246  }
247 
248  if (try_parse_resample_algorithm(argv[iarg], &resampleAlgorithm)) {
249  continue;
250  }
251 
252  if (try_parse_format(argv[iarg], &format)) {
253  continue;
254  }
255 
256  if (try_parse_channels(argv[iarg], &channels)) {
257  continue;
258  }
259 
260  if (try_parse_sample_rate(argv[iarg], &rate)) {
261  continue;
262  }
263 
264  /* Getting here means we have an unknown parameter. */
265  printf("Warning: Unknown parameter \"%s\"", argv[iarg]);
266  }
267 
268  /* Initialize a decoder for the input file. */
269  decoderConfig = ma_decoder_config_init(format, channels, rate);
270  decoderConfig.resampling.algorithm = resampleAlgorithm;
271  decoderConfig.resampling.linear.lpfOrder = linearOrder;
272 #if defined(ENABLE_SPEEX)
273  decoderConfig.resampling.speex.quality = speexQuality;
274 #endif
275 
276  result = ma_decoder_init_file(argv[1], &decoderConfig, &decoder);
277  if (result != MA_SUCCESS) {
278  printf("Failed to open input file. Check the file exists and the format is supported. Supported input formats:\n");
279  #if defined(dr_opus_h)
280  printf(" Opus\n");
281  #endif
282  #if defined(dr_mp3_h)
283  printf(" MP3\n");
284  #endif
285  #if defined(dr_flac_h)
286  printf(" FLAC\n");
287  #endif
288  #if defined(STB_VORBIS_INCLUDE_STB_VORBIS_H)
289  printf(" Vorbis\n");
290  #endif
291  #if defined(dr_wav_h)
292  printf(" WAV\n");
293  #endif
294  return (int)result;
295  }
296 
297 
298  pOutputFilePath = argv[2];
299 
300  outputResourceFormat = ma_resource_format_wav; /* Wave by default in case we don't know the file extension. */
301  if (ma_path_extension_equal(pOutputFilePath, "wav")) {
302  outputResourceFormat = ma_resource_format_wav;
303  } else {
304  printf("Warning: Unknown file extension \"%s\". Encoding as WAV.\n", ma_path_extension(pOutputFilePath));
305  }
306 
307  /* Initialize the encoder for the output file. */
309  result = ma_encoder_init_file(pOutputFilePath, &encoderConfig, &encoder);
310  if (result != MA_SUCCESS) {
312  printf("Failed to open output file. Check that the directory exists and that the file is not already opened by another process.");
313  return -1;
314  }
315 
316 
317  /* We have our decoder and encoder ready, so now we can do the conversion. */
318  result = do_conversion(&decoder, &encoder, format, rate);
319 
320 
321  /* Done. */
324 
325  return (int)result;
326 }
327 
328 
329 /* stb_vorbis implementation must come after the implementation of miniaudio. */
330 #undef STB_VORBIS_HEADER_ONLY
331 #include "../../extras/stb_vorbis.c"
ma_uint64
uint64_t ma_uint64
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1505
ma_get_bytes_per_frame
static MA_INLINE ma_uint32 ma_get_bytes_per_frame(ma_format format, ma_uint32 channels)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:2673
MA_FALSE
#define MA_FALSE
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1531
main
int main(int argc, char **argv)
Definition: rhino/demo/c/dr_libs/tests/external/miniaudio/tools/audioconverter/audioconverter.c:176
ma_uint8
uint8_t ma_uint8
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1499
ma_encoder_config
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:5313
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
ma_resource_format_wav
@ ma_resource_format_wav
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:5147
ma_decoder_uninit
ma_result ma_decoder_uninit(ma_decoder *pDecoder)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:44739
ma_decoder_config
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:5170
ma_resample_algorithm
ma_resample_algorithm
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:2234
ma_bool32
ma_uint32 ma_bool32
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1529
ma_format_f32
@ ma_format_f32
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1790
ma_decoder_config::resampling
struct ma_decoder_config::@122 resampling
MA_DATA_CONVERTER_STACK_BUFFER_SIZE
#define MA_DATA_CONVERTER_STACK_BUFFER_SIZE
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:4226
ma_decoder_config::linear
struct ma_decoder_config::@122::@123 linear
ma_decoder_config_init
ma_decoder_config ma_decoder_config_init(ma_format outputFormat, ma_uint32 outputChannels, ma_uint32 outputSampleRate)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:40646
ma_format_s32
@ ma_format_s32
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1789
decoder
ma_decoder decoder
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_deviceio/ma_test_deviceio.c:61
print_usage
void print_usage()
Definition: rhino/demo/c/dr_libs/tests/external/miniaudio/tools/audioconverter/audioconverter.c:37
ma_format
ma_format
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1779
ma_format_s24
@ ma_format_s24
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1788
ma_decoder_config::speex
struct ma_decoder_config::@122::@124 speex
ma_format_unknown
@ ma_format_unknown
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1785
ma_encoder_write_pcm_frames
ma_uint64 ma_encoder_write_pcm_frames(ma_encoder *pEncoder, const void *pFramesIn, ma_uint64 frameCount)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:45351
encoder
ma_encoder encoder
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_deviceio/ma_test_deviceio.c:62
try_parse_resample_algorithm
ma_bool32 try_parse_resample_algorithm(const char *str, ma_resample_algorithm *pValue)
Definition: rhino/demo/c/dr_libs/tests/external/miniaudio/tools/audioconverter/audioconverter.c:157
ma_path_extension
static const char * ma_path_extension(const char *path)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:44130
ma_result
int ma_result
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1658
try_parse_format
ma_bool32 try_parse_format(const char *str, ma_format *pValue)
Definition: rhino/demo/c/dr_libs/tests/external/miniaudio/tools/audioconverter/audioconverter.c:122
ma_encoder_uninit
void ma_encoder_uninit(ma_encoder *pEncoder)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:45334
try_parse_uint32_in_range
ma_bool32 try_parse_uint32_in_range(const char *str, ma_uint32 *pValue, ma_uint32 lo, ma_uint32 hi)
Definition: rhino/demo/c/dr_libs/tests/external/miniaudio/tools/audioconverter/audioconverter.c:102
ma_decoder::internalFormat
ma_format internalFormat
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:5199
ma_encoder
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:5324
ma_format_s16
@ ma_format_s16
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1787
ma_encoder_init_file
ma_result ma_encoder_init_file(const char *pFilePath, const ma_encoder_config *pConfig, ma_encoder *pEncoder)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:45279
ma_encoder_config_init
ma_encoder_config ma_encoder_config_init(ma_resource_format resourceFormat, ma_format format, ma_uint32 channels, ma_uint32 sampleRate)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:45183
ma_decoder
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:5193
MA_ASSERT
#define MA_ASSERT(condition)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:770
MA_MAX_SAMPLE_RATE
#define MA_MAX_SAMPLE_RATE
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1755
is_number
ma_bool32 is_number(const char *str)
Definition: rhino/demo/c/dr_libs/tests/external/miniaudio/tools/audioconverter/audioconverter.c:85
ma_decoder::internalChannels
ma_uint32 internalChannels
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:5200
try_parse_channels
ma_bool32 try_parse_channels(const char *str, ma_uint32 *pValue)
Definition: rhino/demo/c/dr_libs/tests/external/miniaudio/tools/audioconverter/audioconverter.c:147
ma_uint32
uint32_t ma_uint32
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1503
ma_format_u8
@ ma_format_u8
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1786
ma_decoder_read_pcm_frames
ma_uint64 ma_decoder_read_pcm_frames(ma_decoder *pDecoder, void *pFramesOut, ma_uint64 frameCount)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:44808
ma_decoder_config::algorithm
ma_resample_algorithm algorithm
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:5180
ma_resample_algorithm_linear
@ ma_resample_algorithm_linear
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:2236
ma_resource_format
ma_resource_format
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:5145
MA_MIN_CHANNELS
#define MA_MIN_CHANNELS
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1752
ma_decoder_init_file
ma_result ma_decoder_init_file(const char *pFilePath, const ma_decoder_config *pConfig, ma_decoder *pDecoder)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:44687
python.test_porcupine.argv
argv
Definition: test_porcupine.py:158
MA_MAX_CHANNELS
#define MA_MAX_CHANNELS
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1753
ma_resample_algorithm_speex
@ ma_resample_algorithm_speex
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:2237
MA_TRUE
#define MA_TRUE
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1530
MA_SUCCESS
#define MA_SUCCESS
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1659
ma_path_extension_equal
static ma_bool32 ma_path_extension_equal(const char *path, const char *extension)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:44181
MA_MIN_SAMPLE_RATE
#define MA_MIN_SAMPLE_RATE
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1754
do_conversion
ma_result do_conversion(ma_decoder *pDecoder, ma_encoder *pEncoder, ma_format format, ma_uint32 rate)
Definition: rhino/demo/c/dr_libs/tests/external/miniaudio/tools/audioconverter/audioconverter.c:54
try_parse_sample_rate
ma_bool32 try_parse_sample_rate(const char *str, ma_uint32 *pValue)
Definition: rhino/demo/c/dr_libs/tests/external/miniaudio/tools/audioconverter/audioconverter.c:152
MA_MAX_FILTER_ORDER
#define MA_MAX_FILTER_ORDER
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1758


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:47