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