aboutsummaryrefslogtreecommitdiff
path: root/src/chips/nuked_opl3_v174.cpp
blob: 675e104c2aafdadec66256556ab5d8e70b6f265b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "nuked_opl3_v174.h"
#include "nuked/nukedopl3_174.h"
#include <cstring>
#include <cmath>

#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
#include <zita-resampler/vresampler.h>
#endif

NukedOPL3v174::NukedOPL3v174() :
    OPLChipBase()
{
    m_chip = new opl3_chip;
#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
    m_resampler = new VResampler;
#endif
    reset(m_rate);
}

NukedOPL3v174::~NukedOPL3v174()
{
    opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
    delete chip_r;
#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
    delete m_resampler;
#endif
}

void NukedOPL3v174::setRate(uint32_t rate)
{
    OPLChipBase::setRate(rate);
    opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
    std::memset(chip_r, 0, sizeof(opl3_chip));
    OPL3v17_Reset(chip_r, rate);
#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
    m_resampler->setup(rate * (1.0 / 49716), 2, 48);
#endif
}

void NukedOPL3v174::reset()
{
    setRate(m_rate);
}

void NukedOPL3v174::reset(uint32_t rate)
{
    setRate(rate);
}

void NukedOPL3v174::writeReg(uint16_t addr, uint8_t data)
{
    opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
    OPL3v17_WriteReg(chip_r, addr, data);
}

int NukedOPL3v174::generate(int16_t *output, size_t frames)
{
#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
    for(size_t i = 0; i < frames; ++i)
    {
        generateResampledHq(output);
        output += 2;
    }
#else
    opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
    OPL3v17_GenerateStream(chip_r, output, (Bit32u)frames);
#endif
    return (int)frames;
}

int NukedOPL3v174::generateAndMix(int16_t *output, size_t frames)
{
#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
    for(size_t i = 0; i < frames; ++i)
    {
        int16_t frame[2];
        generateResampledHq(frame);
        output[0] += (int32_t)frame[0];
        output[1] += (int32_t)frame[1];
        output += 2;
    }
#else
    opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
    OPL3v17_GenerateStreamMix(chip_r, output, (Bit32u)frames);
#endif
    return (int)frames;
}

int NukedOPL3v174::generate32(int32_t *output, size_t frames)
{
    opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
    for(size_t i = 0; i < frames; ++i) {
#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
        (void)chip_r;
        generateResampledHq32(output);
#else
        int16_t frame[2];
        OPL3v17_GenerateResampled(chip_r, frame);
        output[0] = (int32_t)frame[0];
        output[1] = (int32_t)frame[1];
#endif
        output += 2;
    }
    return (int)frames;
}

int NukedOPL3v174::generateAndMix32(int32_t *output, size_t frames)
{
    opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
    for(size_t i = 0; i < frames; ++i) {
#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
        (void)chip_r;
        int32_t frame[2];
        generateResampledHq32(frame);
#else
        int16_t frame[2];
        OPL3v17_GenerateResampled(chip_r, frame);
#endif
        output[0] += (int32_t)frame[0];
        output[1] += (int32_t)frame[1];
        output += 2;
    }
    return (int)frames;
}

#if defined(ADLMIDI_ENABLE_HQ_RESAMPLER)
void NukedOPL3v174::generateResampledHq(int16_t *out)
{
    int32_t temps[2];
    generateResampledHq32(temps);
    for(unsigned i = 0; i < 2; ++i)
    {
        int32_t temp = temps[i];
        temp = (temp > -32768) ? temp : -32768;
        temp = (temp < 32767) ? temp : 32767;
        out[i] = temp;
    }
}

void NukedOPL3v174::generateResampledHq32(int32_t *out)
{
    opl3_chip *chip_r = reinterpret_cast<opl3_chip*>(m_chip);
    VResampler *rsm = m_resampler;
    float f_in[2];
    float f_out[2];
    rsm->inp_count = 0;
    rsm->inp_data = f_in;
    rsm->out_count = 1;
    rsm->out_data = f_out;
    while(rsm->process(), rsm->out_count != 0)
    {
        int16_t in[2];
        OPL3v17_Generate(chip_r, in);
        f_in[0] = (float)in[0], f_in[1] = (float)in[1];
        rsm->inp_count = 1;
        rsm->inp_data = f_in;
        rsm->out_count = 1;
        rsm->out_data = f_out;
    }
    out[0] = std::lround(f_out[0]);
    out[1] = std::lround(f_out[1]);
}
#endif

const char *NukedOPL3v174::emulatorName()
{
    return "Nuked OPL3 (v 1.7.4)";
}