File: dt_load.c

package info (click to toggle)
xmp 3.4.0-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,212 kB
  • sloc: ansic: 47,206; sh: 2,933; asm: 1,013; makefile: 397; xml: 47; cpp: 40
file content (258 lines) | stat: -rw-r--r-- 5,590 bytes parent folder | download | duplicates (2)
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
/* Extended Module Player
 * Copyright (C) 1996-2010 Claudio Matsuoka and Hipolito Carraro Jr
 *
 * This file is part of the Extended Module Player and is distributed
 * under the terms of the GNU General Public License. See doc/COPYING
 * for more information.
 */

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

#include <assert.h>

#include "load.h"
#include "iff.h"
#include "period.h"

#define MAGIC_D_T_	MAGIC4('D','.','T','.')


static int dt_test(FILE *, char *, const int);
static int dt_load (struct xmp_context *, FILE *, const int);

struct xmp_loader_info dt_loader = {
	"DTM",
	"Digital Tracker",
	dt_test,
	dt_load
};

static int dt_test(FILE *f, char *t, const int start)
{
	if (read32b(f) != MAGIC_D_T_)
		return -1;

	read_title(f, t, 0);

	return 0;
}


static int pflag, sflag;
static int realpat;


static void get_d_t_(struct xmp_context *ctx, int size, FILE *f)
{
	struct xmp_player_context *p = &ctx->p;
	struct xmp_mod_context *m = &p->m;
	int b;

	read16b(f);			/* type */
	read16b(f);			/* 0xff then mono */
	read16b(f);			/* reserved */
	m->xxh->tpo = read16b(f);
	if ((b = read16b(f)) > 0)	/* RAMBO.DTM has bpm 0 */
		m->xxh->bpm = b;
	read32b(f);			/* undocumented */

	fread(m->name, 32, 1, f);
	strcpy(m->type, "DTM (Digital Tracker)");

	MODULE_INFO();
}

static void get_s_q_(struct xmp_context *ctx, int size, FILE *f)
{
	struct xmp_player_context *p = &ctx->p;
	struct xmp_mod_context *m = &p->m;
	int i, maxpat;

	m->xxh->len = read16b(f);
	m->xxh->rst = read16b(f);
	read32b(f);	/* reserved */

	for (maxpat = i = 0; i < 128; i++) {
		m->xxo[i] = read8(f);
		if (m->xxo[i] > maxpat)
			maxpat = m->xxo[i];
	}
	m->xxh->pat = maxpat + 1;
}

static void get_patt(struct xmp_context *ctx, int size, FILE *f)
{
	struct xmp_player_context *p = &ctx->p;
	struct xmp_mod_context *m = &p->m;

	m->xxh->chn = read16b(f);
	realpat = read16b(f);
	m->xxh->trk = m->xxh->chn * m->xxh->pat;
}

