File: gmod.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 (461 lines) | stat: -rw-r--r-- 9,535 bytes parent folder | download | duplicates (3)
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
// -*-C++-*-
// This file is part of the gmod package
// Copyright (C) 1997 by Andrew J. Robinson

/*
 *	gmod.c	- Module player for GUS and Linux.
 *		(C) Hannu Savolainen, 1993
 *
 *	NOTE!	This program doesn't try to be a complete module player.
 *		It's just a too I used while developing the driver. In
 *		addition it can be used as an example on programming
 *		the VoxWare Sound Driver with GUS.
 */

/*
 * Many modifications have been done by Andrew J. Robinson.
 * Refer to the ChangeLog for details.
 */


#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

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

#include <linux/ultrasound.h>
#include <stdio.h>
#include <string.h>

#include <time.h>		/* for randomize */
#include <stdlib.h>		/* for randomize */

#include "defines.h"
#include "structs.h"
#include "tables.h"
#include "protos.h"

#ifdef USE_X
#include <limits.h>
#include <qmsgbox.h>

#include "TopShell.h"
#include "TrackShell.h"
#include "OptShell.h"
#include "QueueShell.h"
#include "CommentShell.h"
#else
#include "CursesScr.h"
#endif

#include "SampleShell.h"
#include "Sequencer.h"
#include "Sample.h"

SEQ_DEFINEBUF (2048);

int patternLen[MAX_POSITION];
int patternTempo[MAX_POSITION];
struct noteInfo *patternTable[MAX_PATTERN * MAX_TRACK];

struct voiceInfo voices[MAX_TRACK];

int tune[MAX_POSITION];
short voiceTable[MAX_POSITION][MAX_TRACK];
double tickDuration;

int mixerfd;
Sample *samples[MAX_SAMPLES];
double thisTime, nextTime;
int ticksPerDivision;

#ifndef USE_X
unsigned char stopFlag = 0;
#endif
unsigned char background = 0;
int actualPos = 0;
int positionChange = 0;

unsigned int seqInput = ECHO_NONE;

struct xStruct xInfo;
int currentMod;

#ifdef USE_X
TopShell *topShell;
TrackShell *trackShell;
OptShell *optShell;
QueueShell *queueShell;
CommentShell *commentShell;
char * emptyList[] = { "" };
#else
CursesScr *cursScreen;
#endif
SampleShell *sampleShell;

void readRc (FILE *, char *, struct optionsInfo *);
unsigned int procInput (void);
void setSignals (void);

#ifdef USE_X
  struct optionsInfo options =
    {255, 0, 1, 1, 0, 0, 0, 255, 0, 0, 100, 0, 0};
#else
  struct optionsInfo options =
    {255, 0, 0, 1, 1, 0, 0, 0, 255, 0, 0, 100, 0, 0};
#endif
struct optionsInfo savedOpt;
FILE *rcFp = NULL;
struct songInfo songChar;
int startPos = 0;

Sequencer *seq;

