Skip to content

Commit ee27e71

Browse files
Mixer: use Peak dB instead of RMS dB
1 parent b511522 commit ee27e71

File tree

7 files changed

+39
-61
lines changed

7 files changed

+39
-61
lines changed

src/framework/audio/common/audiotypes.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,11 @@ struct AudioParams {
360360
};
361361

362362
struct AudioSignalVal {
363-
float amplitude = 0.f;
364363
volume_dbfs_t pressure = 0.f;
365364

366365
inline bool operator ==(const AudioSignalVal& other) const
367366
{
368-
return muse::is_equal(amplitude, other.amplitude)
369-
&& pressure == other.pressure;
367+
return pressure == other.pressure;
370368
}
371369
};
372370

@@ -375,9 +373,9 @@ using AudioSignalChanges = async::Channel<AudioSignalValuesMap>;
375373

376374
static constexpr volume_dbfs_t MINIMUM_OPERABLE_DBFS_LEVEL = volume_dbfs_t::make(-100.f);
377375
struct AudioSignalsNotifier {
378-
void updateSignalValues(const audioch_t audioChNumber, const float newAmplitude)
376+
void updateSignalValues(const audioch_t audioChNumber, const float newPeak)
379377
{
380-
volume_dbfs_t newPressure = (newAmplitude > 0.f) ? volume_dbfs_t(muse::linear_to_db(newAmplitude)) : MINIMUM_OPERABLE_DBFS_LEVEL;
378+
volume_dbfs_t newPressure = (newPeak > 0.f) ? volume_dbfs_t(muse::linear_to_db(newPeak)) : MINIMUM_OPERABLE_DBFS_LEVEL;
381379
newPressure = std::max(newPressure, MINIMUM_OPERABLE_DBFS_LEVEL);
382380

383381
AudioSignalVal& signalVal = m_signalValuesMap[audioChNumber];
@@ -390,9 +388,7 @@ struct AudioSignalsNotifier {
390388
return;
391389
}
392390

393-
signalVal.amplitude = newAmplitude;
394391
signalVal.pressure = newPressure;
395-
396392
m_needNotifyAboutChanges = true;
397393
}
398394

src/framework/audio/common/rpc/rpcpacker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,12 @@ inline void unpack_custom(muse::msgpack::UnPacker& p, muse::audio::SoundTrackFor
271271

272272
inline void pack_custom(muse::msgpack::Packer& p, const muse::audio::AudioSignalVal& value)
273273
{
274-
p.process(value.amplitude, value.pressure);
274+
p.process(value.pressure);
275275
}
276276

277277
inline void unpack_custom(muse::msgpack::UnPacker& p, muse::audio::AudioSignalVal& value)
278278
{
279-
p.process(value.amplitude, value.pressure);
279+
p.process(value.pressure);
280280
}
281281

282282
inline void pack_custom(muse::msgpack::Packer& p, const muse::audio::InputProcessingProgress::ChunkInfo& value)

src/framework/audio/engine/internal/mixer.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,6 @@ void Mixer::setOutputSpec(const OutputSpec& spec)
182182

183183
m_outputSpec = spec;
184184

185-
m_limiter = std::make_unique<dsp::Limiter>(spec.sampleRate);
186-
187185
AbstractAudioSource::setOutputSpec(spec);
188186

189187
for (auto& channel : m_trackChannels) {
@@ -557,37 +555,34 @@ void Mixer::completeOutput(float* buffer, samples_t samplesPerChannel)
557555
return;
558556
}
559557

560-
float totalSquaredSum = 0.f;
561-
float volume = muse::db_to_linear(m_masterParams.volume);
558+
const float volume = muse::db_to_linear(m_masterParams.volume);
559+
float globalPeak = 0.f;
562560

563561
for (audioch_t audioChNum = 0; audioChNum < m_outputSpec.audioChannelCount; ++audioChNum) {
564-
float singleChannelSquaredSum = 0.f;
565-
gain_t totalGain = dsp::balanceGain(m_masterParams.balance, audioChNum) * volume;
562+
const gain_t totalGain = dsp::balanceGain(m_masterParams.balance, audioChNum) * volume;
563+
float peak = 0.f;
566564

567565
for (samples_t s = 0; s < samplesPerChannel; ++s) {
568-
int idx = s * m_outputSpec.audioChannelCount + audioChNum;
566+
const size_t idx = s * m_outputSpec.audioChannelCount + audioChNum;
567+
const float resultSample = buffer[idx] * totalGain;
568+
const float absSample = std::fabs(resultSample);
569569

570-
float resultSample = buffer[idx] * totalGain;
571570
buffer[idx] = resultSample;
572571

573-
float squaredSample = resultSample * resultSample;
574-
totalSquaredSum += squaredSample;
575-
singleChannelSquaredSum += squaredSample;
572+
if (absSample > peak) {
573+
peak = absSample;
574+
}
576575
}
577576

578-
float rms = dsp::samplesRootMeanSquare(singleChannelSquaredSum, samplesPerChannel);
579-
m_audioSignalNotifier.updateSignalValues(audioChNum, rms);
580-
}
581-
582-
m_isSilence = RealIsNull(totalSquaredSum);
583-
m_audioSignalNotifier.notifyAboutChanges();
577+
m_audioSignalNotifier.updateSignalValues(audioChNum, peak);
584578

585-
if (!m_limiter->isActive()) {
586-
return;
579+
if (peak > globalPeak) {
580+
globalPeak = peak;
581+
}
587582
}
588583

589-
float totalRms = dsp::samplesRootMeanSquare(totalSquaredSum, samplesPerChannel * m_outputSpec.audioChannelCount);
590-
m_limiter->process(totalRms, buffer, m_outputSpec.audioChannelCount, samplesPerChannel);
584+
m_isSilence = RealIsNull(globalPeak);
585+
m_audioSignalNotifier.notifyAboutChanges();
591586
}
592587

593588
void Mixer::notifyNoAudioSignal()

src/framework/audio/engine/internal/mixer.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "../iclock.h"
3535
#include "../ifxresolver.h"
3636

37-
#include "dsp/limiter.h"
3837
#include "mixerchannel.h"
3938

4039
namespace muse {
@@ -113,8 +112,6 @@ class Mixer : public AbstractAudioSource, public Injectable, public async::Async
113112

114113
std::vector<AuxChannelInfo> m_auxChannelInfoList;
115114

116-
dsp::LimiterPtr m_limiter = nullptr;
117-
118115
std::set<IClockPtr> m_clocks;
119116

120117
mutable AudioSignalsNotifier m_audioSignalNotifier;

src/framework/audio/engine/internal/mixerchannel.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ MixerChannel::MixerChannel(const TrackId trackId, IAudioSourcePtr source, const
3838
const modularity::ContextPtr& iocCtx)
3939
: Injectable(iocCtx), m_trackId(trackId),
4040
m_outputSpec(outputSpec),
41-
m_audioSource(std::move(source)),
42-
m_compressor(std::make_unique<dsp::Compressor>(outputSpec.sampleRate))
41+
m_audioSource(std::move(source))
4342
{
4443
ONLY_AUDIO_ENGINE_THREAD;
4544
}
@@ -219,39 +218,35 @@ samples_t MixerChannel::process(float* buffer, samples_t samplesPerChannel)
219218

220219
void MixerChannel::completeOutput(float* buffer, unsigned int samplesCount)
221220
{
222-
unsigned int channelsCount = audioChannelsCount();
223-
float volume = muse::db_to_linear(m_params.volume);
224-
float totalSquaredSum = 0.f;
221+
const unsigned int channelsCount = audioChannelsCount();
222+
const float volume = muse::db_to_linear(m_params.volume);
223+
float globalPeak = 0.f;
225224

226225
for (audioch_t audioChNum = 0; audioChNum < channelsCount; ++audioChNum) {
227-
float singleChannelSquaredSum = 0.f;
228-
229-
gain_t totalGain = dsp::balanceGain(m_params.balance, audioChNum) * volume;
226+
const gain_t totalGain = dsp::balanceGain(m_params.balance, audioChNum) * volume;
227+
float peak = 0.f;
230228

231229
for (unsigned int s = 0; s < samplesCount; ++s) {
232-
int idx = s * channelsCount + audioChNum;
230+
const unsigned int idx = s * channelsCount + audioChNum;
231+
const float resultSample = buffer[idx] * totalGain;
232+
const float absSample = std::fabs(resultSample);
233233

234-
float resultSample = buffer[idx] * totalGain;
235234
buffer[idx] = resultSample;
236235

237-
float squaredSample = resultSample * resultSample;
238-
singleChannelSquaredSum += squaredSample;
239-
totalSquaredSum += squaredSample;
236+
if (absSample > peak) {
237+
peak = absSample;
238+
}
240239
}
241240

242-
float rms = dsp::samplesRootMeanSquare(singleChannelSquaredSum, samplesCount);
243-
m_audioSignalNotifier.updateSignalValues(audioChNum, rms);
244-
}
241+
m_audioSignalNotifier.updateSignalValues(audioChNum, peak);
245242

246-
m_isSilent = RealIsNull(totalSquaredSum);
247-
m_audioSignalNotifier.notifyAboutChanges();
248-
249-
if (!m_compressor->isActive()) {
250-
return;
243+
if (peak > globalPeak) {
244+
globalPeak = peak;
245+
}
251246
}
252247

253-
float totalRms = dsp::samplesRootMeanSquare(totalSquaredSum, samplesCount * channelsCount);
254-
m_compressor->process(totalRms, buffer, channelsCount, samplesCount);
248+
m_isSilent = RealIsNull(globalPeak);
249+
m_audioSignalNotifier.notifyAboutChanges();
255250
}
256251

257252
bool MixerChannel::isSilent() const

src/framework/audio/engine/internal/mixerchannel.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
#include "../ifxresolver.h"
3030
#include "../ifxprocessor.h"
31-
#include "dsp/compressor.h"
3231
#include "track.h"
3332

3433
namespace muse::audio::engine {
@@ -76,8 +75,6 @@ class MixerChannel : public ITrackAudioOutput, public Injectable, public async::
7675
IAudioSourcePtr m_audioSource = nullptr;
7776
std::vector<IFxProcessorPtr> m_fxProcessors = {};
7877

79-
dsp::CompressorPtr m_compressor = nullptr;
80-
8178
bool m_isSilent = true;
8279

8380
async::Notification m_mutedChanged;

src/framework/audio/tests/rpcpacker_tests.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,9 @@ TEST_F(Audio_RpcPackerTests, AudioSourceParams)
295295
TEST_F(Audio_RpcPackerTests, AudioSignalVal)
296296
{
297297
AudioSignalVal origin;
298-
origin.amplitude = 0.6f;
299298
origin.pressure = 0.5;
300299

301300
KNOWN_FIELDS(origin,
302-
origin.amplitude,
303301
origin.pressure);
304302

305303
ByteArray data = rpc::RpcPacker::pack(origin);

0 commit comments

Comments
 (0)