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