1 
2 /*
3  *  Diverse Bristol audio routines.
4  *  Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
5  *
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 3 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Some structures for device management. These primarily MIDI message control,
24  * and eventually some for device management.
25  */
26 
27 #ifndef _BRISTOL_MIDI_API_H
28 #define _BRISTOL_MIDI_API_H
29 
30 #include <time.h>
31 #include <string.h>
32 
33 #include "bristolmessages.h"
34 
35 extern int bristolMidiOpen();
36 extern int bristolMidiClose();
37 extern int bristolMidiRead();
38 extern int bristolMidiRawWrite();
39 extern int bristolMidiWrite();
40 
41 extern void bristolMidiPrint();
42 extern void bristolMsgPrint();
43 extern void bristolMidiPrintGM2();
44 
45 #define MIDI_CONTROLLER_COUNT 128
46 #define CONTROLLER_RANGE 16384
47 #define C_RANGE_MIN_1 (CONTROLLER_RANGE - 1)
48 
49 /*
50  * API Status codes
51  */
52 #define BRISTOL_MIDI_OK 0
53 #define BRISTOL_MIDI_DEV -1
54 #define BRISTOL_MIDI_HANDLE -2
55 #define BRISTOL_MIDI_DEVICE -3
56 #define BRISTOL_MIDI_DRIVER -4
57 #define BRISTOL_MIDI_CHANNEL -5
58 
59 /* Conns are masked with 0x0ff0 */
60 #define BRISTOL_CONN_UNIX		0x00000010
61 #define BRISTOL_CONN_TCP		0x00000020
62 #define BRISTOL_CONN_MIDI		0x00000040
63 #define BRISTOL_CONN_OSSMIDI	0x00000080 /* As opposed to ALSA */
64 #define BRISTOL_CONN_SEQ		0x00000100 /* ALSA Sequencer interface */
65 #define BRISTOL_CONN_JACK		0x00000200 /* Jack Midi interface */
66 
67 #define BRISTOL_CONN_PASSIVE	0x00001000 /* listen socket. */
68 #define BRISTOL_CONN_FORCE		0x00002000
69 
70 #define BRISTOL_CONN_NBLOCK		0x00004000
71 #define BRISTOL_CONN_SYSEX		0x00008000
72 #define BRISTOL_CONN_FORWARD	0x00010000
73 
74 /*
75  * API flags
76  */
77 #define BRISTOL_RDONLY 1
78 #define BRISTOL_WRONLY 2
79 #define BRISTOL_DUPLEX 3
80 
81 #define BRISTOL_ACCEPT_SOCKET	0x80000000
82 #define BRISTOL_CONTROL_SOCKET	0x40000000
83 #define _BRISTOL_MIDI_DEBUG		0x20000000
84 
85 /*
86  * Midi command codes
87  */
88 #define BRISTOL_MIDI_CHANNELS 16
89 #define BRISTOL_NOTE_ON (1 << (MIDI_NOTE_ON >> 4))
90 #define BRISTOL_NOTE_OFF (1 << (MIDI_NOTE_OFF >> 4))
91 
92 #define MIDI_COMMAND_MASK 0x0f0
93 #define MIDI_CHAN_MASK 0x0f
94 
95 #define MIDI_BANK_SELECT	0x00
96 
97 #define MIDI_STATUS_MASK	0x080
98 #define MIDI_NOTE_ON		0x090
99 #define MIDI_NOTE_OFF		0x080
100 #define MIDI_POLY_PRESS		0x0a0
101 #define MIDI_CONTROL		0x0b0
102 #define MIDI_PROGRAM		0x0c0
103 #define MIDI_CHAN_PRESS		0x0d0
104 #define MIDI_PITCHWHEEL		0x0e0
105 
106 #define MIDI_ALL_SOUNDS_OFF	120
107 #define MIDI_ALL_CONTS_OFF	121
108 #define MIDI_LOCAL_KBD		122
109 #define MIDI_ALL_NOTES_OFF	123
110 #define MIDI_OMNI_ON		124
111 #define MIDI_OMNI_OFF		125
112 #define MIDI_POLY_ON		126
113 #define MIDI_POLY_OFF		127
114 
115 #define MIDI_RP_PW			0x0
116 #define MIDI_RP_FINETUNE	0x1
117 #define MIDI_RP_COARSETUNE	0x2
118 
119 /*
120  * The first just reflect the registered numbers. Breaks if there are more than
121  * 127 registered parameters..... Hm.
122  */
123 #define MIDI_NRP_PW			(0x3f80 + MIDI_RP_PW)
124 #define MIDI_NRP_FINETUNE	(0x3f80 + MIDI_RP_FINETUNE)
125 #define MIDI_NRP_COARSETUNE	(0x3f80 + MIDI_RP_COARSETUNE)
126 
127 /* Then there are mine, top page of NRP counting down. */
128 #define BRISTOL_NRP_NULL		16383 /* = NRP 0x7f-7f = 0x3fff */
129 #define BRISTOL_NRP_GAIN		16382 /* = NRP 0x7f-7e = 0x3ffe */
130 #define BRISTOL_NRP_DETUNE		16381 /* = NRP 0x7f-7d = 0x3ffd */
131 #define BRISTOL_NRP_VELOCITY	16380 /* = NRP 0x7f-7c = 0x3ffc */
132 #define BRISTOL_NRP_DEBUG		16379 /* = NRP 0x7f-7b = 0x3ffb */
133 #define BRISTOL_NRP_GLIDE		16378 /* = NRP 0x7f-7a = 0x3ffa */
134 #define BRISTOL_NRP_ENABLE_NRP	16377 /* = NRP 0x7f-79 = 0x3ff9 */
135 #define BRISTOL_NRP_LWF			16376 /* = NRP 0x7f-78 = 0x3ff8 */
136 #define BRISTOL_NRP_MNL_PREF	16375 /* = NRP 0x7f-77 = 0x3ff7 */
137 #define BRISTOL_NRP_MNL_TRIG	16374 /* = NRP 0x7f-76 = 0x3ff6 */
138 #define BRISTOL_NRP_MNL_VELOC	16373 /* = NRP 0x7f-75 = 0x3ff5 */
139 #define BRISTOL_NRP_FORWARD		16372 /* = NRP 0x7f-74 = 0x3ff4 */
140 #define BRISTOL_NRP_MIDI_GO		16371 /* = NRP 0x7f-73 = 0x3ff3 */
141 #define BRISTOL_NRP_SYSID_H		16370 /* = NRP 0x7f-72 = 0x3ff2 */
142 #define BRISTOL_NRP_SYSID_L		16369 /* = NRP 0x7f-71 = 0x3ff1 */
143 #define BRISTOL_NRP_REQ_SYSEX	16368 /* = NRP 0x7f-70 = 0x3ff0 */
144 #define BRISTOL_NRP_REQ_FORWARD	16367 /* = NRP 0x7f-6f = 0x3fef */
145 #define BRISTOL_NRP_REQ_DEBUG	16366 /* = NRP 0x7f-6e = 0x3fee */
146 
147 #define MIDI_SYSTEM		0x0f0
148 #define MIDI_SYSEX		0x0f8
149 #define MIDI_EOS		0x0f7
150 
151 #define BRISTOL_REQ_ON		0x001
152 #define BRISTOL_REQ_OFF		0x002
153 #define BRISTOL_REQ_PP		0x004
154 #define BRISTOL_REQ_CONTROL	0x008
155 #define BRISTOL_REQ_PROGRAM	0x010
156 #define BRISTOL_REQ_CHAN	0x020
157 #define BRISTOL_REQ_PITCH	0x040
158 #define BRISTOL_REQ_SYSEX	0x080
159 
160 #define BRISTOL_REQ_ALL		0x0ff
161 #define BRISTOL_REQ_NSX		0x07f /* not sysex messages....... */
162 
163 #define MIDI_GM_DATAENTRY	6
164 #define MIDI_GM_NRP			99
165 #define MIDI_GM_RP			101
166 #define MIDI_GM_DATAENTRY_F	38
167 #define MIDI_GM_NRP_F		98
168 #define MIDI_GM_RP_F		100
169 
170 #define BRISTOL_KF_RAW		0
171 #define BRISTOL_KF_TCP		1
172 #define BRISTOL_KF_JACK		2
173 
174 typedef struct KeyMsg {
175 	unsigned char key;
176 	unsigned char velocity;
177 	unsigned char flags;
178 } keyMsg;
179 
180 typedef struct ChanPressMsg {
181 	unsigned char pressure;
182 } chanPressMsg;
183 
184 typedef struct PressureMsg {
185 	unsigned char key;
186 	unsigned char pressure;
187 } pressureMsg;
188 
189 typedef struct PitchMsg {
190 	unsigned char lsb;
191 	unsigned char msb;
192 } pitchMsg;
193 
194 typedef struct SysexMsg {
195 	int length;
196 	unsigned char *data;
197 } sysexMsg;
198 
199 typedef struct ControlMsg {
200 	unsigned char c_id;
201 	unsigned char c_val;
202 } controlMsg;
203 
204 typedef struct ProgMsg {
205 	unsigned char p_id;
206 } programMsg;
207 
208 typedef struct BristolMidiMsg {
209 	unsigned char midiHandle;
210 	unsigned char channel;
211 	unsigned char mychannel;
212 	unsigned char command;
213 	struct timeval timestamp;
214 	int offset;
215 	unsigned int sequence;
216 	union {
217 		keyMsg key;
218 		pressureMsg pressure;
219 		chanPressMsg channelpress;
220 		pitchMsg pitch;
221 		controlMsg controller;
222 		programMsg program;
223 		bristolMsg bristol;
224 		bristolMsgType2 bristolt2;
225 		sysexMsg *sysex;
226 	} params;
227 	/*
228 	 * For continuous controllers we have another table. These may change the
229 	 * controller value as some are coarse and fine controls. All values are
230 	 * now floats to respect the interface resolutions. It is up to the synth
231 	 * to decide which values to use, but anticipated users are the GUI CC
232 	 * linkage and the engine GM-2 defaults.
233 	 */
234 	struct {
235 		int c_id; /* The NRP will start by reflecting the controllers */
236 		int c_id_coarse, c_id_fine; /* the two 7 bit c_id from MIDI encoding. */
237 		float value; /* 0.0 1.0 */
238 		int intvalue; /* 14 bit value */
239 		int coarse, fine; /* the two 7 bit values from MIDI encoding. */
240 	} GM2;
241 } bristolMidiMsg;
242 
243 #define DEF_TAB_SIZE 128
244 
245 typedef struct TableEntry {
246 	float defnote;
247 	float rate;
248 	float gain;
249 } tableEntry;
250 
251 /*
252  * The remainder are actually specific to bristol, and could be moved to a
253  * separate header file.
254  */
255 extern tableEntry defaultTable[DEF_TAB_SIZE];
256 extern tableEntry gainTable[CONTROLLER_RANGE];
257 
258 typedef int (*midiHandlerRoutine)();
259 
260 typedef struct fTab {
261 	float step;
262 	float freq;
263 } fTab;
264 
265 typedef struct MidiHandler {
266 	midiHandlerRoutine callback;
267 	int map[128];
268 	float floatmap[128];
269 } midiHandler;
270 
271 typedef struct BristolMidiHandler {
272 	midiHandler bmr[8];
273 	fTab freq[128];
274 } bristolMidiHandler;
275 
276 #endif /* _BRISTOL_MIDI_API_H */
277 
278