summaryrefslogtreecommitdiff
path: root/apps/plugins/mikmod/load_669.c
blob: 000e51d9859c609de3119af16e67a36efbf35ae5 (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
366
367
368
369
/*	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_669.c,v 1.3 2005/04/07 19:57:38 realtech Exp $

  Composer 669 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 */

/* header */
typedef struct S69HEADER {   
	UBYTE	marker[2];
	CHAR	message[108];
	UBYTE	nos;
	UBYTE	rbnop;
	UBYTE	looporder;
	UBYTE	orders[0x80];
	UBYTE	tempos[0x80];
	UBYTE	breaks[0x80];
} S69HEADER;

/* sample information */
typedef struct S69SAMPLE {
	CHAR	filename[13];
	SLONG	length;
	SLONG	loopbeg;
	SLONG	loopend;
} S69SAMPLE;

/* encoded note */
typedef struct S69NOTE {
	UBYTE	a,b,c;
} S69NOTE;

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

/* current pattern */
static	S69NOTE* s69pat=NULL;
/* Module header */
static	S69HEADER* mh=NULL;

/* file type identification */
static	CHAR* S69_Version[]={
	"Composer 669",
	"Extended 669"
};

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

static int S69_Test(void)
{
	UBYTE buf[0x80];

	if(!_mm_read_UBYTES(buf,2,modreader))
		return 0;
	/* look for id */
	if(!memcmp(buf,"if",2) || !memcmp(buf,"JN",2)) {
		int i;

		/* skip song message */
		_mm_fseek(modreader,108,SEEK_CUR);
		/* sanity checks */
		if(_mm_read_UBYTE(modreader) > 64) return 0;
		if(_mm_read_UBYTE(modreader) > 128) return 0;
		if(_mm_read_UBYTE(modreader) > 127) return 0;
		/* check order table */
		if(!_mm_read_UBYTES(buf,0x80,modreader)) return 0;
		for(i=0;i<0x80;i++)
			if((buf[i]>=0x80)&&(buf[i]!=0xff)) return 0;
		/* check tempos table */
		if(!_mm_read_UBYTES(buf,0x80,modreader)) return 0;
		for(i=0;i<0x80;i++)
			if((!buf[i])||(buf[i]>32)) return 0;
		/* check pattern length table */
		if(!_mm_read_UBYTES(buf,0x80,modreader)) return 0;
		for(i=0;i<0x80;i++)
			if(buf[i]>0x3f) return 0;
	} else
		return 0;

	return 1;
}

static int S69_Init(void)
{
	if(!(s69pat=(S69NOTE *)MikMod_malloc(64*8*sizeof(S69NOTE)))) return 0;
	if(!(mh=(S69HEADER *)MikMod_malloc(sizeof(S69HEADER)))) return 0;

	return 1;
}

static void S69_Cleanup(void)
{
	MikMod_free(s69pat);
	MikMod_free(mh);
}

static int S69_LoadPatterns(void)
{
	int track,row,channel;
	UBYTE note,inst,vol,effect,lastfx,lastval;
	S69NOTE *cur;
	int tracks=0;
	
	if(!AllocPatterns()) return 0;
	if(!AllocTracks()) return 0;

	for(track=0;track<of.numpat;track++) {
		/* set pattern break locations */
		of.pattrows[track]=mh->breaks[track]+1;

		/* load the 669 pattern */
		cur=s69pat;
		for(row=0;row<64;row++) {
			for(channel=0;channel<8;channel++,cur++) {
				cur->a = _mm_read_UBYTE(modreader);
				cur->b = _mm_read_UBYTE(modreader);
				cur->c = _mm_read_UBYTE(modreader);
			}
		}

		if(_mm_eof(modreader)) {
			_mm_errno = MMERR_LOADING_PATTERN;
			return 0;
		}

		/* translate the pattern */
		for(channel=0;channel<8;channel++) {
			UniReset();
			/* set pattern tempo */
			UniPTEffect(0xf,78);
			UniPTEffect(0xf,mh->tempos[track]);

			lastfx=0xff,lastval=0;

			for(row=0;row<=mh->breaks[track];row++) {
				int a,b,c;

				/* fetch the encoded note */
				a=s69pat[(row*8)+channel].a;
				b=s69pat[(row*8)+channel].b;
				c=s69pat[(row*8)+channel].c;

				/* decode it */
				note=a>>2;
				inst=((a&0x3)<<4)|((b&0xf0)>>4);
				vol=b&0xf;

				if (a<0xff) {
					if (a<0xfe) {
						UniInstrument(inst);
						UniNote(note+2*OCTAVE);
						lastfx=0xff; /* reset background effect memory */
					}
					UniPTEffect(0xc,vol<<2);
				}

				if ((c!=0xff)||(lastfx!=0xff)) {
					if(c==0xff)
						c=lastfx,effect=lastval;
					else
						effect=c&0xf;

					switch(c>>4) {
						case 0: /* porta up */
							UniPTEffect(0x1,effect);
							lastfx=c,lastval=effect;
							break;
						case 1: /* porta down */
							UniPTEffect(0x2,effect);
							lastfx=c,lastval=effect;
							break;
						case 2: /* porta to note */
							UniPTEffect(0x3,effect);
							lastfx=c,lastval=effect;
							break;
						case 3: /* frequency adjust */
							/* DMP converts this effect to S3M FF1. Why not ? */
							UniEffect(UNI_S3MEFFECTF,0xf0|effect);
							break;
						case 4: /* vibrato */
							UniPTEffect(0x4,effect);
							lastfx=c,lastval=effect;
							break;
						case 5: /* set speed */
							if (effect)
								UniPTEffect(0xf,effect);
							else 
							  if(mh->marker[0]!=0x69) {
#ifdef MIKMOD_DEBUG
								fprintf(stderr,"\r669: unsupported super fast tempo at pat=%d row=%d chan=%d\n",
								       track,row,channel);
#endif
							}
							break;
					}
				}
				UniNewline();
			}
			if(!(of.tracks[tracks++]=UniDup())) return 0;
		}
	}

	return 1;
}

static int S69_Load(int curious)
{
	int i;
	SAMPLE *current;
	S69SAMPLE sample;
    (void)curious;

	/* module header */
	_mm_read_UBYTES(mh->marker,2,modreader);
	_mm_read_UBYTES(mh->message,108,modreader);
	mh->nos=_mm_read_UBYTE(modreader);
	mh->rbnop=_mm_read_UBYTE(modreader);
	mh->looporder=_mm_read_UBYTE(modreader);
	_mm_read_UBYTES(mh->orders,0x80,modreader);
	for(i=0;i<0x80;i++)
		if ((mh->orders[i]>=0x80)&&(mh->orders[i]!=0xff)) {
			_mm_errno=MMERR_NOT_A_MODULE;
			return 1;
		}
	_mm_read_UBYTES(mh->tempos,0x80,modreader);
	for(i=0;i<0x80;i++)
		if ((!mh->tempos[i])||(mh->tempos[i]>32)) {
			_mm_errno=MMERR_NOT_A_MODULE;
			return 1;
		}
	_mm_read_UBYTES(mh->breaks,0x80,modreader);
	for(i=0;i<0x80;i++)
		if (mh->breaks[i]>0x3f) {
			_mm_errno=MMERR_NOT_A_MODULE;
			return 1;
		}

	/* set module variables */
	of.initspeed=4;
	of.inittempo=78;
	of.songname=DupStr(mh->message,36,1);
	of.modtype=StrDup(S69_Version[memcmp(mh->marker,"JN",2)==0]);
	of.numchn=8;
	of.numpat=mh->rbnop;
	of.numins=of.numsmp=mh->nos;
	of.numtrk=of.numchn*of.numpat;
	of.flags=UF_XMPERIODS|UF_LINEAR;

	for(i=   35;(i>=   0)&&(mh->message[i]==' ');i--) mh->message[i]=0;
	for(i=36+35;(i>=36+0)&&(mh->message[i]==' ');i--) mh->message[i]=0;
	for(i=72+35;(i>=72+0)&&(mh->message[i]==' ');i--) mh->message[i]=0;
	if((mh->message[0])||(mh->message[36])||(mh->message[72]))
		if((of.comment=(CHAR*)MikMod_malloc(3*(36+1)+1))) {
			strncpy(of.comment,mh->message,36);
			strcat(of.comment,"\r");
			if (mh->message[36]) strncat(of.comment,mh->message+36,36);
			strcat(of.comment,"\r");
			if (mh->message[72]) strncat(of.comment,mh->message+72,36);
			strcat(of.comment,"\r");
			of.comment[3*(36+1)]=0;
		}

	if(!AllocPositions(0x80)) return 0;
	for(i=0;i<0x80;i++) {
		if(mh->orders[i]>=mh->rbnop) break;
		of.positions[i]=mh->orders[i];
	}
	of.numpos=i;
	of.reppos=mh->looporder<of.numpos?mh->looporder:0;

	if(!AllocSamples()) return 0;
	current=of.samples;

	for(i=0;i<of.numins;i++) {
		/* sample information */
		_mm_read_UBYTES((UBYTE*)sample.filename,13,modreader);
		sample.length=_mm_read_I_SLONG(modreader);
		sample.loopbeg=_mm_read_I_SLONG(modreader);
		sample.loopend=_mm_read_I_SLONG(modreader);
		if (sample.loopend==0xfffff) sample.loopend=0;

		if((sample.length<0)||(sample.loopbeg<-1)||(sample.loopend<-1)) {
			_mm_errno = MMERR_LOADING_HEADER;
			return 0;
		}

		current->samplename=DupStr(sample.filename,13,1);
		current->seekpos=0;
		current->speed=0;
		current->length=sample.length;
		current->loopstart=sample.loopbeg;
		current->loopend=sample.loopend;
		current->flags=(sample.loopbeg<sample.loopend)?SF_LOOP:0;
		current->volume=64;

		current++;
	}

	if(!S69_LoadPatterns()) return 0;

	return 1;
}

static CHAR *S69_LoadTitle(void)
{
	CHAR s[36];

	_mm_fseek(modreader,2,SEEK_SET);
	if(!_mm_read_UBYTES(s,36,modreader)) return NULL;

	return(DupStr(s,36,1));
}

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

MIKMODAPI MLOADER load_669={
	NULL,
	"669",
	"669 (Composer 669, Unis 669)",
	S69_Init,
	S69_Test,
	S69_Load,
	S69_Cleanup,
	S69_LoadTitle
};

/* ex:set ts=4: */