summaryrefslogtreecommitdiff
path: root/apps/plugins/mikmod/load_okt.c
blob: 6a40b5fe03f55c97ddf61aa21411b13542b6faf7 (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*	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_okt.c,v 1.3 2005/04/07 19:57:38 realtech Exp $

  Oktalyzer (OKT) module loader

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

/*
	Written by UFO <ufo303@poczta.onet.pl>
	based on the file description compiled by Harald Zappe
	                                                      <zappe@gaea.sietec.de>

*/

#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 blocks */

/* sample information */
typedef struct OKTSAMPLE {
	CHAR sampname[20];
	ULONG len;
	UWORD loopbeg;
	UWORD looplen;
	UBYTE volume;
} OKTSAMPLE;

typedef struct OKTNOTE {
	UBYTE note, ins, eff, dat;
} OKTNOTE;

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

static OKTNOTE *okttrk = NULL;

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

static int OKT_Test(void)
{
	CHAR id[8];

	if (!_mm_read_UBYTES(id, 8, modreader))
		return 0;
	if (!memcmp(id, "OKTASONG", 8))
		return 1;

	return 0;
}

/*	Pattern analysis routine.
	Effects not implemented (yet) : (in decimal)
	11 Arpeggio 4: Change note every 50Hz tick between N,H,N,L
	12 Arpeggio 5: Change note every 50Hz tick between H,H,N
                   N = normal note being played in this channel (1-36)
                   L = normal note number minus upper four bits of 'data'.
                   H = normal note number plus  lower four bits of 'data'.
    13 Decrease note number by 'data' once per tick.
    17 Increase note number by 'data' once per tick.
    21 Decrease note number by 'data' once per line.
    30 Increase note number by 'data' once per line.
*/
static UBYTE *OKT_ConvertTrack(UBYTE patrows)
{
	int t;
	UBYTE ins, note, eff, dat;

	UniReset();
	for (t = 0; t < patrows; t++) {
		note = okttrk[t].note;
		ins = okttrk[t].ins;
		eff = okttrk[t].eff;
		dat = okttrk[t].dat;

		if (note) {
			UniNote(note + 3 * OCTAVE - 1);
			UniInstrument(ins);
		}

		if (eff)
			switch (eff) {
			case 1:				/* Porta Up */
				UniPTEffect(0x1, dat);
				break;
			case 2:				/* Portamento Down */
				UniPTEffect(0x2, dat);
				break;
			/* case 9: what is this? */
			case 10:			/* Arpeggio 3 */
			case 11:			/* Arpeggio 4 */
			case 12:			/* Arpeggio 5 */
				UniWriteByte(UNI_OKTARP);
				UniWriteByte(eff + 3 - 10);
				UniWriteByte(dat);
				break;
			case 15:			/* Amiga filter toggle, ignored */
				break;
			case 25:			/* Pattern Jump */
				dat = (dat >> 4) * 10 + (dat & 0x0f);
				UniPTEffect(0xb, dat);
				break;
			case 27:			/* Release - similar to Keyoff */
				UniWriteByte(UNI_KEYOFF);
				break;
			case 28:			/* Set Tempo */
				UniPTEffect(0xf, dat & 0x0f);
				break;
			case 31:			/* volume Control */
				if (dat <= 0x40)
					UniPTEffect(0xc, dat);
				else if (dat <= 0x50)
					UniEffect(UNI_XMEFFECTA, (dat - 0x40));	/* fast fade out */
				else if (dat <= 0x60)
					UniEffect(UNI_XMEFFECTA, (dat - 0x50) << 4);	/* fast fade in */
				else if (dat <= 0x70)
					UniEffect(UNI_XMEFFECTEB, (dat - 0x60));	/* slow fade out */
				else if (dat <= 0x80)
					UniEffect(UNI_XMEFFECTEA, (dat - 0x70));	/* slow fade in */
				break;
#ifdef MIKMOD_DEBUG
			default:
				fprintf(stderr, "\rUnimplemented effect (%02d,%02x)\n",
						eff, dat);
#endif
			}

		UniNewline();
	}
	return UniDup();
}

/* Read "channel modes" i.e. channel number and panning information */
static void OKT_doCMOD(void)
{
	/* amiga channel panning table */
	UBYTE amigapan[4] = { 0x00, 0xff, 0xff, 0x00 };
	int t;

	of.numchn = 0;
	of.flags |= UF_PANNING;

	for (t = 0; t < 4; t++)
		if (_mm_read_M_UWORD(modreader)) {
			/* two channels tied to the same Amiga hardware voice */
			of.panning[of.numchn++] = amigapan[t];
			of.panning[of.numchn++] = amigapan[t];
		} else
			/* one channel tied to the Amiga hardware voice */
			of.panning[of.numchn++] = amigapan[t];
}

/* Read sample information */
static int OKT_doSAMP(int len)
{
	int t;
	SAMPLE *q;
	OKTSAMPLE s;

	of.numins = of.numsmp = (len / 0x20);
	if (!AllocSamples())
		return 0;

	for (t = 0, q = of.samples; t < of.numins; t++, q++) {
		_mm_read_UBYTES(s.sampname, 20, modreader);
		s.len = _mm_read_M_ULONG(modreader);
		s.loopbeg = _mm_read_M_UWORD(modreader) * 2;
		s.looplen = _mm_read_M_UWORD(modreader) * 2;
		(void)_mm_read_UBYTE(modreader);
		s.volume = _mm_read_UBYTE(modreader);
		_mm_read_M_UWORD(modreader);

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

		if (!s.len)
			q->seekpos = q->length = q->loopstart = q->loopend = q->flags = 0;
		else {
			s.len--;
			/* sanity checks */
			if (s.loopbeg > s.len)
				s.loopbeg = s.len;
			if (s.loopbeg + s.looplen > s.len)
				s.looplen = s.len - s.loopbeg;
			if (s.looplen < 2)
				s.looplen = 0;

			q->length = s.len;
			q->loopstart = s.loopbeg;
			q->loopend = s.looplen + q->loopstart;
			q->volume = s.volume;
			q->flags = SF_SIGNED;

			if (s.looplen)
				q->flags |= SF_LOOP;
		}
		q->samplename = DupStr(s.sampname, 20, 1);
		q->speed = 8287;
	}
	return 1;
}

/* Read speed information */
static void OKT_doSPEE(void)
{
	int tempo = _mm_read_M_UWORD(modreader);

	of.initspeed = tempo;
}

/* Read song length information */
static void OKT_doSLEN(void)
{
	of.numpat = _mm_read_M_UWORD(modreader);
}

/* Read pattern length information */
static void OKT_doPLEN(void)
{
	of.numpos = _mm_read_M_UWORD(modreader);
}

/* Read order table */
static int OKT_doPATT(void)
{
	int t;

	if (!of.numpos || !AllocPositions(of.numpos))
		return 0;

	for (t = 0; t < 128; t++)
		if (t < of.numpos)
			of.positions[t] = _mm_read_UBYTE(modreader);
		else
			break;

	return 1;
}

static int OKT_doPBOD(int patnum)
{
	char *patbuf;
	int rows, i;
	int u;

	if (!patnum) {
		of.numtrk = of.numpat * of.numchn;

		if (!AllocTracks() || !AllocPatterns())
			return 0;
	}

	/* Read pattern */
	of.pattrows[patnum] = rows = _mm_read_M_UWORD(modreader);

	if (!(okttrk = (OKTNOTE *) MikMod_calloc(rows, sizeof(OKTNOTE))) ||
	    !(patbuf = (char *)MikMod_calloc(rows * of.numchn, sizeof(OKTNOTE))))
		return 0;
	_mm_read_UBYTES(patbuf, rows * of.numchn * sizeof(OKTNOTE), modreader);
	if (_mm_eof(modreader)) {
		_mm_errno = MMERR_LOADING_PATTERN;
		return 0;
	}

	for (i = 0; i < of.numchn; i++) {
		for (u = 0; u < rows; u++) {
			okttrk[u].note = patbuf[(u * of.numchn + i) * sizeof(OKTNOTE)];
			okttrk[u].ins = patbuf[(u * of.numchn + i) * sizeof(OKTNOTE) + 1];
			okttrk[u].eff = patbuf[(u * of.numchn + i) * sizeof(OKTNOTE) + 2];
			okttrk[u].dat = patbuf[(u * of.numchn + i) * sizeof(OKTNOTE) + 3];
		}

		if (!(of.tracks[patnum * of.numchn + i] = OKT_ConvertTrack(rows)))
			return 0;
	}
	MikMod_free(patbuf);
	MikMod_free(okttrk);
	return 1;
}

static void OKT_doSBOD(int insnum)
{
	of.samples[insnum].seekpos = _mm_ftell(modreader);
}

static int OKT_Load(int curious)
{
	UBYTE id[4];
	ULONG len;
	ULONG fp;
	int seen_cmod = 0, seen_samp = 0, seen_slen = 0, seen_plen = 0, seen_patt
			= 0, seen_spee = 0;
	int patnum = 0, insnum = 0;
    (void)curious;

	/* skip OKTALYZER header */
	_mm_fseek(modreader, 8, SEEK_SET);
	of.songname = StrDup("");

	of.modtype = StrDup("Amiga Oktalyzer");
	of.numpos = of.reppos = 0;
	
	/* default values */
	of.initspeed = 6;
	of.inittempo = 125;
	
	while (1) {
		/* read block header */
		_mm_read_UBYTES(id, 4, modreader);
		len = _mm_read_M_ULONG(modreader);
		
		if (_mm_eof(modreader))
			break;
		fp = _mm_ftell(modreader);
		
		if (!memcmp(id, "CMOD", 4)) {
			if (!seen_cmod) {
				OKT_doCMOD();
				seen_cmod = 1;
			} else {
				_mm_errno = MMERR_LOADING_HEADER;
				return 0;
			}
		} else if (!memcmp(id, "SAMP", 4)) {
			if (!seen_samp && OKT_doSAMP(len))
				seen_samp = 1;
			else {
				_mm_errno = MMERR_LOADING_HEADER;
				return 0;
			}
		} else if (!memcmp(id, "SPEE", 4)) {
			if (!seen_spee) {
				OKT_doSPEE();
				seen_spee = 1;
			} else {
				_mm_errno = MMERR_LOADING_HEADER;
				return 0;
			}
		} else if (!memcmp(id, "SLEN", 4)) {
			if (!seen_slen) {
				OKT_doSLEN();
				seen_slen = 1;
			} else {
				_mm_errno = MMERR_LOADING_HEADER;
				return 0;
			}
		} else if (!memcmp(id, "PLEN", 4)) {
			if (!seen_plen) {
				OKT_doPLEN();
				seen_plen = 1;
			} else {
				_mm_errno = MMERR_LOADING_HEADER;
				return 0;
			}
		} else if (!memcmp(id, "PATT", 4)) {
			if (!seen_plen) {
				_mm_errno = MMERR_LOADING_HEADER;
				return 0;
			}
			if (!seen_patt && OKT_doPATT())
				seen_patt = 1;
			else {
				_mm_errno = MMERR_LOADING_HEADER;
				return 0;
			}
		} else if (!memcmp(id,"PBOD", 4)) {
			/* need to know numpat and numchn */
			if (!seen_slen || !seen_cmod || (patnum >= of.numpat)) {
				_mm_errno = MMERR_LOADING_HEADER;
				return 0;
			}
			if (!OKT_doPBOD(patnum++)) {
				_mm_errno = MMERR_LOADING_PATTERN;
				return 0;
			}
		} else if (!memcmp(id,"SBOD",4)) {
			/* need to know numsmp */
			if (!seen_samp) {
				_mm_errno = MMERR_LOADING_HEADER;
				return 0;
			}
			while ((insnum < of.numins) && !of.samples[insnum].length)
				insnum++;
			if (insnum >= of.numins) {
				_mm_errno = MMERR_LOADING_HEADER;
				return 0;
			}
			OKT_doSBOD(insnum++);
		}

		/* goto next block start position */
		_mm_fseek(modreader, fp + len, SEEK_SET);
	}

	if (!seen_cmod || !seen_samp || !seen_patt ||
		!seen_slen || !seen_plen || (patnum != of.numpat)) {
		_mm_errno = MMERR_LOADING_HEADER;
		return 0;
	}

	return 1;
}

static CHAR *OKT_LoadTitle(void)
{
	return StrDup("");
}

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

MIKMODAPI MLOADER load_okt = {
	NULL,
	"OKT",
	"OKT (Amiga Oktalyzer)",
	NULL,
	OKT_Test,
	OKT_Load,
	NULL,
	OKT_LoadTitle
};

/* ex:set ts=4: */