-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOPL3.h
117 lines (90 loc) · 2.38 KB
/
OPL3.h
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
//----------------------------------------------------------------------------------------------------------------------
// OPL3 emulation for QB64-PE using Opal
// Copyright (c) 2024 Samuel Gomes
//----------------------------------------------------------------------------------------------------------------------
#pragma once
#include "Types.h"
#include "external/opal.h"
#include <memory>
class OPL3
{
private:
std::unique_ptr<Opal> chip;
uint32_t sampleRate;
public:
OPL3(uint32_t sampleRate)
{
this->sampleRate = sampleRate;
Reset();
}
~OPL3()
{
chip.reset();
}
void Reset()
{
chip.reset();
chip = std::make_unique<Opal>(sampleRate);
}
uint32_t GetSampleRate() const { return sampleRate; }
void WriteRegister(uint16_t address, uint8_t data)
{
chip->Port(address, data);
}
void GenerateSamples(float *buffer, uint32_t frames)
{
static constexpr float normalization_factor = 1.0f / 32768.0f;
std::pair<int16_t, int16_t> output;
uint32_t sample = 0;
while (sample < frames * 2)
{
chip->Sample(&output.first, &output.second);
buffer[sample] = output.first * normalization_factor;
buffer[sample + 1] = output.second * normalization_factor;
sample += 2;
}
}
OPL3() = delete;
OPL3(const OPL3 &) = delete;
OPL3(OPL3 &&) = delete;
OPL3 &operator=(const OPL3 &) = delete;
OPL3 &operator=(OPL3 &&) = delete;
};
static std::unique_ptr<OPL3> g_OPL3Chip;
inline qb_bool __OPL3_Initialize(uint32_t sampleRate)
{
if (g_OPL3Chip)
return QB_TRUE;
if (!sampleRate)
return QB_FALSE;
g_OPL3Chip = std::make_unique<OPL3>(sampleRate);
return TO_QB_BOOL(g_OPL3Chip != nullptr);
}
inline void __OPL3_Finalize()
{
if (!g_OPL3Chip)
return;
g_OPL3Chip.reset();
}
inline qb_bool OPL3_IsInitialized()
{
return TO_QB_BOOL(g_OPL3Chip != nullptr);
}
inline void OPL3_Reset()
{
if (!g_OPL3Chip)
return;
g_OPL3Chip->Reset();
}
inline void OPL3_WriteRegister(uint16_t address, uint8_t data)
{
if (!g_OPL3Chip)
return;
g_OPL3Chip->WriteRegister(address, data);
}
inline void __OPL3_GenerateSamples(float *buffer, uint32_t frames)
{
if (!g_OPL3Chip)
return;
g_OPL3Chip->GenerateSamples(buffer, frames);
}