aboutsummaryrefslogtreecommitdiff
path: root/pd/src/s_unix.c
blob: 85282f3bfa7539378740fa52fa8b8dc57449c480 (plain)
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
/* Copyright (c) 1997-1999 Miller Puckette and others.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* this file is misnamed.  It seems to handle clock functions and MIDI I/O;
probably should be called "s_midi.c"  */

#include "m_imp.h"
#ifdef UNIX
#include <unistd.h>
#include <sys/time.h>
#ifdef HAVE_BSTRING_H
#include <bstring.h>
#endif
#endif
#ifdef NT
#include <winsock.h>
#include <sys/types.h>
#include <sys/timeb.h> 
#include <wtypes.h>
#endif
#include <string.h>
#include <stdio.h>
#include <signal.h>

#ifdef NT
static LARGE_INTEGER nt_inittime;
static double nt_freq = 0;

static void sys_initntclock(void)
{
    LARGE_INTEGER f1;
    LARGE_INTEGER now;
    QueryPerformanceCounter(&now);
    if (!QueryPerformanceFrequency(&f1))
    {
          fprintf(stderr, "pd: QueryPerformanceFrequency failed\n");
          f1.QuadPart = 1;
    }
    nt_freq = f1.QuadPart;
    nt_inittime = now;
}

#if 0
    /* this is a version you can call if you did the QueryPerformanceCounter
    call yourself.  Necessary for time tagging incoming MIDI at interrupt
    level, for instance; but we're not doing that just now. */

double nt_tixtotime(LARGE_INTEGER *dumbass)
{
    if (nt_freq == 0) sys_initntclock();
    return (((double)(dumbass->QuadPart - nt_inittime.QuadPart)) / nt_freq);
}
#endif
#endif /* NT */

double sys_getrealtime(void)	/* get "real time" in seconds */
{
#ifdef UNIX
    static struct timeval then;
    struct timeval now;
    gettimeofday(&now, 0);
    if (then.tv_sec == 0 && then.tv_usec == 0) then = now;
    return ((now.tv_sec - then.tv_sec) +
    	(1./1000000.) * (now.tv_usec - then.tv_usec));
#endif
#ifdef NT
    LARGE_INTEGER now;
    QueryPerformanceCounter(&now);
    if (nt_freq == 0) sys_initntclock();
    return (((double)(now.QuadPart - nt_inittime.QuadPart)) / nt_freq);
#endif
}

typedef struct _midiqelem
{
    double q_time;
    int q_portno;
    unsigned char q_onebyte;
    unsigned char q_byte1;
    unsigned char q_byte2;
    unsigned char q_byte3;
} t_midiqelem;

#define MIDIQSIZE 1024

t_midiqelem midi_outqueue[MIDIQSIZE];
int midi_outhead, midi_outtail;
t_midiqelem midi_inqueue[MIDIQSIZE];
int midi_inhead, midi_intail;
static double sys_midiinittime;

    /* this is our current estimate for at what "system" real time the
    current logical time's output should occur. */
static double sys_dactimeminusrealtime;
    /* same for input, should be schduler advance earlier. */
static double sys_adctimeminusrealtime;

static double sys_newdactimeminusrealtime = -1e20;
static double sys_newadctimeminusrealtime = -1e20;
static double sys_whenupdate;

void sys_initmidiqueue( void)
{
    sys_midiinittime = clock_getlogicaltime();
    sys_dactimeminusrealtime = sys_adctimeminusrealtime = 0;
}

    /* this is called from the OS dependent code from time to time when we
    think we know the delay (outbuftime) in seconds, at which the last-output
    audio sample will go out the door. */
