~ubuntu-branches/ubuntu/oneiric/oss4/oneiric-proposed

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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "../../include/soundcard.h"
#include <sys/time.h>

#undef  DEBUG

extern int __seqfd;

static midi_packet_t midi_packet;

static unsigned char *midibuf = &midi_packet.payload[0];
static int mp = 0;

oss_midi_time_t current_tick = 0;

static int timer_started = 0;

void
OSS_set_timebase (int tb)
{
/* TODO? */
}

#if 1
#define _write write
#else
static size_t
_write (int fildes, void *b, size_t nbyte)
{
  midi_packet_header_t *hdr = b;
  unsigned char *buf;
  int l = nbyte;


  buf = (unsigned char *) b + sizeof (*hdr);

  if (hdr->magic == MIDI_HDR_MAGIC)	/* Timed mode write */
    {
      l -= sizeof (*hdr);

      switch (hdr->event_type)
	{
	case MIDI_EV_WRITE:
	  printf ("%9lu: MIDI_EV_WRITE: ", hdr->time);
	  break;
	case MIDI_EV_TEMPO:
	  printf ("%9lu: MIDI_EV_TEMPO: ", hdr->time);
	  break;
	case MIDI_EV_ECHO:
	  printf ("%9lu: MIDI_EV_ECHO: ", hdr->time);
	  break;
	case MIDI_EV_START:
	  printf ("%9lu: MIDI_EV_START: ", hdr->time);
	  break;
	case MIDI_EV_STOP:
	  printf ("%9lu: MIDI_EV_STOP: ", hdr->time);
	  break;
	default:
	  printf ("%9lu: MIDI_EV_%d: ", hdr->time, hdr->event_type);
	  break;
	}

      if (hdr->options & MIDI_OPT_TIMED)
	printf ("TIMED ");

      if (l > 0)
	{
	  int i;

	  printf ("%d bytes (", l);

	  for (i = 0; i < l; i++)
	    printf ("%02x ", buf[i]);
	  printf (")");
	}

      printf ("\n");
    }

  return write (fildes, b, nbyte);
}
#endif

static void
start_timer (void)
{
#ifndef DEBUG
  midi_packet_header_t hdr;

  hdr.magic = MIDI_HDR_MAGIC;
  hdr.event_type = MIDI_EV_START;
  hdr.options = MIDI_OPT_NONE;
  hdr.time = 0;
  hdr.parm = 0;
  if (_write (__seqfd, &hdr, sizeof (hdr)) != sizeof (hdr))
    {
      perror ("Write start timer");
      exit (-1);
    }

  timer_started = 1;
  printf ("Timer started\n");
#endif
}

void
_dump_midi (void)
{
  if (mp > 0)
    {
      if (!timer_started)
	start_timer ();
      midi_packet.hdr.magic = MIDI_HDR_MAGIC;
      midi_packet.hdr.options = MIDI_OPT_TIMED;
      midi_packet.hdr.event_type = MIDI_EV_WRITE;
      midi_packet.hdr.parm = 0;
      midi_packet.hdr.time = current_tick;

#ifdef DEBUG
      if (write (1, &midi_packet.payload[0], mp) == -1)
#else
      if (_write (__seqfd, &midi_packet, sizeof (midi_packet_header_t) + mp)
	  == -1)
#endif
	{
	  perror ("MIDI write");
	  exit (-1);
	}
      mp = 0;
    }
}

static void
oss_do_timing (unsigned char *ev)
{
  unsigned int parm = *(unsigned int *) &ev[4];
  midi_packet_header_t hdr;

  oss_midi_time_t tick;

  _dump_midi ();

  switch (ev[1])
    {
    case TMR_TEMPO:
#ifndef DEBUG
      if (!timer_started)
	start_timer ();
      hdr.magic = MIDI_HDR_MAGIC;
      hdr.event_type = MIDI_EV_TEMPO;
      hdr.options = MIDI_OPT_TIMED;
      hdr.time = current_tick;
      hdr.parm = parm;
      if (_write (__seqfd, &hdr, sizeof (hdr)) != sizeof (hdr))
	{
	  perror ("Write tempo");
	  exit (-1);
	}
#endif
      break;

    case TMR_WAIT_REL:
      tick = current_tick + parm;

      current_tick = tick;
      break;

    case TMR_WAIT_ABS:
      tick = parm;
      current_tick = tick;
      break;
    }

}

static void
out_midi3 (unsigned char a, unsigned char b, unsigned char c)
{
  if (mp > 950)
    _dump_midi ();
  midibuf[mp++] = a;
  midibuf[mp++] = b;
  midibuf[mp++] = c;
/* printf("Out %02x %02x %02x\n", a, b, c); */
}

static void
out_midi2 (unsigned char a, unsigned char b)
{
  if (mp > 950)
    _dump_midi ();
  midibuf[mp++] = a;
  midibuf[mp++] = b;
/* printf("Out %02x %02x\n", a, b); */
}

void
play_event (unsigned char *ev)
{
  int i;

  switch (ev[0])
    {
    case EV_TIMING:
      oss_do_timing (ev);
      return;
      break;

    case EV_SEQ_LOCAL:
      break;

    case EV_CHN_COMMON:
      switch (ev[2])
	{
	case MIDI_PGM_CHANGE:
	  /* printf("PGM change %02x %d\n", ev[2]|ev[3], ev[4]); */
	  out_midi2 (ev[2] | ev[3], ev[4]);
	  break;

	case MIDI_CHN_PRESSURE:
	  out_midi2 (ev[2] | ev[3], ev[4]);
	  break;

	case MIDI_CTL_CHANGE:
	  out_midi3 (ev[2] | ev[3], ev[4], *(short *) &ev[6]);
	  break;

	default:
	  out_midi3 (ev[2] | ev[3], ev[4], *(short *) &ev[6]);
	}
      return;
      break;

    case EV_CHN_VOICE:
      out_midi3 (ev[2] | ev[3], ev[4], ev[5]);
      return;
      break;

    case EV_SYSEX:
      {
	int i, l;
	l = 8;
	for (i = 2; i < 8; i++)
	  if (ev[i] == 0xff)
	    {
	      l = i;
	      break;
	    }
	if (mp > 950)
	  _dump_midi ();
	for (i = 2; i < l; i++)
	  midibuf[mp++] = ev[i];
      }
      return;
      break;

    case EV_SYSTEM:
      printf ("EV_SYSTEM: ");
      break;

    default:
      printf ("Unknown event %d: ", ev[0]);

    }

  for (i = 0; i < 8; i++)
    printf ("%02x ", ev[i]);
  printf ("\n");
}