Home | History | Annotate | Line # | Download | only in dev
midisynvar.h revision 1.9.14.15
      1  1.9.14.15      chap /*	$NetBSD: midisynvar.h,v 1.9.14.15 2006/06/08 13:21:48 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.9.14.11      chap #include "midictl.h"
     43  1.9.14.11      chap 
     44        1.1  augustss typedef struct midisyn midisyn;
     45        1.1  augustss 
     46  1.9.14.13      chap /*
     47  1.9.14.13      chap  * Important: any synth driver that uses midisyn must set up its methods
     48  1.9.14.13      chap  * structure in a way that refers to members /by name/ and zeroes the rest
     49  1.9.14.13      chap  * (as is the effect of C99 initializers with designators). This discipline
     50  1.9.14.13      chap  * will allow the structure to evolve methods for newly implemented
     51  1.9.14.13      chap  * functionality or to exploit capabilities of new drivers with a minimal
     52  1.9.14.13      chap  * versioning burden.  Because midisyn is at present a very rudimentary and
     53  1.9.14.13      chap  * partial implementation, change should be expected in this set of methods.
     54  1.9.14.13      chap  * Do not hesitate to add one in the course of implementing new stuff, as
     55  1.9.14.13      chap  * long as it will be generally useful and there is some reasonable fallback
     56  1.9.14.13      chap  * for drivers without it.
     57  1.9.14.13      chap  */
     58        1.1  augustss struct midisyn_methods {
     59  1.9.14.14      chap 	int  (*open)	(midisyn *, int /* flags */);
     60        1.4  augustss 	void (*close)   (midisyn *);
     61  1.9.14.10      chap 	int  (*ioctl)   (midisyn *, u_long, caddr_t, int, struct lwp *);
     62  1.9.14.14      chap 	/*
     63  1.9.14.14      chap 	 * allocv(midisyn *ms, uint_fast8_t chan, uint_fast8_t key);
     64  1.9.14.14      chap 	 * Allocate one of the devices actual voices (stealing one if
     65  1.9.14.14      chap 	 * necessary) to play note number 'key' (the MIDI note number, not
     66  1.9.14.14      chap 	 * a frequency) associated with MIDI channel chan. An implementation
     67  1.9.14.14      chap 	 * might want to choose a voice already last used on chan, to save
     68  1.9.14.14      chap 	 * shuffling of patches.
     69  1.9.14.14      chap 	 * One day a variant of this method will probably be needed, with an
     70  1.9.14.14      chap 	 * extra argument indicating whether a melodic or percussive voice is
     71  1.9.14.14      chap 	 * wanted.
     72  1.9.14.14      chap 	 */
     73  1.9.14.15      chap 	uint_fast16_t  (*allocv)  (midisyn *, uint_fast8_t, uint_fast8_t);
     74  1.9.14.15      chap 	/*
     75  1.9.14.15      chap 	 * attackv(midisyn *ms,
     76  1.9.14.15      chap 	 *         uint_fast16_t voice, uint32_t miditune, int16_t level_cB);
     77  1.9.14.15      chap 	 * Attack the voice 'voice' at pitch 'miditune' with level 'level_cB'.
     78  1.9.14.15      chap 	 * The pitch is in MIDI Tuning units and accounts for all of the pitch
     79  1.9.14.15      chap 	 * adjustment controls that midisyn supports and that the driver has
     80  1.9.14.15      chap 	 * not reported handling internally. The level is in centibels of
     81  1.9.14.15      chap 	 * attenuation (0 == no attenuation, full output, lower levels are
     82  1.9.14.15      chap 	 * negative) and again accounts for all level adjustments midisyn
     83  1.9.14.15      chap 	 * supports, except any the driver reports handling itself.
     84  1.9.14.15      chap 	 * The program used for the voice should be the current program of the
     85  1.9.14.15      chap 	 * voice's associated MIDI channel, and can be queried with MS_GETPGM.
     86  1.9.14.15      chap 	 */
     87  1.9.14.15      chap 	void (*attackv)  (midisyn *, uint_fast16_t, uint32_t, int16_t);
     88  1.9.14.15      chap 	/*
     89  1.9.14.15      chap 	 * attackv_vel(midisyn *ms, uint_fast16_t voice,
     90  1.9.14.15      chap 	 *             uint32_t miditune, int16_t level_cB, uint_fast8_t vel);
     91  1.9.14.15      chap 	 * If the driver can do something useful with the voice's attack
     92  1.9.14.15      chap 	 * velocity, such as vary the attack envelope or timbre, it should
     93  1.9.14.15      chap 	 * provide this method. Velocity 64 represents the normal attack for
     94  1.9.14.15      chap 	 * the patch, lower values are softer, higher harder. IF the driver
     95  1.9.14.15      chap 	 * does not supply this method, midisyn will call the attackv method
     96  1.9.14.15      chap 	 * instead, and include the velocity in the calculation of level_cB.
     97  1.9.14.15      chap 	 */
     98  1.9.14.15      chap 	void (*attackv_vel) (midisyn *,
     99  1.9.14.15      chap 	                     uint_fast16_t, uint32_t, int16_t, uint_fast8_t);
    100  1.9.14.15      chap 	/*
    101  1.9.14.15      chap 	 * releasev(midisyn *ms, uint_fast16_t voice, uint_fast8_t vel);
    102  1.9.14.15      chap 	 * Release the voice 'voice' with release velocity 'vel' where lower
    103  1.9.14.15      chap 	 * values mean slower decay, 64 refers to the normal decay envelope
    104  1.9.14.15      chap 	 * for the patch, and higher values mean decay faster.
    105  1.9.14.15      chap 	 */
    106  1.9.14.15      chap 	void (*releasev) (midisyn *, uint_fast16_t, uint_fast8_t);
    107  1.9.14.15      chap 	/*
    108  1.9.14.15      chap 	 * repitchv(midisyn *ms, uint_fast16_t voice, uint32_t miditune);
    109  1.9.14.15      chap 	 * A driver should provide this method if it is able to change the
    110  1.9.14.15      chap 	 * pitch of a sounding voice without rearticulating or glitching it.
    111  1.9.14.15      chap 	 * [not yet implemented in midisyn]
    112  1.9.14.15      chap 	 */
    113  1.9.14.15      chap 	void (*repitchv) (midisyn *, uint_fast16_t, uint32_t);
    114  1.9.14.15      chap 	/*
    115  1.9.14.15      chap 	 * relevelv(midisyn *ms, uint_fast16_t voice, int16_t level_cB);
    116  1.9.14.15      chap 	 * A driver should provide this method if it is able to change the
    117  1.9.14.15      chap 	 * level of a sounding voice without rearticulating or glitching it.
    118  1.9.14.15      chap 	 * How the driver should adjust 'level_cB' to account for envelope
    119  1.9.14.15      chap 	 * decay since the initial level is not (quite, yet) specified; the
    120  1.9.14.15      chap 	 * driver may save the last level it got from midisyn, subtract from
    121  1.9.14.15      chap 	 * this one, and use the difference to adjust the current level out of
    122  1.9.14.15      chap 	 * the envelope generator. Or not.... [not yet implemented in midisyn]
    123  1.9.14.15      chap 	 */
    124  1.9.14.15      chap 	void (*relevelv) (midisyn *, uint_fast16_t, int16_t);
    125  1.9.14.15      chap 	/*
    126  1.9.14.15      chap 	 * pgmchg(midisyn *ms, uint_fast8_t chan, uint_fast8_t pgm);
    127  1.9.14.15      chap 	 * If the driver supports changing programs, it should do whatever it
    128  1.9.14.15      chap 	 * does AND be sure to store pgm in ms->pgms[chan].  (XXX?)
    129  1.9.14.15      chap 	 */
    130  1.9.14.15      chap 	void (*pgmchg)  (midisyn *, uint_fast8_t, uint_fast8_t);
    131  1.9.14.15      chap 	/*
    132  1.9.14.15      chap 	 * ctlnotice(midisyn *ms,
    133  1.9.14.15      chap 	 *           midictl_evt evt, uint_fast8_t chan, uint_fast16_t key);
    134  1.9.14.15      chap 	 * Reports any of the events on channel 'chan' that midictl (which see)
    135  1.9.14.15      chap 	 * manages. Return 0 for a particular event if it is not handled by the
    136  1.9.14.15      chap 	 * driver, nonzero if the driver handles it. Many events can be handled
    137  1.9.14.15      chap 	 * by midisyn if the driver ignores them, but will be left to the driver
    138  1.9.14.15      chap 	 * if it returns non-zero. For example, midisyn will usually combine
    139  1.9.14.15      chap 	 * the volume and expression controllers, plus a note's attack velocity,
    140  1.9.14.15      chap 	 * into a single 'level' parameter to attackv, but if the driver is
    141  1.9.14.15      chap 	 * responding to the volume controller on its own, midisyn will leave
    142  1.9.14.15      chap 	 * that controller out of the combined level computation.
    143  1.9.14.15      chap 	 *   The driver should respond to MIDICTL_RESET events specially. There
    144  1.9.14.15      chap 	 * are some items of state other than controllers that are specified to
    145  1.9.14.15      chap 	 * be reset; if the driver keeps such state, it should be reset. The
    146  1.9.14.15      chap 	 * driver should also midictl_read() all controllers it can respond to,
    147  1.9.14.15      chap 	 * which will ensure that midictl tracks them.
    148  1.9.14.15      chap 	 *   There are also things that RP-015 specifies do NOT get reset by
    149  1.9.14.15      chap 	 * the MIDICTL_RESET event. Don't reset those. :)
    150  1.9.14.15      chap 	 */
    151  1.9.14.15      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.2  augustss 	u_char inuse;
    160        1.1  augustss };
    161        1.1  augustss 
    162        1.2  augustss #define MIDI_MAX_CHANS 16
    163        1.2  augustss 
    164        1.1  augustss struct midisyn {
    165        1.1  augustss 	/* Filled by synth driver */
    166        1.1  augustss 	struct midisyn_methods *mets;
    167        1.1  augustss 	char name[32];
    168        1.1  augustss 	int nvoice;
    169        1.1  augustss 	int flags;
    170  1.9.14.14      chap #define MS_DOALLOC	1 /* obsolescent: implied if driver has no allocv */
    171  1.9.14.15      chap #define MS_FREQXLATE	2 /* obsolescent: everybody gets miditunes */
    172        1.1  augustss 	void *data;
    173        1.1  augustss 
    174  1.9.14.11      chap 	/* Set up by midisyn but available to synth driver for reading ctls */
    175  1.9.14.11      chap 	/*
    176  1.9.14.11      chap 	 * Note - there is currently no locking on this structure; if the synth
    177  1.9.14.11      chap 	 * driver interacts with midictl it should do so synchronously, when
    178  1.9.14.11      chap 	 * processing a call from midisyn, and not at other times such as upon
    179  1.9.14.11      chap 	 * an interrupt. (may revisit locking if problems crop up.)
    180  1.9.14.11      chap 	 */
    181  1.9.14.11      chap 	midictl ctl;
    182  1.9.14.11      chap 
    183        1.1  augustss 	/* Used by midisyn driver */
    184        1.1  augustss 	struct voice *voices;
    185        1.1  augustss 	u_int seqno;
    186  1.9.14.12      chap 	u_int16_t pgms[MIDI_MAX_CHANS]; /* ref'd if driver uses MS_GETPGM */
    187        1.1  augustss };
    188        1.1  augustss 
    189        1.2  augustss #define MS_GETPGM(ms, vno) ((ms)->pgms[MS_GETCHAN(&(ms)->voices[vno])])
    190        1.2  augustss 
    191        1.2  augustss struct midi_softc;
    192        1.2  augustss 
    193  1.9.14.10      chap extern const struct midi_hw_if midisyn_hw_if;
    194        1.2  augustss 
    195        1.4  augustss void	midisyn_attach (struct midi_softc *, midisyn *);
    196        1.1  augustss 
    197  1.9.14.14      chap /*
    198  1.9.14.14      chap  * Convert a 14-bit volume or expression controller value to centibels using
    199  1.9.14.14      chap  * the General MIDI formula. The maximum controller value translates to 0 cB
    200  1.9.14.14      chap  * (no attenuation), a half-range controller to -119 cB (level cut by 11.9 dB)
    201  1.9.14.14      chap  * and a zero controller to INT16_MIN. If you are converting a 7-bit value
    202  1.9.14.14      chap  * just shift it 7 bits left first.
    203  1.9.14.14      chap  */
    204  1.9.14.14      chap extern int16_t midisyn_vol2cB(uint_fast16_t);
    205  1.9.14.14      chap 
    206  1.9.14.14      chap /*
    207  1.9.14.14      chap  * MIDI RP-012 constitutes a MIDI Tuning Specification. The units are
    208  1.9.14.14      chap  * fractional-MIDIkeys, that is, the key number 00 - 7f left shifted
    209  1.9.14.14      chap  * 14 bits to provide a 14-bit fraction that divides each semitone. The
    210  1.9.14.14      chap  * whole thing is just a 21-bit number that is bent and tuned simply by
    211  1.9.14.14      chap  * adding and subtracting--the same offset is the same pitch change anywhere
    212  1.9.14.14      chap  * on the scale. One downside is that a cent is 163.84 of these units, so
    213  1.9.14.14      chap  * you can't expect a lengthy integer sum of cents to come out in tune; if you
    214  1.9.14.14      chap  * do anything in cents it is best to use them only for local adjustment of
    215  1.9.14.14      chap  * a pitch.
    216  1.9.14.14      chap  *
    217  1.9.14.14      chap  * This function converts a pitch in MIDItune units to Hz left-shifted 18 bits.
    218  1.9.14.14      chap  * That should leave you enough to shift down to whatever precision the hardware
    219  1.9.14.14      chap  * supports. You can generate a float representation in Hz (where float is
    220  1.9.14.14      chap  * supported) with scalbn(midisyn_mt2hz18(mt),-18).
    221  1.9.14.14      chap  */
    222  1.9.14.14      chap extern uint32_t midisyn_mt2hz18(uint32_t);
    223  1.9.14.14      chap 
    224  1.9.14.14      chap #define MIDISYN_HZ18_TO_HZ(f) ((f) >> 18)
    225  1.9.14.14      chap #define MIDISYN_KEY_TO_MT(k) ((k)<<14)
    226  1.9.14.14      chap #define MIDISYN_MT_TO_KEY(m) (((m)+(1<<13))>>14)
    227  1.9.14.14      chap 
    228  1.9.14.15      chap #define MIDITUNE_OCTAVE  196608
    229  1.9.14.15      chap #define MIDITUNE_SEMITONE 16384
    230  1.9.14.15      chap #define MIDITUNE_CENT       164 /* this, regrettably, is not exact. */
    231        1.2  augustss 
    232        1.2  augustss #endif /* _SYS_DEV_MIDISYNVAR_H_ */
    233