void sys_setmiditimediff(double inbuftime, double outbuftime)
{
    double dactimeminusrealtime =
    	.001 * clock_gettimesince(sys_midiinittime)
	    - outbuftime - sys_getrealtime();
    double adctimeminusrealtime =
    	.001 * clock_gettimesince(sys_midiinittime)
	    + inbuftime - sys_getrealtime();
    if (dactimeminusrealtime > sys_newdactimeminusrealtime)
    	sys_newdactimeminusrealtime = dactimeminusrealtime;
    if (adctimeminusrealtime > sys_newadctimeminusrealtime)
    	sys_newadctimeminusrealtime = adctimeminusrealtime;
    if (sys_getrealtime() > sys_whenupdate)
    {
    	sys_dactimeminusrealtime = sys_newdactimeminusrealtime;
    	sys_adctimeminusrealtime = sys_newadctimeminusrealtime;
	sys_newdactimeminusrealtime = -1e20;
	sys_newadctimeminusrealtime = -1e20;
    	sys_whenupdate = sys_getrealtime() + 1;
    }
}

    /* return the logical time of the DAC sample we believe is currently
    going out, based on how much "system time" has elapsed since the
    last time sys_setmiditimediff got called. */
static double sys_getmidioutrealtime( void)
{
    return (sys_getrealtime() + sys_dactimeminusrealtime);
}

static double sys_getmidiinrealtime( void)
{
    return (sys_getrealtime() + sys_adctimeminusrealtime);
}

static void sys_putnext( void)
{
    int portno = midi_outqueue[midi_outtail].q_portno;
    if (midi_outqueue[midi_outtail].q_onebyte)
    	sys_putmidibyte(portno, midi_outqueue[midi_outtail].q_byte1);
    else sys_putmidimess(portno, midi_outqueue[midi_outtail].q_byte1,
    	    midi_outqueue[midi_outtail].q_byte2,
	    	midi_outqueue[midi_outtail].q_byte3);
    midi_outtail  = (midi_outtail + 1 == MIDIQSIZE ? 0 : midi_outtail + 1);
}

/*  #define TEST_DEJITTER */

void sys_pollmidioutqueue( void)
{
#ifdef TEST_DEJITTER
    static int db = 0;
#endif
    double midirealtime = sys_getmidioutrealtime();
#ifdef TEST_DEJITTER
    if (midi_outhead == midi_outtail)
    	db = 0;
#endif
    while (midi_outhead != midi_outtail)
    {
#ifdef TEST_DEJITTER
    	if (!db)
	{
    	    post("out: del %f, midiRT %f logicaltime %f, RT %f dacminusRT %f",
	    	(midi_outqueue[midi_outtail].q_time - midirealtime),
		    midirealtime, .001 * clock_gettimesince(sys_midiinittime),
		    	sys_getrealtime(), sys_dactimeminusrealtime);
	    db = 1;
	}
#endif
    	if (midi_outqueue[midi_outtail].q_time <= midirealtime)
	    sys_putnext();
	else break;
    }
}

static void sys_queuemidimess(int portno, int onebyte, int a, int b, int c)
{
    t_midiqelem *midiqelem;
    int newhead = midi_outhead +1;
    if (newhead == MIDIQSIZE)
    	newhead = 0;
    	    /* if FIFO is full flush an element to make room */
    if (newhead == midi_outtail)
    	sys_putnext();
    midi_outqueue[midi_outhead].q_portno = portno;
    midi_outqueue[midi_outhead].q_onebyte = onebyte;
    midi_outqueue[midi_outhead].q_byte1 = a;
    midi_outqueue[midi_outhead].q_byte2 = b;
    midi_outqueue[midi_outhead].q_byte3 = c;
    midi_outqueue[midi_outhead].q_time =
    	.001 * clock_gettimesince(sys_midiinittime);
    midi_outhead = newhead;
    sys_pollmidioutqueue();
}

#define MIDI_NOTEON 144
#define MIDI_POLYAFTERTOUCH 160
#define MIDI_CONTROLCHANGE 176
#define MIDI_PROGRAMCHANGE 192
#define MIDI_AFTERTOUCH 208
#define MIDI_PITCHBEND 224

void outmidi_noteon(int portno, int channel, int pitch, int velo)
{
    if (pitch < 0) pitch = 0;
    else if (pitch > 127) pitch = 127;
    if (velo < 0) velo = 0;
    else if (velo > 127) velo = 127;
    sys_queuemidimess(portno, 0, MIDI_NOTEON + (channel & 0xf), pitch, velo);
}

