| 1 |
/* |
| 2 |
* This file is part of libsidplayfp, a SID player engine. |
| 3 |
* |
| 4 |
* Copyright 2011-2022 Leandro Nini <[email protected]> |
| 5 |
* Copyright 2007-2010 Antti Lankila |
| 6 |
* Copyright 2004 Dag Lem <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef VOICE_H |
| 24 |
#define VOICE_H |
| 25 |
|
| 26 |
#include <memory> |
| 27 |
|
| 28 |
#include "siddefs-fp.h" |
| 29 |
#include "WaveformGenerator.h" |
| 30 |
#include "EnvelopeGenerator.h" |
| 31 |
|
| 32 |
#include "sidcxx11.h" |
| 33 |
|
| 34 |
namespace reSIDfp |
| 35 |
{ |
| 36 |
|
| 37 |
/** |
| 38 |
* Representation of SID voice block. |
| 39 |
*/ |
| 40 |
class Voice |
| 41 |
{ |
| 42 |
private: |
| 43 |
std::unique_ptr<WaveformGenerator> const waveformGenerator; |
| 44 |
|
| 45 |
std::unique_ptr<EnvelopeGenerator> const envelopeGenerator; |
| 46 |
|
| 47 |
/// The DAC LUT for analog waveform output |
| 48 |
float* wavDAC; //-V730_NOINIT this is initialized in the SID constructor |
| 49 |
|
| 50 |
/// The DAC LUT for analog envelope output |
| 51 |
float* envDAC; //-V730_NOINIT this is initialized in the SID constructor |
| 52 |
|
| 53 |
public: |
| 54 |
/** |
| 55 |
* Amplitude modulated waveform output. |
| 56 |
* |
| 57 |
* The waveform DAC generates a voltage between virtual ground and Vdd |
| 58 |
* (5-12 V for the 6581 and 4.75-9 V for the 8580) |
| 59 |
* corresponding to oscillator state 0 .. 4095. |
| 60 |
* |
| 61 |
* The envelope DAC generates a voltage between waveform gen output and |
| 62 |
* the virtual ground level, corresponding to envelope state 0 .. 255. |
| 63 |
* |
| 64 |
* Ideal range [-2048*255, 2047*255]. |
| 65 |
* |
| 66 |
* @param ringModulator Ring-modulator for waveform |
| 67 |
* @return the voice analog output |
| 68 |
*/ |
| 69 |
RESID_INLINE |
| 70 |
int output(const WaveformGenerator* ringModulator) const |
| 71 |
{ |
| 72 |
unsigned int const wav = waveformGenerator->output(ringModulator); |
| 73 |
unsigned int const env = envelopeGenerator->output(); |
| 74 |
|
| 75 |
// DAC imperfections are emulated by using the digital output |
| 76 |
// as an index into a DAC lookup table. |
| 77 |
return static_cast<int>(wavDAC[wav] * envDAC[env]); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Constructor. |
| 82 |
*/ |
| 83 |
Voice() : |
| 84 |
waveformGenerator(new WaveformGenerator()), |
| 85 |
envelopeGenerator(new EnvelopeGenerator()) {} |
| 86 |
|
| 87 |
/** |
| 88 |
* Set the analog DAC emulation for waveform generator. |
| 89 |
* Must be called before any operation. |
| 90 |
* |
| 91 |
* @param dac |
| 92 |
*/ |
| 93 |
void setWavDAC(float* dac) { wavDAC = dac; } |
| 94 |
|
| 95 |
/** |
| 96 |
* Set the analog DAC emulation for envelope. |
| 97 |
* Must be called before any operation. |
| 98 |
* |
| 99 |
* @param dac |
| 100 |
*/ |
| 101 |
void setEnvDAC(float* dac) { envDAC = dac; } |
| 102 |
|
| 103 |
WaveformGenerator* wave() const { return waveformGenerator.get(); } |
| 104 |
|
| 105 |
EnvelopeGenerator* envelope() const { return envelopeGenerator.get(); } |
| 106 |
|
| 107 |
/** |
| 108 |
* Write control register. |
| 109 |
* |
| 110 |
* @param control Control register value. |
| 111 |
*/ |
| 112 |
void writeCONTROL_REG(unsigned char control) |
| 113 |
{ |
| 114 |
waveformGenerator->writeCONTROL_REG(control); |
| 115 |
envelopeGenerator->writeCONTROL_REG(control); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* SID reset. |
| 120 |
*/ |
| 121 |
void reset() |
| 122 |
{ |
| 123 |
waveformGenerator->reset(); |
| 124 |
envelopeGenerator->reset(); |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
} // namespace reSIDfp |
| 129 |
|
| 130 |
#endif |