-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsmdFile.cpp
More file actions
474 lines (441 loc) · 11 KB
/
smdFile.cpp
File metadata and controls
474 lines (441 loc) · 11 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
#include "smdFile.hpp"
#include <cstdio>
#include <cstring>
#include <exception>
#include <fstream>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <stdint.h>
#include <string>
#include <vector>
using namespace std;
std::ostream& operator<<(std::ostream& os, const smdSong &p) {
os << "Name: " << p.name << "\nTrack count: " << p.tracks.size() << endl;
os << "Instrument group: " << p.instrumentGroup << endl;
for(vector< smdTrack >::const_iterator it=p.tracks.begin();it!=p.tracks.end();++it)
os << *it << endl;
return os;
}
smdSong::smdSong(std::ifstream& file) {
off_t myStart = file.tellg();
{ // Check magic numbers
char magic[4];
file.read(magic,sizeof(magic));
if(strncmp(magic,"smdl",4))
throw runtime_error("Not a .smd file");
file.seekg(myStart+0x40);
file.read(magic,sizeof(magic));
if(strncmp(magic,"song",4))
throw runtime_error("Doesn't look like a .smd file");
}
{ // Get name
file.seekg(myStart+0x20);
char fname[0x11];
file.read(fname,0x10);
fname[0x10]='\0';
name = fname;
}
int nTrackChunks;
{ // Get number of track chunks
file.seekg(myStart+0x56);
char count[1];
file.read(count,1);
nTrackChunks = count[0];
}
{ // Get instrument group
file.seekg(myStart+0x0E);
char field[1];
file.read(field,1);
instrumentGroup = field[0];
}
{ // Read the tracks
file.seekg(myStart+0x80);
for(int i=0;i<nTrackChunks;i++)
tracks.push_back(smdTrack(file,instrumentGroup));
// TODO try/catch block because of issue #1
}
}
std::string smdSong::GetName() const {
return name;
}
int smdSong::GetInstrumentGroup() const {
return instrumentGroup;
}
int smdSong::GetTrackCount() const {
return tracks.size();
}
const std::vector< smdTrack >& smdSong::Tracks() const {
return tracks;
}
const smdTrack& smdSong::operator[](int i) const {
return tracks[i];
}
bool smdSong::OutputInUse(int n) const {
for(vector< smdTrack >::const_iterator it=tracks.begin();it!=tracks.end();++it)
if(n==it->GetOutputID())
return true;
return false;
}
bool smdSong::OutputInUseNotDrum(int n) const {
for(vector< smdTrack >::const_iterator it=tracks.begin();it!=tracks.end();++it)
if(n==it->GetOutputID())
if(!it->IsDrum())
return true;
return false;
}
//////////
smdTrack::smdTrack(std::ifstream& file, int instrumentGroup) : instrumentGroup(instrumentGroup) {
off_t myStart = file.tellg();
{ // Check magic number
char magic[4];
file.read(magic,sizeof(magic));
if(strncmp(magic,"trk ",4))
throw runtime_error("Invalid SMD track header");
}
size_t dataLen;
{ // Track data length
file.seekg(myStart+0x0C);
uint32_t rDataLen;
file.read((char*)&rDataLen,4);
dataLen = rDataLen;
}
{ // ID numbers
char idNum[2];
file.read(idNum,2);
trackID = idNum[0];
outputID = idNum[1];
}
{ // Track events
file.seekg(myStart+0x14);
smdEvent::EventType prevType;
do {
off_t startPos = file.tellg();
try {
if(startPos > myStart+0x10+dataLen)
throw runtime_error("Read past end of track");
smdEvent newEvent(file);
prevType = newEvent.GetType();
events.push_back(newEvent);
} catch (exception& e) {
stringstream errorMessage;
char buffer[16];
sprintf(buffer,"0x%08X",startPos);
errorMessage << "smdTrack::smdTrack(): " << e.what() << " (trackID=" << trackID << ",startPos=" << buffer << ')';
throw runtime_error(errorMessage.str());
}
} while (prevType != smdEvent::TRACK_END);
}
// Cue to end of track
file.seekg(myStart+0x10+dataLen+((dataLen%4)?(4-(dataLen%4)):0));
}
std::ostream& operator<<(std::ostream& os, const smdTrack& p) {
os << "tID=" << p.trackID << ", oID=" << p.outputID << endl;
os << "Events: (" << p.events.size() << " count)\n";
smdEvent::DisplayBytes = p.LongestCmdSize();
int when = 0;
char whenBuffer[64];
int loopPos = -1;
for(int i=0;i<p.events.size();i++) {
sprintf(whenBuffer,"%6u:%1u.%02u %-12u",when/192 +1,(when%192)/48 +1,(when%48),when);
try {
os << i << '\t' << whenBuffer << '\t' << p.events[i];
} catch (exception& e) {
stringstream errorMessage;
char buffer[16];
errorMessage << "operator<<(smdTrack): " << e.what() << " (trackID=" << p.trackID << " @ " << whenBuffer << ')';
throw runtime_error(errorMessage.str());
}
if(p.events[i].GetType() == smdEvent::LOOP_POINT)
loopPos = when;
when += p.events[i].TickLength();
}
os << "(end events for tID=" << p.trackID;
if(loopPos!=-1)
os << ", LOOP=" << ((when-loopPos)/192) << ':' << ((when-loopPos)%192)/48 << '.' << ((when-loopPos)%48);
os << ")\n";
return os;
}
int smdTrack::GetTrackID() const {
return trackID;
}
int smdTrack::GetOutputID() const {
return outputID;
}
int smdTrack::GetInstrumentGroup() const {
return instrumentGroup;
}
bool smdTrack::IsDrum() const {
for(vector< smdEvent >::const_iterator it=events.begin();it!=events.end();++it)
if(it->GetType() == smdEvent::SET_SAMPLE)
if(it->params[0] >= 0x7C)
return true;
return false;
}
int smdTrack::GetEventCount() const {
return events.size();
}
const std::vector< smdEvent >& smdTrack::Events() const {
return events;
}
const smdEvent& smdTrack::operator[](int i) const {
return events[i];
}
size_t smdTrack::LongestCmdSize() const {
size_t result = 0;
for(vector< smdEvent >::const_iterator it=events.begin();it!=events.end();++it)
if(it->CmdSize()>result)
result=it->CmdSize();
return result;
}
//////////
int smdEvent::prevWaitLength;
size_t smdEvent::DisplayBytes;
smdEvent::smdEvent(std::ifstream& file) {
char readByte;
file.read(&readByte,1);
eventCode = readByte;
if(eventCode <= 0x7F) {
// NOTE_PLAY
file.read(&readByte,1);
params.push_back(readByte);
switch(params[0]&0xC0) {
case 0x80:
file.read(&readByte,1);
params.push_back(readByte);
case 0x40:
file.read(&readByte,1);
params.push_back(readByte);
case 0x00:
break;
default:
throw runtime_error("Bad flags");
}
} else if (eventCode > 0x8F)
switch(eventCode) {
case 0xDC:
file.read(&readByte,1);
params.push_back(readByte);
file.read(&readByte,1);
params.push_back(readByte);
case 0xD4:
case 0xE2:
case 0xEA:
file.read(&readByte,1);
params.push_back(readByte);
case WAIT_2BYTE:
case 0xA8:
case 0xB4:
case 0xD6:
case SET_BEND:
file.read(&readByte,1);
params.push_back(readByte);
case WAIT_ADD:
case WAIT_1BYTE:
case 0x9C:
case SET_OCTAVE:
case SET_TEMPO:
case 0xA9:
case 0xAA:
case SET_SAMPLE:
case 0xB2:
case 0xB5:
case SET_MODU:
case 0xBF:
case 0xD0:
case 0xD1:
case 0xD2:
case 0xDB:
case SET_VOLUME:
case SET_XPRESS:
case SET_PAN:
case 0xF4:
case 0xF6:
file.read(&readByte,1);
params.push_back(readByte);
case WAIT_AGAIN:
case TRACK_END:
case LOOP_POINT:
case 0x9D:
case 0xC0:
break;
default:
{
char buffer[8];
sprintf(buffer,"UNK_%02X",eventCode);
throw runtime_error((string)"Bad opcode: "+buffer);
}
}
if((GetType()==WAIT_1BYTE) || (GetType()==WAIT_2BYTE))
prevWaitLength = TickLength();
if(GetType()==WAIT_ADD)
prevWaitLength += (signed int8_t)params[0];
waitAgainLength = prevWaitLength;
}
static const char* NoteNames[12] =
{"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
std::ostream& operator<<(std::ostream& os, const smdEvent& p) {
char buffer[200];
sprintf(buffer,"0x%02X",p.eventCode);
os << buffer;
int howMany=smdEvent::DisplayBytes-1;
if(howMany<=0)
howMany=p.params.size();
for(int i=0;i<howMany;i++)
if(i>=p.params.size())
os << " ";
else {
sprintf(buffer," 0x%02X",p.params[i]);
os << buffer;
}
os << '\t';
if(p.eventCode <= 0x7F) {
os << "Note: Velocity=" << (int)p.eventCode << ", ";
switch((p.params[0]>>4) & 0x3) {
case 0:
os << "Oct-2, ";
break;
case 1:
os << "Oct-1, ";
break;
case 3:
os << "Oct+1, ";
break;
}
if((p.params[0]&0xF) < 12)
os << "Key=" << NoteNames[p.params[0]&0xF];
else
throw runtime_error("Bad key");
switch(p.params[0]&0xC0) {
case 0x00:
break;
case 0x40:
os << ", Length=" << (int)p.params[1] << "/192";
break;
case 0x80:
os << ", Length=" << (int)((p.params[1]<<8) + p.params[2]) << "/192";
}
} else
switch(p.GetType()) {
case smdEvent::DELTA_TIME:
os << "Delta: ";
os << "1/" << (2<<((p.eventCode&0xF)/3) + ((p.eventCode&0xF)%3 == 1));
switch((p.eventCode&0xF)%3) {
case 0:
os << " note";
break;
case 1:
os << " dot";
break;
case 2:
os << " trip";
break;
}
break;
case smdEvent::WAIT_AGAIN:
os << "WaitAgain: " << p.waitAgainLength << "/192";
break;
case smdEvent::WAIT_ADD:
os << "WaitAdd: " << p.waitAgainLength << "/192 (d=" << (int)(int8_t)p.params[0] << "/192)";
break;
case smdEvent::WAIT_1BYTE:
sprintf(buffer,"Wait: 0x%02X ( ",p.params[0]);
os << buffer;
os << (int)p.params[0] << "/192 )";
break;
case smdEvent::WAIT_2BYTE:
sprintf(buffer,"Wait: 0x%02X%02X ( ",p.params[1],p.params[0]);
os << buffer;
os << (int)p.params[0] + (int)p.params[1]*256 << "/192 )";
break;
case smdEvent::TRACK_END:
os << "-- END --";
break;
case smdEvent::LOOP_POINT:
os << "-- LOOP --";
break;
case smdEvent::SET_OCTAVE:
os << "Octave: " << (int)p.params[0];
break;
case smdEvent::SET_TEMPO:
os << "Tempo: " << (int)p.params[0] << "BPM";
break;
case smdEvent::SET_SAMPLE:
os << "Instrument: " << (int)p.params[0];
break;
case smdEvent::SET_MODU:
os << "Modulation: " << (int)p.params[0];
break;
case smdEvent::SET_BEND:
os << "Bend: " << (int16_t)(p.params[0]*256 + p.params[1]) << " cents";
break;
case smdEvent::SET_VOLUME:
os << "Volume: " << (int)p.params[0];
break;
case smdEvent::SET_XPRESS:
os << "Express: " << (int)p.params[0];
break;
case smdEvent::SET_PAN:
os << "Pan: " << (int)(p.params[0] - 0x40);
break;
default:
sprintf(buffer,"UNK_%2X",p.eventCode);
os << buffer;
}
os << endl;
return os;
}
smdEvent::EventType smdEvent::GetType() const {
if(eventCode <= 0x7F)
return NOTE_PLAY;
if(eventCode <= 0x8F)
return DELTA_TIME;
switch(eventCode) {
case WAIT_AGAIN:
case WAIT_ADD:
case WAIT_1BYTE:
case WAIT_2BYTE:
case TRACK_END:
case LOOP_POINT:
case SET_OCTAVE:
case SET_TEMPO:
case SET_SAMPLE:
case SET_MODU:
case SET_BEND:
case SET_VOLUME:
case SET_XPRESS:
case SET_PAN:
return (EventType)eventCode;
default:
return GENERAL_UNKNOWN;
}
}
uint8_t smdEvent::GetEventCode() const {
return eventCode;
}
int smdEvent::GetParamCount() const {
return params.size();
}
uint8_t smdEvent::Param(int i) const {
return params[i];
}
static const int DTimeTickTable[16] =
{96,72,64,48,36,32,24,18,16,12,9,8,6,4,3,2};
int smdEvent::TickLength() const {
switch(GetType()) {
case DELTA_TIME:
return DTimeTickTable[eventCode-0x80];
case WAIT_AGAIN:
case WAIT_ADD:
return waitAgainLength;
case WAIT_1BYTE:
return params[0];
case WAIT_2BYTE:
return params[0] + 256*params[1];
default:
return 0;
}
}
size_t smdEvent::CmdSize() const {
return 1+params.size();
}