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

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

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

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

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

#ifdef USE_X
#include "TopShell.h"
#endif

#include "Sequencer.h"

int position;
int jumpToPos;
struct optionsInfo pOptions;
struct songInfo *pSongChar;
char played[MAX_POSITION];
struct effectInfo effects;
int patStart;
double savedTd;
int savedTpd;
int savedPosition;

void
playModule (int startPosition, struct songInfo *songCharX,
	     struct optionsInfo optionsX, int startDelay)
{
  SEQ_DECLAREBUF();
  int i;
  extern Sequencer *seq;
  extern Sample *samples[];

  pOptions = optionsX;
  pSongChar = songCharX;
  memset(&effects, 0, sizeof(effects));
  initVoices();

  for (i = 0; i < MAX_SAMPLES && !samples[i]; i++)
    ;

  seq->numVoices(pSongChar->nrChannels, pSongChar->volType, samples[i]);
  seq->panFactor(pOptions.panFactor);

#ifdef USE_X
  seq->writeEnabled(TRUE);
#endif

  if (startPosition == 0)
    for (i = 0; i < pSongChar->songlength; i++)
      played[i] = MY_FALSE;

  SEQ_START_TIMER ();

  for (i = 0; i < pSongChar->nrChannels; i++)
    {
      seq->mainVolume(pOptions.mainVolume * pSongChar->globalVol / 255);
      seq->pan(i, pSongChar->panning[i]);
    }

  nextTime = 0.0;

  if (startPosition == 0)
    {
      TICKS_PER_DIVISION (pSongChar->playSpeed);
      TEMPO (pSongChar->tempo, pSongChar->clockSpeed);
    }
  else
    {
      ticksPerDivision = savedTpd;
      tickDuration = savedTd;
    }

  thisTime = 0.0;
  
  if (startPosition == 0)
    nextTime += startDelay; // was tickDuration

  syncTime ();

  position = startPosition;
  jumpToPos = 0;
  patStart = 0;
  playNextPosition();
}

int
doRepeat()
{
  if ((position >= pSongChar->songlength) && (pOptions.repeat))
    {
      playModule(0, pSongChar, pOptions, 0);
      return (0);
    }

  return (position >= pSongChar->songlength);
}

int
playNextPosition()
{
  SEQ_DECLAREBUF();
  int extraTicks;
  int i;
  int voice;
  int tick, pattern, channel, pos, goTo;
  extern Sequencer *seq;

  savedPosition = 0;

  if (position >= pSongChar->songlength)
    return (doRepeat());

  if ((played[position] > MY_FALSE) && !(jumpToPos & MOVE_LOOP))
    {
#ifdef USE_X
      syncTime ();
      SEQ_ECHO_BACK (ECHO_LOOP);
#else
      if (pOptions.loopBreaker == 1)
	{
	  position = pSongChar->songlength;
	  return (doRepeat());
	}
      else
#endif
	for (i = 0; i < pSongChar->songlength; i++)
	  played[i] = MY_FALSE;
    }

  played[position] = MY_TRUE; // queued
  pos = tune[position];

  if (pos == ORDER_STOP)
    {
      position = pSongChar->songlength;
      return (doRepeat());
    }
  else if ((pos == ORDER_SKIP) || (pos >= pSongChar->nrPatterns))
    {
      position++;
      return (doRepeat());
    }

  syncTime ();
  SEQ_ECHO_BACK ((((unsigned int) position << 16) & 0x00ff0000) |
		 (((unsigned int) pos << 8) & 0x0000ff00) |
		 ECHO_MESSAGE);

  if (patternTempo[pos])
    {
      if (patternTempo[pos] < 32)
	TICKS_PER_DIVISION(patternTempo[pos]);
      else
	TEMPO(patternTempo[pos], pSongChar->clockSpeed);
    }

  jumpToPos = 0;
  for (pattern = patStart; pattern < patternLen[pos] && jumpToPos == 0; pattern++)
    {
#ifdef USE_X
      syncTime ();
      SEQ_ECHO_BACK(((unsigned int)pattern << 8) | ECHO_PATTERN);
#endif
      for (channel = 0; channel < pSongChar->nrChannels; channel++)
	{
	  voice = voiceTable[pos][channel];
	  if ((goTo = playNote (channel, position, pattern,
				  &((patternTable[voice])[pattern]),
				  pSongChar, &effects, &pOptions)) != 0)
	    jumpToPos |= goTo;
	}

      /* nextTime += tickDuration; */
      for (extraTicks = 0; extraTicks <= effects.delayNotes; extraTicks++)
	{
	  for (tick = 0; tick < ticksPerDivision; tick++)
	    {
	      int didPreUpdate = 0;

	      for (channel = 0; channel < pSongChar->nrChannels; channel++)
		didPreUpdate |= seq->doTick(channel, tick, pSongChar);

	      if (didPreUpdate)
		nextTime += (tickDuration > 1 ? 1 : tickDuration);

	      for (channel = 0; channel < pSongChar->nrChannels; channel++)
		seq->doUpdates(channel);

	      if (didPreUpdate)
		{
		  if (tickDuration > 1)
		    nextTime += tickDuration - 1;
		}
	      else
		nextTime += tickDuration;
	    }
	}

      effects.delayNotes = 0;

#ifndef USE_X
      if (stopFlag)
	jumpToPos = MOVE_EXIT;
#endif
    }			/* pattern */

  patStart = 0;

  if (jumpToPos & MOVE_LOOP)
    {
      patStart = effects.pattern;
      effects.pattern = 0;
      position -= 1;
    }
  if (jumpToPos & MOVE_JUMP)
    {
      patStart = effects.pattern;
      position = effects.position - 1;
    }
  if (jumpToPos & MOVE_BREAK)
    {
      patStart = effects.pattern;
    }
  if (jumpToPos & MOVE_EXIT)
    {
      savedPosition = position;
      position = pSongChar->songlength - 1;
    }
  /*
    }

    if (pOptions.repeat)
    startPosition = 0;
    }
    while (pOptions.repeat
#ifndef USE_X
    && !stopFlag
#endif
    );
    */
  position++;

  //return (position >= pSongChar->songlength);
  return (doRepeat());
}

int
endModule(unsigned char lstopFlag)
{
#ifdef USE_X
  extern TopShell *topShell;
  //  extern TrackShell *trackShell;
#endif
  int i;

  if (lstopFlag != STOP_FORWBACK)
    freePatterns ();
  else
    {
      savedTpd = ticksPerDivision;
      savedTd = tickDuration;

      // prevent a loop from being detected

      for (i = 0; i < pSongChar->songlength; i++)
	if (played[i] == MY_TRUE)
	  played[i] = 0;
      
      // prevent loop detection by "rewind"
      played[actualPos] = 0;
    }
  
#ifdef USE_X
      topShell->setPosition(-1);
#endif

  return (savedPosition);
}