From 876817329d054d00c40265968da3c4748921203c Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Wed, 16 Oct 2024 13:19:41 +0300 Subject: [PATCH] Prepare for the further adding of the gaining setting (to adjust the general volume) --- src/opl/chips/opl_chip_base.h | 1 + src/opl/generator.cpp | 30 ++++++++++++++++++++++++++++-- src/opl/generator.h | 7 +++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/opl/chips/opl_chip_base.h b/src/opl/chips/opl_chip_base.h index 5fc58ba..ead229e 100644 --- a/src/opl/chips/opl_chip_base.h +++ b/src/opl/chips/opl_chip_base.h @@ -71,6 +71,7 @@ class OPLChipBase virtual void nativePreGenerate() = 0; virtual void nativePostGenerate() = 0; virtual void nativeGenerate(int16_t *frame) = 0; + virtual void resampledGenerate(int32_t *frame) = 0; virtual void generate(int16_t *output, size_t frames) = 0; virtual void generateAndMix(int16_t *output, size_t frames) = 0; diff --git a/src/opl/generator.cpp b/src/opl/generator.cpp index 42958f7..3cbca22 100644 --- a/src/opl/generator.cpp +++ b/src/opl/generator.cpp @@ -2009,9 +2009,35 @@ void Generator::updateChannelManager() void Generator::generate(int16_t *frames, unsigned nframes) { chip->generate(frames, nframes); - // 2x Gain by default + // Gain by default for(size_t i = 0; i < nframes * 2; ++i) - frames[i] *= 2; + frames[i] *= m_gain; +} + +void Generator::generate(float* frames, unsigned int nframes) +{ + int32_t out[2]; + + chip->nativePreGenerate(); + + for(unsigned int i = 0; i < nframes; ++i) + { + chip->resampledGenerate(out); + *frames++ = (out[1] / 32767.0f) * m_gain; + *frames++ = (out[2] / 32767.0f) * m_gain; + } + + chip->nativePostGenerate(); +} + +void Generator::setGain(float gain) +{ + m_gain = gain; +} + +float Generator::getGain() const +{ + return m_gain; } Generator::NotesManager::NotesManager() diff --git a/src/opl/generator.h b/src/opl/generator.h index 42cadde..53c7791 100644 --- a/src/opl/generator.h +++ b/src/opl/generator.h @@ -101,6 +101,10 @@ class Generator void switchChip(OPL_Chips chipId); void generate(int16_t *frames, unsigned nframes); + void generate(float *frames, unsigned nframes); + + void setGain(float gain); + float getGain() const; /** * @brief Set the tone frequency on the chip channel and turn note on @@ -243,6 +247,9 @@ class Generator uint32_t m_rate = 44100; + //! Adjust the volume of the output to not be too quite + float m_gain = 2.0f; + struct OPLChipDelete { void operator()(OPLChipBase *); }; std::unique_ptr chip; OPLChipBase::ChipType m_chipType = OPLChipBase::CHIPTYPE_OPL3;