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