-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMuNote.cpp
executable file
·340 lines (292 loc) · 6.29 KB
/
MuNote.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
//*********************************************
//***************** NCM-UnB *******************
//******** (c) Carlos Eduardo Mello ***********
//*********************************************
// This softwre may be freely reproduced,
// copied, modified, and reused, as long as
// it retains, in all forms, the above credits.
//*********************************************
/** @file MuNote.cpp
*
* @brief Note Class Implementation
*
* @author Carlos Eduardo Mello
* @date 3/5/2009
*
**/
#include "MuNote.h"
MuNote::MuNote(void)
{
instr = 0;
start = 0.0;
dur = 0.0;
pitch = 0;
amp = 0.0;
next = NULL;
}
MuNote * MuNote::Next(void)
{
return next;
}
// Copy Constructor
MuNote::MuNote( const MuNote & inNote )
{
instr = inNote.instr;
start = inNote.start;
dur = inNote.dur;
pitch = inNote.pitch;
amp = inNote.amp;
param = inNote.param;
}
// Assignment...
MuNote & MuNote::operator=( const MuNote & inNote )
{
instr = inNote.instr;
start = inNote.start;
dur = inNote.dur;
pitch = inNote.pitch;
amp = inNote.amp;
param = inNote.param;
return *this;
}
// Equality test...
bool MuNote::operator==( const MuNote & inNote )
{
bool result = false;
if(
( instr == inNote.instr ) &&
( start == inNote.start ) &&
( dur == inNote.dur ) &&
( pitch == inNote.pitch ) &&
( amp == inNote.amp ) &&
( param == inNote.param )
)
result = true;
return result;
}
// Inequality test...
bool MuNote::operator!=( const MuNote & inNote )
{
return !( *this == inNote );
}
void MuNote::SetNext(MuNote * nextNote)
{
next = nextNote;
}
void MuNote::Clear(void)
{
instr = 0;
start = 0.0;
dur = 0.0;
pitch = 0;
next = NULL;
}
uShort MuNote::Instr(void)
{
return instr;
}
void MuNote::SetInstr(uShort newInstr)
{
instr = newInstr;
}
float MuNote::Start(void)
{
return start;
}
void MuNote::SetStart(float newStart)
{
start = newStart;
}
float MuNote::Dur(void)
{
return dur;
}
void MuNote::SetDur(float newDur)
{
dur = newDur;
}
float MuNote::End(void)
{
return (start + dur);
}
short MuNote::Pitch(void)
{
return pitch;
}
void MuNote::SetPitch(short newPitch)
{
pitch = newPitch;
if (pitch < 0)
pitch = 0;
}
void MuNote::SetPitch(cs_pitch newPitch)
{
pitch = newPitch.pitch + ( ( newPitch.octave - 3 ) * 12 );
if(pitch < 0)
pitch = 0;
}
void MuNote::ColapsePitch(void)
{
pitch = ( pitch % 12 ) + MIDDLE_C;
}
float MuNote::Amp(void)
{
return amp;
}
void MuNote::SetAmp(float newAmp)
{
amp = newAmp;
}
MuParamBlock MuNote::Params(void)
{
return param;
}
void MuNote::SetParams(MuParamBlock inParams)
{
param = inParams;
}
cs_pitch MuNote::CsPitch(void)
{
cs_pitch csp;
if(pitch == 0)
{
csp.octave = 0;
csp.pitch = 0;
}
else
{
csp.octave = ((pitch / 12) + OCTAVE_OFFSET);
csp.pitch = ((pitch % 12));
}
return csp;
}
string MuNote::PitchString(void)
{
string pitchString;
cs_pitch p;
char buff[10];
p = CsPitch();
sprintf(buff, "%d.", p.octave);
pitchString += buff;
if(p.pitch < 10)
pitchString += "0";
sprintf(buff, "%d", p.pitch);
pitchString += buff;
return pitchString;
}
string MuNote::CsString(void)
{
string cs_string;
char buff[10];
MuParamBlock pBlock;
float paramVal = 0;
short numberOfParams = 0;
MuError err(MuERROR_NONE);
cs_string += "i";
sprintf(buff,"%d",Instr());
cs_string += buff;
cs_string += "\t";
sprintf(buff,"%.3f",Start());
cs_string += buff;
cs_string += "\t\t";
sprintf(buff,"%.3f",Dur());
cs_string += buff;
cs_string += "\t\t";
cs_string += PitchString();
cs_string += "\t\t";
sprintf(buff,"%.3f",Amp());
cs_string += buff;
cs_string += "\t\t";
pBlock = Params();
numberOfParams = pBlock.Num();
if( numberOfParams != 0)
{
for(short k = 0; k < numberOfParams; k++)
{
err = pBlock.Val(k, ¶mVal);
if(err.Get() != MuERROR_NONE)
cout << err.Message();
else
{
sprintf(buff,"%.3f",paramVal);
cs_string += buff;
cs_string += "\t";
}
}
}
return cs_string;
}
string MuNote::PitchName(int languageChoice, int accidentals)
{
string name;
string englishSharps[12] = {"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
string englishFlats[12] = {"C","Db","D","Eb","E","F","Gb","G","Ab","A","Bb","B"};
string portSharps[12] = {"DO","DO#","RE","RE#","MI","FA","FA#","SOL","SOL#","LA","LA#","B"};
string portFlats[12] = {"DO","REb","RE","MIb","MI","FA","SOLb","SOL","LAb","LA","SIb","SI"};
// zero pitch indicates a rest
if(this->Pitch() == 0)
name = " -- ";
else
{
int semitone = Pitch()%12;
switch (languageChoice)
{
case ENGLISH:
{
switch (accidentals)
{
case ACC_FAVOR_FLATS:
name = englishFlats[semitone];
break;
case ACC_FAVOR_SHARPS:
name = englishSharps[semitone];
break;
}
break;
}
case PORTUGUESE:
{
switch (accidentals)
{
case ACC_FAVOR_FLATS:
name = portFlats[semitone];
break;
case ACC_FAVOR_SHARPS:
name = portSharps[semitone];
break;
}
break;
}
}
}
return name;
}
MuMIDIMessage MuNote::MIDIOn(void)
{
MuMIDIMessage noteOn;
noteOn.status = 0x90;
noteOn.data1 = pitch;
noteOn.data2 = amp * 127;
noteOn.time = start;
return noteOn;
}
MuMIDIMessage MuNote::MIDIOff(void)
{
MuMIDIMessage noteOff;
noteOff.status = 0x80;
noteOff.data1 = pitch;
noteOff.data2 = 0;
noteOff.time = ( start + dur );
return noteOff;
}
void MuNote::SetFromMIDI(MuMIDIMessage noteOn, MuMIDIMessage noteOff)
{
// This was a quick and dirty implementation!!
// FIX: perform sanity checks on input data.
// FIX: verify ranges, data types and numeric conversions
SetStart(noteOn.time);
SetPitch(noteOn.data1);
SetAmp(noteOn.data2/128.0);
SetInstr((noteOn.status & 0x0F) + 1);
SetDur(noteOff.time - noteOn.time);
}