Home | History | Annotate | Line # | Download | only in dev
midisynvar.h revision 1.9.14.14
      1 /*	$NetBSD: midisynvar.h,v 1.9.14.14 2006/06/08 04:55:23 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 	int  (*allocv)  (midisyn *, uint_fast8_t, uint_fast8_t);
     74 	void (*noteon)  (midisyn *, uint32_t, uint32_t, uint32_t);
     75 	void (*noteoff) (midisyn *, uint32_t, uint32_t, uint32_t);
     76 	void (*pgmchg)  (midisyn *, uint32_t, uint32_t);
     77 };
     78 
     79 struct voice {
     80 	u_int chan_note;	/* channel and note */
     81 #define MS_CHANNOTE(chan, note) ((chan) * 256 + (note))
     82 #define MS_GETCHAN(v) ((v)->chan_note >> 8)
     83 	u_int seqno;		/* allocation index (increases with time) */
     84 	u_char inuse;
     85 };
     86 
     87 #define MIDI_MAX_CHANS 16
     88 
     89 struct midisyn {
     90 	/* Filled by synth driver */
     91 	struct midisyn_methods *mets;
     92 	char name[32];
     93 	int nvoice;
     94 	int flags;
     95 #define MS_DOALLOC	1 /* obsolescent: implied if driver has no allocv */
     96 #define MS_FREQXLATE	2
     97 	void *data;
     98 
     99 	/* Set up by midisyn but available to synth driver for reading ctls */
    100 	/*
    101 	 * Note - there is currently no locking on this structure; if the synth
    102 	 * driver interacts with midictl it should do so synchronously, when
    103 	 * processing a call from midisyn, and not at other times such as upon
    104 	 * an interrupt. (may revisit locking if problems crop up.)
    105 	 */
    106 	midictl ctl;
    107 
    108 	/* Used by midisyn driver */
    109 	struct voice *voices;
    110 	u_int seqno;
    111 	u_int16_t pgms[MIDI_MAX_CHANS]; /* ref'd if driver uses MS_GETPGM */
    112 };
    113 
    114 #define MS_GETPGM(ms, vno) ((ms)->pgms[MS_GETCHAN(&(ms)->voices[vno])])
    115 
    116 struct midi_softc;
    117 
    118 extern const struct midi_hw_if midisyn_hw_if;
    119 
    120 void	midisyn_attach (struct midi_softc *, midisyn *);
    121 
    122 /*
    123  * Convert a 14-bit volume or expression controller value to centibels using
    124  * the General MIDI formula. The maximum controller value translates to 0 cB
    125  * (no attenuation), a half-range controller to -119 cB (level cut by 11.9 dB)
    126  * and a zero controller to INT16_MIN. If you are converting a 7-bit value
    127  * just shift it 7 bits left first.
    128  */
    129 extern int16_t midisyn_vol2cB(uint_fast16_t);
    130 
    131 /*
    132  * MIDI RP-012 constitutes a MIDI Tuning Specification. The units are
    133  * fractional-MIDIkeys, that is, the key number 00 - 7f left shifted
    134  * 14 bits to provide a 14-bit fraction that divides each semitone. The
    135  * whole thing is just a 21-bit number that is bent and tuned simply by
    136  * adding and subtracting--the same offset is the same pitch change anywhere
    137  * on the scale. One downside is that a cent is 163.84 of these units, so
    138  * you can't expect a lengthy integer sum of cents to come out in tune; if you
    139  * do anything in cents it is best to use them only for local adjustment of
    140  * a pitch.
    141  *
    142  * This function converts a pitch in MIDItune units to Hz left-shifted 18 bits.
    143  * That should leave you enough to shift down to whatever precision the hardware
    144  * supports. You can generate a float representation in Hz (where float is
    145  * supported) with scalbn(midisyn_mt2hz18(mt),-18).
    146  */
    147 extern uint32_t midisyn_mt2hz18(uint32_t);
    148 
    149 #define MIDISYN_HZ18_TO_HZ(f) ((f) >> 18)
    150 #define MIDISYN_KEY_TO_MT(k) ((k)<<14)
    151 #define MIDISYN_MT_TO_KEY(m) (((m)+(1<<13))>>14)
    152 
    153 #define MIDISYN_FREQ_TO_HZ(f) ((f) >> 16) /* deprecated; going away RSN */
    154 
    155 #endif /* _SYS_DEV_MIDISYNVAR_H_ */
    156