File: playmus.c

package info (click to toggle)
lxmusserv 0.94-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 176 kB
  • ctags: 189
  • sloc: ansic: 1,530; makefile: 73
file content (174 lines) | stat: -rw-r--r-- 4,159 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
/*************************************************************************
 *  playmus.c
 *
 *  Updated to lxdoom, added support for additional MUS file events,
 *  bugfixed and tidyuped the code
 *  Copyright (C) 1999 Rafael Reilova (rreilova@ececs.uc.edu)
 *
 *  Copyright (C) 1995 Michael Heasley (mheasley@hmc.edu)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *************************************************************************/

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include "musserver.h"
#include "sequencer.h"

int unknevents;
int timer_res;
int curtime = 0;

int playmus(const struct MUSfile *song, int newsong, int looping)
{
  static int lastvol[N_CHN];
  static unsigned char *ptr;

  int last = 0;
  int controleqv;
  unsigned char event;
  unsigned char eventtype;
  unsigned char channelnum;
  unsigned char notenum;
  unsigned char pitchwheel;
  unsigned char controlnum;
  unsigned char controlval;

  if (newsong)
    ptr = song->musdata;

  {
    event = *ptr++;
    channelnum = event & 15;

    /* map channel 16 to channel 10 (drum channel) */
    if (channelnum >= CHN_PERCUSSION)
      {
	channelnum++;
	if (channelnum == MUS_CHN_PERCUSSION + 1)
	  channelnum = CHN_PERCUSSION;
      }

    eventtype = (event >> 4) & 7;
    last = event & 128;

    switch (eventtype)
      {
      case MUS_EVENT_NOTE_OFF:
	notenum = *ptr++;
	note_off(notenum & 127, channelnum, lastvol[channelnum]);
	break;

      case MUS_EVENT_NOTE_ON:
	notenum = *ptr++;
	if (notenum & 128)
	  {
	    unsigned char notevol = *ptr++;
	    lastvol[channelnum] = notevol & 127;
	  }
	note_on(notenum & 127, channelnum, lastvol[channelnum]);
	break;

      case MUS_EVENT_BENDER:
	pitchwheel = *ptr++;
	pitch_bend(channelnum, pitchwheel & 127);
	break;

      case MUS_EVENT_SYS:
	event = *ptr++;
	system_event(event, channelnum);
	break;

      case MUS_EVENT_CTL_CHANGE:
	controlnum = *ptr++ & 127;
	controlval = *ptr++ & 127;
	controleqv = -1;
	switch (controlnum)
	  {
	  case MUS_CTL_PATCH_CHG: /* patch change */
	    patch_change(controlval, channelnum);
	    break;
	  case MUS_CTL_BNK_SEL:
	    controleqv = CTL_BANK_SELECT;
	    break;
	  case MUS_CTL_MOD_POT:
	    controleqv = CTL_MODWHEEL;
	    break;
	  case MUS_CTL_MAIN_VOL:
	    controleqv = CTL_MAIN_VOLUME;
	    break;
	  case MUS_CTL_PAN_BAL:
	    controleqv = CTL_PAN;
	    break;
	  case MUS_CTL_EXPR_POT:
	    controleqv = CTL_EXPRESSION;
	    break;
	  case MUS_CTL_CHORUS:
	    controleqv = CTL_CHORUS_DEPTH;
	    break;
	  case MUS_CTL_SUSTAIN:
	    controleqv = CTL_SUSTAIN;
	    break;
	  case MUS_CTL_SOFT_PEDAL:
	    controleqv = CTL_SOFT_PEDAL;
	    break;
	  case MUS_CTL_REVRB:
	    controleqv = CTL_EXT_EFF_DEPTH;
	    break;
	  default:
	    unknevents++;
	    break; 
	  }
	if (controleqv != -1)
	  control_change(controleqv, channelnum, controlval);
	break;

      case MUS_EVENT_SCORE_END:
        if (!looping)
          return 0; /* finished */
        else
          {
	    ptr = song->musdata;
	    midi_timer(MIDI_START);
	    last = 0;
          }
	break;

      default:	/* unknown */
	unknevents++;
	break;
      }

    if (last)	/* next data portion is time data */
      {
	unsigned char tmpch;
	int dticks = 0;

	do
	  {
	    tmpch = *ptr++;
	    dticks = (dticks << 7) | (tmpch & 127);
	  }
	while (tmpch & 128);
	curtime += dticks;
	midi_wait(curtime*timer_res/TICS_PER_SECOND);
      }

  }
  return 1; /* still playing */
}