-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathOneLoneCoder_PGE_MIDI.cpp
More file actions
739 lines (620 loc) · 19.7 KB
/
Copy pathOneLoneCoder_PGE_MIDI.cpp
File metadata and controls
739 lines (620 loc) · 19.7 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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*
Programming MIDI: Parsing, Displaying (& Playing) MIDI Files
"Better get these done before im virused..." - javidx9
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2018-2020 OneLoneCoder.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions or derivations of source code must retain the above
copyright notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce
the above copyright notice. This list of conditions and the following
disclaimer must be reproduced in the documentation and/or other
materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Relevant Video: https://youtu.be/040BKtnDdg0
Links
~~~~~
YouTube: https://www.youtube.com/javidx9
https://www.youtube.com/javidx9extra
Discord: https://discord.gg/WhwHUMV
Twitter: https://www.twitter.com/javidx9
Twitch: https://www.twitch.tv/javidx9
GitHub: https://www.github.com/onelonecoder
Patreon: https://www.patreon.com/javidx9
Homepage: https://www.onelonecoder.com
Community: https://community.onelonecoder.com
Author
~~~~~~
David Barr, aka javidx9, �OneLoneCoder 2018, 2019, 2020
*/
/*
On Linux need to install fluidsynth, compile with -lfluidsynth,
and add a midi font file `midi_font.sf2`.
https://github.com/FluidSynth/fluidsynth
Install fluidsynth (default location is /usr/local)
```
wget https://github.com/FluidSynth/fluidsynth/archive/refs/tags/v2.3.2.zip
unzip v2.3.2.zip
cd fluidsynth-2.3.2
mkdir -p build
cd build
cmake ..
cmake --build .
cmake --install .
```
Compile with `-lfluidsynth`
```
g++ -o midi OneLoneCoder_PGE_MIDI.cpp -lfluidsynth -I../BiggerProjects/CarCrimeCity/Part2 -lGL -lpng -lX11 -lpthread -lstdc++fs -std=c++17
```
Download a MIDI Font (there are many online), or install `vlc-plugin-fluidsynth`, it adds a default.sf2 at `/usr/share/sounds/sf2/default-GM.sf2`
```
sudo apt install vlc-plugin-fluidsynth
```
Once you have `ff7_battle.mid` and `midi_font.sf2`, just run
```
./midi
```
*/
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
#include <fstream>
#include <array>
#ifdef WIN32
//#pragma comment(lib, "winmm.lib")
#else
#include <fluidsynth.h>
#endif
struct MidiEvent
{
enum class Type
{
NoteOff,
NoteOn,
Other
} event;
uint8_t nKey = 0;
uint8_t nVelocity = 0;
uint32_t nDeltaTick = 0;
};
struct MidiNote
{
uint8_t nKey = 0;
uint8_t nVelocity = 0;
uint32_t nStartTime = 0;
uint32_t nDuration = 0;
};
struct MidiTrack
{
std::string sName;
std::string sInstrument;
std::vector<MidiEvent> vecEvents;
std::vector<MidiNote> vecNotes;
uint8_t nMaxNote = 64;
uint8_t nMinNote = 64;
};
class MidiFile
{
public:
enum EventName : uint8_t
{
VoiceNoteOff = 0x80,
VoiceNoteOn = 0x90,
VoiceAftertouch = 0xA0,
VoiceControlChange = 0xB0,
VoiceProgramChange = 0xC0,
VoiceChannelPressure = 0xD0,
VoicePitchBend = 0xE0,
SystemExclusive = 0xF0,
};
enum MetaEventName : uint8_t
{
MetaSequence = 0x00,
MetaText = 0x01,
MetaCopyright = 0x02,
MetaTrackName = 0x03,
MetaInstrumentName = 0x04,
MetaLyrics = 0x05,
MetaMarker = 0x06,
MetaCuePoint = 0x07,
MetaChannelPrefix = 0x20,
MetaEndOfTrack = 0x2F,
MetaSetTempo = 0x51,
MetaSMPTEOffset = 0x54,
MetaTimeSignature = 0x58,
MetaKeySignature = 0x59,
MetaSequencerSpecific = 0x7F,
};
public:
MidiFile()
{
}
MidiFile(const std::string& sFileName)
{
ParseFile(sFileName);
}
void Clear()
{
}
bool ParseFile(const std::string& sFileName)
{
// Open the MIDI File as a stream
std::ifstream ifs;
ifs.open(sFileName, std::fstream::in | std::ios::binary);
if (!ifs.is_open())
return false;
// Helper Utilities ====================
// Swaps byte order of 32-bit integer
auto Swap32 = [](uint32_t n)
{
return (((n >> 24) & 0xff) | ((n << 8) & 0xff0000) | ((n >> 8) & 0xff00) | ((n << 24) & 0xff000000));
};
// Swaps byte order of 16-bit integer
auto Swap16 = [](uint16_t n)
{
return ((n >> 8) | (n << 8));
};
// Reads nLength bytes form file stream, and constructs a text string
auto ReadString = [&ifs](uint32_t nLength)
{
std::string s;
for (uint32_t i = 0; i < nLength; i++) s += ifs.get();
return s;
};
// Reads a compressed MIDI value. This can be up to 32 bits long. Essentially if the first byte, first
// bit is set to 1, that indicates that the next byte is required to construct the full word. Only
// the bottom 7 bits of each byte are used to construct the final word value. Each successive byte
// that has MSB set, indicates a further byte needs to be read.
auto ReadValue = [&ifs]()
{
uint32_t nValue = 0;
uint8_t nByte = 0;
// Read byte
nValue = ifs.get();
// Check MSB, if set, more bytes need reading
if (nValue & 0x80)
{
// Extract bottom 7 bits of read byte
nValue &= 0x7F;
do
{
// Read next byte
nByte = ifs.get();
// Construct value by setting bottom 7 bits, then shifting 7 bits
nValue = (nValue << 7) | (nByte & 0x7F);
}
while (nByte & 0x80); // Loop whilst read byte MSB is 1
}
// Return final construction (always 32-bit unsigned integer internally)
return nValue;
};
uint32_t n32 = 0;
uint16_t n16 = 0;
// Read MIDI Header (Fixed Size)
ifs.read((char*)&n32, sizeof(uint32_t));
uint32_t nFileID = Swap32(n32);
ifs.read((char*)&n32, sizeof(uint32_t));
uint32_t nHeaderLength = Swap32(n32);
ifs.read((char*)&n16, sizeof(uint16_t));
uint16_t nFormat = Swap16(n16);
ifs.read((char*)&n16, sizeof(uint16_t));
uint16_t nTrackChunks = Swap16(n16);
ifs.read((char*)&n16, sizeof(uint16_t));
uint16_t nDivision = Swap16(n16);
for (uint16_t nChunk = 0; nChunk < nTrackChunks; nChunk++)
{
std::cout << "===== NEW TRACK" << std::endl;
// Read Track Header
ifs.read((char*)&n32, sizeof(uint32_t));
uint32_t nTrackID = Swap32(n32);
ifs.read((char*)&n32, sizeof(uint32_t));
uint32_t nTrackLength = Swap32(n32);
bool bEndOfTrack = false;
vecTracks.push_back(MidiTrack());
uint32_t nWallTime = 0;
uint8_t nPreviousStatus = 0;
while (!ifs.eof() && !bEndOfTrack)
{
// Fundamentally all MIDI Events contain a timecode, and a status byte*
uint32_t nStatusTimeDelta = 0;
uint8_t nStatus = 0;
// Read Timecode from MIDI stream. This could be variable in length
// and is the delta in "ticks" from the previous event. Of course this value
// could be 0 if two events happen simultaneously.
nStatusTimeDelta = ReadValue();
// Read first byte of message, this could be the status byte, or it could not...
nStatus = ifs.get();
// All MIDI Status events have the MSB set. The data within a standard MIDI event
// does not. A crude yet utilised form of compression is to omit sending status
// bytes if the following sequence of events all refer to the same MIDI Status.
// This is called MIDI Running Status, and is essential to succesful decoding of
// MIDI streams and files.
//
// If the MSB of the read byte was not set, and on the whole we were expecting a
// status byte, then Running Status is in effect, so we refer to the previous
// confirmed status byte.
if (nStatus < 0x80)
{
// MIDI Running Status is happening, so refer to previous valid MIDI Status byte
nStatus = nPreviousStatus;
// We had to read the byte to assess if MIDI Running Status is in effect. But!
// that read removed the byte form the stream, and that will desync all of the
// following code because normally we would have read a status byte, but instead
// we have read the data contained within a MIDI message. The simple solution is
// to put the byte back :P
ifs.seekg(-1, std::ios_base::cur);
}
if ((nStatus & 0xF0) == EventName::VoiceNoteOff)
{
nPreviousStatus = nStatus;
uint8_t nChannel = nStatus & 0x0F;
uint8_t nNoteID = ifs.get();
uint8_t nNoteVelocity = ifs.get();
vecTracks[nChunk].vecEvents.push_back({ MidiEvent::Type::NoteOff, nNoteID, nNoteVelocity, nStatusTimeDelta });
}
else if ((nStatus & 0xF0) == EventName::VoiceNoteOn)
{
nPreviousStatus = nStatus;
uint8_t nChannel = nStatus & 0x0F;
uint8_t nNoteID = ifs.get();
uint8_t nNoteVelocity = ifs.get();
if(nNoteVelocity == 0)
vecTracks[nChunk].vecEvents.push_back({ MidiEvent::Type::NoteOff, nNoteID, nNoteVelocity, nStatusTimeDelta });
else
vecTracks[nChunk].vecEvents.push_back({ MidiEvent::Type::NoteOn, nNoteID, nNoteVelocity, nStatusTimeDelta });
}
else if ((nStatus & 0xF0) == EventName::VoiceAftertouch)
{
nPreviousStatus = nStatus;
uint8_t nChannel = nStatus & 0x0F;
uint8_t nNoteID = ifs.get();
uint8_t nNoteVelocity = ifs.get();
vecTracks[nChunk].vecEvents.push_back({ MidiEvent::Type::Other });
}
else if ((nStatus & 0xF0) == EventName::VoiceControlChange)
{
nPreviousStatus = nStatus;
uint8_t nChannel = nStatus & 0x0F;
uint8_t nControlID = ifs.get();
uint8_t nControlValue = ifs.get();
vecTracks[nChunk].vecEvents.push_back({ MidiEvent::Type::Other });
}
else if ((nStatus & 0xF0) == EventName::VoiceProgramChange)
{
nPreviousStatus = nStatus;
uint8_t nChannel = nStatus & 0x0F;
uint8_t nProgramID = ifs.get();
vecTracks[nChunk].vecEvents.push_back({ MidiEvent::Type::Other });
}
else if ((nStatus & 0xF0) == EventName::VoiceChannelPressure)
{
nPreviousStatus = nStatus;
uint8_t nChannel = nStatus & 0x0F;
uint8_t nChannelPressure = ifs.get();
vecTracks[nChunk].vecEvents.push_back({ MidiEvent::Type::Other });
}
else if ((nStatus & 0xF0) == EventName::VoicePitchBend)
{
nPreviousStatus = nStatus;
uint8_t nChannel = nStatus & 0x0F;
uint8_t nLS7B = ifs.get();
uint8_t nMS7B = ifs.get();
vecTracks[nChunk].vecEvents.push_back({ MidiEvent::Type::Other });
}
else if ((nStatus & 0xF0) == EventName::SystemExclusive)
{
nPreviousStatus = 0;
if (nStatus == 0xFF)
{
// Meta Message
uint8_t nType = ifs.get();
uint8_t nLength = ReadValue();
switch (nType)
{
case MetaSequence:
std::cout << "Sequence Number: " << ifs.get() << ifs.get() << std::endl;
break;
case MetaText:
std::cout << "Text: " << ReadString(nLength) << std::endl;
break;
case MetaCopyright:
std::cout << "Copyright: " << ReadString(nLength) << std::endl;
break;
case MetaTrackName:
vecTracks[nChunk].sName = ReadString(nLength);
std::cout << "Track Name: " << vecTracks[nChunk].sName << std::endl;
break;
case MetaInstrumentName:
vecTracks[nChunk].sInstrument = ReadString(nLength);
std::cout << "Instrument Name: " << vecTracks[nChunk].sInstrument << std::endl;
break;
case MetaLyrics:
std::cout << "Lyrics: " << ReadString(nLength) << std::endl;
break;
case MetaMarker:
std::cout << "Marker: " << ReadString(nLength) << std::endl;
break;
case MetaCuePoint:
std::cout << "Cue: " << ReadString(nLength) << std::endl;
break;
case MetaChannelPrefix:
std::cout << "Prefix: " << ifs.get() << std::endl;
break;
case MetaEndOfTrack:
bEndOfTrack = true;
break;
case MetaSetTempo:
// Tempo is in microseconds per quarter note
if (m_nTempo == 0)
{
(m_nTempo |= (ifs.get() << 16));
(m_nTempo |= (ifs.get() << 8));
(m_nTempo |= (ifs.get() << 0));
m_nBPM = (60000000 / m_nTempo);
std::cout << "Tempo: " << m_nTempo << " (" << m_nBPM << "bpm)" << std::endl;
}
break;
case MetaSMPTEOffset:
std::cout << "SMPTE: H:" << ifs.get() << " M:" << ifs.get() << " S:" << ifs.get() << " FR:" << ifs.get() << " FF:" << ifs.get() << std::endl;
break;
case MetaTimeSignature:
std::cout << "Time Signature: " << ifs.get() << "/" << (2 << ifs.get()) << std::endl;
std::cout << "ClocksPerTick: " << ifs.get() << std::endl;
// A MIDI "Beat" is 24 ticks, so specify how many 32nd notes constitute a beat
std::cout << "32per24Clocks: " << ifs.get() << std::endl;
break;
case MetaKeySignature:
std::cout << "Key Signature: " << ifs.get() << std::endl;
std::cout << "Minor Key: " << ifs.get() << std::endl;
break;
case MetaSequencerSpecific:
std::cout << "Sequencer Specific: " << ReadString(nLength) << std::endl;
break;
default:
std::cout << "Unrecognised MetaEvent: " << nType << std::endl;
}
}
if (nStatus == 0xF0)
{
// System Exclusive Message Begin
std::cout << "System Exclusive Begin: " << ReadString(ReadValue()) << std::endl;
}
if (nStatus == 0xF7)
{
// System Exclusive Message Begin
std::cout << "System Exclusive End: " << ReadString(ReadValue()) << std::endl;
}
}
else
{
std::cout << "Unrecognised Status Byte: " << nStatus << std::endl;
}
}
}
// Convert Time Events to Notes
for (auto& track : vecTracks)
{
uint32_t nWallTime = 0;
std::list<MidiNote> listNotesBeingProcessed;
for (auto& event : track.vecEvents)
{
nWallTime += event.nDeltaTick;
if (event.event == MidiEvent::Type::NoteOn)
{
// New Note
listNotesBeingProcessed.push_back({ event.nKey, event.nVelocity, nWallTime, 0 });
}
if (event.event == MidiEvent::Type::NoteOff)
{
auto note = std::find_if(listNotesBeingProcessed.begin(), listNotesBeingProcessed.end(), [&](const MidiNote& n) { return n.nKey == event.nKey; });
if (note != listNotesBeingProcessed.end())
{
note->nDuration = nWallTime - note->nStartTime;
track.vecNotes.push_back(*note);
track.nMinNote = std::min(track.nMinNote, note->nKey);
track.nMaxNote = std::max(track.nMaxNote, note->nKey);
listNotesBeingProcessed.erase(note);
}
}
}
}
return true;
}
public:
std::vector<MidiTrack> vecTracks;
uint32_t m_nTempo = 0;
uint32_t m_nBPM = 0;
};
class olcMIDIViewer : public olc::PixelGameEngine
{
public:
olcMIDIViewer()
{
sAppName = "MIDI File Viewer";
}
MidiFile midi;
#ifdef WIN32
//HMIDIOUT hInstrument;
#else
fluid_settings_t *settings = NULL;
fluid_synth_t *synth = NULL;
fluid_audio_driver_t *adriver = NULL;
int sfont_id;
int i, key;
const char *sMidiFontFile = "midi_font.sf2";
#endif
size_t nCurrentNote[16]{ 0 };
double dSongTime = 0.0;
double dRunTime = 0.0;
uint32_t nMidiClock = 0;
public:
bool OnUserCreate() override
{
midi.ParseFile("ff7_battle.mid");
#ifdef WIN32
/*
int nMidiDevices = midiOutGetNumDevs();
if (nMidiDevices > 0)
{
if (midiOutOpen(&hInstrument, 2, NULL, 0, NULL) == MMSYSERR_NOERROR)
{
std::cout << "Opened midi" << std::endl;
}
}
*/
#else
/* Create the settings. */
settings = new_fluid_settings();
if (settings == NULL)
{
puts("Failed to create the settings!");
return false;
}
/* Change the settings if necessary*/
/* Create the synthesizer. */
synth = new_fluid_synth(settings);
if (synth == NULL)
{
puts("Failed to create the synth!");
return false;
}
/* Load a SoundFont and reset presets (so that new instruments
* get used from the SoundFont)
* Depending on the size of the SoundFont, this will take some time to complete...
*/
sfont_id = fluid_synth_sfload(synth, sMidiFontFile, 1);
if (sfont_id == FLUID_FAILED)
{
puts("Loading the SoundFont failed!");
return false;
}
/* Create the audio driver. The synthesizer starts playing as soon
as the driver is created. */
adriver = new_fluid_audio_driver(settings, synth);
if (adriver == NULL)
{
puts("Failed to create the audio driver!");
return false;
}
#endif
return true;
}
float nTrackOffset = 1000;
bool OnUserUpdate(float fElapsedTime) override
{
Clear(olc::BLACK);
uint32_t nTimePerColumn = 50;
uint32_t nNoteHeight = 2;
uint32_t nOffsetY = 0;
if (GetKey(olc::Key::LEFT).bHeld) nTrackOffset -= 10000.0f * fElapsedTime;
if (GetKey(olc::Key::RIGHT).bHeld) nTrackOffset += 10000.0f * fElapsedTime;
for (auto& track : midi.vecTracks)
{
if (!track.vecNotes.empty())
{
uint32_t nNoteRange = track.nMaxNote - track.nMinNote;
FillRect(0, nOffsetY, ScreenWidth(), (nNoteRange + 1) * nNoteHeight, olc::DARK_GREY);
DrawString(1, nOffsetY + 1, track.sName);
for (auto& note : track.vecNotes)
{
FillRect((note.nStartTime - nTrackOffset) / nTimePerColumn, (nNoteRange - (note.nKey - track.nMinNote)) * nNoteHeight + nOffsetY, note.nDuration / nTimePerColumn, nNoteHeight, olc::WHITE);
}
nOffsetY += (nNoteRange + 1) * nNoteHeight + 4;
}
}
// BELOW - ABSOLUTELY HORRIBLE BODGE TO PLAY SOUND
// DO NOT USE THIS CODE...
/*
dRunTime += fElapsedTime;
uint32_t nTempo = 4;
int nTrack = 1;
while (dRunTime >= 1.0 / double(midi.m_nBPM * 8))
{
dRunTime -= 1.0 / double(midi.m_nBPM * 8);
// Single MIDI Clock
nMidiClock++;
int i = 0;
int nTrack = 1;
//for (nTrack = 1; nTrack < 3; nTrack++)
{
if (nCurrentNote[nTrack] < midi.vecTracks[nTrack].vecEvents.size())
{
if (midi.vecTracks[nTrack].vecEvents[nCurrentNote[nTrack]].nDeltaTick == 0)
{
uint32_t nStatus = 0;
uint32_t nNote = midi.vecTracks[nTrack].vecEvents[nCurrentNote[nTrack]].nKey;
uint32_t nVelocity = midi.vecTracks[nTrack].vecEvents[nCurrentNote[nTrack]].nVelocity;
#ifdef WIN32
if (midi.vecTracks[nTrack].vecEvents[nCurrentNote[nTrack]].event == MidiEvent::Type::NoteOn)
nStatus = 0x90;
else
nStatus = 0x80;
midiOutShortMsg(hInstrument, (nVelocity << 16) | (nNote << 8) | nStatus);
#else
if (midi.vecTracks[nTrack].vecEvents[nCurrentNote[nTrack]].event == MidiEvent::Type::NoteOn)
fluid_synth_noteon(synth, nTrack, nNote, nVelocity);
else
fluid_synth_noteoff(synth, nTrack, nNote);
#endif
nCurrentNote[nTrack]++;
}
else
midi.vecTracks[nTrack].vecEvents[nCurrentNote[nTrack]].nDeltaTick--;
}
}
}
#ifdef WIN32
if (GetKey(olc::Key::SPACE).bPressed)
{
midiOutShortMsg(hInstrument, 0x00403C90);
}
if (GetKey(olc::Key::SPACE).bReleased)
{
midiOutShortMsg(hInstrument, 0x00003C80);
}
#else
if (GetKey(olc::Key::SPACE).bPressed)
{
fluid_synth_noteon(synth, 0, 0x3C, 0x40);
}
if (GetKey(olc::Key::SPACE).bReleased)
{
fluid_synth_noteoff(synth, 0, 0x3C);
}
#endif
*/
return true;
}
#ifndef WIN32
bool OnUserDestroy() override
{
/* Clean up */
delete_fluid_audio_driver(adriver);
delete_fluid_synth(synth);
delete_fluid_settings(settings);
return true;
}
#endif
};
int main()
{
olcMIDIViewer demo;
if (demo.Construct(1280, 960, 1, 1))
demo.Start();
return 0;
}