-
Notifications
You must be signed in to change notification settings - Fork 1
/
soundplayer.cpp
571 lines (508 loc) · 16.8 KB
/
soundplayer.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//////////////////////////////////////////////////////////////////////////////
// soundplayer --- an fmgon sound player
// Copyright (C) 2015 Katayama Hirofumi MZ. All Rights Reserved.
//////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "soundplayer.h"
#define CLOCK 8000000
#define SAMPLERATE 44100
#define LFO_INTERVAL 150
class VskLFOCtrl {
int m_waveform;
int m_qperiod; // quarter of period
int m_count;
int m_phase; // 0, 1, 2 or 3
float m_adj_p_max;
float m_adj_v_max[4];
float m_adj_p_diff;
float m_adj_v_diff[4];
public:
float m_adj_p; // for pitch
float m_adj_v[4]; // for volume
public:
VskLFOCtrl() {
m_adj_p = 0;
memset(m_adj_v_diff, 0, sizeof(m_adj_v_diff));
}
void init_for_timbre(YM2203_Timbre *p_timbre) {
int i;
m_waveform = p_timbre->waveForm;
if (p_timbre->speed) {
m_qperiod = 900 * LFO_INTERVAL / (4*p_timbre->speed);
} else {
m_qperiod = 0;
}
//m_count = 0;
m_phase = 0;
m_adj_p_max = p_timbre->pmd * (float)p_timbre->pms / 2.0f; // TBD
for (i = 0; i < 4; ++i) {
m_adj_v_max[i] =
p_timbre->amd * (float)p_timbre->ams[i] / 2; // TBD
}
init_for_phase(true);
}
void init_for_keyon(YM2203_Timbre *p_timbre) {
if (p_timbre->sync) {
m_phase = 0;
init_for_phase();
}
}
void increment() {
int i;
if (0 == m_qperiod) {
return;
}
m_count++;
if (m_count < m_qperiod) {
m_adj_p += m_adj_p_diff;
for (i = 0; i < 4; ++i) {
m_adj_v[i] += m_adj_v_diff[i];
}
} else {
m_phase = (m_phase + 1) & 3;
init_for_phase();
}
}
protected:
void init_for_phase(bool flag_first = false) {
int i;
m_count = 0;
if (flag_first) {
switch (m_waveform) {
case 0: // saw
m_adj_p = 0;
for (i = 0; i < 4; ++i) {
m_adj_v[i] = 0;
}
m_adj_p_diff = m_adj_p_max / (m_qperiod * 2);
for (i = 0; i < 4; ++i) {
m_adj_v_diff[i] = m_adj_v_max[i] / (m_qperiod * 2);
}
break;
case 1: // square
m_adj_p = -m_adj_p_max;
for (i = 0; i < 4; ++i) {
m_adj_v[i] = -m_adj_v_max[i];
}
m_adj_p_diff = 0;
for (i = 0; i < 4; ++i) {
m_adj_v_diff[i] = 0;
}
break;
case 2: // triangle
m_adj_p = 0;
for (i = 0; i < 4; ++i) {
m_adj_v[i] = 0;
}
m_adj_p_diff = m_adj_p_max / m_qperiod;
for (i = 0; i < 4; ++i) {
m_adj_v_diff[i] = m_adj_v_max[i] / m_qperiod;
}
break;
default: // sample and hold
//m_adj_p = m_adj_p_max * (rand() * 2.0 / RAND_MAX - 1);
//for (i = 0; i < 4; ++i) {
// m_adj_v[i] = m_adj_v_max[i] * (rand() * 2.0 / RAND_MAX - 1);
//}
m_adj_p_diff = 0;
for (i = 0; i < 4; ++i) {
m_adj_v_diff[i] = 0;
}
break;
}
}
switch (m_waveform) {
case 0: // saw
if (0 == m_phase) {
m_adj_p = 0;
for (i = 0; i < 4; ++i) {
m_adj_v[i] = 0;
}
} else if (2 == m_phase) {
m_adj_p = -m_adj_p;
for (i = 0; i < 4; ++i) {
m_adj_v[i] = -m_adj_v[i];
}
}
break;
case 1: // square
if (0 == (m_phase & 1)) {
m_adj_p = -m_adj_p;
for (i = 0; i < 4; ++i) {
m_adj_v[i] = -m_adj_v[i];
}
}
break;
case 2: // triangle
if (0 == m_phase) {
m_adj_p = 0;
for (i = 0; i < 4; ++i) {
m_adj_v[i] = 0;
}
} else if (1 == (m_phase & 1)) {
m_adj_p_diff = -m_adj_p_diff;
for (i = 0; i < 4; ++i) {
m_adj_v_diff[i] = -m_adj_v_diff[i];
}
}
break;
default: // sample and hold
if (0 == (m_phase & 1)) {
m_adj_p = float(
m_adj_p_max * (rand() * 2.0 / RAND_MAX - 1)
);
for (i = 0; i < 4; ++i) {
m_adj_v[i] = float(
m_adj_v_max[i] * (rand() * 2.0 / RAND_MAX - 1)
);
}
}
break;
}
}
}; // class VskLFOCtrl
//////////////////////////////////////////////////////////////////////////////
float VskNote::get_sec(int tempo, float length) const {
float sec;
assert(tempo != 0);
// NOTE: 24 is the length of a quarter note
if (m_dot) {
sec = length * (60.0f * 1.5f / 24.0f) / tempo;
} else {
sec = length * (60.0f / 24.0f) / tempo;
}
return sec;
} // VskNote::get_sec
void VskNote::set_key_from_char(char ch) {
if ((ch != 'R') && (ch != 0)) {
static const char keys[KEY_NUM + 1] = "C+D+EF+G+A+B";
const char *ptr = strchr(keys, ch);
assert(ptr != NULL);
assert(*ptr == ch);
m_key = int(ptr - keys);
switch (m_sign) {
case '+': case '#':
if (m_key == KEY_B) {
m_key = KEY_C;
} else {
++m_key;
}
break;
case '-':
if (m_key == KEY_C) {
m_key = KEY_B;
} else {
--m_key;
}
break;
default:
break;
}
} else {
m_key = -1;
}
} // VskNote::char_to_key
//////////////////////////////////////////////////////////////////////////////
void VskPhrase::destroy() {
if (m_buffer != ALuint(-1)) {
alDeleteBuffers(1, &m_buffer);
m_buffer = ALuint(-1);
}
if (m_source != ALuint(-1)) {
alDeleteSources(1, &m_source);
m_source = ALuint(-1);
}
} // VskPhrase::destroy
void VskPhrase::rescan_notes() {
std::vector<VskNote> new_notes;
for (size_t i = 0; i < m_notes.size(); ++i) {
if (m_notes[i].m_and) {
size_t k = 0;
float length = 0, sec = 0;
do {
length += m_notes[i + k].m_length;
sec += m_notes[i + k].m_sec;
++k;
} while (m_notes[i + k].m_and);
length += m_notes[i + k].m_length;
sec += m_notes[i + k].m_sec;
m_notes[i].m_length = length;
m_notes[i].m_sec = sec;
new_notes.push_back(m_notes[i]);
i += k;
} else {
new_notes.push_back(m_notes[i]);
}
}
m_notes = std::move(new_notes);
} // VskPhrase::rescan_notes
void VskPhrase::calc_total() {
float gate = 0;
for (auto& note : m_notes) {
note.m_gate = gate;
gate += note.m_sec;
}
m_goal = gate;
} // VskPhrase::calc_total
void VskPhrase::realize(VskSoundPlayer *player) {
destroy();
calc_total();
rescan_notes();
// initialize YM2203
YM2203& ym = player->m_ym;
ym.init(CLOCK, SAMPLERATE);
ym.reset();
// create wave data
uint32_t isample = 0;
auto size = uint32_t((m_goal + 1) * SAMPLERATE * 2);
unique_ptr<FM_SAMPLETYPE[]> data(new FM_SAMPLETYPE[size]);
memset(&data[0], 0, size * sizeof(FM_SAMPLETYPE));
if (m_setting.m_fm) {
int ch = FM_CH1;
int tone = -1;
VskLFOCtrl lc;
for (auto& note : m_notes) {
// do key on
auto& timbre = m_setting.m_timbre;
if (note.m_key != -1) {
// change tone if necessary
if (tone != note.m_tone) {
const auto new_tone = note.m_tone;
assert((0 <= new_tone) && (new_tone < NUM_TONES));
timbre.set(ym2203_tone_table[new_tone]);
ym.set_timbre(ch, &timbre);
lc.init_for_timbre(&timbre);
tone = new_tone;
}
ym.set_pitch(ch, note.m_octave, note.m_key);
ym.set_volume(ch, int(note.m_volume));
ym.note_on(ch);
}
lc.init_for_keyon(&timbre);
// render sound
auto sec = note.m_sec * note.m_quantity / 8.0f;
auto nsamples = int(SAMPLERATE * sec);
int unit;
while (nsamples) {
unit = SAMPLERATE / LFO_INTERVAL;
if (unit > nsamples) {
unit = nsamples;
}
ym.mix(&data[isample * 2], unit);
isample += unit;
if (note.m_key != -1) {
lc.increment();
int adj[4] = {
int(lc.m_adj_v[0]), int(lc.m_adj_v[1]),
int(lc.m_adj_v[2]), int(lc.m_adj_v[3]),
};
ym.set_volume(ch, int(note.m_volume), adj);
ym.set_pitch(ch, note.m_octave, note.m_key,
int(lc.m_adj_p));
}
nsamples -= unit;
}
ym.count(uint32_t(sec * 1000 * 1000));
isample += nsamples;
// do key off
sec = note.m_sec * (8.0f - note.m_quantity) / 8.0f;
nsamples = int(SAMPLERATE * sec);
ym.note_off(ch);
unit = SAMPLERATE;
if (unit > nsamples) {
unit = nsamples;
}
ym.mix(&data[isample * 2], unit);
ym.count(uint32_t(sec * 1000 * 1000));
isample += nsamples;
}
} else {
int ch = SSG_CH_A;
ym.set_tone_or_noise(ch, TONE_MODE);
for (auto& note : m_notes) {
// do key on
if (note.m_key != -1) {
ym.set_pitch(ch, note.m_octave, note.m_key);
ym.set_volume(ch, int(note.m_volume));
ym.note_on(ch);
}
// render sound
auto sec = note.m_sec * note.m_quantity / 8.0f;
auto nsamples = int(SAMPLERATE * sec);
ym.mix(&data[isample * 2], nsamples);
ym.count(uint32_t(sec * 1000 * 1000));
isample += nsamples;
// do key off
sec = note.m_sec * (8.0f - note.m_quantity) / 8.0f;
nsamples = int(SAMPLERATE * sec);
ym.note_off(ch);
ym.mix(&data[isample * 2], nsamples);
ym.count(uint32_t(sec * 1000 * 1000));
isample += nsamples;
}
}
// reverb of 1sec
{
auto sec = 1;
auto nsamples = int(SAMPLERATE * sec);
ym.mix(&data[isample * 2], nsamples);
ym.count(uint32_t(sec * 1000 * 1000));
}
// generate an OpenAL buffer
alGenBuffers(1, &m_buffer);
assert(m_buffer != ALuint(-1));
alBufferData(m_buffer, AL_FORMAT_STEREO16, &data[0],
sizeof(FM_SAMPLETYPE) * size, SAMPLERATE);
// generate an OpenAL source
alGenSources(1, &m_source);
assert(m_source != ALuint(-1));
alSourcei(m_source, AL_BUFFER, m_buffer);
} // VskPhrase::realize
//////////////////////////////////////////////////////////////////////////////
/*static*/ int VskSoundPlayer::m_next_async_sound_id = 0;
bool VskSoundPlayer::wait_for_stop(uint32_t milliseconds) {
return m_stopping_event.wait_for_event(milliseconds);
}
bool VskSoundPlayer::play_and_wait(VskScoreBlock& block, uint32_t milliseconds) {
play(block);
return wait_for_stop(milliseconds);
}
void VskSoundPlayer::play(VskScoreBlock& block) {
for (auto& phrase : block) {
if (phrase) {
phrase->realize(this);
}
}
m_play_lock.lock();
m_melody_line.push_back(block);
m_play_lock.unlock();
if (m_playing_music) {
return;
}
m_playing_music = false;
m_stopping_event.pulse();
m_playing_music = true;
std::thread(
[this](int dummy) {
VskScoreBlock phrases;
for (;;) {
// get the next block
m_play_lock.lock();
if (m_melody_line.empty()) {
m_play_lock.unlock();
m_stopping_event.pulse();
break;
}
phrases = m_melody_line.front();
m_melody_line.pop_front();
m_play_lock.unlock();
// get the goal
float goal = 0;
for (auto& phrase : phrases) {
if (phrase) {
if (goal < phrase->m_goal) {
goal = phrase->m_goal;
}
}
}
// play phrases
for (auto& phrase : phrases) {
if (phrase) {
alSourcePlay(phrase->m_source);
}
}
auto msec = uint32_t(goal * 1000.0);
m_stopping_event.wait_for_event(msec);
}
if (m_playing_music) {
m_playing_music = false;
alutSleep(1.0);
}
},
0
).detach();
} // VskSoundPlayer::play
void VskSoundPlayer::stop() {
m_playing_music = false;
m_stopping_event.pulse();
m_play_lock.lock();
m_melody_line.clear();
m_play_lock.unlock();
m_play_async_lock.lock();
m_async_sound_map.clear();
m_play_async_lock.unlock();
} // VskSoundPlayer::stop
void VskSoundPlayer::play_async(VskScoreBlock& block) {
for (auto& phrase : block) {
if (phrase) {
phrase->realize(this);
}
}
std::thread(
[this, block](int dummy) {
int id;
m_play_async_lock.lock();
id = m_next_async_sound_id++;
m_async_sound_map[id] = block;
m_play_async_lock.unlock();
// get the goal
float goal = 0;
for (auto& phrase : block) {
if (phrase) {
if (goal < phrase->m_goal) {
goal = phrase->m_goal;
}
}
}
// play phrases
for (auto& phrase : block) {
if (phrase) {
alSourcePlay(phrase->m_source);
}
}
auto msec = uint32_t(goal * 1000.0);
if (m_stopping_event.wait_for_event(msec)) {
alutSleep(1.0);
}
m_play_async_lock.lock();
m_async_sound_map.erase(id);
m_play_async_lock.unlock();
},
0
).detach();
} // VskSoundPlayer::play_async
//////////////////////////////////////////////////////////////////////////////
// beep
void VskSoundPlayer::init_beep() {
m_beep_buffer = alutCreateBufferWaveform(
ALUT_WAVEFORM_SINE,
2400,
0,
0.5
);
//m_beep_buffer = alutCreateBufferFromFile("beep.wav");
alGenSources(1, &m_beep_source);
alSourcei(m_beep_source, AL_BUFFER, m_beep_buffer);
} // VskSoundPlayer::init_beep
void VskSoundPlayer::beep(int i) {
switch (i) {
case -1:
alSourceStop(m_beep_source);
alSourcei(m_beep_source, AL_LOOPING, AL_FALSE);
alSourcePlay(m_beep_source);
break;
case 0:
alSourceStop(m_beep_source);
alSourcei(m_beep_source, AL_LOOPING, AL_FALSE);
break;
case 1:
alSourceStop(m_beep_source);
alSourcei(m_beep_source, AL_LOOPING, AL_TRUE);
alSourcePlay(m_beep_source);
}
} // VskSoundPlayer::beep
void VskSoundPlayer::free_beep() {
alDeleteBuffers(1, &m_beep_buffer);
alDeleteSources(1, &m_beep_source);
}
//////////////////////////////////////////////////////////////////////////////