-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMidiTrack.cpp
More file actions
134 lines (117 loc) · 3.71 KB
/
Copy pathMidiTrack.cpp
File metadata and controls
134 lines (117 loc) · 3.71 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
#include "MidiTrack.h"
#include <iostream>
#include <windows.h>
#include <mmeapi.h>
#include <string>
#include <fstream>
#include "MidiSpecificEnums.h"
using namespace std;
MidiTrack::MidiTrack(string* fileName, uint32_t* trackStartByte, MidiPiece* midi, HMIDIOUT* toSendInterface) {
this->trackStartByte = new uint32_t(*trackStartByte);
this->trackStrem = new ifstream(*fileName, ios::in | ios::binary);
this->midi = midi;
this->toSendInterface = toSendInterface;
}
MidiTrack::MidiTrack() {
}
void MidiTrack::start(uint8_t* threadNumber) {
printf("Thread number %d is on.\n", *threadNumber);
trackStrem->seekg(*trackStartByte);
bool* isTrackOver = new bool(false);
uint8_t* currentlyExaminedByte = new uint8_t;
uint32_t* msg = new uint32_t(0);
uint32_t* tmp = new uint32_t();
read4();
read4();
while (!*isTrackOver) {
readDeltaTime();
*currentlyExaminedByte = *read1();
// Meta events
if (*currentlyExaminedByte == 0xFF) {
*currentlyExaminedByte = *read1();
if (*currentlyExaminedByte == endOfTrack) {
read1();
*isTrackOver = true;
}
else if (*currentlyExaminedByte == setTempo) {
// lenght is fixed - 3
midi->read1();
uint32_t* microsecondsPerQuarterNote = new uint32_t(0);
uint16_t* first2Bytes = new uint16_t(*midi->read2());
uint8_t* secondByte = new uint8_t(*midi->read1());
*microsecondsPerQuarterNote = *first2Bytes;
*microsecondsPerQuarterNote = (*microsecondsPerQuarterNote << 8) | *secondByte;
midi->setMicrosecnodsPerQuaterNote(microsecondsPerQuarterNote);
delete(first2Bytes, secondByte);
}
else {
// represents other, unimplemented meta events
*tmp = *read1();
for (int i = 0; i < *tmp; i++) {
read1();
}
}
}
// Channel events
else {
*msg = 0;
if ((*currentlyExaminedByte >> 4) == noteOff || (*currentlyExaminedByte >> 4) == noteOn || (*currentlyExaminedByte >> 4) == noteAftertouch
|| (*currentlyExaminedByte >> 4) == controller || (*currentlyExaminedByte >> 4) == pitchBend) {
*msg = *currentlyExaminedByte | (*read1() << 8);
*msg = *msg | (*read1() << 16);
midiOutShortMsg(*toSendInterface, *msg);
}
else if ((*currentlyExaminedByte >> 4) == programChange || (*currentlyExaminedByte >> 4) == channelAfterTouch) {
*msg = *currentlyExaminedByte | (*read1() << 8);
midiOutShortMsg(*toSendInterface, *msg);
}
}
}
}
uint8_t* MidiTrack::read1() {
uint8_t* result = new uint8_t;
trackStrem->read((char*)result, sizeof(uint8_t));
return result;
}
uint16_t* MidiTrack::read2() {
uint16_t* result = new uint16_t;
trackStrem->read((char*)result, sizeof(uint16_t));
*result = _byteswap_ushort((unsigned short)*result);
return result;
}
uint32_t* MidiTrack::read4() {
uint32_t* result = new uint32_t;
trackStrem->read((char*)result, sizeof(uint32_t));
*result = _byteswap_ulong((unsigned long)*result);
return result;
}
void MidiTrack::readDeltaTime() {
uint8_t* currentDeltaTime = new uint8_t[4];
currentDeltaTime[0] = *read1();
uint8_t counter = 0;
while (currentDeltaTime[counter] >= 128) {
currentDeltaTime[counter] = currentDeltaTime[counter] - 128;
if (counter < 4) {
counter += 1;
currentDeltaTime[counter] = *read1();
}
}
applyDelay(currentDeltaTime, counter);
}
void MidiTrack::applyDelay(uint8_t* currentDeltaTime, uint8_t counter) {
// For metrical timing
if (currentDeltaTime[0] == 0) {
return;
}
else {
uint32_t* deltaTime = new uint32_t(currentDeltaTime[0]);
for (int i = 0; i <= counter; i++) {
*deltaTime = (*deltaTime << i * 7) | currentDeltaTime[i];
}
double* t1 = new double(*deltaTime / (double)*midi->getDivision());
*t1 = *t1 * *midi->getMicrosecondsPerQuaterNote();
*t1 = *t1 / 1000;
uint32_t* properInt = new uint32_t(*t1);
Sleep(*properInt);
}
}