| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#define _CRT_SECURE_NO_WARNINGS |
| 9 |
|
| 10 |
#define STB_VORBIS_HEADER_ONLY |
| 11 |
#include "../../extras/stb_vorbis.c" |
| 12 |
|
| 13 |
#define MA_NO_DEVICE_IO |
| 14 |
#define MA_NO_THREADING |
| 15 |
#define MINIAUDIO_IMPLEMENTATION |
| 16 |
#include "../../miniaudio.h" |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
|
| 20 |
void print_usage() |
| 21 |
{ |
| 22 |
printf("USAGE: audioconverter [input file] [output file] [format] [channels] [rate]\n"); |
| 23 |
printf(" [format] is optional and can be one of the following:\n"); |
| 24 |
printf(" u8 8-bit unsigned integer\n"); |
| 25 |
printf(" s16 16-bit signed integer\n"); |
| 26 |
printf(" s24 24-bit signed integer (tightly packed)\n"); |
| 27 |
printf(" s32 32-bit signed integer\n"); |
| 28 |
printf(" f32 32-bit floating point\n"); |
| 29 |
printf(" [channels] is optional and in the range of %d and %d\n", MA_MIN_CHANNELS, MA_MAX_CHANNELS); |
| 30 |
printf(" [rate] is optional and in the range of %d and %d\n", ma_standard_sample_rate_min, ma_standard_sample_rate_max); |
| 31 |
printf("\n"); |
| 32 |
printf("PARAMETERS:\n"); |
| 33 |
printf(" --linear-order [0..%d]\n", MA_MAX_FILTER_ORDER); |
| 34 |
} |
| 35 |
|
| 36 |
ma_result do_conversion(ma_decoder* pDecoder, ma_encoder* pEncoder) |
| 37 |
{ |
| 38 |
ma_result result = MA_SUCCESS; |
| 39 |
|
| 40 |
MA_ASSERT(pDecoder != NULL); |
| 41 |
MA_ASSERT(pEncoder != NULL); |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
for (;;) { |
| 48 |
ma_uint8 pRawData[MA_DATA_CONVERTER_STACK_BUFFER_SIZE]; |
| 49 |
ma_uint64 framesReadThisIteration; |
| 50 |
ma_uint64 framesToReadThisIteration; |
| 51 |
|
| 52 |
framesToReadThisIteration = sizeof(pRawData) / ma_get_bytes_per_frame(pDecoder->outputFormat, pDecoder->outputChannels); |
| 53 |
result = ma_decoder_read_pcm_frames(pDecoder, pRawData, framesToReadThisIteration, &framesReadThisIteration); |
| 54 |
if (result != MA_SUCCESS) { |
| 55 |
break; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
ma_encoder_write_pcm_frames(pEncoder, pRawData, framesReadThisIteration, NULL); |
| 60 |
|
| 61 |
|
| 62 |
if (framesReadThisIteration < framesToReadThisIteration) { |
| 63 |
break; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return result; |
| 68 |
} |
| 69 |
|
| 70 |
ma_bool32 is_number(const char* str) |
| 71 |
{ |
| 72 |
if (str == NULL || str[0] == '\0') { |
| 73 |
return MA_FALSE; |
| 74 |
} |
| 75 |
|
| 76 |
while (str[0] != '\0') { |
| 77 |
if (str[0] < '0' || str[0] > '9') { |
| 78 |
return MA_FALSE; |
| 79 |
} |
| 80 |
|
| 81 |
str += 1; |
| 82 |
} |
| 83 |
|
| 84 |
return MA_TRUE; |
| 85 |
} |
| 86 |
|
| 87 |
ma_bool32 try_parse_uint32_in_range(const char* str, ma_uint32* pValue, ma_uint32 lo, ma_uint32 hi) |
| 88 |
{ |
| 89 |
ma_uint32 x; |
| 90 |
|
| 91 |
if (!is_number(str)) { |
| 92 |
return MA_FALSE; |
| 93 |
} |
| 94 |
|
| 95 |
x = (ma_uint32)atoi(str); |
| 96 |
if (x < lo || x > hi) { |
| 97 |
return MA_FALSE; |
| 98 |
} |
| 99 |
|
| 100 |
if (pValue != NULL) { |
| 101 |
*pValue = x; |
| 102 |
} |
| 103 |
|
| 104 |
return MA_TRUE; |
| 105 |
} |
| 106 |
|
| 107 |
ma_bool32 try_parse_format(const char* str, ma_format* pValue) |
| 108 |
{ |
| 109 |
ma_format format; |
| 110 |
|
| 111 |
if (strcmp(str, "u8") == 0) { |
| 112 |
format = ma_format_u8; |
| 113 |
} else if (strcmp(str, "s16") == 0) { |
| 114 |
format = ma_format_s16; |
| 115 |
} else if (strcmp(str, "s24") == 0) { |
| 116 |
format = ma_format_s24; |
| 117 |
} else if (strcmp(str, "s32") == 0) { |
| 118 |
format = ma_format_s32; |
| 119 |
} else if (strcmp(str, "f32") == 0) { |
| 120 |
format = ma_format_f32; |
| 121 |
} else { |
| 122 |
return MA_FALSE; |
| 123 |
} |
| 124 |
|
| 125 |
if (pValue != NULL) { |
| 126 |
*pValue = format; |
| 127 |
} |
| 128 |
|
| 129 |
return MA_TRUE; |
| 130 |
} |
| 131 |
|
| 132 |
ma_bool32 try_parse_channels(const char* str, ma_uint32* pValue) |
| 133 |
{ |
| 134 |
return try_parse_uint32_in_range(str, pValue, MA_MIN_CHANNELS, MA_MAX_CHANNELS); |
| 135 |
} |
| 136 |
|
| 137 |
ma_bool32 try_parse_sample_rate(const char* str, ma_uint32* pValue) |
| 138 |
{ |
| 139 |
return try_parse_uint32_in_range(str, pValue, ma_standard_sample_rate_min, ma_standard_sample_rate_max); |
| 140 |
} |
| 141 |
|
| 142 |
ma_bool32 try_parse_resample_algorithm(const char* str, ma_resample_algorithm* pValue) |
| 143 |
{ |
| 144 |
ma_resample_algorithm algorithm; |
| 145 |
|
| 146 |
if (strcmp(str, "linear") == 0) { |
| 147 |
algorithm = ma_resample_algorithm_linear; |
| 148 |
} else { |
| 149 |
return MA_FALSE; |
| 150 |
} |
| 151 |
|
| 152 |
if (pValue != NULL) { |
| 153 |
*pValue = algorithm; |
| 154 |
} |
| 155 |
|
| 156 |
return MA_TRUE; |
| 157 |
} |
| 158 |
|
| 159 |
int main(int argc, char** argv) |
| 160 |
{ |
| 161 |
ma_result result; |
| 162 |
ma_decoder_config decoderConfig; |
| 163 |
ma_decoder decoder; |
| 164 |
ma_encoder_config encoderConfig; |
| 165 |
ma_encoder encoder; |
| 166 |
ma_encoding_format outputEncodingFormat; |
| 167 |
ma_format format = ma_format_unknown; |
| 168 |
ma_uint32 channels = 0; |
| 169 |
ma_uint32 rate = 0; |
| 170 |
ma_resample_algorithm resampleAlgorithm; |
| 171 |
ma_uint32 linearOrder = 8; |
| 172 |
int iarg; |
| 173 |
const char* pOutputFilePath; |
| 174 |
|
| 175 |
|
| 176 |
if (argc == 2) { |
| 177 |
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { |
| 178 |
print_usage(); |
| 179 |
return 0; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
if (argc < 3) { |
| 184 |
print_usage(); |
| 185 |
return -1; |
| 186 |
} |
| 187 |
|
| 188 |
resampleAlgorithm = ma_resample_algorithm_linear; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
for (iarg = 3; iarg < argc; iarg += 1) { |
| 195 |
if (strcmp(argv[iarg], "--linear-order") == 0) { |
| 196 |
iarg += 1; |
| 197 |
if (iarg >= argc) { |
| 198 |
break; |
| 199 |
} |
| 200 |
|
| 201 |
if (!try_parse_uint32_in_range(argv[iarg], &linearOrder, 0, 8)) { |
| 202 |
printf("Expecting a number between 0 and %d for --linear-order.\n", MA_MAX_FILTER_ORDER); |
| 203 |
return -1; |
| 204 |
} |
| 205 |
|
| 206 |
continue; |
| 207 |
} |
| 208 |
|
| 209 |
if (try_parse_resample_algorithm(argv[iarg], &resampleAlgorithm)) { |
| 210 |
continue; |
| 211 |
} |
| 212 |
|
| 213 |
if (try_parse_format(argv[iarg], &format)) { |
| 214 |
continue; |
| 215 |
} |
| 216 |
|
| 217 |
if (try_parse_channels(argv[iarg], &channels)) { |
| 218 |
continue; |
| 219 |
} |
| 220 |
|
| 221 |
if (try_parse_sample_rate(argv[iarg], &rate)) { |
| 222 |
continue; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
printf("Warning: Unknown parameter \"%s\"", argv[iarg]); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
decoderConfig = ma_decoder_config_init(format, channels, rate); |
| 231 |
decoderConfig.resampling.algorithm = resampleAlgorithm; |
| 232 |
decoderConfig.resampling.linear.lpfOrder = linearOrder; |
| 233 |
|
| 234 |
result = ma_decoder_init_file(argv[1], &decoderConfig, &decoder); |
| 235 |
if (result != MA_SUCCESS) { |
| 236 |
printf("Failed to open input file. Check the file exists and the format is supported. Supported input formats:\n"); |
| 237 |
#if defined(ma_dr_opus_h) |
| 238 |
printf(" Opus\n"); |
| 239 |
#endif |
| 240 |
#if defined(ma_dr_mp3_h) |
| 241 |
printf(" MP3\n"); |
| 242 |
#endif |
| 243 |
#if defined(ma_dr_flac_h) |
| 244 |
printf(" FLAC\n"); |
| 245 |
#endif |
| 246 |
#if defined(STB_VORBIS_INCLUDE_STB_VORBIS_H) |
| 247 |
printf(" Vorbis\n"); |
| 248 |
#endif |
| 249 |
#if defined(ma_dr_wav_h) |
| 250 |
printf(" WAV\n"); |
| 251 |
#endif |
| 252 |
return (int)result; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
pOutputFilePath = argv[2]; |
| 257 |
|
| 258 |
outputEncodingFormat = ma_encoding_format_wav; |
| 259 |
if (ma_path_extension_equal(pOutputFilePath, "wav")) { |
| 260 |
outputEncodingFormat = ma_encoding_format_wav; |
| 261 |
} else { |
| 262 |
printf("Warning: Unknown file extension \"%s\". Encoding as WAV.\n", ma_path_extension(pOutputFilePath)); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
encoderConfig = ma_encoder_config_init(outputEncodingFormat, decoder.outputFormat, decoder.outputChannels, decoder.outputSampleRate); |
| 267 |
result = ma_encoder_init_file(pOutputFilePath, &encoderConfig, &encoder); |
| 268 |
if (result != MA_SUCCESS) { |
| 269 |
ma_decoder_uninit(&decoder); |
| 270 |
printf("Failed to open output file. Check that the directory exists and that the file is not already opened by another process. %s", ma_result_description(result)); |
| 271 |
return -1; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
result = do_conversion(&decoder, &encoder); |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
ma_encoder_uninit(&encoder); |
| 281 |
ma_decoder_uninit(&decoder); |
| 282 |
|
| 283 |
return (int)result; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
#if defined(_MSC_VER) && !defined(__clang__) |
| 289 |
#pragma warning(push) |
| 290 |
#pragma warning(disable:4100) |
| 291 |
#pragma warning(disable:4244) |
| 292 |
#pragma warning(disable:4245) |
| 293 |
#pragma warning(disable:4456) |
| 294 |
#pragma warning(disable:4457) |
| 295 |
#pragma warning(disable:4701) |
| 296 |
#else |
| 297 |
#endif |
| 298 |
#undef STB_VORBIS_HEADER_ONLY |
| 299 |
#include "../../extras/stb_vorbis.c" |
| 300 |
#if defined(_MSC_VER) && !defined(__clang__) |
| 301 |
#pragma warning(pop) |
| 302 |
#else |
| 303 |
#endif |