-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
207 lines (173 loc) · 4.8 KB
/
main.cpp
File metadata and controls
207 lines (173 loc) · 4.8 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdio.h>
#include <string>
#include "streamingmgr.h"
// 16bit整数のPSG
class PSG16 {
private:
// 波形の精度を上げる為の嵩まし分
static const int freq_multiple = 128;
// 音量 最大が 1.0f
float left_volume;
float right_volume;
int16_t cur_lvol;
int16_t cur_rvol;
int base_hz;
int base_hz_mult;
int wavetype; // 波形タイプ(On/Off比) 1-15
float freq;
int cur_freq;
int wave_cnt;
int wave_cnt_h; // h区間のカウンタ(コレを超えるとlになる)
int waveindex; // 波形index 1-16
bool isKeyOn;
public:
// 引数:outputhz = 出力周波数
PSG16(int outputhz) : base_hz(outputhz), left_volume(0.0f), right_volume(0.0f), wavetype(8), freq(440.0f), wave_cnt(0), waveindex(1), isKeyOn(false)
{
this->base_hz_mult = base_hz * PSG16::freq_multiple;
this->cur_lvol = this->left_volume * 32767.0f;
this->cur_rvol = this->right_volume * 32767.0f;
this->cur_freq = this->freq * PSG16::freq_multiple;
}
// 周波数設定
void SetFreq(float hz)
{
if (hz * 2 >= base_hz) {
// 値域オーバーなので何もしない
return;
}
this->freq = hz;
this->cur_freq = this->freq;
}
// 音量設定
void SetVolume(float leftv, float rightv)
{
this->left_volume = leftv;
this->right_volume = rightv;
this->cur_lvol = this->left_volume * 32767.0f;
this->cur_rvol = this->right_volume * 32767.0f;
}
// KeyOn
void SetKeyOn()
{
this->waveindex = 1; // 波形位置を先頭にリセット
this->wave_cnt = 0;
this->isKeyOn = true;
}
// KeyOff
void SetKeyOff()
{
this->isKeyOn = false;
}
// 波形指定
// 引数 : wtype 1-15
void SetWaveType(int wtype)
{
if (wtype < 1 || wtype > 15) return;
this->wavetype = wtype;
this->wave_cnt_h = this->base_hz * this->wavetype / 16;
}
// 波形生成
void MakeWave(int16_t* buffer, int samples)
{
if (isKeyOn == false) {
// キーオンしてないので無音
for (int _i = 0; _i < samples; _i++) {
buffer[_i * 2 + 0] = 0x0000;
buffer[_i * 2 + 1] = 0x0000;
}
}
else {
for (int _i = 0; _i < samples; _i++) {
this->wave_cnt += this->cur_freq;
if (this->wave_cnt >= this->base_hz) this->wave_cnt -= this->base_hz;
// 出力
if (this->wave_cnt < this->wave_cnt_h)
{
buffer[_i * 2 + 0] = this->cur_lvol;
buffer[_i * 2 + 1] = this->cur_rvol;
}
else {
buffer[_i * 2 + 0] = 0 - this->cur_lvol;
buffer[_i * 2 + 1] = 0 - this->cur_rvol;
}
}
}
}
};
PSG16* psg = nullptr;
void wavecallbackfunc(void* buf, int samples)
{
//LOG_INFO(logger, "wavecallback {0:d}samples.", samples);
int16_t* pbuf = (int16_t*)buf;
psg->MakeWave(pbuf,samples);
}
// 440 * 2^((x-o4a)/12)
quill::Logger* logger = nullptr;
int main()
{
quill::Backend::start();
auto console_sink = quill::Frontend::create_or_get_sink<quill::ConsoleSink>("sink_id_1");
logger = quill::Frontend::create_or_get_logger("root", std::move(console_sink));
// テスト
bool r = Initialize_STMGR();
LOG_INFO(logger, "return {}", r);
psg = new PSG16(48000);
WAVEFORMATEX w{};
w.wFormatTag = WAVE_FORMAT_PCM;
w.nChannels = 2;
w.nSamplesPerSec = 48000;
w.wBitsPerSample = 16;
w.nBlockAlign = w.wBitsPerSample * w.nChannels / 8;
w.nAvgBytesPerSec = w.nSamplesPerSec * w.nBlockAlign;
std::function<void(void*, int)> cbf = wavecallbackfunc;
int ch = MakeChannel_STMGR(w, w.nSamplesPerSec / 100, cbf);
LOG_INFO(logger, "ch {}", ch);
psg->SetFreq(440.0 * std::pow(2.0f, (0.0-9.0) / 12));
psg->SetVolume(0.7f, 0.3f);
psg->SetWaveType(8);
psg->SetKeyOn();
r = PlayChannel_STMGR(ch);
LOG_INFO(logger, "play = {}", r);
Sleep(500);
//psg->SetFreq(440.0 * std::pow(2.0f, 1.0 / 12));
//psg->SetVolume(0.3f, 0.7f);
//Sleep(500);
psg->SetFreq(440.0 * std::pow(2.0f, (2.0 - 9.0) / 12));
psg->SetVolume(0.7f, 0.3f);
Sleep(500);
//psg->SetFreq(440.0 * std::pow(2.0f, 3.0 / 12));
//psg->SetVolume(0.3f, 0.7f);
//Sleep(500);
psg->SetFreq(440.0 * std::pow(2.0f, (4.0 - 9.0) / 12));
psg->SetVolume(0.7f, 0.3f);
Sleep(500);
psg->SetFreq(440.0 * std::pow(2.0f, (5.0 - 9.0) / 12));
psg->SetVolume(0.3f, 0.7f);
Sleep(500);
//psg->SetFreq(440.0 * std::pow(2.0f, 6.0 / 12));
//psg->SetVolume(0.7f, 0.3f);
//Sleep(500);
psg->SetFreq(440.0 * std::pow(2.0f, (7.0 - 9.0) / 12));
psg->SetVolume(0.3f, 0.7f);
Sleep(500);
//psg->SetFreq(440.0 * std::pow(2.0f, 8.0 / 12));
//psg->SetVolume(0.7f, 0.3f);
//Sleep(500);
psg->SetFreq(440.0 * std::pow(2.0f, (9.0 - 9.0) / 12));
psg->SetVolume(0.3f, 0.7f);
Sleep(500);
//psg->SetFreq(440.0 * std::pow(2.0f, 10.0 / 12));
//psg->SetVolume(0.7f, 0.3f);
//Sleep(500);
psg->SetFreq(440.0 * std::pow(2.0f, (11.0 - 9.0) / 12));
psg->SetVolume(0.3f, 0.7f);
Sleep(500);
psg->SetFreq(440.0 * std::pow(2.0f, (12.0 - 9.0) / 12));
psg->SetVolume(0.3f, 0.7f);
Sleep(500);
StopChannel_STMGR(ch);
Release_STMGR();
delete psg;
return 0;
}