Diffstat (limited to 'core/multimedia/opieplayer/modplug/load_mtm.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/multimedia/opieplayer/modplug/load_mtm.cpp | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/core/multimedia/opieplayer/modplug/load_mtm.cpp b/core/multimedia/opieplayer/modplug/load_mtm.cpp new file mode 100644 index 0000000..7d40b1d --- a/dev/null +++ b/core/multimedia/opieplayer/modplug/load_mtm.cpp | |||
@@ -0,0 +1,167 @@ | |||
1 | /* | ||
2 | * This program is free software; you can redistribute it and modify it | ||
3 | * under the terms of the GNU General Public License as published by the | ||
4 | * Free Software Foundation; either version 2 of the license or (at your | ||
5 | * option) any later version. | ||
6 | * | ||
7 | * Authors: Olivier Lapicque <olivierl@jps.net> | ||
8 | */ | ||
9 | |||
10 | #include "stdafx.h" | ||
11 | #include "sndfile.h" | ||
12 | |||
13 | //#pragma warning(disable:4244) | ||
14 | |||
15 | ////////////////////////////////////////////////////////// | ||
16 | // MTM file support (import only) | ||
17 | |||
18 | #pragma pack(1) | ||
19 | |||
20 | |||
21 | typedef struct tagMTMSAMPLE | ||
22 | { | ||
23 | char samplename[22]; // changed from CHAR | ||
24 | DWORD length; | ||
25 | DWORD reppos; | ||
26 | DWORD repend; | ||
27 | CHAR finetune; | ||
28 | BYTE volume; | ||
29 | BYTE attribute; | ||
30 | } Q_PACKED MTMSAMPLE; | ||
31 | |||
32 | |||
33 | typedef struct tagMTMHEADER | ||
34 | { | ||
35 | char id[4]; // MTM file marker + version // changed from CHAR | ||
36 | char songname[20];// ASCIIZ songname // changed from CHAR | ||
37 | WORD numtracks; // number of tracks saved | ||
38 | BYTE lastpattern;// last pattern number saved | ||
39 | BYTE lastorder; // last order number to play (songlength-1) | ||
40 | WORD commentsize;// length of comment field | ||
41 | BYTE numsamples;// number of samples saved | ||
42 | BYTE attribute; // attribute byte (unused) | ||
43 | BYTE beatspertrack; | ||
44 | BYTE numchannels;// number of channels used | ||
45 | BYTE panpos[32];// voice pan positions | ||
46 | } Q_PACKED MTMHEADER; | ||
47 | |||
48 | |||
49 | #pragma pack() | ||
50 | |||
51 | |||
52 | BOOL CSoundFile::ReadMTM(LPCBYTE lpStream, DWORD dwMemLength) | ||
53 | //----------------------------------------------------------- | ||
54 | { | ||
55 | MTMHEADER *pmh = (MTMHEADER *)lpStream; | ||
56 | DWORD dwMemPos = 66; | ||
57 | |||
58 | if ((!lpStream) || (dwMemLength < 0x100)) return FALSE; | ||
59 | if ((strncmp(pmh->id, "MTM", 3)) || (pmh->numchannels > 32) | ||
60 | || (pmh->numsamples >= MAX_SAMPLES) || (!pmh->numsamples) | ||
61 | || (!pmh->numtracks) || (!pmh->numchannels) | ||
62 | || (!pmh->lastpattern) || (pmh->lastpattern > MAX_PATTERNS)) return FALSE; | ||
63 | strncpy(m_szNames[0], pmh->songname, 20); | ||
64 | m_szNames[0][20] = 0; | ||
65 | if (dwMemPos + 37*pmh->numsamples + 128 + 192*pmh->numtracks | ||
66 | + 64 * (pmh->lastpattern+1) + pmh->commentsize >= dwMemLength) return FALSE; | ||
67 | m_nType = MOD_TYPE_MTM; | ||
68 | m_nSamples = pmh->numsamples; | ||
69 | m_nChannels = pmh->numchannels; | ||
70 | // Reading instruments | ||
71 | for(UINT i=1; i<=m_nSamples; i++) | ||
72 | { | ||
73 | MTMSAMPLE *pms = (MTMSAMPLE *)(lpStream + dwMemPos); | ||
74 | strncpy(m_szNames[i], pms->samplename, 22); | ||
75 | m_szNames[i][22] = 0; | ||
76 | Ins[i].nVolume = pms->volume << 2; | ||
77 | Ins[i].nGlobalVol = 64; | ||
78 | DWORD len = pms->length; | ||
79 | if ((len > 4) && (len <= MAX_SAMPLE_LENGTH)) | ||
80 | { | ||
81 | Ins[i].nLength = len; | ||
82 | Ins[i].nLoopStart = pms->reppos; | ||
83 | Ins[i].nLoopEnd = pms->repend; | ||
84 | if (Ins[i].nLoopEnd > Ins[i].nLength) Ins[i].nLoopEnd = Ins[i].nLength; | ||
85 | if (Ins[i].nLoopStart + 4 >= Ins[i].nLoopEnd) Ins[i].nLoopStart = Ins[i].nLoopEnd = 0; | ||
86 | if (Ins[i].nLoopEnd) Ins[i].uFlags |= CHN_LOOP; | ||
87 | Ins[i].nFineTune = MOD2XMFineTune(pms->finetune); | ||
88 | if (pms->attribute & 0x01) | ||
89 | { | ||
90 | Ins[i].uFlags |= CHN_16BIT; | ||
91 | Ins[i].nLength >>= 1; | ||
92 | Ins[i].nLoopStart >>= 1; | ||
93 | Ins[i].nLoopEnd >>= 1; | ||
94 | } | ||
95 | Ins[i].nPan = 128; | ||
96 | } | ||
97 | dwMemPos += 37; | ||
98 | } | ||
99 | // Setting Channel Pan Position | ||
100 | for (UINT ich=0; ich<m_nChannels; ich++) | ||
101 | { | ||
102 | ChnSettings[ich].nPan = ((pmh->panpos[ich] & 0x0F) << 4) + 8; | ||
103 | ChnSettings[ich].nVolume = 64; | ||
104 | } | ||
105 | // Reading pattern order | ||
106 | memcpy(Order, lpStream + dwMemPos, pmh->lastorder+1); | ||
107 | dwMemPos += 128; | ||
108 | // Reading Patterns | ||
109 | LPCBYTE pTracks = lpStream + dwMemPos; | ||
110 | dwMemPos += 192 * pmh->numtracks; | ||
111 | LPWORD pSeq = (LPWORD)(lpStream + dwMemPos); | ||
112 | for (UINT pat=0; pat<=pmh->lastpattern; pat++) | ||
113 | { | ||
114 | PatternSize[pat] = 64; | ||
115 | if ((Patterns[pat] = AllocatePattern(64, m_nChannels)) == NULL) break; | ||
116 | for (UINT n=0; n<32; n++) if ((pSeq[n]) && (pSeq[n] <= pmh->numtracks) && (n < m_nChannels)) | ||
117 | { | ||
118 | LPCBYTE p = pTracks + 192 * (pSeq[n]-1); | ||
119 | MODCOMMAND *m = Patterns[pat] + n; | ||
120 | for (UINT i=0; i<64; i++, m+=m_nChannels, p+=3) | ||
121 | { | ||
122 | if (p[0] & 0xFC) m->note = (p[0] >> 2) + 37; | ||
123 | m->instr = ((p[0] & 0x03) << 4) | (p[1] >> 4); | ||
124 | UINT cmd = p[1] & 0x0F; | ||
125 | UINT param = p[2]; | ||
126 | if (cmd == 0x0A) | ||
127 | { | ||
128 | if (param & 0xF0) param &= 0xF0; else param &= 0x0F; | ||
129 | } | ||
130 | m->command = cmd; | ||
131 | m->param = param; | ||
132 | if ((cmd) || (param)) ConvertModCommand(m); | ||
133 | } | ||
134 | } | ||
135 | pSeq += 32; | ||
136 | } | ||
137 | dwMemPos += 64*(pmh->lastpattern+1); | ||
138 | if ((pmh->commentsize) && (dwMemPos + pmh->commentsize < dwMemLength)) | ||
139 | { | ||
140 | UINT n = pmh->commentsize; | ||
141 | m_lpszSongComments = new char[n+1]; | ||
142 | if (m_lpszSongComments) | ||
143 | { | ||
144 | memcpy(m_lpszSongComments, lpStream+dwMemPos, n); | ||
145 | m_lpszSongComments[n] = 0; | ||
146 | for (UINT i=0; i<n; i++) | ||
147 | { | ||
148 | if (!m_lpszSongComments[i]) | ||
149 | { | ||
150 | m_lpszSongComments[i] = ((i+1) % 40) ? 0x20 : 0x0D; | ||
151 | } | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | dwMemPos += pmh->commentsize; | ||
156 | // Reading Samples | ||
157 | for (UINT ismp=1; ismp<=m_nSamples; ismp++) | ||
158 | { | ||
159 | if (dwMemPos >= dwMemLength) break; | ||
160 | dwMemPos += ReadSample(&Ins[ismp], (Ins[ismp].uFlags & CHN_16BIT) ? RS_PCM16U : RS_PCM8U, | ||
161 | (LPSTR)(lpStream + dwMemPos), dwMemLength - dwMemPos); | ||
162 | } | ||
163 | m_nMinPeriod = 64; | ||
164 | m_nMaxPeriod = 32767; | ||
165 | return TRUE; | ||
166 | } | ||
167 | |||