summaryrefslogtreecommitdiffstats
path: root/apps/plugins/mikmod/load_dsm.c
blob: 59abf0bcfebb9e8a107acd0c9448e0d5bb675fc1 (plain)
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
/*	MikMod sound library
	(c) 1998, 1999, 2000, 2001, 2002 Miodrag Vallat and others - see file
	AUTHORS for complete list.

	This library is free software; you can redistribute it and/or modify
	it under the terms of the GNU Library General Public License as
	published by the Free Software Foundation; either version 2 of
	the License, or (at your option) any later version.
 
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Library General Public License for more details.
 
	You should have received a copy of the GNU Library General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
	02111-1307, USA.
*/

/*==============================================================================

  $Id: load_dsm.c,v 1.3 2005/04/07 19:57:38 realtech Exp $

  DSIK internal format (DSM) module loader

==============================================================================*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <stdio.h>
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <string.h>

#include "mikmod_internals.h"

#ifdef SUNOS
extern int fprintf(FILE *, const char *, ...);
#endif

/*========== Module structure */

#define DSM_MAXCHAN (16)
#define DSM_MAXORDERS (128)

typedef struct DSMSONG {
	CHAR	songname[28];
	UWORD	version;
	UWORD	flags;
	ULONG	reserved2;
	UWORD	numord;
	UWORD	numsmp;
	UWORD	numpat;
	UWORD	numtrk;
	UBYTE	globalvol;
	UBYTE	mastervol;
	UBYTE	speed;
	UBYTE	bpm;
	UBYTE	panpos[DSM_MAXCHAN];
	UBYTE	orders[DSM_MAXORDERS];
} DSMSONG;

typedef struct DSMINST {
	CHAR	filename[13];
	UWORD	flags;
	UBYTE	volume;
	ULONG	length;
	ULONG	loopstart;
	ULONG	loopend;
	ULONG	reserved1;
	UWORD	c2spd;
	UWORD	period;
	CHAR	samplename[28];
} DSMINST;

typedef struct DSMNOTE {
	UBYTE	note,ins,vol,cmd,inf;
} DSMNOTE;

#define DSM_SURROUND (0xa4)

/*========== Loader variables */

static	CHAR* SONGID="SONG";
static	CHAR* INSTID="INST";
static	CHAR* PATTID="PATT";

static	UBYTE blockid[4];
static	ULONG blockln;
static	ULONG blocklp;
static	DSMSONG* mh=NULL;
static	DSMNOTE* dsmbuf=NULL;

static	CHAR DSM_Version[]="DSIK DSM-format";

static	unsigned char DSMSIG[4+4]={'R','I','F','F','D','S','M','F'};

/*========== Loader code */

static int DSM_Test(void)
{
	UBYTE id[12];

	if(!_mm_read_UBYTES(id,12,modreader)) return 0;
	if(!memcmp(id,DSMSIG,4) && !memcmp(id+8,DSMSIG+4,4)) return 1;

	return 0;
}

static int DSM_Init(void)
{
	if(!(dsmbuf=(DSMNOTE *)MikMod_malloc(DSM_MAXCHAN*64*sizeof(DSMNOTE)))) return 0;
	if(!(mh=(DSMSONG *)MikMod_calloc(1,sizeof(DSMSONG)))) return 0;
	return 1;
}

static void DSM_Cleanup(void)
{
	MikMod_free(dsmbuf);
	MikMod_free(mh);
}

static int GetBlockHeader(void)
{
	/* make sure we're at the right position for reading the
	   next riff block, no matter how many bytes read */
	_mm_fseek(modreader, blocklp+blockln, SEEK_SET);
   
	while(1) {
		_mm_read_UBYTES(blockid,4,modreader);
		blockln=_mm_read_I_ULONG(modreader);
		if(_mm_eof(modreader)) {
			_mm_errno = MMERR_LOADING_HEADER;
			return 0;
		}

		if(memcmp(blockid,SONGID,4) && memcmp(blockid,INSTID,4) &&
		   memcmp(blockid,PATTID,4)) {
#ifdef MIKMOD_DEBUG
				fprintf(stderr,"\rDSM: Skipping unknown block type %4.4s\n",blockid);
#endif
				_mm_fseek(modreader, blockln, SEEK_CUR);
		} else
			break;
	}

	blocklp = _mm_ftell(modreader);

	return 1;
}

