midisynvar.h revision 1.10 1 1.10 chap /* $NetBSD: midisynvar.h,v 1.10 2006/06/30 13:56:25 chap Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.3 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.7 keihan * by Lennart Augustsson (augustss (at) NetBSD.org).
9 1.1 augustss *
10 1.1 augustss * Redistribution and use in source and binary forms, with or without
11 1.1 augustss * modification, are permitted provided that the following conditions
12 1.1 augustss * are met:
13 1.1 augustss * 1. Redistributions of source code must retain the above copyright
14 1.1 augustss * notice, this list of conditions and the following disclaimer.
15 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer in the
17 1.1 augustss * documentation and/or other materials provided with the distribution.
18 1.1 augustss * 3. All advertising materials mentioning features or use of this software
19 1.1 augustss * must display the following acknowledgement:
20 1.1 augustss * This product includes software developed by the NetBSD
21 1.1 augustss * Foundation, Inc. and its contributors.
22 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 augustss * contributors may be used to endorse or promote products derived
24 1.1 augustss * from this software without specific prior written permission.
25 1.1 augustss *
26 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
37 1.1 augustss */
38 1.1 augustss
39 1.2 augustss #ifndef _SYS_DEV_MIDISYNVAR_H_
40 1.2 augustss #define _SYS_DEV_MIDISYNVAR_H_
41 1.2 augustss
42 1.10 chap #include "midictl.h"
43 1.10 chap
44 1.1 augustss typedef struct midisyn midisyn;
45 1.1 augustss
46 1.10 chap /*
47 1.10 chap * Important: any synth driver that uses midisyn must set up its methods
48 1.10 chap * structure in a way that refers to members /by name/ and zeroes the rest
49 1.10 chap * (as is the effect of C99 initializers with designators). This discipline
50 1.10 chap * will allow the structure to evolve methods for newly implemented
51 1.10 chap * functionality or to exploit capabilities of new drivers with a minimal
52 1.10 chap * versioning burden. Because midisyn is at present a very rudimentary and
53 1.10 chap * partial implementation, change should be expected in this set of methods.
54 1.10 chap * Do not hesitate to add one in the course of implementing new stuff, as
55 1.10 chap * long as it will be generally useful and there is some reasonable fallback
56 1.10 chap * for drivers without it.
57 1.10 chap */
58 1.1 augustss struct midisyn_methods {
59 1.10 chap int (*open) (midisyn *, int /* flags */);
60 1.4 augustss void (*close) (midisyn *);
61 1.9 christos int (*ioctl) (midisyn *, u_long, caddr_t, int, struct lwp *);
62 1.10 chap /*
63 1.10 chap * allocv(midisyn *ms, uint_fast8_t chan, uint_fast8_t key);
64 1.10 chap * Allocate one of the devices actual voices (stealing one if
65 1.10 chap * necessary) to play note number 'key' (the MIDI note number, not
66 1.10 chap * a frequency) associated with MIDI channel chan. An implementation
67 1.10 chap * might want to choose a voice already last used on chan, to save
68 1.10 chap * shuffling of patches.
69 1.10 chap * One day a variant of this method will probably be needed, with an
70 1.10 chap * extra argument indicating whether a melodic or percussive voice is
71 1.10 chap * wanted.
72 1.10 chap */
73 1.10 chap uint_fast16_t (*allocv) (midisyn *, uint_fast8_t, uint_fast8_t);
74 1.10 chap /*
75 1.10 chap * attackv(midisyn *ms,
76 1.10 chap * uint_fast16_t voice, midipitch_t mp, int16_t level_cB);
77 1.10 chap * Attack the voice 'voice' at pitch 'midipitch' with level 'level_cB'.
78 1.10 chap * The pitch is in MIDI Tuning units and accounts for all of the pitch
79 1.10 chap * adjustment controls that midisyn supports and that the driver has
80 1.10 chap * not reported handling internally. The level is in centibels of
81 1.10 chap * attenuation (0 == no attenuation, full output; reduced levels are
82 1.10 chap * negative) and again accounts for all level adjustments midisyn
83 1.10 chap * supports, except any the driver reports handling itself.
84 1.10 chap * The program used for the voice should be the current program of the
85 1.10 chap * voice's associated MIDI channel, and can be queried with MS_GETPGM.
86 1.10 chap */
87 1.10 chap void (*attackv) (midisyn *, uint_fast16_t, midipitch_t, int16_t);
88 1.10 chap /*
89 1.10 chap * attackv_vel(midisyn *ms, uint_fast16_t voice,
90 1.10 chap * midipitch_t mp, int16_t level_cB, uint_fast8_t vel);
91 1.10 chap * If the driver can do something useful with the voice's attack
92 1.10 chap * velocity, such as vary the attack envelope or timbre, it should
93 1.10 chap * provide this method. Velocity 64 represents the normal attack for
94 1.10 chap * the patch, lower values are softer, higher harder. IF the driver
95 1.10 chap * does not supply this method, midisyn will call the attackv method
96 1.10 chap * instead, and include the velocity in the calculation of level_cB.
97 1.10 chap */
98 1.10 chap void (*attackv_vel) (midisyn *,
99 1.10 chap uint_fast16_t, midipitch_t, int16_t, uint_fast8_t);
100 1.10 chap /*
101 1.10 chap * releasev(midisyn *ms, uint_fast16_t voice, uint_fast8_t vel);
102 1.10 chap * Release the voice 'voice' with release velocity 'vel' where lower
103 1.10 chap * values mean slower decay, 64 refers to the normal decay envelope
104 1.10 chap * for the patch, and higher values mean decay faster.
105 1.10 chap */
106 1.10 chap void (*releasev) (midisyn *, uint_fast16_t, uint_fast8_t);
107 1.10 chap /*
108 1.10 chap * repitchv(midisyn *ms, uint_fast16_t voice, midipitch_t mp);
109 1.10 chap * A driver should provide this method if it is able to change the
110 1.10 chap * pitch of a sounding voice without rearticulating or glitching it.
111 1.10 chap * [not yet implemented in midisyn]
112 1.10 chap */
113 1.10 chap void (*repitchv) (midisyn *, uint_fast16_t, midipitch_t);
114 1.10 chap /*
115 1.10 chap * relevelv(midisyn *ms, uint_fast16_t voice, int16_t level_cB);
116 1.10 chap * A driver should provide this method if it is able to change the
117 1.10 chap * level of a sounding voice without rearticulating or glitching it.
118 1.10 chap * How the driver should adjust 'level_cB' to account for envelope
119 1.10 chap * decay since the initial level is not (quite, yet) specified; the
120 1.10 chap * driver may save the last level it got from midisyn, subtract from
121 1.10 chap * this one, and use the difference to adjust the current level out of
122 1.10 chap * the envelope generator. Or not.... [not yet implemented in midisyn]
123 1.10 chap */
124 1.10 chap void (*relevelv) (midisyn *, uint_fast16_t, int16_t);
125 1.10 chap /*
126 1.10 chap * pgmchg(midisyn *ms, uint_fast8_t chan, uint_fast8_t pgm);
127 1.10 chap * If the driver supports changing programs, it should do whatever it
128 1.10 chap * does AND be sure to store pgm in ms->pgms[chan]. (XXX?)
129 1.10 chap */
130 1.10 chap void (*pgmchg) (midisyn *, uint_fast8_t, uint_fast8_t);
131 1.10 chap /*
132 1.10 chap * ctlnotice(midisyn *ms,
133 1.10 chap * midictl_evt evt, uint_fast8_t chan, uint_fast16_t key);
134 1.10 chap * Reports any of the events on channel 'chan' that midictl (which see)
135 1.10 chap * manages. Return 0 for a particular event if it is not handled by the
136 1.10 chap * driver, nonzero if the driver handles it. Many events can be handled
137 1.10 chap * by midisyn if the driver ignores them, but will be left to the driver
138 1.10 chap * if it returns non-zero. For example, midisyn will usually combine
139 1.10 chap * the volume and expression controllers, plus a note's attack velocity,
140 1.10 chap * into a single 'level' parameter to attackv, but if the driver is
141 1.10 chap * responding to the volume controller on its own, midisyn will leave
142 1.10 chap * that controller out of the combined level computation.
143 1.10 chap * The driver should respond to MIDICTL_RESET events specially. There
144 1.10 chap * are some items of state other than controllers that are specified to
145 1.10 chap * be reset; if the driver keeps such state, it should be reset. The
146 1.10 chap * driver should also midictl_read() all controllers it can respond to,
147 1.10 chap * which will ensure that midictl tracks them.
148 1.10 chap * There are also things that RP-015 specifies do NOT get reset by
149 1.10 chap * the MIDICTL_RESET event. Don't reset those. :)
150 1.10 chap */
151 1.10 chap int (*ctlnotice) (midisyn *, midictl_evt, uint_fast8_t, uint_fast16_t);
152 1.1 augustss };
153 1.1 augustss
154 1.1 augustss struct voice {
155 1.1 augustss u_int chan_note; /* channel and note */
156 1.2 augustss #define MS_CHANNOTE(chan, note) ((chan) * 256 + (note))
157 1.2 augustss #define MS_GETCHAN(v) ((v)->chan_note >> 8)
158 1.1 augustss u_int seqno; /* allocation index (increases with time) */
159 1.10 chap int16_t velcB; /* cB based on attack vel (relevel may need) */
160 1.2 augustss u_char inuse;
161 1.1 augustss };
162 1.1 augustss
163 1.2 augustss #define MIDI_MAX_CHANS 16
164 1.2 augustss
165 1.10 chap struct channelstate;
166 1.10 chap
167 1.1 augustss struct midisyn {
168 1.1 augustss /* Filled by synth driver */
169 1.1 augustss struct midisyn_methods *mets;
170 1.1 augustss char name[32];
171 1.1 augustss int nvoice;
172 1.1 augustss int flags;
173 1.1 augustss void *data;
174 1.1 augustss
175 1.10 chap /* Set up by midisyn but available to synth driver for reading ctls */
176 1.10 chap /*
177 1.10 chap * Note - there is currently no locking on this structure; if the synth
178 1.10 chap * driver interacts with midictl it should do so synchronously, when
179 1.10 chap * processing a call from midisyn, and not at other times such as upon
180 1.10 chap * an interrupt. (may revisit locking if problems crop up.)
181 1.10 chap */
182 1.10 chap midictl ctl;
183 1.10 chap
184 1.1 augustss /* Used by midisyn driver */
185 1.1 augustss struct voice *voices;
186 1.1 augustss u_int seqno;
187 1.10 chap u_int16_t pgms[MIDI_MAX_CHANS]; /* ref'd if driver uses MS_GETPGM */
188 1.10 chap struct channelstate *chnstate;
189 1.1 augustss };
190 1.1 augustss
191 1.2 augustss #define MS_GETPGM(ms, vno) ((ms)->pgms[MS_GETCHAN(&(ms)->voices[vno])])
192 1.2 augustss
193 1.2 augustss struct midi_softc;
194 1.2 augustss
195 1.8 yamt extern const struct midi_hw_if midisyn_hw_if;
196 1.2 augustss
197 1.4 augustss void midisyn_attach (struct midi_softc *, midisyn *);
198 1.1 augustss
199 1.10 chap /*
200 1.10 chap * Convert a 14-bit volume or expression controller value to centibels using
201 1.10 chap * the General MIDI formula. The maximum controller value translates to 0 cB
202 1.10 chap * (no attenuation), a half-range controller to -119 cB (level cut by 11.9 dB)
203 1.10 chap * and a zero controller to INT16_MIN. If you are converting a 7-bit value
204 1.10 chap * just shift it 7 bits left first.
205 1.10 chap */
206 1.10 chap extern int16_t midisyn_vol2cB(uint_fast16_t);
207 1.2 augustss
208 1.2 augustss #endif /* _SYS_DEV_MIDISYNVAR_H_ */
209