From 26d6844bc6422869e4277043e46afa672014d8ec Mon Sep 17 00:00:00 2001 From: tildearrow Date: Thu, 6 Nov 2025 01:13:12 -0500 Subject: [PATCH] add output format command line option also auto-detect format based on extension --- src/main.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index b39daa9db..898338fa5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -117,6 +117,8 @@ bool infoMode=false; bool noReportError=false; +bool hasOutFormat=false; + std::vector params; #ifdef HAVE_LOCALE @@ -451,6 +453,36 @@ TAParamResult pOutMode(String val) { return TA_PARAM_SUCCESS; } +TAParamResult pOutFormat(String val) { + if (hasOutFormat) { + logE("the output format is already set."); + return TA_PARAM_ERROR; + } + hasOutFormat=true; + if (val=="u8") { + exportOptions.format=DIV_EXPORT_FORMAT_WAV; + exportOptions.wavFormat=DIV_EXPORT_WAV_U8; + } else if (val=="s16") { + exportOptions.format=DIV_EXPORT_FORMAT_WAV; + exportOptions.wavFormat=DIV_EXPORT_WAV_S16; + } else if (val=="f32") { + exportOptions.format=DIV_EXPORT_FORMAT_WAV; + exportOptions.wavFormat=DIV_EXPORT_WAV_F32; + } else if (val=="opus") { + exportOptions.format=DIV_EXPORT_FORMAT_OPUS; + } else if (val=="flac") { + exportOptions.format=DIV_EXPORT_FORMAT_FLAC; + } else if (val=="vorbis") { + exportOptions.format=DIV_EXPORT_FORMAT_VORBIS; + } else if (val=="mp3") { + exportOptions.format=DIV_EXPORT_FORMAT_MPEG_L3; + } else { + logE("invalid value for outformat! valid values are: u8, s16, f32, opus, flac, vorbis and mp3."); + return TA_PARAM_ERROR; + } + return TA_PARAM_SUCCESS; +} + TAParamResult pBenchmark(String val) { if (val=="render") { benchMode=1; @@ -467,6 +499,30 @@ TAParamResult pBenchmark(String val) { TAParamResult pOutput(String val) { outName=val; e.setAudio(DIV_AUDIO_DUMMY); + + // detect output format + if (!hasOutFormat) { + size_t extPos=outName.rfind('.'); + if (extPos!=String::npos) { + String lowerCase=outName.substr(extPos); + for (char& i: lowerCase) { + if (i>='A' && i<='Z') i+='a'-'A'; + } + + if (lowerCase==".wav") { + exportOptions.format=DIV_EXPORT_FORMAT_WAV; + } else if (lowerCase==".ogg") { + // not defaulting to Vorbis in order to encourage Opus usage + exportOptions.format=DIV_EXPORT_FORMAT_OPUS; + } else if (lowerCase==".flac") { + exportOptions.format=DIV_EXPORT_FORMAT_FLAC; + } else if (lowerCase==".opus") { + exportOptions.format=DIV_EXPORT_FORMAT_OPUS; + } else if (lowerCase==".mp3") { + exportOptions.format=DIV_EXPORT_FORMAT_MPEG_L3; + } + } + } return TA_PARAM_SUCCESS; } @@ -520,6 +576,7 @@ void initParams() { params.push_back(TAParam("a","audio",true,pAudio,"jack|sdl|portaudio|pipe","set audio engine (SDL by default)")); params.push_back(TAParam("o","output",true,pOutput,"","output audio to file")); + params.push_back(TAParam("f","outformat",true,pOutFormat,"u8|s16|f32|opus|flac|vorbis|mp3","set audio output format")); params.push_back(TAParam("O","vgmout",true,pVGMOut,"","output .vgm data")); params.push_back(TAParam("D","direct",false,pDirect,"","set VGM export direct stream mode")); params.push_back(TAParam("C","cmdout",true,pCmdOut,"","output command stream"));