Skip to content

Commit 2f082c2

Browse files
committed
AudioProcessor: attempt to not re-initialize AudioStream
When that's possible: when formats aren't actually changed after first initialisation.
1 parent 47e1c05 commit 2f082c2

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

_common/AudioProcessor/audio_processor.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,37 @@ bool MoondustAudioProcessor::init_cvt_stream()
7373

7474
m_curChunk = 0;
7575

76+
m_cvt_in_channels = spec_src.m_channels;
77+
m_cvt_in_sample_format = spec_src.m_sample_format;
78+
m_cvt_in_sample_rate = spec_src.m_sample_rate;
79+
80+
m_cvt_out_channels = spec_dst.m_channels;
81+
m_cvt_out_sample_format = spec_dst.m_sample_format;
82+
m_cvt_out_sample_rate = spec_dst.m_sample_rate;
83+
84+
return true;
85+
}
86+
87+
bool MoondustAudioProcessor::update_cvt_stream()
88+
{
89+
if(!m_cvt_stream)
90+
return init_cvt_stream();
91+
92+
const auto &spec_src = m_in_file->getSpec();
93+
const auto &spec_dst = m_out_file->getSpec();
94+
95+
if(m_cvt_in_channels != spec_src.m_channels ||
96+
m_cvt_in_sample_format != spec_src.m_sample_format ||
97+
m_cvt_in_sample_rate != spec_src.m_sample_rate ||
98+
m_cvt_out_channels != spec_dst.m_channels ||
99+
m_cvt_out_sample_format != spec_dst.m_sample_format ||
100+
m_cvt_out_sample_rate != spec_dst.m_sample_rate)
101+
{
102+
return init_cvt_stream();
103+
}
104+
105+
SDL_AudioStreamClear(m_cvt_stream);
106+
76107
return true;
77108
}
78109

@@ -352,7 +383,7 @@ bool MoondustAudioProcessor::openOutFile(const std::string &file, int dstFormat,
352383
return false;
353384
}
354385

355-
if(!init_cvt_stream())
386+
if(!update_cvt_stream())
356387
{
357388
m_out_file.reset(nullptr);
358389
SDL_RWclose(m_rw_out);
@@ -451,7 +482,7 @@ bool MoondustAudioProcessor::rewindRead()
451482
m_stat_read = 0;
452483
m_curChunk = 0.0;
453484

454-
if(!init_cvt_stream())
485+
if(!update_cvt_stream())
455486
return false;
456487

457488
return m_in_file->readRewind();

_common/AudioProcessor/audio_processor.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ class MoondustAudioProcessor
4444
std::unique_ptr<MDAudioFile> m_out_file;
4545

4646
SDL_AudioStream *m_cvt_stream = nullptr;
47+
int m_cvt_in_channels = 0;
48+
int m_cvt_in_sample_format = 0;
49+
int m_cvt_in_sample_rate = 0;
50+
int m_cvt_out_channels = 0;
51+
int m_cvt_out_sample_format = 0;
52+
int m_cvt_out_sample_rate = 0;
4753
bool init_cvt_stream();
54+
bool update_cvt_stream();
4855

4956
double m_curChunk = 0;
5057
double m_numChunks = 0;

0 commit comments

Comments
 (0)