-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_ult.cpp
222 lines (209 loc) · 5.42 KB
/
load_ult.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
/*
* This source code is public domain.
*
* Authors: Olivier Lapicque <[email protected]>
*/
#include "stdafx.h"
#include "sndfile.h"
//#pragma warning(disable:4244)
#define ULT_16BIT 0x04
#define ULT_LOOP 0x08
#define ULT_BIDI 0x10
#pragma pack(1)
// Raw ULT header struct:
typedef struct tagULTHEADER
{
char id[15]; // changed from CHAR
char songtitle[32]; // changed from CHAR
BYTE reserved;
} ULTHEADER;
// Raw ULT sampleinfo struct:
typedef struct tagULTSAMPLE
{
CHAR samplename[32];
CHAR dosname[12];
LONG loopstart;
LONG loopend;
LONG sizestart;
LONG sizeend;
BYTE volume;
BYTE flags;
WORD finetune;
} ULTSAMPLE;
#pragma pack()
BOOL CSoundFile::ReadUlt(const BYTE *lpStream, DWORD dwMemLength)
//---------------------------------------------------------------
{
ULTHEADER *pmh = (ULTHEADER *)lpStream;
ULTSAMPLE *pus;
UINT nos, nop;
DWORD dwMemPos = 0;
// try to read module header
if ((!lpStream) || (dwMemLength < 0x100)) return FALSE;
if (strncmp(pmh->id,"MAS_UTrack_V00",14)) return FALSE;
// Warning! Not supported ULT format, trying anyway
// if ((pmh->id[14] < '1') || (pmh->id[14] > '4')) return FALSE;
m_nType = MOD_TYPE_ULT;
m_nDefaultSpeed = 6;
m_nDefaultTempo = 125;
memcpy(m_szNames[0], pmh->songtitle, 32);
// read songtext
dwMemPos = sizeof(ULTHEADER);
if ((pmh->reserved) && (dwMemPos + pmh->reserved * 32 < dwMemLength))
{
UINT len = pmh->reserved * 32;
m_lpszSongComments = new char[len + 1 + pmh->reserved];
if (m_lpszSongComments)
{
for (UINT l=0; l<pmh->reserved; l++)
{
memcpy(m_lpszSongComments+l*33, lpStream+dwMemPos+l*32, 32);
m_lpszSongComments[l*33+32] = 0x0D;
}
m_lpszSongComments[len] = 0;
}
dwMemPos += len;
}
if (dwMemPos >= dwMemLength) return TRUE;
nos = lpStream[dwMemPos++];
m_nSamples = nos;
if (m_nSamples >= MAX_SAMPLES) m_nSamples = MAX_SAMPLES-1;
UINT smpsize = 64;
if (pmh->id[14] >= '4') smpsize += 2;
if (dwMemPos + nos*smpsize + 256 + 2 > dwMemLength) return TRUE;
for (UINT ins=1; ins<=nos; ins++, dwMemPos+=smpsize) if (ins<=m_nSamples)
{
pus = (ULTSAMPLE *)(lpStream+dwMemPos);
MODINSTRUMENT *pins = &Ins[ins];
memcpy(m_szNames[ins], pus->samplename, 32);
memcpy(pins->name, pus->dosname, 12);
pins->nLoopStart = pus->loopstart;
pins->nLoopEnd = pus->loopend;
pins->nLength = pus->sizeend - pus->sizestart;
pins->nVolume = pus->volume;
pins->nGlobalVol = 64;
pins->nC4Speed = 8363;
if (pmh->id[14] >= '4')
{
pins->nC4Speed = pus->finetune;
}
if (pus->flags & ULT_LOOP) pins->uFlags |= CHN_LOOP;
if (pus->flags & ULT_BIDI) pins->uFlags |= CHN_PINGPONGLOOP;
if (pus->flags & ULT_16BIT)
{
pins->uFlags |= CHN_16BIT;
pins->nLoopStart >>= 1;
pins->nLoopEnd >>= 1;
}
}
memcpy(Order, lpStream+dwMemPos, 256);
dwMemPos += 256;
m_nChannels = lpStream[dwMemPos] + 1;
nop = lpStream[dwMemPos+1] + 1;
dwMemPos += 2;
if (m_nChannels > 32) m_nChannels = 32;
// Default channel settings
for (UINT nSet=0; nSet<m_nChannels; nSet++)
{
ChnSettings[nSet].nVolume = 64;
ChnSettings[nSet].nPan = (nSet & 1) ? 0x40 : 0xC0;
}
// read pan position table for v1.5 and higher
if(pmh->id[14]>='3')
{
if (dwMemPos + m_nChannels > dwMemLength) return TRUE;
for(UINT t=0; t<m_nChannels; t++)
{
ChnSettings[t].nPan = (lpStream[dwMemPos++] << 4) + 8;
if (ChnSettings[t].nPan > 256) ChnSettings[t].nPan = 256;
}
}
// Allocating Patterns
for (UINT nAllocPat=0; nAllocPat<nop; nAllocPat++)
{
if (nAllocPat < MAX_PATTERNS)
{
PatternSize[nAllocPat] = 64;
Patterns[nAllocPat] = AllocatePattern(64, m_nChannels);
}
}
// Reading Patterns
for (UINT nChn=0; nChn<m_nChannels; nChn++)
{
for (UINT nPat=0; nPat<nop; nPat++)
{
MODCOMMAND *pat = NULL;
if (nPat < MAX_PATTERNS)
{
pat = Patterns[nPat];
if (pat) pat += nChn;
}
UINT row = 0;
while (row < 64)
{
if (dwMemPos + 6 > dwMemLength) return TRUE;
UINT rep = 1;
UINT note = lpStream[dwMemPos++];
if (note == 0xFC)
{
rep = lpStream[dwMemPos];
note = lpStream[dwMemPos+1];
dwMemPos += 2;
}
UINT instr = lpStream[dwMemPos++];
UINT eff = lpStream[dwMemPos++];
UINT dat1 = lpStream[dwMemPos++];
UINT dat2 = lpStream[dwMemPos++];
UINT cmd1 = eff & 0x0F;
UINT cmd2 = eff >> 4;
if (cmd1 == 0x0C) dat1 >>= 2; else
if (cmd1 == 0x0B) { cmd1 = dat1 = 0; }
if (cmd2 == 0x0C) dat2 >>= 2; else
if (cmd2 == 0x0B) { cmd2 = dat2 = 0; }
while ((rep != 0) && (row < 64))
{
if (pat)
{
pat->instr = instr;
if (note) pat->note = note + 36;
if (cmd1 | dat1)
{
if (cmd1 == 0x0C)
{
pat->volcmd = VOLCMD_VOLUME;
pat->vol = dat1;
} else
{
pat->command = cmd1;
pat->param = dat1;
ConvertModCommand(pat);
}
}
if (cmd2 == 0x0C)
{
pat->volcmd = VOLCMD_VOLUME;
pat->vol = dat2;
} else
if ((cmd2 | dat2) && (!pat->command))
{
pat->command = cmd2;
pat->param = dat2;
ConvertModCommand(pat);
}
pat += m_nChannels;
}
row++;
rep--;
}
}
}
}
// Reading Instruments
for (UINT smp=1; smp<=m_nSamples; smp++) if (Ins[smp].nLength)
{
if (dwMemPos >= dwMemLength) return TRUE;
UINT flags = (Ins[smp].uFlags & CHN_16BIT) ? RS_PCM16S : RS_PCM8S;
dwMemPos += ReadSample(&Ins[smp], flags, (LPSTR)(lpStream+dwMemPos), dwMemLength - dwMemPos);
}
return TRUE;
}