File: load_mtm.C

package info (click to toggle)
gmod 3.1-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,352 kB
  • ctags: 809
  • sloc: cpp: 7,772; makefile: 77
file content (296 lines) | stat: -rw-r--r-- 7,738 bytes parent folder | download | duplicates (6)
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
// -*-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 <string.h>
#include <sys/fcntl.h>

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

#include "mtm.h"

#define HEADER_SIZE	66
#define SAMPLE_SIZE	37
#define TRACK_SIZE	192
#define PATTERN_SIZE	64
#define NOTES_PER_TRACK	64
#define NR_CHANNELS	32


typedef struct
{
  char magic[3];		/* must be "MTM" */
  u_char version;		/* upper 4 bits - major, lower minor */
  char name[20];		/* null-term song name string */
  u_short nrSavedTracks;	/* number of tracks saved */
  u_char lastSavedPattern;	/* last pattern number saved */
  u_char lastPlayedPattern;	/* last pattern to play (songlength-1) */
  u_short commentLen;		/* length of extra comment field */
  u_char nrSamples;		/* number of samples saved (NOS) */
  u_char attribute;		/* currently defined as 0 */
  u_char bpt;			/* beats per track */
  u_char nrPlayedTracks;	/* number of tracks toplay back */
  u_char panPositions[32];	/* pan positions for each voice (0-15) */
}

mtmHeader;

typedef struct
{
  u_char pitch;
  u_char instrument;
  u_char effect;
  u_char argument;
} mtmNote;

typedef struct
{
  mtmNote notes[NOTES_PER_TRACK];
} mtmTrack;

typedef struct
{
  u_short tracks[NR_CHANNELS];
} mtmPattern;

void
dumpMtmHeader (mtmHeader * h, struct songInfo *songChar)
{
  strncpy (songChar->name, h->name, 20);
  songChar->name[20] = '\0';
  sprintf (songChar->desc, "MultiTracker v%d.%d", h->version >> 4,
	   h->version & 0x0f);
}

#define PITCH(p)	((*(u_char *)(p))>>2)
#define INSTRUMENT(p)	(((*(u_char *)(p)<<4) | (*((u_char *)(p)+1)>>4)) & 077)
#define EFFECT(p)	(*((u_char *)(p)+1) & 0xf)
#define ARGUMENT(p)	(*((u_char *)(p)+2))

static mtmTrack
getTrack (char *buf)
{
  mtmTrack t;
  int i;

  for (i = 0; i < NOTES_PER_TRACK; i++, buf += 3)
    {
      t.notes[i].pitch = PITCH (buf);
      t.notes[i].instrument = INSTRUMENT (buf);
      t.notes[i].effect = EFFECT (buf);
      t.notes[i].argument = ARGUMENT (buf);
    }

  return (t);
}

