1 /*	$OpenBSD: midiio.h,v 1.2 2002/03/14 03:16:12 millert Exp $	*/
2 /*	$NetBSD: midiio.h,v 1.7 1998/11/25 22:17:07 augustss Exp $	*/
3 
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (augustss@netbsd.org).
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #ifndef _SYS_MIDIIO_H_
41 #define _SYS_MIDIIO_H_
42 
43 /*
44  * The API defined here is compatible with the OSS MIDI API except
45  * for naming.
46  */
47 
48 #ifndef _POSIX_SOURCE
49 #define __MIDIIO_UNSET_POSIX_SOURCE
50 #define _POSIX_SOURCE		/* make sure we don't get all the gunk */
51 #endif
52 #include <machine/endian.h>
53 #ifdef __MIDIIO_UNSET_POSIX_SOURCE
54 #undef _POSIX_SOURCE
55 #undef __MIDIIO_UNSET_POSIX_SOURCE
56 #endif
57 
58 /*
59  * ioctl() commands for /dev/midi##
60  */
61 typedef struct {
62 	unsigned	char cmd;
63 	char		nr_args, nr_returns;
64 	unsigned char	data[30];
65 } mpu_command_rec;
66 
67 #define MIDI_PRETIME		_IOWR('m', 0, int)
68 #define MIDI_MPUMODE		_IOWR('m', 1, int)
69 #define MIDI_MPUCMD		_IOWR('m', 2, mpu_command_rec)
70 
71 
72 /* The MPU401 command acknowledge and active sense command */
73 #define MIDI_ACK	0xfe
74 
75 
76 /* Sequencer */
77 #define SEQUENCER_RESET			_IO  ('Q', 0)
78 #define SEQUENCER_SYNC			_IO  ('Q', 1)
79 #define SEQUENCER_INFO			_IOWR('Q', 2, struct synth_info)
80 #define SEQUENCER_CTRLRATE		_IOWR('Q', 3, int)
81 #define SEQUENCER_GETOUTCOUNT		_IOR ('Q', 4, int)
82 #define SEQUENCER_GETINCOUNT		_IOR ('Q', 5, int)
83 /*#define SEQUENCER_PERCMODE		_IOW ('Q', 6, int)*/
84 /*#define SEQUENCER_TESTMIDI		_IOW ('Q', 8, int)*/
85 #define SEQUENCER_RESETSAMPLES		_IOW ('Q', 9, int)
86 #define SEQUENCER_NRSYNTHS		_IOR ('Q',10, int)
87 #define SEQUENCER_NRMIDIS		_IOR ('Q',11, int)
88 /*#define SEQUENCER_MIDI_INFO		_IOWR('Q',12, struct midi_info)*/
89 #define SEQUENCER_THRESHOLD		_IOW ('Q',13, int)
90 #define SEQUENCER_MEMAVL		_IOWR('Q',14, int)
91 /*#define SEQUENCER_FM_4OP_ENABLE		_IOW ('Q',15, int)*/
92 #define SEQUENCER_PANIC			_IO  ('Q',17)
93 #define SEQUENCER_OUTOFBAND		_IOW ('Q',18, struct seq_event_rec)
94 #define SEQUENCER_GETTIME		_IOR ('Q',19, int)
95 /*#define SEQUENCER_ID			_IOWR('Q',20, struct synth_info)*/
96 /*#define SEQUENCER_CONTROL		_IOWR('Q',21, struct synth_control)*/
97 /*#define SEQUENCER_REMOVESAMPLE		_IOWR('Q',22, struct remove_sample)*/
98 
99 #if 0
100 typedef struct synth_control {
101 	int	devno;		/* Synthesizer # */
102 	char	data[4000];	/* Device specific command/data record */
103 } synth_control;
104 
105 typedef struct remove_sample {
106 	int	devno;		/* Synthesizer # */
107 	int	bankno;		/* MIDI bank # (0=General MIDI) */
108 	int	instrno;	/* MIDI instrument number */
109 } remove_sample;
110 #endif
111 
112 #define CMDSIZE 8
113 typedef struct seq_event_rec {
114 	u_char	arr[CMDSIZE];
115 } seq_event_rec;
116 
117 struct synth_info {
118 	char	name[30];
119 	int	device;
120 	int	synth_type;
121 #define SYNTH_TYPE_FM			0
122 #define SYNTH_TYPE_SAMPLE		1
123 #define SYNTH_TYPE_MIDI			2
124 
125 	int	synth_subtype;
126 #define SYNTH_SUB_FM_TYPE_ADLIB		0x00
127 #define SYNTH_SUB_FM_TYPE_OPL3		0x01
128 #define SYNTH_SUB_MIDI_TYPE_MPU401	0x401
129 
130 #define SYNTH_SUB_SAMPLE_TYPE_BASIC	0x10
131 #define SYNTH_SUB_SAMPLE_TYPE_GUS	SAMPLE_TYPE_BASIC
132 
133 	int	nr_voices;
134 	int	instr_bank_size;
135 	u_int	capabilities;
136 #define SYNTH_CAP_OPL3			0x00000002
137 #define SYNTH_CAP_INPUT			0x00000004
138 };
139 
140 /* Sequencer timer */
141 #define SEQUENCER_TMR_TIMEBASE		_IOWR('T', 1, int)
142 #define SEQUENCER_TMR_START		_IO  ('T', 2)
143 #define SEQUENCER_TMR_STOP		_IO  ('T', 3)
144 #define SEQUENCER_TMR_CONTINUE		_IO  ('T', 4)
145 #define SEQUENCER_TMR_TEMPO		_IOWR('T', 5, int)
146 #define SEQUENCER_TMR_SOURCE		_IOWR('T', 6, int)
147 #  define SEQUENCER_TMR_INTERNAL	0x00000001
148 #if 0
149 #  define SEQUENCER_TMR_EXTERNAL	0x00000002
150 #  define SEQUENCER_TMR_MODE_MIDI	0x00000010
151 #  define SEQUENCER_TMR_MODE_FSK	0x00000020
152 #  define SEQUENCER_TMR_MODE_CLS	0x00000040
153 #  define SEQUENCER_TMR_MODE_SMPTE	0x00000080
154 #endif
155 #define SEQUENCER_TMR_METRONOME		_IOW ('T', 7, int)
156 #define SEQUENCER_TMR_SELECT		_IOW ('T', 8, int)
157 
158 
159 #define MIDI_CTRL_ALLOFF 123
160 #define MIDI_CTRL_RESET 121
161 #define MIDI_BEND_NEUTRAL (1<<13)
162 
163 #define MIDI_NOTEOFF		0x80
164 #define MIDI_NOTEON		0x90
165 #define MIDI_KEY_PRESSURE	0xA0
166 #define MIDI_CTL_CHANGE		0xB0
167 #define MIDI_PGM_CHANGE		0xC0
168 #define MIDI_CHN_PRESSURE	0xD0
169 #define MIDI_PITCH_BEND		0xE0
170 #define MIDI_SYSTEM_PREFIX	0xF0
171 
172 #define MIDI_IS_STATUS(d) ((d) >= 0x80)
173 #define MIDI_IS_COMMON(d) ((d) >= 0xf0)
174 
175 #define MIDI_SYSEX_START	0xF0
176 #define MIDI_SYSEX_END		0xF7
177 
178 #define MIDI_GET_STATUS(d) ((d) & 0xf0)
179 #define MIDI_GET_CHAN(d) ((d) & 0x0f)
180 
181 #define MIDI_HALF_VEL 64
182 
183 #define SEQ_LOCAL		0x80
184 #define SEQ_TIMING		0x81
185 #define SEQ_CHN_COMMON		0x92
186 #define SEQ_CHN_VOICE		0x93
187 #define SEQ_SYSEX		0x94
188 #define SEQ_FULLSIZE		0xfd
189 
190 #define SEQ_MK_CHN_VOICE(e, unit, cmd, chan, key, vel) (\
191     (e)->arr[0] = SEQ_CHN_VOICE, (e)->arr[1] = (unit), (e)->arr[2] = (cmd),\
192     (e)->arr[3] = (chan), (e)->arr[4] = (key), (e)->arr[5] = (vel),\
193     (e)->arr[6] = 0, (e)->arr[7] = 0)
194 #define SEQ_MK_CHN_COMMON(e, unit, cmd, chan, p1, p2, w14) (\
195     (e)->arr[0] = SEQ_CHN_COMMON, (e)->arr[1] = (unit), (e)->arr[2] = (cmd),\
196     (e)->arr[3] = (chan), (e)->arr[4] = (p1), (e)->arr[5] = (p2),\
197     *(short *)&(e)->arr[6] = (w14))
198 
199 #if _QUAD_LOWWORD == 1
200 /* big endian */
201 #define SEQ_PATCHKEY(id) (0xfd00|id)
202 #else
203 /* little endian */
204 #define SEQ_PATCHKEY(id) ((id<<8)|0xfd)
205 #endif
206 struct sysex_info {
207 	u_int16_t	key;	/* Use SYSEX_PATCH or MAUI_PATCH here */
208 #define SEQ_SYSEX_PATCH	SEQ_PATCHKEY(0x05)
209 #define SEQ_MAUI_PATCH	SEQ_PATCHKEY(0x06)
210 	int16_t	device_no;	/* Synthesizer number */
211 	int32_t	len;		/* Size of the sysex data in bytes */
212 	u_char	data[1];	/* Sysex data starts here */
213 };
214 #define SEQ_SYSEX_HDRSIZE ((u_long)((struct sysex_info *)0)->data)
215 
216 typedef unsigned char sbi_instr_data[32];
217 struct sbi_instrument {
218 	u_int16_t key;	/* FM_PATCH or OPL3_PATCH */
219 #define SBI_FM_PATCH	SEQ_PATCHKEY(0x01)
220 #define SBI_OPL3_PATCH	SEQ_PATCHKEY(0x03)
221 	int16_t		device;
222 	int32_t		channel;
223 	sbi_instr_data	operators;
224 };
225 
226 #define TMR_RESET		0
227 #define TMR_WAIT_REL		1	/* Time relative to the prev time */
228 #define TMR_WAIT_ABS		2	/* Absolute time since TMR_START */
229 #define TMR_STOP		3
230 #define TMR_START		4
231 #define TMR_CONTINUE		5
232 #define TMR_TEMPO		6
233 #define TMR_ECHO		8
234 #define TMR_CLOCK		9	/* MIDI clock */
235 #define TMR_SPP			10	/* Song position pointer */
236 #define TMR_TIMESIG		11	/* Time signature */
237 
238 /* Old sequencer definitions */
239 #define SEQOLD_CMDSIZE 4
240 
241 #define SEQOLD_NOTEOFF		0
242 #define SEQOLD_NOTEON		1
243 #define SEQOLD_WAIT		TMR_WAIT_ABS
244 #define SEQOLD_PGMCHANGE	3
245 #define SEQOLD_SYNCTIMER	TMR_START
246 #define SEQOLD_MIDIPUTC		5
247 #define SEQOLD_ECHO		TMR_ECHO
248 #define SEQOLD_AFTERTOUCH	9
249 #define SEQOLD_CONTROLLER	10
250 #define SEQOLD_PRIVATE		0xfe
251 #define SEQOLD_EXTENDED		0xff
252 
253 #endif /* !_SYS_MIDIIO_H_ */
254