A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/* MikMod sound library
2 (c) 1998, 1999, 2000, 2001, 2002 Miodrag Vallat and others - see file
3 AUTHORS for complete list.
4
5 This library is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 02111-1307, USA.
19*/
20
21/*==============================================================================
22
23 $Id$
24
25 MTM module loader
26
27==============================================================================*/
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#ifdef HAVE_UNISTD_H
34#include <unistd.h>
35#endif
36
37#include <stdio.h>
38#ifdef HAVE_MEMORY_H
39#include <memory.h>
40#endif
41#include <string.h>
42
43#include "mikmod_internals.h"
44
45#ifdef SUNOS
46extern int fprintf(FILE *, const char *, ...);
47#endif
48
49/*========== Module structure */
50
51typedef struct MTMHEADER {
52 UBYTE id[3]; /* MTM file marker */
53 UBYTE version; /* upper major, lower nibble minor version number */
54 CHAR songname[20]; /* ASCIIZ songname */
55 UWORD numtracks; /* number of tracks saved */
56 UBYTE lastpattern; /* last pattern number saved */
57 UBYTE lastorder; /* last order number to play (songlength-1) */
58 UWORD commentsize; /* length of comment field */
59 UBYTE numsamples; /* number of samples saved */
60 UBYTE attribute; /* attribute byte (unused) */
61 UBYTE beatspertrack;
62 UBYTE numchannels; /* number of channels used */
63 UBYTE panpos[32]; /* voice pan positions */
64} MTMHEADER;
65
66typedef struct MTMSAMPLE {
67 CHAR samplename[22];
68 ULONG length;
69 ULONG reppos;
70 ULONG repend;
71 UBYTE finetune;
72 UBYTE volume;
73 UBYTE attribute;
74} MTMSAMPLE;
75
76typedef struct MTMNOTE {
77 UBYTE a,b,c;
78} MTMNOTE;
79
80/*========== Loader variables */
81
82static MTMHEADER *mh = NULL;
83static MTMNOTE *mtmtrk = NULL;
84static UWORD pat[32];
85
86static CHAR MTM_Version[] = "MTM";
87
88/*========== Loader code */
89
90static int MTM_Test(void)
91{
92 UBYTE id[3];
93
94 if(!_mm_read_UBYTES(id,3,modreader)) return 0;
95 if(!memcmp(id,"MTM",3)) return 1;
96 return 0;
97}
98
99static int MTM_Init(void)
100{
101 if(!(mtmtrk=(MTMNOTE*)MikMod_calloc(64,sizeof(MTMNOTE)))) return 0;
102 if(!(mh=(MTMHEADER*)MikMod_malloc(sizeof(MTMHEADER)))) return 0;
103
104 return 1;
105}
106
107static void MTM_Cleanup(void)
108{
109 MikMod_free(mtmtrk);
110 MikMod_free(mh);
111 mtmtrk=NULL;
112 mh=NULL;
113}
114
115static UBYTE* MTM_Convert(void)
116{
117 int t;
118 UBYTE a,b,inst,note,eff,dat;
119
120 UniReset();
121 for(t=0;t<64;t++) {
122 a=mtmtrk[t].a;
123 b=mtmtrk[t].b;
124 inst=((a&0x3)<<4)|(b>>4);
125 note=a>>2;
126 eff=b&0xf;
127 dat=mtmtrk[t].c;
128
129 if(inst) UniInstrument(inst-1);
130 if(note) UniNote(note+2*OCTAVE);
131
132 /* MTM bug workaround : when the effect is volslide, slide-up *always*
133 overrides slide-down. */
134 if(eff==0xa && (dat&0xf0)) dat&=0xf0;
135
136 /* Convert pattern jump from Dec to Hex */
137 if(eff==0xd)
138 dat=(((dat&0xf0)>>4)*10)+(dat&0xf);
139 UniPTEffect(eff,dat);
140 UniNewline();
141 }
142 return UniDup();
143}
144
145static int MTM_Load(int curious)
146{
147 int t,u;
148 MTMSAMPLE s;
149 SAMPLE *q;
150 (void)curious;
151
152 /* try to read module header */
153 _mm_read_UBYTES(mh->id,3,modreader);
154 mh->version =_mm_read_UBYTE(modreader);
155 _mm_read_string(mh->songname,20,modreader);
156 mh->numtracks =_mm_read_I_UWORD(modreader);
157 mh->lastpattern =_mm_read_UBYTE(modreader);
158 mh->lastorder =_mm_read_UBYTE(modreader);
159 mh->commentsize =_mm_read_I_UWORD(modreader);
160 mh->numsamples =_mm_read_UBYTE(modreader);
161 mh->attribute =_mm_read_UBYTE(modreader);
162 mh->beatspertrack=_mm_read_UBYTE(modreader);
163 mh->numchannels =_mm_read_UBYTE(modreader);
164 _mm_read_UBYTES(mh->panpos,32,modreader);
165
166 if(_mm_eof(modreader)) {
167 _mm_errno = MMERR_LOADING_HEADER;
168 return 0;
169 }
170
171 /* set module variables */
172 of.initspeed = 6;
173 of.inittempo = 125;
174 of.modtype = MikMod_strdup(MTM_Version);
175 of.numchn = mh->numchannels;
176 of.numtrk = mh->numtracks+1; /* get number of channels */
177 of.songname = DupStr(mh->songname,20,1); /* make a cstr of songname */
178 of.numpos = mh->lastorder+1; /* copy the songlength */
179 of.numpat = mh->lastpattern+1;
180 of.reppos = 0;
181 of.flags |= UF_PANNING;
182 for(t=0;t<32;t++) of.panning[t]=mh->panpos[t]<< 4;
183 of.numins=of.numsmp=mh->numsamples;
184
185 if(!AllocSamples()) return 0;
186 q=of.samples;
187 for(t=0;t<of.numins;t++) {
188 /* try to read sample info */
189 _mm_read_string(s.samplename,22,modreader);
190 s.length =_mm_read_I_ULONG(modreader);
191 s.reppos =_mm_read_I_ULONG(modreader);
192 s.repend =_mm_read_I_ULONG(modreader);
193 s.finetune =_mm_read_UBYTE(modreader);
194 s.volume =_mm_read_UBYTE(modreader);
195 s.attribute =_mm_read_UBYTE(modreader);
196
197 if(_mm_eof(modreader)) {
198 _mm_errno = MMERR_LOADING_SAMPLEINFO;
199 return 0;
200 }
201
202 q->samplename = DupStr(s.samplename,22,1);
203 q->seekpos = 0;
204 q->speed = finetune[s.finetune];
205 q->length = s.length;
206 q->loopstart = s.reppos;
207 q->loopend = s.repend;
208 q->volume = s.volume;
209 if((s.repend-s.reppos)>2) q->flags |= SF_LOOP;
210
211 if(s.attribute&1) {
212 /* If the sample is 16-bits, convert the length and replen
213 byte-values into sample-values */
214 q->flags|=SF_16BITS;
215 q->length>>=1;
216 q->loopstart>>=1;
217 q->loopend>>=1;
218 }
219 q++;
220 }
221
222 if(!AllocPositions(of.numpos)) return 0;
223 for(t=0;t<of.numpos;t++) {
224 of.positions[t]=_mm_read_UBYTE(modreader);
225 if (of.positions[t]>of.numpat) { /* SANITIY CHECK */
226 /* fprintf(stderr,"positions[%d]=%d > numpat=%d\n",t,of.positions[t],of.numpat);*/
227 _mm_errno = MMERR_LOADING_HEADER;
228 return 0;
229 }
230 }
231 for(;t<128;t++) _mm_skip_BYTE(modreader);
232 if(_mm_eof(modreader)) {
233 _mm_errno = MMERR_LOADING_HEADER;
234 return 0;
235 }
236
237 if(!AllocTracks()) return 0;
238 if(!AllocPatterns()) return 0;
239
240 of.tracks[0]=MTM_Convert(); /* track 0 is empty */
241 for(t=1;t<of.numtrk;t++) {
242 int s_idx;
243
244 for(s_idx=0;s_idx<64;s_idx++) {
245 mtmtrk[s_idx].a=_mm_read_UBYTE(modreader);
246 mtmtrk[s_idx].b=_mm_read_UBYTE(modreader);
247 mtmtrk[s_idx].c=_mm_read_UBYTE(modreader);
248 }
249
250 if(_mm_eof(modreader)) {
251 _mm_errno = MMERR_LOADING_TRACK;
252 return 0;
253 }
254
255 if(!(of.tracks[t]=MTM_Convert())) return 0;
256 }
257
258 for(t=0;t<of.numpat;t++) {
259 _mm_read_I_UWORDS(pat,32,modreader);
260 for(u=0;u<of.numchn;u++)
261 of.patterns[((long)t*of.numchn)+u]=pat[u];
262 }
263
264 /* read comment field */
265 if(mh->commentsize)
266 if(!ReadLinedComment(mh->commentsize, 40)) return 0;
267
268 return 1;
269}
270
271static CHAR *MTM_LoadTitle(void)
272{
273 CHAR s[20];
274
275 _mm_fseek(modreader,4,SEEK_SET);
276 if(!_mm_read_UBYTES(s,20,modreader)) return NULL;
277
278 return(DupStr(s,20,1));
279}
280
281/*========== Loader information */
282
283MIKMODAPI MLOADER load_mtm={
284 NULL,
285 "MTM",
286 "MTM (MultiTracker Module editor)",
287 MTM_Init,
288 MTM_Test,
289 MTM_Load,
290 MTM_Cleanup,
291 MTM_LoadTitle
292};
293
294/* ex:set ts=4: */