void outmidi_controlchange(int portno, int channel, int ctl, int value)
{
    if (ctl < 0) ctl = 0;
    else if (ctl > 127) ctl = 127;
    if (value < 0) value = 0;
    else if (value > 127) value = 127;
    sys_queuemidimess(portno, 0, MIDI_CONTROLCHANGE + (channel & 0xf),
    	ctl, value);
}

void outmidi_programchange(int portno, int channel, int value)
{
    if (value < 0) value = 0;
    else if (value > 127) value = 127;
    sys_queuemidimess(portno, 0,
    	MIDI_PROGRAMCHANGE + (channel & 0xf), value, 0);
}

void outmidi_pitchbend(int portno, int channel, int value)
{
    if (value < 0) value = 0;
    else if (value > 16383) value = 16383;
    sys_queuemidimess(portno, 0, MIDI_PITCHBEND + (channel & 0xf),
    	(value & 127), ((value>>7) & 127));
}

void outmidi_aftertouch(int portno, int channel, int value)
{
    if (value < 0) value = 0;
    else if (value > 127) value = 127;
    sys_queuemidimess(portno, 0, MIDI_AFTERTOUCH + (channel & 0xf), value, 0);
}

void outmidi_polyaftertouch(int portno, int channel, int pitch, int value)
{
    if (pitch < 0) pitch = 0;
    else if (pitch > 127) pitch = 127;
    if (value < 0) value = 0;
    else if (value > 127) value = 127;
    sys_queuemidimess(portno, 0, MIDI_POLYAFTERTOUCH + (channel & 0xf),
    	pitch, value);
}

void outmidi_mclk(int portno)
{
   sys_queuemidimess(portno, 1, 0xf8, 0,0);
}

/* ------------------------- MIDI input queue handling ------------------ */
typedef struct midiparser
{
    int mp_status;
    int mp_sysex;
    int mp_gotbyte1;
    int mp_byte1;
} t_midiparser;

#define MIDINOTEOFF       0x80
#define MIDINOTEON        0x90
#define MIDIPOLYTOUCH     0xa0
#define MIDICONTROLCHANGE 0xb0
#define MIDIPROGRAMCHANGE 0xc0
#define MIDICHANNELTOUCH  0xd0
#define MIDIPITCHBEND     0xe0
    /* functions in x_midi.c */
void inmidi_realtimein(int portno, int cmd);
void inmidi_byte(int portno, int byte);
void inmidi_sysex(int portno, int byte);
void inmidi_noteon(int portno, int channel, int pitch, int velo);
void inmidi_controlchange(int portno, int channel, int ctlnumber, int value);
void inmidi_programchange(int portno, int channel, int value);
void inmidi_pitchbend(int portno, int channel, int value);
void inmidi_aftertouch(int portno, int channel, int value);
void inmidi_polyaftertouch(int portno, int channel, int pitch, int value);

