xref: /openbsd/usr.bin/sndiod/midi.h (revision 1342ff69)
1 /*	$OpenBSD: midi.h,v 1.16 2024/05/03 05:18:09 ratchov Exp $	*/
2 /*
3  * Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #ifndef MIDI_H
18 #define MIDI_H
19 
20 #include "abuf.h"
21 #include "miofile.h"
22 
23 /*
24  * masks to extract command and channel of status byte
25  */
26 #define MIDI_CMDMASK	0xf0
27 #define MIDI_CHANMASK	0x0f
28 
29 /*
30  * MIDI status bytes of voice messages
31  */
32 #define MIDI_NOFF	0x80		/* note off */
33 #define MIDI_NON	0x90		/* note on */
34 #define MIDI_KAT	0xa0		/* key after touch */
35 #define MIDI_CTL	0xb0		/* controller */
36 #define MIDI_PC		0xc0		/* program change */
37 #define MIDI_CAT	0xd0		/* channel after touch */
38 #define MIDI_BEND	0xe0		/* pitch bend */
39 #define MIDI_ACK	0xfe		/* active sensing message */
40 
41 /*
42  * MIDI controller numbers
43  */
44 #define MIDI_CTL_VOL	7		/* volume */
45 
46 /*
47  * Max coarse value
48  */
49 #define MIDI_MAXCTL		127
50 
51 /*
52  * midi stream state structure
53  */
54 
55 struct midiops
56 {
57 	void (*imsg)(void *, unsigned char *, int);
58 	void (*omsg)(void *, unsigned char *, int);
59 	void (*fill)(void *, int);
60 	void (*exit)(void *);
61 };
62 
63 struct midi {
64 	struct midiops *ops;		/* port/sock/dev callbacks */
65 	struct midi *owner;		/* current writer stream */
66 	unsigned int mode;		/* MODE_{MIDIIN,MIDIOUT} */
67 	void *arg;			/* user data for callbacks */
68 #define MIDI_MSGMAX	16		/* max size of MIDI msg */
69 	unsigned char msg[MIDI_MSGMAX];	/* parsed input message */
70 	unsigned int st;		/* input MIDI running status */
71 	unsigned int last_st;		/* backup of st during sysex */
72 	unsigned int used;		/* bytes used in ``msg'' */
73 	unsigned int idx;		/* current ``msg'' size */
74 	unsigned int len;		/* expected ``msg'' length */
75 	unsigned int txmask;		/* list of ep we send to */
76 	unsigned int self;		/* equal (1 << index) */
77 	int tickets;			/* max bytes we can process */
78 	struct abuf obuf;		/* output buffer */
79 };
80 
81 /*
82  * midi port
83  */
84 struct port {
85 	struct port *next;
86 	struct port_mio mio;
87 #define PORT_CFG	0
88 #define PORT_INIT	1
89 #define PORT_DRAIN	2
90 	unsigned int state;
91 	unsigned int num;		/* port serial number */
92 	char *path;
93 	struct port *alt_next;
94 	int hold;			/* hold the port open ? */
95 	struct midi *midi;
96 };
97 
98 /*
99  * midi control ports
100  */
101 extern struct port *port_list;
102 
103 void midi_init(void);
104 void midi_done(void);
105 struct midi *midi_new(struct midiops *, void *, int);
106 void midi_del(struct midi *);
107 void midi_log(struct midi *);
108 void midi_tickets(struct midi *);
109 void midi_in(struct midi *, unsigned char *, int);
110 void midi_out(struct midi *, unsigned char *, int);
111 void midi_send(struct midi *, unsigned char *, int);
112 void midi_fill(struct midi *);
113 void midi_tag(struct midi *, unsigned int);
114 unsigned int midi_tags(struct midi *);
115 unsigned int midi_rxmask(struct midi *);
116 void midi_link(struct midi *, struct midi *);
117 void midi_abort(struct midi *);
118 void midi_migrate(struct midi *, struct midi *);
119 
120 void port_log(struct port *);
121 struct port *port_new(char *, unsigned int, int);
122 struct port *port_bynum(int);
123 void port_del(struct port *);
124 int  port_ref(struct port *);
125 void port_unref(struct port *);
126 int  port_init(struct port *);
127 void port_done(struct port *);
128 void port_drain(struct port *);
129 int  port_close(struct port *);
130 struct port *port_alt_ref(int);
131 struct port *port_migrate(struct port *);
132 
133 #endif /* !defined(MIDI_H) */
134