static int DSM_ReadPattern(void)
{
	int flag,row=0;
	SWORD length;
	DSMNOTE *n;

	/* clear pattern data */
	memset(dsmbuf,255,DSM_MAXCHAN*64*sizeof(DSMNOTE));
	length=_mm_read_I_SWORD(modreader);

	while(row<64) {
		flag=_mm_read_UBYTE(modreader);
		if((_mm_eof(modreader))||(--length<0)) {
			_mm_errno = MMERR_LOADING_PATTERN;
			return 0;
		}

		if(flag) {
			n=&dsmbuf[((flag&0xf)*64)+row];
			if(flag&0x80) n->note=_mm_read_UBYTE(modreader);
			if(flag&0x40) n->ins=_mm_read_UBYTE(modreader);
			if(flag&0x20) n->vol=_mm_read_UBYTE(modreader);
			if(flag&0x10) {
				n->cmd=_mm_read_UBYTE(modreader);
				n->inf=_mm_read_UBYTE(modreader);
			}
		} else
			row++;
	}

	return 1;
}

static UBYTE *DSM_ConvertTrack(DSMNOTE *tr)
{
	int t;
	UBYTE note,ins,vol,cmd,inf;

	UniReset();
	for(t=0;t<64;t++) {
		note=tr[t].note;
		ins=tr[t].ins;
		vol=tr[t].vol;
		cmd=tr[t].cmd;
		inf=tr[t].inf;

		if(ins!=0 && ins!=255) UniInstrument(ins-1);
		if(note!=255) UniNote(note-1); /* normal note */
		if(vol<65) UniPTEffect(0xc,vol);

		if(cmd!=255) {
			if(cmd==0x8) {
				if(inf==DSM_SURROUND)
					UniEffect(UNI_ITEFFECTS0,0x91);
				else
				  if(inf<=0x80) {
					inf=(inf<0x80)?inf<<1:255;
					UniPTEffect(cmd,inf);
				}
			} else
			  if(cmd==0xb) {
				if(inf<=0x7f) UniPTEffect(cmd,inf);
			} else {
				/* Convert pattern jump from Dec to Hex */
				if(cmd == 0xd)
					inf = (((inf&0xf0)>>4)*10)+(inf&0xf);
				UniPTEffect(cmd,inf);
			}
		}
		UniNewline();
	}
	return UniDup();
}