static void sys_dispatchnextmidiin( void)
{
    static t_midiparser parser[MAXMIDIINDEV], *parserp;
    int portno = midi_inqueue[midi_intail].q_portno,
    	byte = midi_inqueue[midi_intail].q_byte1;
    if (!midi_inqueue[midi_intail].q_onebyte)
    	bug("sys_dispatchnextmidiin");
    if (portno < 0 || portno >= MAXMIDIINDEV)
    	bug("sys_dispatchnextmidiin 2");
    parserp = parser + portno;
    outlet_setstacklim();
    
    if (byte >= 0xf8)
    	inmidi_realtimein(portno, byte);
    else
    {
    	inmidi_byte(portno, byte);
	if (byte < 0xf0)
	{
	    if (byte & 0x80)
	    {
	    	parserp->mp_status = byte;
		parserp->mp_gotbyte1 = 0;
	    }
	    else
	    {
	    	int cmd = (parserp->mp_status & 0xf0);
		int chan = (parserp->mp_status & 0xf);
		int byte1 = parserp->mp_byte1, gotbyte1 = parserp->mp_gotbyte1;
		switch (cmd)
		{
		case MIDINOTEOFF:
		    if (gotbyte1)
		    	inmidi_noteon(portno, chan, byte1, 0),
			    parserp->mp_gotbyte1 = 0;
		    else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1;
		    break;
		case MIDINOTEON:
		    if (gotbyte1)
		    	inmidi_noteon(portno, chan, byte1, byte),
			    parserp->mp_gotbyte1 = 0;
		    else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1;
		    break;
		case MIDIPOLYTOUCH:
		    if (gotbyte1)
		    	inmidi_polyaftertouch(portno, chan, byte1, byte),
			    parserp->mp_gotbyte1 = 0;
		    else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1;
		    break;
		case MIDICONTROLCHANGE:
		    if (gotbyte1)
		    	inmidi_controlchange(portno, chan, byte1, byte),
			    parserp->mp_gotbyte1 = 0;
		    else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1;
		    break;
		case MIDIPROGRAMCHANGE:
		    inmidi_programchange(portno, chan, byte);
		    break;
		case MIDICHANNELTOUCH:
		    inmidi_aftertouch(portno, chan, byte);
		    break;
		case MIDIPITCHBEND:
		    if (gotbyte1)
		    	inmidi_pitchbend(portno, chan, ((byte << 7) + byte1)),
			    parserp->mp_gotbyte1 = 0;
		    else parserp->mp_byte1 = byte, parserp->mp_gotbyte1 = 1;
		    break;
		}
	    }
	 }
    }  
    midi_intail  = (midi_intail + 1 == MIDIQSIZE ? 0 : midi_intail + 1);
}

void sys_pollmidiinqueue( void)
{
#ifdef TEST_DEJITTER
    static int db = 0;
#endif
    double logicaltime = .001 * clock_gettimesince(sys_midiinittime);
#ifdef TEST_DEJITTER
    if (midi_inhead == midi_intail)
    	db = 0;
#endif
    while (midi_inhead != midi_intail)
    {
#ifdef TEST_DEJITTER
    	if (!db)
	{
    	    post("in del %f, logicaltime %f, RT %f adcminusRT %f",
	    	(midi_inqueue[midi_intail].q_time - logicaltime),
		    logicaltime, sys_getrealtime(), sys_adctimeminusrealtime);
	    db = 1;
	}
#endif
#if 0
    	if (midi_inqueue[midi_intail].q_time <= logicaltime - 0.007)
	    post("late %f",
	    	1000 * (logicaltime - midi_inqueue[midi_intail].q_time));
#endif
    	if (midi_inqueue[midi_intail].q_time <= logicaltime)
	{
#if 0
	    post("diff %f",
	    	1000* (logicaltime - midi_inqueue[midi_intail].q_time));
#endif
	    sys_dispatchnextmidiin();
	}
	else break;
    }
}

    /* this should be called from the system dependent MIDI code when a byte
    comes in, as a result of our calling sys_poll_midi.  We stick it on a
    timetag queue and dispatch it at the appropriate logical time. */


void sys_midibytein(int portno, int byte)
{
    static int warned = 0;
    t_midiqelem *midiqelem;
    int newhead = midi_inhead +1;
    if (newhead == MIDIQSIZE)
    	newhead = 0;
    	    /* if FIFO is full flush an element to make room */
    if (newhead == midi_intail)
    {
    	if (!warned)
	{
	    post("warning: MIDI timing FIFO overflowed");
	    warned = 1;
	}
	sys_dispatchnextmidiin();
    }
    midi_inqueue[midi_inhead].q_portno = portno;
    midi_inqueue[midi_inhead].q_onebyte = 1;
    midi_inqueue[midi_inhead].q_byte1 = byte;
    midi_inqueue[midi_inhead].q_time = sys_getmidiinrealtime();
    midi_inhead = newhead;
    sys_pollmidiinqueue();
}

void sys_pollmidiqueue( void)
{
#if 0
    static double lasttime;
    double newtime = sys_getrealtime();
    if (newtime - lasttime > 0.007)
    	post("delay %d", (int)(1000 * (newtime - lasttime)));
    lasttime = newtime;
#endif
    sys_poll_midi();	/* OS dependent poll for MIDI input */
    sys_pollmidioutqueue();
    sys_pollmidiinqueue();
}