File: load_ult.C

package info (click to toggle)
gmod 3.1-2
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 1,348 kB
  • ctags: 808
  • sloc: cpp: 7,755; makefile: 82
file content (393 lines) | stat: -rw-r--r-- 9,355 bytes parent folder | download
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
// -*-C++-*-
// This file is part of the gmod package
// Copyright (C) 1997 by Andrew J. Robinson

#include <sys/types.h>

#ifdef USE_LOCAL
#include "soundcard.h"
#else
#include <sys/soundcard.h>
#endif

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/fcntl.h>

#include "commands.h"
#include "defines.h"
#include "structs.h"
#include "globals.h"
#include "protos.h"

#include "ult.h"

#define HEADER_SIZE	48
#define SAMPLE_SIZE	64
#define TRACK_SIZE	192
#define PATTERN_SIZE	64
#define NOTES_PER_TRACK	64
#define NR_CHANNELS	32

#define REPEAT_NOTE	0xfc	/* note signifying a RLE pattern */

typedef struct
{
  char title[33];
  int version;
  int textLen;
  char *text;
  int nrSamples;
  int nrChannels;
  int nrPatterns;
  int order[256];
  int panPositions[32];
  int sampleOffset;
  int patternOffset;
  /*  int eohOffset; */
}

ultHeader;

typedef struct
{
  int a;
}

ultPattern;

void convertUltEffect (u_long *, u_long *);

void
dumpUltHeader (ultHeader * h, struct songInfo *songChar)
{
  sprintf (songChar->desc, "UltraTracker v%d", h->version);
  strcpy (songChar->name, h->title);

  if (h->version >= 2 && h->textLen)
    {
      songChar->comment = (char *) realloc (songChar->comment, h->textLen + 1);
      bzero (songChar->comment, h->textLen + 1);
      strncpy (songChar->comment, h->text, h->textLen);
      songChar->commentLineLen = 32;
    }
}

ultHeader
getUltHeader (FILE * modFd, unsigned char *buffer, u_char **sampleData)
{
  int i;
  u_char *rawData;
  u_char tmpchar;
  int totalSampleSize;
  ultHeader header;
  u_long sampleSize = SAMPLE_SIZE;

  /* read in first part of header */
  rawData = (unsigned char *) malloc (HEADER_SIZE);
  memcpy (rawData, buffer, HDR_SIZE);	/* HEADER_SIZE == HDR_SIZE ! */
  /* fread (rawData, 1, HEADER_SIZE, modFd); */

  memcpy (header.title, rawData + 15, 32);	/* copy song title */
  header.title[32] = 0;
  rawData[15] = 0;		/* terminate "V00x" in magic number.
				 * WARNING: may overwrite title, so copy
				 * title first */

  /* get format version:
   *  2 has header+47 defined as a textLen byte,
   *  3 has pan-position table after NOP byte
   */
  header.version = atoi ((char *)(rawData + 12));

  if (header.version >= 2)
    header.textLen = rawData[47] * 32;
  else
    header.textLen = 0;
  free (rawData);

  if (header.textLen)
    {
      header.text = (char *)malloc (header.textLen + 1);
      fread (header.text, 1, header.textLen, modFd);
      header.text[header.textLen] = 0;
    }
  else
    header.text = NULL;

  /* get samples */
  fread (&tmpchar, 1, 1, modFd);
  header.nrSamples = tmpchar;

  header.sampleOffset = HEADER_SIZE + header.textLen + 1;

  /* ======== get sample data =============================== */

  /* version 4 files have C2 frequency */
  if (header.version >= 4)
    sampleSize += 2;

  totalSampleSize = sampleSize * header.nrSamples;
  *sampleData = (u_char *)malloc(totalSampleSize);
  fread (*sampleData, 1, totalSampleSize, modFd);

  rawData = (unsigned char *)malloc (258);
  fread (rawData, 1, 258, modFd);

  for (i = 0; i < 256; i++)
    header.order[i] = (unsigned) rawData[i];

  /* these are stored as "last channel" and "last pattern",
   * so we add one to make it ordinal
   */
  header.nrChannels = rawData[i++] + 1;
  header.nrPatterns = rawData[i] + 1;

  header.patternOffset = header.sampleOffset +
    header.nrSamples * sampleSize + 258;

  if (header.version >= 3)
    {
      fread (rawData, 1, header.nrChannels, modFd);
      for (i = 0; i < header.nrChannels; i++)
	header.panPositions[i] = rawData[i];

      header.patternOffset += header.nrChannels;
    }
  else
    {
      for (i = 0; i < header.nrChannels; i++)
	header.panPositions[i] = 7;
    }

  free (rawData);

  return (header);
}


int
loadUltModule (FILE * modFd, struct songInfo *songChar,
		 struct optionsInfo options, unsigned char *buffer)
{
  extern Sample *samples[];
  extern Sequencer *seq;

  ultHeader header;
  int i;
  u_char *sampleData;
  int chani, notei, pati, absi;
  int voice;
  int sampleSize;
#ifdef DEBUG
  int tn = 0;
#endif

  songChar->lowestNote = 36;
  songChar->highestNote = 95;
  songChar->volOnZero = MY_FALSE;
  songChar->slideType = SLIDE_PERIOD_LIN;
  songChar->clockSpeed = 60;

  header = getUltHeader(modFd, buffer, &sampleData);

  songChar->nrSamples = header.nrSamples;
  songChar->nrPatterns = header.nrPatterns;

  /* ========== copy pattern order into tune[] ============== */
#ifdef SHOW_ORDER
  for (i = 0; i < 256 && header.order[i] != 255; i++)
    printf ("%03d%c", header.order[i], (i + 1) % 20 ? ' ' : '\n');
  putchar ('\n');
#endif

