Skip to content

Commit 8268229

Browse files
committed
Implement 24bit int flac encode
1 parent cc88988 commit 8268229

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

src/framework/audio/engine/internal/export/flacencoder.cpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,23 @@ bool FlacEncoder::init(const io::path_t& path, const SoundTrackFormat& format, c
6666
m_progress.progress(current, total);
6767
});
6868

69+
int bitsPerSample = 0;
70+
switch (m_format.sampleFormat) {
71+
case AudioSampleFormat::Int16:
72+
bitsPerSample = 16;
73+
break;
74+
case AudioSampleFormat::Int24:
75+
bitsPerSample = 24;
76+
break;
77+
default:
78+
return false;
79+
}
80+
6981
if (!m_flac->set_verify(true)
7082
|| !m_flac->set_compression_level(0)
7183
|| !m_flac->set_channels(m_format.outputSpec.audioChannelCount)
7284
|| !m_flac->set_sample_rate(m_format.outputSpec.sampleRate)
73-
|| !m_flac->set_bits_per_sample(16)
85+
|| !m_flac->set_bits_per_sample(bitsPerSample)
7486
|| !m_flac->set_total_samples_estimate(totalSamplesNumber)) {
7587
return false;
7688
}
@@ -107,18 +119,34 @@ size_t FlacEncoder::encode(samples_t samplesPerChannel, const float* input)
107119
uint32_t frameSize = 1024;
108120
size_t stepSize = frameSize * m_format.outputSpec.audioChannelCount;
109121

110-
std::vector<FLAC__int32> buff(samplesPerChannel * sizeof(float));
122+
int bitsPerSample = 0;
123+
switch (m_format.sampleFormat) {
124+
case AudioSampleFormat::Int16:
125+
bitsPerSample = 16;
126+
break;
127+
case AudioSampleFormat::Int24:
128+
bitsPerSample = 24;
129+
break;
130+
default:
131+
return 0;
132+
}
133+
134+
std::vector<FLAC__int32> buff(totalSamplesNumber);
111135

112-
for (size_t i = 0; i < buff.size(); ++i) {
113-
buff[i] = dsp::convertFloatSamples<FLAC__int32>(input[i], 16);
136+
for (size_t i = 0; i < totalSamplesNumber; ++i) {
137+
buff[i] = dsp::convertFloatSamples<FLAC__int32>(input[i], bitsPerSample);
114138
}
115139

116140
std::vector<FLAC__int32> intermBuff(stepSize);
117141

118142
for (size_t i = 0; i < totalSamplesNumber; i += stepSize) {
119-
std::copy(buff.data() + i, buff.data() + i + stepSize, intermBuff.data());
143+
size_t remainingSamples = totalSamplesNumber - i;
144+
size_t samplesToCopy = std::min(stepSize, remainingSamples);
145+
uint32_t samplesPerChannelToProcess = samplesToCopy / m_format.outputSpec.audioChannelCount;
146+
147+
std::copy(buff.data() + i, buff.data() + i + samplesToCopy, intermBuff.data());
120148

121-
if (m_flac->process_interleaved(intermBuff.data(), frameSize)) {
149+
if (m_flac->process_interleaved(intermBuff.data(), samplesPerChannelToProcess)) {
122150
result += stepSize;
123151
} else {
124152
break;

0 commit comments

Comments
 (0)