static void get_inst(struct xmp_context *ctx, int size, FILE *f)
{
	struct xmp_player_context *p = &ctx->p;
	struct xmp_mod_context *m = &p->m;
	int i, c2spd;
	uint8 name[30];

	m->xxh->ins = m->xxh->smp = read16b(f);
	reportv(ctx, 0, "Instruments    : %d ", m->xxh->ins);

	INSTRUMENT_INIT();

	reportv(ctx, 1, "\n     Instrument name        Len   LBeg  LSize LS Res Vol Fine C2Spd");
	for (i = 0; i < m->xxh->ins; i++) {
		int fine, replen, flag;

		m->xxi[i] = calloc(sizeof(struct xxm_instrument), 1);

		read32b(f);		/* reserved */
		m->xxs[i].len = read32b(f);
		m->xxih[i].nsm = !!m->xxs[i].len;
		fine = read8s(f);	/* finetune */
		m->xxi[i][0].vol = read8(f);
		m->xxi[i][0].pan = 0x80;
		m->xxs[i].lps = read32b(f);
		replen = read32b(f);
		m->xxs[i].lpe = m->xxs[i].lps + replen - 1;
		m->xxs[i].flg = replen > 2 ?  WAVE_LOOPING : 0;

		fread(name, 22, 1, f);
		copy_adjust(m->xxih[i].name, name, 22);

		flag = read16b(f);	/* bit 0-7:resol 8:stereo */
		m->xxs[i].flg |= (flag & 0xff) > 8 ? WAVE_16_BITS : 0;

		read32b(f);		/* midi note (0x00300000) */
		c2spd = read32b(f);	/* frequency */
		c2spd_to_note(c2spd, &m->xxi[i][0].xpo, &m->xxi[i][0].fin);

		/* It's strange that we have both c2spd and finetune */
		m->xxi[i][0].fin += fine;

		m->xxi[i][0].sid = i;

		if (strlen((char *)m->xxih[i].name) || m->xxs[i].len > 0) {
			if (V(1))
				report("\n[%2X] %-22.22s %05x%c%05x %05x %c%c %2db V%02x F%+03d %5d",
					i, m->xxih[i].name,
					m->xxs[i].len,
					m->xxs[i].flg & WAVE_16_BITS ? '+' : ' ',
					m->xxs[i].lps,
					replen,
					m->xxs[i].flg & WAVE_LOOPING ? 'L' : ' ',
					flag & 0x100 ? 'S' : ' ',
					flag & 0xff,
					m->xxi[i][0].vol,
					fine,
					c2spd);
			else
				report(".");
		}
	}
	reportv(ctx, 0, "\n");
}

static void get_dapt(struct xmp_context *ctx, int size, FILE *f)
{
	struct xmp_player_context *p = &ctx->p;
	struct xmp_mod_context *m = &p->m;
	int pat, i, j, k;
	struct xxm_event *event;
	static int last_pat;
	int rows;

	if (!pflag) {
		reportv(ctx, 0, "Stored patterns: %d ", m->xxh->pat);
		pflag = 1;
		last_pat = 0;
		PATTERN_INIT();
	}

	read32b(f);	/* 0xffffffff */
	i = pat = read16b(f);
	rows = read16b(f);

	for (i = last_pat; i <= pat; i++) {
		PATTERN_ALLOC(i);
		m->xxp[i]->rows = rows;
		TRACK_ALLOC(i);
	}
	last_pat = pat + 1;

	for (j = 0; j < rows; j++) {
		for (k = 0; k < m->xxh->chn; k++) {
			uint8 a, b, c, d;

			event = &EVENT(pat, k, j);
			a = read8(f);
			b = read8(f);
			c = read8(f);
			d = read8(f);
			if (a) {
				a--;
				event->note = 12 * (a >> 4) + (a & 0x0f);
			}
			event->vol = (b & 0xfc) >> 2;
			event->ins = ((b & 0x03) << 4) + (c >> 4);
			event->fxt = c & 0xf;
			event->fxp = d;
		}
	}

	reportv(ctx, 0, ".");
}

static void get_dait(struct xmp_context *ctx, int size, FILE *f)
{
	struct xmp_player_context *p = &ctx->p;
	struct xmp_mod_context *m = &p->m;
	static int i = 0;

	if (!sflag) {
		reportv(ctx, 0, "\nStored samples : %d ", m->xxh->smp);
		sflag = 1;
		i = 0;
	}

	if (size > 2) {
		xmp_drv_loadpatch(ctx, f, m->xxi[i][0].sid, m->c4rate,
				XMP_SMP_BIGEND, &m->xxs[m->xxi[i][0].sid], NULL);
		reportv(ctx, 0, ".");
	}

	i++;
}

static int dt_load(struct xmp_context *ctx, FILE *f, const int start)
{
	struct xmp_player_context *p = &ctx->p;
	struct xmp_mod_context *m = &p->m;

	LOAD_INIT();

	pflag = sflag = 0;
	
	/* IFF chunk IDs */
	iff_register("D.T.", get_d_t_);
	iff_register("S.Q.", get_s_q_);
	iff_register("PATT", get_patt);
	iff_register("INST", get_inst);
	iff_register("DAPT", get_dapt);
	iff_register("DAIT", get_dait);

	/* Load IFF chunks */
	while (!feof(f))
		iff_chunk(ctx, f);

	reportv(ctx, 0, "\n");

	iff_release();

	return 0;
}