int
main (int argc, char *argv[])
{
  static char ident[] = IDENT;
  int i, seqOpenRet, nameStart, numFiles, randSwap;
#ifndef USE_X
  int exitCode = ERR_NOERROR;
  char mixerName[13];
  int mixDevmask;
#endif
  char *tmpArgv, *rcFilename;
  
  // do all the opens first in case the process is SUID or SGID

  Sequencer lseq;
  seq = &lseq;
  seqOpenRet = lseq.open();

#ifndef USE_X
  if (options.mixer == 255)
    sprintf (mixerName, "%s", "/dev/mixer");
  else
    sprintf (mixerName, "%s%u", "/dev/mixer", options.mixer);

  mixerfd = open (mixerName, O_RDWR, 0);
#endif

  // drop SUID and SGID priveledge if we have it
  setuid(getuid());
  setgid(getgid());

  songChar.comment = (char *)calloc (1, 1);

  for (i = 0; i < MAX_PATTERN * MAX_TRACK; i++)
    patternTable[i] = NULL;
  
  for (i = 0; i < MAX_SAMPLES; i++)
    samples[i] = 0;

#ifdef USE_X
  QApplication a(argc, argv);
  commentShell = new CommentShell;
  optShell = new OptShell;
  queueShell = new QueueShell;
  sampleShell = new SampleShell;
  trackShell = new TrackShell;
  topShell = new TopShell(commentShell, optShell, queueShell, sampleShell, trackShell);
  a.setMainWidget(topShell);
  topShell->show();
#endif

  nameStart = parseArgs (argc, argv, &options);

#ifdef USE_X
  savedOpt.extendOct = options.extendOct;
#endif

#ifndef USE_X
  printf (HEADING);
  printf ("Original source (C) Hannu Savolainen, 1993\n");
  printf ("MTM/ULT loaders by Robert Sanders\n");
  printf ("Continuing development by Andrew J. Robinson\n\n");

  if (nameStart == argc)
    {
      printf ("\nUsage: %s [options] modfile . . .\n", argv[0]);
      printf ("Use %s -h for help.\n\n", argv[0]);
      exit (ERR_BADARGS);
    }
#endif

  switch (seqOpenRet)
    {
    case -1:
#ifdef USE_X
      QMessageBox::message("Xgmod Error", "Cannot open /dev/sequencer");
#else
      perror ("/dev/sequencer");
#endif
      exit (ERR_SEQUENCER);
    case -2:
#ifdef USE_X
      QMessageBox::message("Xgmod Error", "Cannot determine number of synths");
#else
      perror ("/dev/sequencer");
#endif
      exit (ERR_SEQUENCER);
    case -3:
#ifdef USE_X
      QMessageBox::message("Xgmod Error", "Cannot determine synth information");
#else
      perror ("/dev/sequencer");
#endif
      exit (ERR_SEQUENCER);
    case -4:
#ifdef USE_X
      QMessageBox::message("Xgmod Error", "Gravis Ultrasound not detected");
#else
      fprintf (stderr, "Gravis Ultrasound not detected\n");
#endif
      exit (ERR_NOGUS);
    }

#ifndef USE_X
  if (options.mixer == 255)
    sprintf (mixerName, "%s", "/dev/mixer");
  else
    sprintf (mixerName, "%s%u", "/dev/mixer", options.mixer);

  if (mixerfd == -1)
    printf ("Mixer (%s) not available.\n", mixerName);
  else
    {
      ioctl (mixerfd, SOUND_MIXER_READ_DEVMASK, &mixDevmask);
      if (!(mixDevmask & SOUND_MASK_SYNTH))
	{
	  printf ("This mixer (%s) does not support volume control.\n", mixerName);
	  close (mixerfd);
	  mixerfd = -1;
	}
    }
#endif

  if ((tmpArgv = getenv ("HOME")) != NULL)
    {
      rcFilename = (char *) malloc (strlen (tmpArgv) + 9);
      strcpy (rcFilename, tmpArgv);
      strcat (rcFilename, USER_RC_NAME);
      rcFp = fopen (rcFilename, "r");
      free (rcFilename);
    }

  if (rcFp == NULL)
    rcFp = fopen (RC_NAME, "r");

#ifndef USE_X
  cursScreen = new CursesScr(background);
#ifdef USE_NCURSES
  sampleShell = new SampleShell(background);
#else
  sampleShell = new SampleShell(background, options.showEmptySamples);
#endif
#endif /* USE_X */

  numFiles = argc - nameStart;
  srand (time (NULL));

  if (options.randomize)
    {
      for (i = nameStart; i < argc; i++)
	{
	  randSwap = nameStart + (rand () % numFiles);
	  tmpArgv = argv[i];
	  argv[i] = argv[randSwap];
	  argv[randSwap] = tmpArgv;
	}
    }

  xInfo.nrFileStrings = argc - nameStart;

#ifndef USE_X
#define String char *
#endif

  if (xInfo.nrFileStrings > 0)
    {
      xInfo.fileStrings = (char * *)malloc (sizeof (char *) * xInfo.nrFileStrings);

      for (i = nameStart; i < argc; i++)
	{
	  xInfo.fileStrings[i - nameStart] = strdup (argv[i]);
#ifdef USE_X
	  queueShell->addFile(xInfo.fileStrings[i - nameStart]);
#endif
	}
    }
  else
    xInfo.fileStrings = NULL;
  
  savedOpt = options;

#ifdef USE_X
  seq->readEnabled(TRUE);
#else
  setSignals ();
#endif

  //seq->stopAllChannels(MY_FALSE);

#ifdef USE_X
  currentMod = -1;

  a.exec();
#else
  currentMod = 0;

  while ((stopFlag != STOP_EXIT) && (currentMod < xInfo.nrFileStrings))
    {
      if (startPlayback (stopFlag))
	{
	  seqInput = ECHO_NONE;

	  while (seqInput != ECHO_END)
	    {
	      NoXProcessEvent ();
	    }
	  
	  ioctl(seq->seqFd(), SNDCTL_SEQ_RESET, 0);
	  startPos = endModule (stopFlag);

	  switch (stopFlag)
	    {
	    case STOP_FORWBACK:
	      startPos = actualPos + positionChange;
	      
	      if (startPos < 0)
		startPos = 0;
	      
	      break;
	    case STOP_EXIT:
	      break;
	    case STOP_PREV:
	      if (currentMod > 0)
		currentMod--;
	      startPos = 0;
	      break;
	    default:
	    case STOP_NEXT:
	      currentMod++;
	      startPos = 0;
	      break;
	    }
	}
      else
	{
	  currentMod++;
	  startPos = 0;
	}
    }

  //SEQ_DUMPBUF ();

  //lseq.close(); // destructor does close
  fclose (rcFp);
  delete cursScreen;
  exit (exitCode);
#endif /* not USE_X */
}

