mixer.h
/*
* This file is part of libsidplayfp, a SID player engine.
*
* Copyright 2011-2023 Leandro Nini <drfiemost@users.sourceforge.net>
* Copyright 2007-2010 Antti Lankila
* Copyright (C) 2000 Simon White
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef MIXER_H
#define MIXER_H
#include "sidcxx11.h"
#include <stdint.h>
#include <vector>
namespace libsidplayfp
{
class sidemu;
class Mixer
{
private:
// random number generator for dithering
template <int MAX_VAL>
class randomLCG
{
static_assert((MAX_VAL != 0) && ((MAX_VAL & (MAX_VAL - 1)) == 0), "MAX_VAL must be a power of two");
private:
uint32_t rand_seed;
public:
randomLCG(uint32_t seed) :
rand_seed(seed)
{}
int get()
{
rand_seed = (214013 * rand_seed + 2531011);
return static_cast<int>((rand_seed >> 16) & (MAX_VAL-1));
}
};
public:
class badBufferSize {};
public:
static constexpr unsigned int MAX_SIDS = 3;
private:
static constexpr int_least32_t SCALE_FACTOR = 1 << 16;
static constexpr double SQRT_2 = 1.41421356237;
static constexpr double SQRT_3 = 1.73205080757;
static constexpr int_least32_t SCALE[3] = {
SCALE_FACTOR, // 1 chip, no scale
static_cast<int_least32_t>((1.0 / SQRT_2) * SCALE_FACTOR), // 2 chips, scale by sqrt(2)
static_cast<int_least32_t>((1.0 / SQRT_3) * SCALE_FACTOR) // 3 chips, scale by sqrt(3)
};
private:
using mixer_func_t = int_least32_t (Mixer::*)() const;
using scale_func_t = int (Mixer::*)(unsigned int);
public:
static constexpr int_least32_t VOLUME_MAX = 1024;
private:
std::vector<sidemu*> m_chips;
std::vector<short*> m_buffers;
std::vector<int_least32_t> m_iSamples;
std::vector<int_least32_t> m_volume;
std::vector<mixer_func_t> m_mix;
std::vector<scale_func_t> m_scale;
int m_oldRandomValue = 0;
int m_fastForwardFactor = 1;
// Mixer settings
short *m_sampleBuffer = nullptr;
uint_least32_t m_sampleCount = 0;
uint_least32_t m_sampleIndex = 0;
uint_least32_t m_sampleRate = 0;
bool m_stereo = false;
bool m_wait = false;
randomLCG<VOLUME_MAX> m_rand;
private:
void updateParams();
int triangularDithering()
{
const int prevValue = m_oldRandomValue;
m_oldRandomValue = m_rand.get();
return m_oldRandomValue - prevValue;
}
int scale(unsigned int ch)
{
const int_least32_t sample = (this->*(m_mix[ch]))();
return (sample * m_volume[ch] + triangularDithering()) / VOLUME_MAX;
}
int noScale(unsigned int ch)
{
return (this->*(m_mix[ch]))();
}
/*
* Channel matrix
*
* C1
* L 1.0
* R 1.0
*
* C1 C2
* L 1.0 0.5
* R 0.5 1.0
*
* C1 C2 C3
* L 1.0 1.0 0.5
* R 0.5 1.0 1.0
*/
// Mono mixing
template <int Chips>
int_least32_t mono() const
{
int_least32_t res = 0;
for (int i = 0; i < Chips; i++)
res += m_iSamples[i];
return res * SCALE[Chips-1] / SCALE_FACTOR;
}
// Stereo mixing
int_least32_t stereo_OneChip() const { return m_iSamples[0]; }
int_least32_t stereo_ch1_TwoChips() const
{
return (m_iSamples[0] + 0.5*m_iSamples[1]) * SCALE[1] / SCALE_FACTOR;
}
int_least32_t stereo_ch2_TwoChips() const
{
return (0.5*m_iSamples[0] + m_iSamples[1]) * SCALE[1] / SCALE_FACTOR;
}
int_least32_t stereo_ch1_ThreeChips() const
{
return (m_iSamples[0] + m_iSamples[1] + 0.5*m_iSamples[2]) * SCALE[2] / SCALE_FACTOR;
}
int_least32_t stereo_ch2_ThreeChips() const
{
return (0.5*m_iSamples[0] + m_iSamples[1] + m_iSamples[2]) * SCALE[2] / SCALE_FACTOR;
}
public:
Mixer() :
m_rand(257254)
{
m_mix.push_back(&Mixer::mono<1>);
}
void doMix();
void clockChips();
void resetBufs();
void begin(short *buffer, uint_least32_t count);
void clearSids();
void addSid(sidemu *chip);
sidemu* getSid(unsigned int i) const { return (i < m_chips.size()) ? m_chips[i] : nullptr; }
bool setFastForward(int ff);
void setVolume(int_least32_t left, int_least32_t right);
void setStereo(bool stereo);
void setSamplerate(uint_least32_t rate);
bool notFinished() const { return m_sampleIndex < m_sampleCount; }
uint_least32_t samplesGenerated() const { return m_sampleIndex; }
/*
* Wait till we consume the buffer.
*/
bool wait() const { return m_wait; }
};
}
#endif // MIXER_H
libsidplayfp::Mixer::badBufferSize
Definition mixer.h:66
libsidplayfp::Mixer
Definition mixer.h:42
libsidplayfp::Mixer::addSid
void addSid(sidemu *chip)
Definition mixer.cpp:142
libsidplayfp::Mixer::resetBufs
void resetBufs()
Definition mixer.cpp:40
libsidplayfp::Mixer::Mixer
Mixer()
Definition mixer.h:191
libsidplayfp::Mixer::clearSids
void clearSids()
Definition mixer.cpp:136
libsidplayfp::Mixer::notFinished
bool notFinished() const
Definition mixer.h:275
libsidplayfp::Mixer::clockChips
void clockChips()
Definition mixer.cpp:34
libsidplayfp::Mixer::setStereo
void setStereo(bool stereo)
Definition mixer.cpp:156
libsidplayfp::Mixer::setVolume
void setVolume(int_least32_t left, int_least32_t right)
Definition mixer.cpp:182
libsidplayfp::Mixer::VOLUME_MAX
static constexpr int_least32_t VOLUME_MAX
Maximum allowed volume, must be a power of 2.
Definition mixer.h:91
libsidplayfp::Mixer::getSid
sidemu * getSid(unsigned int i) const
Definition mixer.h:240
libsidplayfp::Mixer::samplesGenerated
uint_least32_t samplesGenerated() const
Definition mixer.h:280
libsidplayfp::Mixer::setSamplerate
void setSamplerate(uint_least32_t rate)
Definition mixer.cpp:168
libsidplayfp::Mixer::doMix
void doMix()
Definition mixer.cpp:46
libsidplayfp::Mixer::begin
void begin(short *buffer, uint_least32_t count)
Definition mixer.cpp:102
libsidplayfp::Mixer::MAX_SIDS
static constexpr unsigned int MAX_SIDS
Maximum number of supported SIDs.
Definition mixer.h:70
libsidplayfp::Mixer::setFastForward
bool setFastForward(int ff)
Definition mixer.cpp:173
libsidplayfp::sidemu
Definition sidemu.h:47