static mtmPattern
getPattern (char *buf)
{
  mtmPattern p;
  int i;

  for (i = 0; i < NR_CHANNELS; i++, buf += 2)
    p.tracks[i] = INTEL_SHORT (buf);

  return (p);
}


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

  mtmHeader header;
  int totalSampleSize, i;
  char *rawData;
  unsigned char *sampleData;
  mtmTrack *tracks;
  mtmPattern *patterns;
  u_char order[128];
  int tracki, notei, pati;
  int voice;
  int params, effect, sample, note;

  songChar->lowestNote = 36;
  songChar->highestNote = 98;
  songChar->volType = VOL_LINEAR;
  songChar->volOnZero = MY_FALSE;
  songChar->slideType = SLIDE_PERIOD_LIN;
  songChar->clockSpeed = 60;

  /* read in header */
  rawData = (char *) malloc (HEADER_SIZE);
  memcpy (rawData, buffer, HDR_SIZE);
  fread (rawData + HDR_SIZE, 1, HEADER_SIZE - HDR_SIZE, modFd);

  memcpy (&header.magic, rawData, 3);
  header.version = BYTE (rawData + 3);
  memcpy (header.name, rawData + 4, 20);
  header.nrSavedTracks = INTEL_SHORT (rawData + 24);
  header.lastSavedPattern = BYTE (rawData + 26);
  header.lastPlayedPattern = BYTE (rawData + 27);
  header.commentLen = INTEL_SHORT (rawData + 28);
  header.nrSamples = BYTE (rawData + 30);
  header.attribute = BYTE (rawData + 31);
  header.bpt = BYTE (rawData + 32);
  header.nrPlayedTracks = BYTE (rawData + 33);
  memcpy (header.panPositions, rawData + 34, 32);

  songChar->nrSamples = header.nrSamples;
  free (rawData);
  dumpMtmHeader (&header, songChar);

  /* ======== get sample data =============================== */
  totalSampleSize = SAMPLE_SIZE * header.nrSamples;
  sampleData = (unsigned char *)malloc(totalSampleSize);
  fread (sampleData, 1, totalSampleSize, modFd);

  fread (order, 1, 128, modFd);

  /* =========== read tracks ============== */
  rawData = (char *) malloc (TRACK_SIZE);
  tracks = (mtmTrack *) malloc (sizeof (mtmTrack));

  patternTable[0] = (struct noteInfo *)
    calloc (1, sizeof (struct noteInfo) * NOTES_PER_TRACK);

  for (i = 0; i < header.nrSavedTracks; i++)
    {
      fread (rawData, 1, TRACK_SIZE, modFd);

      tracks[0] = getTrack (rawData);

      patternTable[i + 1] = (struct noteInfo *)
	malloc (sizeof (struct noteInfo) * NOTES_PER_TRACK);

      for (notei = 0; notei < NOTES_PER_TRACK; notei++)
	{
	  params = tracks[0].notes[notei].argument;
	  effect = tracks[0].notes[notei].effect;
	  sample = tracks[0].notes[notei].instrument;
	  note = tracks[0].notes[notei].pitch;

	  if (effect == CMD_EXTENDED)
	    {
	      effect = ((CMD_EXTENDED << 4) & 0xf0) +
		((params >> 4) & 0x0f);
	      params &= 0x0f;
	    }

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

	  switch (effect)
	    {
	    case CMD_SPEED:
	      effect = CVT_MOD_SPEED (params);
	      break;
	    case CMD_VOLUME:
	      if (params > 0)
		params = (params * 4) - 1;
	      break;
	    case CMD_BREAK:
	      params = ((params >> 4) & 0x0f) * 10 + (params & 0x0f);
	      break;
	    case CMD_SET_PAN:
	      params *= 17;
	      break;
	    }

	  (patternTable[i + 1])[notei].note = note;
	  (patternTable[i + 1])[notei].sample = sample;
	  (patternTable[i + 1])[notei].command[0] = effect;
	  (patternTable[i + 1])[notei].parm1[0] = params;
	  (patternTable[i + 1])[notei].parm2[0] = 0;
	  (patternTable[i + 1])[notei].command[1] = 0;
	  (patternTable[i + 1])[notei].parm1[1] = 0;
	  (patternTable[i + 1])[notei].parm2[1] = 0;
	}
    }
  free (rawData);

  /* =========== read patterns ============== */
  rawData = (char *) malloc (PATTERN_SIZE * (header.lastSavedPattern + 1));
  patterns = (mtmPattern *) malloc (sizeof (mtmPattern) *
				     (header.lastSavedPattern + 1));

  fread (rawData, 1, PATTERN_SIZE * (header.lastSavedPattern + 1), modFd);
  for (i = 0; i <= header.lastSavedPattern; i++)
    patterns[i] = getPattern (rawData + i * PATTERN_SIZE);

  free (rawData);

  for (i = 0; i <= header.lastSavedPattern; i++)
    for (voice = 0; voice < NR_CHANNELS; voice++)
      voiceTable[i][voice] = patterns[i].tracks[voice];


  songChar->comment = (char *) realloc (songChar->comment, header.commentLen + 1);
  bzero (songChar->comment, header.commentLen + 1);
  fread (songChar->comment, 1, header.commentLen, modFd);
  songChar->commentLineLen = 0;

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

  songChar->nrTracks = header.nrSavedTracks + 1;
  songChar->songlength = header.lastPlayedPattern + 1;
  songChar->nrChannels = header.nrPlayedTracks;
  songChar->playSpeed = 6;
  songChar->tempo = 125;
  songChar->nrPatterns = header.lastSavedPattern + 1;

  /* copy pattern orders into the tune area */
  for (i = 0; i <= header.lastPlayedPattern; i++)
    {
      tune[i] = order[i];
    }

  /* determine maximum channel actually used */

  songChar->nrChannels = 0;

  for (tracki = 0; tracki < header.nrPlayedTracks; tracki++)
    for (pati = 0; pati < (header.lastSavedPattern + 1); pati++)
      if ((tracki > songChar->nrChannels) && (patterns[pati].tracks[tracki]))
	songChar->nrChannels = tracki;

  songChar->nrChannels++;

  /* set proper panning */

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

  /* all done */
  free(sampleData);
  free (tracks);
  free (patterns);

  return (1);
}