static int DSM_Load(int curious)
{
	int t;
	DSMINST s;
	SAMPLE *q;
	int cursmp=0,curpat=0,track=0;
    (void)curious;

	blocklp=0;
	blockln=12;

	if(!GetBlockHeader()) return 0;
	if(memcmp(blockid,SONGID,4)) {
		_mm_errno = MMERR_LOADING_HEADER;
		return 0;
	}

	_mm_read_UBYTES(mh->songname,28,modreader);
	mh->version=_mm_read_I_UWORD(modreader);
	mh->flags=_mm_read_I_UWORD(modreader);
	mh->reserved2=_mm_read_I_ULONG(modreader);
	mh->numord=_mm_read_I_UWORD(modreader);
	mh->numsmp=_mm_read_I_UWORD(modreader);
	mh->numpat=_mm_read_I_UWORD(modreader);
	mh->numtrk=_mm_read_I_UWORD(modreader);
	mh->globalvol=_mm_read_UBYTE(modreader);
	mh->mastervol=_mm_read_UBYTE(modreader);
	mh->speed=_mm_read_UBYTE(modreader);
	mh->bpm=_mm_read_UBYTE(modreader);
	_mm_read_UBYTES(mh->panpos,DSM_MAXCHAN,modreader);
	_mm_read_UBYTES(mh->orders,DSM_MAXORDERS,modreader);

	/* set module variables */
	of.initspeed=mh->speed;
	of.inittempo=mh->bpm;
	of.modtype=StrDup(DSM_Version);
	of.numchn=mh->numtrk;
	of.numpat=mh->numpat;
	of.numtrk=of.numchn*of.numpat;
	of.songname=DupStr(mh->songname,28,1); /* make a cstr of songname */
	of.reppos=0;
	of.flags |= UF_PANNING;
	/* XXX whenever possible, we should try to determine the original format.
	   Here we assume it was S3M-style wrt bpmlimit... */
	of.bpmlimit = 32;

	for(t=0;t<DSM_MAXCHAN;t++)
		of.panning[t]=mh->panpos[t]==DSM_SURROUND?PAN_SURROUND:
		              mh->panpos[t]<0x80?(mh->panpos[t]<<1):255;

	if(!AllocPositions(mh->numord)) return 0;
	of.numpos=0;
	for(t=0;t<mh->numord;t++) {
		int order=mh->orders[t];
		if(order==255) order=LAST_PATTERN;
		of.positions[of.numpos]=order;
		if(mh->orders[t]<254) of.numpos++;
	}

	of.numins=of.numsmp=mh->numsmp;

	if(!AllocSamples()) return 0;
	if(!AllocTracks()) return 0;
	if(!AllocPatterns()) return 0;

	while(cursmp<of.numins||curpat<of.numpat) {
		if(!GetBlockHeader()) return 0;
		if(!memcmp(blockid,INSTID,4) && cursmp<of.numins) {
			q=&of.samples[cursmp];

			/* try to read sample info */
			_mm_read_UBYTES(s.filename,13,modreader);
			s.flags=_mm_read_I_UWORD(modreader);
			s.volume=_mm_read_UBYTE(modreader);
			s.length=_mm_read_I_ULONG(modreader);
			s.loopstart=_mm_read_I_ULONG(modreader);
			s.loopend=_mm_read_I_ULONG(modreader);
			s.reserved1=_mm_read_I_ULONG(modreader);
			s.c2spd=_mm_read_I_UWORD(modreader);
			s.period=_mm_read_I_UWORD(modreader);
			_mm_read_UBYTES(s.samplename,28,modreader);

			q->samplename=DupStr(s.samplename,28,1);
			q->seekpos=_mm_ftell(modreader);
			q->speed=s.c2spd;
			q->length=s.length;
			q->loopstart=s.loopstart;
			q->loopend=s.loopend;
			q->volume=s.volume;

			if(s.flags&1) q->flags|=SF_LOOP;
			if(s.flags&2) q->flags|=SF_SIGNED;
			/* (s.flags&4) means packed sample,
			   but did they really exist in dsm ?*/
			cursmp++;
		} else
		  if(!memcmp(blockid,PATTID,4) && curpat<of.numpat) {
			DSM_ReadPattern();
			for(t=0;t<of.numchn;t++)
				if(!(of.tracks[track++]=DSM_ConvertTrack(&dsmbuf[t*64]))) return 0;
			curpat++;
		}
	}

	return 1;
}

static CHAR *DSM_LoadTitle(void)
{
	CHAR s[28];

	_mm_fseek(modreader,12,SEEK_SET);
	if(!_mm_read_UBYTES(s,28,modreader)) return NULL;
   
	return(DupStr(s,28,1));
}

/*========== Loader information */

MIKMODAPI MLOADER load_dsm={
	NULL,
	"DSM",
	"DSM (DSIK internal format)",
	DSM_Init,
	DSM_Test,
	DSM_Load,
	DSM_Cleanup,
	DSM_LoadTitle
};


/* ex:set ts=4: */