int
startPlayback(unsigned char lstopFlag)
{
  int loadRc = 0;
  int startDelay; 
  extern char played[MAX_POSITION];
#ifdef USE_X
  extern TopShell *topShell;
#endif

#ifdef USE_X
  if (optShell->highlightChecked() == TRUE)
    queueShell->currentClicked();
#endif
  if (lstopFlag != STOP_FORWBACK)
    {
      struct timeval tvStart, tvEnd;
      startPos = 0;

      gettimeofday(&tvStart, NULL);

      options = savedOpt;

#ifdef USE_X
      if (optShell->fiftyhzChecked() == TRUE)
	options.use_50hz = 1;
      else
	options.use_50hz = 0;
      
      if (optShell->ntscChecked() == TRUE)
	options.ntsc = 1;
      else
	options.ntsc = 0;
      
      if (optShell->bpmChecked() == TRUE)
	options.bpmTempos = 0;
      else
	options.bpmTempos = 1;

      options.extendOct = optShell->octaveSelected();
      options.checkMagic = optShell->magicChecked();
#endif

      if (rcFp != NULL)
	{
	  readRc (rcFp, xInfo.fileStrings[currentMod], &options);
	}
      
      loadRc = loadModule (xInfo.fileStrings[currentMod], &songChar, options);
      
      gettimeofday(&tvEnd, NULL);
      tvEnd.tv_sec -= tvStart.tv_sec;
      
      if (tvStart.tv_usec > tvEnd.tv_usec)
	{
	  tvEnd.tv_sec -= 1;
	  tvEnd.tv_usec = tvEnd.tv_usec + 1000000 - tvStart.tv_usec;
	}
      else
	tvEnd.tv_usec -= tvStart.tv_usec;

      if (tvEnd.tv_sec >= 1)
	startDelay = 0;
      else
	startDelay = (1000000 - tvEnd.tv_usec) / 10000;
    }
  else
    {
      loadRc = 1;
      startDelay = 0;
      // prevent "rewind" from detecting a loop
      played[startPos] = 0;
    }

#ifndef USE_X
  stopFlag = 0;
#endif

  if (loadRc)
    {
      actualPos = 0;
      positionChange = 0;
      
      playModule(startPos, &songChar, options, startDelay);
    }
#ifdef USE_X
  else
    topShell->moduleTitle("Load Failed");
#endif

  return (loadRc);
}