  /* copy pattern orders into the tune area and find song length
   * NB: not in the docs, but apparently empty patterns are set
   * to 255; I'm assuming the first 255 ends the song.
   */
  songChar->songlength = 0;
  for (i = 0; i < 256 && header.order[i] != 255; i++)
    {
      tune[i] = header.order[i];
      songChar->songlength++;
    }

  songChar->nrTracks = header.nrPatterns * header.nrChannels;
  songChar->nrChannels = header.nrChannels;
  songChar->playSpeed = 6;
  songChar->tempo = 125;

  /* set panning */

  for (i = 0; i < header.nrChannels; i++)
    songChar->panning[i] = (header.panPositions[i] & 0x0f) * 17;

  dumpUltHeader (&header, songChar);

  /* get patterns from file */
  for (chani = 0; chani < header.nrChannels; chani++)
    {
      /* allocate memory for track */

      for (pati = 0; pati < header.nrPatterns; pati++)
	patternTable[header.nrPatterns * chani + pati] = (struct noteInfo *)
	  malloc (sizeof (struct noteInfo) * NOTES_PER_TRACK);

      for (absi = 0; absi < (NOTES_PER_TRACK * header.nrPatterns);)
	{
	  unsigned char *p;
	  u_char noteBytes[7];
	  int repeat;

	  u_long param[2], note, effect[2], sample, param2;

	  fread (noteBytes, 1, 5, modFd);
	  if (noteBytes[0] == REPEAT_NOTE)
	    {
	      fread (noteBytes + 5, 1, 2, modFd);
	      repeat = noteBytes[1];

	      if (repeat == 0)
		{
		  repeat = 1;
		}
	      p = noteBytes + 2;
	    }
	  else
	    {
	      repeat = 1;
	      p = noteBytes;
	    }

	  note = p[0];
	  sample = p[1];
	  effect[0] = p[2] >> 4;/* are these switched? */
	  effect[1] = p[2] & 0xf;
	  param[0] = INTEL_SHORT (p + 3) >> 8;
	  param[1] = INTEL_SHORT (p + 3) & 0xff;

	  convertUltEffect (&effect[0], &param[0]);
	  convertUltEffect (&effect[1], &param[1]);

	  /* special case for setoffset fine */

	  if ((effect[0] == CMD_SETOFFSET_1024) && (effect[1] == CMD_SETOFFSET_1024))
	    {
	      effect[0] = CMD_SETOFFSET_FINE;
	      effect[1] = 0;
	      param2 = param[1];
	      param[1] = 0;
	    }
	  else
	    param2 = 0;


	  if (note)		/* note->period */
	    {
	      note = note + 3 * 12 - 1;	/* shift up 3 octaves */
	    }

	  for (i = 0; i < repeat && absi < (NOTES_PER_TRACK * header.nrPatterns); i++)
	    {
	      pati = absi / NOTES_PER_TRACK;
	      notei = absi % NOTES_PER_TRACK;

	      voice = header.nrPatterns * chani + pati;
	      voiceTable[pati][chani] = voice;

	      (patternTable[voice])[notei].note = note;
	      (patternTable[voice])[notei].sample = sample;
	      (patternTable[voice])[notei].command[0] = effect[0];
	      (patternTable[voice])[notei].parm1[0] = param[0];
	      (patternTable[voice])[notei].parm2[0] = param2;
	      (patternTable[voice])[notei].command[1] = effect[1];
	      (patternTable[voice])[notei].parm1[1] = param[1];
	      (patternTable[voice])[notei].parm2[1] = 0;
	      absi++;
#ifdef DEBUG
	      tn++;
#endif
	    }
#ifdef DEBUG
	  if (i != repeat)
	    fprintf (stderr, "bail: i:%d repeat:%d\n", i, repeat);
#endif
	}

      if (options.compress)
	for (pati = 0; pati < header.nrPatterns; pati++)
	  voiceTable[pati][chani] =
	    compressVoice (voiceTable[pati][chani],
			    voiceTable[pati][chani],
			    NOTES_PER_TRACK, 1);
    }

  sampleSize = SAMPLE_SIZE;

  if (header.version >= 4)
    sampleSize += 2;

  for (i = 0; i < header.nrSamples; i++)
    {
      samples[i] = new ULT_sample;
      samples[i]->load(*seq, modFd, i, 1, sampleData + i * sampleSize,
		       &header.version);
    }

  /* all done */
  free (sampleData);

  if (header.textLen)
    free (header.text);		/* song text of size header.textLen */

  if (header.version == 1)
    songChar->volType = VOL_LOG;
  else
    songChar->volType = VOL_LINEAR;

  return (1);
}


void
convertUltEffect (u_long * effect, u_long * param)
{
  switch (*effect)
    {
    case 0:			/* arpeggio */
    case 1:			/* portamento up */
    case 2:			/* portamento down */
    case 3:			/* tone portamento */
    case 4:			/* vibrato */
    case 7:			/* tremolo */
    case 0xa:			/* volslide */
    case 0xc:			/* volume */
      break;
    case 9:			/* sample offset */
      *effect = CMD_SETOFFSET_1024;
      break;
    case 0xd:			/* pattern break */
      *param = ((*param >> 4) & 0x0f) * 10 + (*param & 0x0f);
      break;
    case 0xe:
      *effect = (0xe0) + ((*param >> 4) & 0x0f);
      *param &= 0x0f;
      break;
    case 0xf:			/* set speed */
      *effect = CVT_MOD_SPEED (*param);
      break;
    case 5:			/* special sample commands */
      *effect = 0;
      *param = 0;
      break;
    case 0xb:			/* set balance */
      *effect = CMD_SET_PAN;
      *param = (*param & 0x0f) * 17;
      break;
    default:
      *effect = 0;
      *param = 0;
    }
}