/
Ims.cpp
347 lines (280 loc) · 7.12 KB
/
Ims.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
#include "Main.h"
Ims::Ims ()
{
m_basicTempo = 120; // 현재 빠르기
m_songDataTotalIndex = 0;
m_songDataIndex = 0;
m_tick = 0;
}
Ims::~Ims ()
{
free(m_ims->songData);
free(m_ims->raw);
free(m_ims->instIndex);
free(m_ims);
}
int Ims::GetCurrentTick()
{
return m_tick;
}
int Ims::GetBasicTempo()
{
return m_basicTempo;
}
wxString Ims::GetTitle()
{
char title[30];
han_conv(0, m_ims->header.szTuneName, title);
wchar_t wszBuffer[30];
// cp949 -> unicode
MultiByteToWideChar(CP_ACP, 0, title, -1, wszBuffer, 30);
// unicode -> utf-8
WideCharToMultiByte(CP_UTF8, 0, wszBuffer, -1, title, 30, NULL, NULL);
return wxString(wszBuffer, wxConvUTF8);
}
char *Ims::GetTitleJohab()
{
return m_ims->header.szTuneName;
}
long Ims::GetTotalTick()
{
return m_ims->header.nTotalTick;
}
long Ims::GetDuration()
{
int basicTempo = GetBasicTempo();
int totalTicks = m_ims->header.nTotalTick;
int tickBeat = m_ims->header.nTickBeat;
int ticksPerMin = basicTempo * tickBeat;
int ticksPerSec = ticksPerMin / 60;
int totalSec = totalTicks/ticksPerSec;
return totalSec;
}
bool Ims::Open(wxString file)
{
int x;
FILE *fp;
m_ims = (MYADLIB_IMS *)malloc(sizeof(MYADLIB_IMS));
if (m_ims == NULL) return NULL;
memset(m_ims, 0, sizeof(MYADLIB_IMS));
m_fileName = file;
char path[1024];
strcpy( path, (const char*)file.mb_str(wxConvUTF8) );
// open
fp = fopen(path, "rb");
if (fp == NULL) return NULL;
fseek(fp, 0, SEEK_END);
m_fileSize = ftell(fp);
// 파일 사이즈가 1MB를 넘어서면 에러로 간주함
if (m_fileSize > 1024*1024) {
fclose(fp);
return NULL;
}
m_ims->raw = (unsigned char *)malloc(m_fileSize);
if (m_ims->raw == NULL) {
fclose(fp);
return NULL;
}
// dump whole
fseek(fp, 0, SEEK_SET);
fread((unsigned char *)(m_ims->raw),
m_fileSize, sizeof(unsigned char), fp);
m_ims->rawPtr = m_ims->raw;
fclose(fp);
// read header
memcpy(&m_ims->header, m_ims->rawPtr, sizeof(IMS_HEADER));
m_ims->rawPtr += sizeof(IMS_HEADER);
m_ims->rawPtr++;
// read song data
m_ims->songData = (unsigned char *)malloc(m_ims->header.cbDataSize);
if (m_ims->songData == NULL) return NULL;
memcpy(m_ims->songData, m_ims->rawPtr, m_ims->header.cbDataSize);
m_ims->rawPtr += m_ims->header.cbDataSize;
m_songDataTotalIndex = m_ims->header.cbDataSize;
// read instrument count
m_ims->rawPtr++;
unsigned short int instCount;
memcpy(&instCount, m_ims->rawPtr, sizeof(unsigned short int));
m_ims->instCount = instCount;
m_ims->rawPtr += sizeof(unsigned short int);
// read instrument names
m_ims->instIndex = (IMS_INST *)malloc(
sizeof(IMS_INST) * m_ims->instCount);
if (m_ims->instIndex == NULL) return NULL;
for (x = 0; x < m_ims->instCount; x++)
{
memcpy(m_ims->instIndex[x].name, m_ims->rawPtr, 9);
m_ims->rawPtr += 9;
}
return true;
}
void Ims::Rewind(void)
{
m_basicTempo = m_ims->header.nBasicTempo;
m_songDataIndex = 0;
m_tick = 0;
}
int Ims::PlayEvent(void)
{
static unsigned char stCode;
unsigned char curCode;
int delay, delayTime = 0;
int voice, note, volume, index;
int *paramArray;
unsigned short int pitchBend;
int instIndexInBnk;
again:
curCode = m_ims->songData[m_songDataIndex];
//fflush(stdout);
//return (nElapsed * MAX_SLIDER_PROGRESS / m_cbImsDataSize);
// 0x7F == 127
if (curCode > 0x7F)
{
m_songDataIndex++;
stCode = curCode;
}
else
curCode = stCode;
voice = curCode & 0x0F; // 오른쪽 4비트를 얻는다 (채널 번호)
// 왼쪽 4비트에 이벤트 종류가 저장되어 있다
switch (curCode & 0xF0)
{
case 0x80: // note off
note = m_ims->songData[m_songDataIndex];
volume = m_ims->songData[m_songDataIndex + 1];
NoteOff(voice);
SetVoiceVolume(voice, volume);
NoteOn(voice, note);
m_songDataIndex += 2;
break;
case 0x90: // note on
note = m_ims->songData[m_songDataIndex];
volume = m_ims->songData[m_songDataIndex + 1];
NoteOff(voice);
if (volume) {
SetVoiceVolume(voice, volume);
NoteOn(voice, note);
}
m_songDataIndex += 2;
break;
case 0xA0: // Set volume
volume = m_ims->songData[m_songDataIndex];
SetVoiceVolume(voice, volume);
m_songDataIndex++;
break;
case 0xC0: // Set Instrument
index = m_ims->songData[m_songDataIndex];
instIndexInBnk = m_ims->instIndex[index].index;
// 악기가 있다면..
if ( instIndexInBnk >= 0 &&
instIndexInBnk < m_ims->m_bnk->header.totalEntry)
{
paramArray =
&m_ims->m_bnk->instRecord32[instIndexInBnk].op1.keyScaleLevel;
SetVoiceTimbre(voice, paramArray);
}
else
{
/*
printf("%d/%d\n", instIndexInBnk,
m_ims->m_bnk->header.totalEntry);
fflush(stdout);
*/
}
m_songDataIndex++;
break;
case 0xE0: // Set Pitch
memcpy(&pitchBend, &m_ims->songData[m_songDataIndex],
sizeof(unsigned short int));
pitchBend = pitchBend / 2;
SetVoicePitch(voice, pitchBend);
m_songDataIndex += sizeof(unsigned short int);
break;
case 0xF0: // Set Tempo
m_songDataIndex += 2;
m_basicTempo = (m_ims->header.nBasicTempo *
m_ims->songData[m_songDataIndex]);
m_basicTempo += m_ims->header.nBasicTempo *
m_ims->songData[m_songDataIndex + 1] / 128;
m_songDataIndex += 3;
break;
}
while (1)
{
delay = m_ims->songData[m_songDataIndex];
m_songDataIndex++;
// IMS finish code
if (m_ims->songData[m_songDataIndex] == 0xFC)
{
if ( m_repeatMode == REPEAT_THIS )
{
Rewind();
}
else
{
m_playMode=SONG_END;
break;
}
delayTime += delay;
return delayTime;
}
if (delay == 0xF8)
delayTime += 240;
else
break;
}
delayTime += delay;
if (delayTime == 0)
goto again;
else
m_tick += delayTime;
return delayTime;
}
bool Ims::MatchBnk(MYADLIB_BNK *bnk)
{
if (m_ims == NULL) return false;
wxString notFoundInstList = wxEmptyString;
for (int i = 0; i < m_ims->instCount; i++)
{
bool exist = false;
for (int j = 0; j < bnk->header.totalEntry; j++)
{
if (strcasecmp(bnk->nameRecord[j].name,
m_ims->instIndex[i].name) == 0)
{
m_ims->instIndex[i].index = bnk->nameRecord[j].index;
exist=true;
break;
}
}
if ( exist == false )
{
const char* instname = m_ims->instIndex[i].name;
wxString string(instname, wxConvUTF8);
notFoundInstList = notFoundInstList + string;
notFoundInstList = notFoundInstList + wxT("\n");
}
}
m_ims->m_bnk = bnk;
if ( notFoundInstList.Length() > 0 )
{
wxMessageBox(wxT("Not found instrument(s) in bank.\n\n") +
notFoundInstList, wxT("IMS Player"), wxICON_ERROR);
return false;
}
return true;
}
bool Ims::Reset(void)
{
if (m_ims == NULL) return false;
YM3812ResetChip(ym3812p);
SndOutput(1, 0x20); // Enable waveform select (bit 5)
SetMode(m_ims->header.nSoundMode);
for (int i=0; i<MAX_VOICE; i++)
{
NoteOff(i);
SetVoiceVolume(i, 0);
}
Rewind();
return true;
}