Skip to content

Commit 47e1c05

Browse files
committed
AudioProcessor: Use the wanted spec when possible
This also avoids glitches happen at the XQOA while encoding
1 parent de14fd1 commit 47e1c05

4 files changed

Lines changed: 65 additions & 5 deletions

File tree

XTConvert/src/libxtconvert.cpp

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,32 @@ class Converter
12941294

12951295
bool convert_sfx(const QString&, const QString& in_path, const QString& out_path)
12961296
{
1297-
if(!m_audioCvt.openInFile(in_path.toStdString()))
1297+
MDAudioFileSpecWanted spec_wanted;
1298+
1299+
switch(m_spec.target_platform)
1300+
{
1301+
default:
1302+
spec_wanted.m_sample_rate = 44100;
1303+
break;
1304+
1305+
case TargetPlatform::T3X:
1306+
spec_wanted.m_sample_rate = 44100;
1307+
spec_wanted.m_channels = 2;
1308+
break;
1309+
1310+
case TargetPlatform::TPL:
1311+
spec_wanted.m_sample_format = AUDIO_S16MSB;
1312+
spec_wanted.m_sample_rate = 32000;
1313+
spec_wanted.m_channels = 2;
1314+
break;
1315+
1316+
case TargetPlatform::DSG:
1317+
spec_wanted.m_sample_rate = 16384;
1318+
spec_wanted.m_channels = 1;
1319+
break;
1320+
}
1321+
1322+
if(!m_audioCvt.openInFile(in_path.toStdString(), std::string(), nullptr, &spec_wanted))
12981323
{
12991324
log_file(LogCategory::SkippedInvalid, in_path);
13001325
m_audioCvt.close();
@@ -1353,7 +1378,32 @@ class Converter
13531378

13541379
bool convert_music_xqoa(const QString&, const QString& in_path, const QString& out_path, const QString &musicArgs = QString())
13551380
{
1356-
if(!m_audioCvt.openInFile(in_path.toStdString(), musicArgs.toStdString()))
1381+
MDAudioFileSpecWanted spec_wanted;
1382+
1383+
switch(m_spec.target_platform)
1384+
{
1385+
default:
1386+
spec_wanted.m_sample_rate = 44100;
1387+
break;
1388+
1389+
case TargetPlatform::T3X:
1390+
spec_wanted.m_sample_rate = 44100;
1391+
spec_wanted.m_channels = 2;
1392+
break;
1393+
1394+
case TargetPlatform::TPL:
1395+
spec_wanted.m_sample_format = AUDIO_S16MSB;
1396+
spec_wanted.m_sample_rate = 32000;
1397+
spec_wanted.m_channels = 2;
1398+
break;
1399+
1400+
case TargetPlatform::DSG:
1401+
spec_wanted.m_sample_rate = 16384;
1402+
spec_wanted.m_channels = 1;
1403+
break;
1404+
}
1405+
1406+
if(!m_audioCvt.openInFile(in_path.toStdString(), musicArgs.toStdString(), nullptr, &spec_wanted))
13571407
{
13581408
log_file(LogCategory::SkippedInvalid, in_path);
13591409
m_audioCvt.close();

_common/AudioProcessor/audio_processor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ const MDAudioFileSpec &MoondustAudioProcessor::getOutSpec() const
114114
return m_out_file->getSpec();
115115
}
116116

117-
bool MoondustAudioProcessor::openInFile(const std::string &file, const std::string &argsString, int *detectedFormat)
117+
bool MoondustAudioProcessor::openInFile(const std::string &file, const std::string &argsString, int *detectedFormat, const MDAudioFileSpecWanted *spec_wanted)
118118
{
119119
AudioFormats format = FORMAT_UNKNOWN;
120120
MusicArgs args(argsString);
@@ -260,6 +260,9 @@ bool MoondustAudioProcessor::openInFile(const std::string &file, const std::stri
260260
return false;
261261
}
262262

263+
if(spec_wanted)
264+
m_in_file->setWantedSpec(*spec_wanted);
265+
263266
m_in_file->setArgs(args);
264267

265268
if(!m_in_file->openRead(m_rw_in))

_common/AudioProcessor/audio_processor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class MoondustAudioProcessor
7979
*/
8080
const MDAudioFileSpec &getOutSpec() const;
8181

82-
bool openInFile(const std::string &file, const std::string &args = std::string(), int *detectedFormat = nullptr);
82+
bool openInFile(const std::string &file, const std::string &args = std::string(), int *detectedFormat = nullptr, const MDAudioFileSpecWanted *spec_wanted = nullptr);
8383
bool openOutFile(const std::string &file, int dstFormat, const MDAudioFileSpec &dstSpec);
8484

8585
void close();

test/Maintainer/AudioConverterTest/coverter_dialogue.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ void CoverterDialogue::on_runCvt_clicked()
158158

159159
QString inPath = ui->fileIn->text();
160160
QString inPathArgs;
161+
MDAudioFileSpecWanted wanted;
161162
int argsBegin = inPath.indexOf('|');
162163

163164
if(argsBegin >= 0)
@@ -166,7 +167,13 @@ void CoverterDialogue::on_runCvt_clicked()
166167
inPath.remove(argsBegin, inPath.size() - argsBegin);
167168
}
168169

169-
if(!m_cvt.openInFile(inPath.toStdString(), inPathArgs.toStdString()))
170+
if(ui->channels->isChecked())
171+
wanted.m_channels = ui->dstChannels->value();
172+
173+
if(ui->rate->isChecked())
174+
wanted.m_sample_rate = ui->dstRate->value();
175+
176+
if(!m_cvt.openInFile(inPath.toStdString(), inPathArgs.toStdString(), nullptr, &wanted))
170177
{
171178
auto err = QString("Failed to open input file %1: %2").arg(inPath, QString::fromStdString(m_cvt.getLastError()));
172179
qWarning() << err;

0 commit comments

Comments
 (0)