xref: /freebsd/sys/dev/speaker/spkr.c (revision e614aa8a3b45d7f3b0fad2ea374d6425165f8eb7)
15b81b6b3SRodney W. Grimes /*
2e614aa8aSAndrey A. Chernov  * spkr.c -- device driver for console speaker
35b81b6b3SRodney W. Grimes  *
4e614aa8aSAndrey A. Chernov  * v1.4 by Eric S. Raymond (esr@snark.thyrsus.com) Aug 1993
5e614aa8aSAndrey A. Chernov  * modified for FreeBSD by Andrew A. Chernov <ache@astral.msk.su>
66f78ca60SRodney W. Grimes  *
7e614aa8aSAndrey A. Chernov  *    $Id: spkr.c,v 1.3 1993/10/16 13:46:21 rgrimes Exp $
85b81b6b3SRodney W. Grimes  */
95b81b6b3SRodney W. Grimes 
105b81b6b3SRodney W. Grimes #include "speaker.h"
115b81b6b3SRodney W. Grimes 
125b81b6b3SRodney W. Grimes #if NSPEAKER > 0
135b81b6b3SRodney W. Grimes 
145b81b6b3SRodney W. Grimes #include "param.h"
155b81b6b3SRodney W. Grimes #include "kernel.h"
165b81b6b3SRodney W. Grimes #include "errno.h"
175b81b6b3SRodney W. Grimes #include "buf.h"
185b81b6b3SRodney W. Grimes #include "uio.h"
19e614aa8aSAndrey A. Chernov #include "machine/speaker.h"
20e614aa8aSAndrey A. Chernov 
21e614aa8aSAndrey A. Chernov #ifdef HZ
22e614aa8aSAndrey A. Chernov #undef HZ
23e614aa8aSAndrey A. Chernov #endif
24e614aa8aSAndrey A. Chernov #define HZ hz
255b81b6b3SRodney W. Grimes 
265b81b6b3SRodney W. Grimes /**************** MACHINE DEPENDENT PART STARTS HERE *************************
275b81b6b3SRodney W. Grimes  *
285b81b6b3SRodney W. Grimes  * This section defines a function tone() which causes a tone of given
295b81b6b3SRodney W. Grimes  * frequency and duration from the 80x86's console speaker.
305b81b6b3SRodney W. Grimes  * Another function endtone() is defined to force sound off, and there is
315b81b6b3SRodney W. Grimes  * also a rest() entry point to do pauses.
325b81b6b3SRodney W. Grimes  *
335b81b6b3SRodney W. Grimes  * Audible sound is generated using the Programmable Interval Timer (PIT) and
345b81b6b3SRodney W. Grimes  * Programmable Peripheral Interface (PPI) attached to the 80x86's speaker. The
355b81b6b3SRodney W. Grimes  * PPI controls whether sound is passed through at all; the PIT's channel 2 is
365b81b6b3SRodney W. Grimes  * used to generate clicks (a square wave) of whatever frequency is desired.
375b81b6b3SRodney W. Grimes  */
385b81b6b3SRodney W. Grimes 
395b81b6b3SRodney W. Grimes /*
405b81b6b3SRodney W. Grimes  * PIT and PPI port addresses and control values
415b81b6b3SRodney W. Grimes  *
425b81b6b3SRodney W. Grimes  * Most of the magic is hidden in the TIMER_PREP value, which selects PIT
435b81b6b3SRodney W. Grimes  * channel 2, frequency LSB first, square-wave mode and binary encoding.
445b81b6b3SRodney W. Grimes  * The encoding is as follows:
455b81b6b3SRodney W. Grimes  *
465b81b6b3SRodney W. Grimes  * +----------+----------+---------------+-----+
475b81b6b3SRodney W. Grimes  * |  1    0  |  1    1  |  0    1    1  |  0  |
485b81b6b3SRodney W. Grimes  * | SC1  SC0 | RW1  RW0 | M2   M1   M0  | BCD |
495b81b6b3SRodney W. Grimes  * +----------+----------+---------------+-----+
505b81b6b3SRodney W. Grimes  *   Counter     Write        Mode 3      Binary
515b81b6b3SRodney W. Grimes  *  Channel 2  LSB first,  (Square Wave) Encoding
525b81b6b3SRodney W. Grimes  *             MSB second
535b81b6b3SRodney W. Grimes  */
545b81b6b3SRodney W. Grimes #define PPI		0x61	/* port of Programmable Peripheral Interface */
555b81b6b3SRodney W. Grimes #define PPI_SPKR	0x03	/* turn these PPI bits on to pass sound */
565b81b6b3SRodney W. Grimes #define PIT_CTRL	0x43	/* PIT control address */
575b81b6b3SRodney W. Grimes #define PIT_COUNT	0x42	/* PIT count address */
585b81b6b3SRodney W. Grimes #define PIT_MODE	0xB6	/* set timer mode for sound generation */
595b81b6b3SRodney W. Grimes 
605b81b6b3SRodney W. Grimes /*
615b81b6b3SRodney W. Grimes  * Magic numbers for timer control.
625b81b6b3SRodney W. Grimes  */
635b81b6b3SRodney W. Grimes #define TIMER_CLK	1193180L	/* corresponds to 18.2 MHz tick rate */
645b81b6b3SRodney W. Grimes 
655b81b6b3SRodney W. Grimes static int endtone()
665b81b6b3SRodney W. Grimes /* turn off the speaker, ending current tone */
675b81b6b3SRodney W. Grimes {
685b81b6b3SRodney W. Grimes     wakeup((caddr_t)endtone);
695b81b6b3SRodney W. Grimes     outb(PPI, inb(PPI) & ~PPI_SPKR);
705b81b6b3SRodney W. Grimes }
715b81b6b3SRodney W. Grimes 
725b81b6b3SRodney W. Grimes static void tone(hz, ticks)
735b81b6b3SRodney W. Grimes /* emit tone of frequency hz for given number of ticks */
745b81b6b3SRodney W. Grimes unsigned int hz, ticks;
755b81b6b3SRodney W. Grimes {
765b81b6b3SRodney W. Grimes     unsigned int divisor = TIMER_CLK / hz;
775b81b6b3SRodney W. Grimes     int sps;
785b81b6b3SRodney W. Grimes 
795b81b6b3SRodney W. Grimes #ifdef DEBUG
80e614aa8aSAndrey A. Chernov     (void) printf("tone: hz=%d ticks=%d\n", hz, ticks);
815b81b6b3SRodney W. Grimes #endif /* DEBUG */
825b81b6b3SRodney W. Grimes 
835b81b6b3SRodney W. Grimes     /* set timer to generate clicks at given frequency in Hertz */
845b81b6b3SRodney W. Grimes     sps = spltty();
855b81b6b3SRodney W. Grimes     outb(PIT_CTRL, PIT_MODE);		/* prepare timer */
86e614aa8aSAndrey A. Chernov     outb(PIT_COUNT, (divisor & 0xff));	/* send lo byte */
875b81b6b3SRodney W. Grimes     outb(PIT_COUNT, (divisor >> 8));	/* send hi byte */
885b81b6b3SRodney W. Grimes     splx(sps);
895b81b6b3SRodney W. Grimes 
905b81b6b3SRodney W. Grimes     /* turn the speaker on */
915b81b6b3SRodney W. Grimes     outb(PPI, inb(PPI) | PPI_SPKR);
925b81b6b3SRodney W. Grimes 
935b81b6b3SRodney W. Grimes     /*
945b81b6b3SRodney W. Grimes      * Set timeout to endtone function, then give up the timeslice.
955b81b6b3SRodney W. Grimes      * This is so other processes can execute while the tone is being
965b81b6b3SRodney W. Grimes      * emitted.
975b81b6b3SRodney W. Grimes      */
985b81b6b3SRodney W. Grimes     timeout((caddr_t)endtone, (caddr_t)NULL, ticks);
99e614aa8aSAndrey A. Chernov     (void) sleep((caddr_t)endtone, PZERO - 1);
1005b81b6b3SRodney W. Grimes }
1015b81b6b3SRodney W. Grimes 
1025b81b6b3SRodney W. Grimes static int endrest()
1035b81b6b3SRodney W. Grimes /* end a rest */
1045b81b6b3SRodney W. Grimes {
1055b81b6b3SRodney W. Grimes     wakeup((caddr_t)endrest);
1065b81b6b3SRodney W. Grimes }
1075b81b6b3SRodney W. Grimes 
1085b81b6b3SRodney W. Grimes static void rest(ticks)
1095b81b6b3SRodney W. Grimes /* rest for given number of ticks */
1105b81b6b3SRodney W. Grimes int	ticks;
1115b81b6b3SRodney W. Grimes {
1125b81b6b3SRodney W. Grimes     /*
1135b81b6b3SRodney W. Grimes      * Set timeout to endrest function, then give up the timeslice.
1145b81b6b3SRodney W. Grimes      * This is so other processes can execute while the rest is being
1155b81b6b3SRodney W. Grimes      * waited out.
1165b81b6b3SRodney W. Grimes      */
1175b81b6b3SRodney W. Grimes #ifdef DEBUG
118e614aa8aSAndrey A. Chernov     (void) printf("rest: %d\n", ticks);
1195b81b6b3SRodney W. Grimes #endif /* DEBUG */
1205b81b6b3SRodney W. Grimes     timeout((caddr_t)endrest, (caddr_t)NULL, ticks);
121e614aa8aSAndrey A. Chernov     (void) sleep((caddr_t)endrest, PZERO - 1);
1225b81b6b3SRodney W. Grimes }
1235b81b6b3SRodney W. Grimes 
1245b81b6b3SRodney W. Grimes /**************** PLAY STRING INTERPRETER BEGINS HERE **********************
1255b81b6b3SRodney W. Grimes  *
1265b81b6b3SRodney W. Grimes  * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement;
127e614aa8aSAndrey A. Chernov  * M[LNS] are missing; the ~ synonym and the _ slur mark and the octave-
128e614aa8aSAndrey A. Chernov  * tracking facility are added.
1295b81b6b3SRodney W. Grimes  * Requires tone(), rest(), and endtone(). String play is not interruptible
1305b81b6b3SRodney W. Grimes  * except possibly at physical block boundaries.
1315b81b6b3SRodney W. Grimes  */
1325b81b6b3SRodney W. Grimes 
1335b81b6b3SRodney W. Grimes typedef int	bool;
1345b81b6b3SRodney W. Grimes #define TRUE	1
1355b81b6b3SRodney W. Grimes #define FALSE	0
1365b81b6b3SRodney W. Grimes 
1375b81b6b3SRodney W. Grimes #define toupper(c)	((c) - ' ' * (((c) >= 'a') && ((c) <= 'z')))
1385b81b6b3SRodney W. Grimes #define isdigit(c)	(((c) >= '0') && ((c) <= '9'))
1395b81b6b3SRodney W. Grimes #define dtoi(c)		((c) - '0')
1405b81b6b3SRodney W. Grimes 
1415b81b6b3SRodney W. Grimes static int octave;	/* currently selected octave */
1425b81b6b3SRodney W. Grimes static int whole;	/* whole-note time at current tempo, in ticks */
1435b81b6b3SRodney W. Grimes static int value;	/* whole divisor for note time, quarter note = 1 */
1445b81b6b3SRodney W. Grimes static int fill;	/* controls spacing of notes */
1455b81b6b3SRodney W. Grimes static bool octtrack;	/* octave-tracking on? */
1465b81b6b3SRodney W. Grimes static bool octprefix;	/* override current octave-tracking state? */
1475b81b6b3SRodney W. Grimes 
1485b81b6b3SRodney W. Grimes /*
1495b81b6b3SRodney W. Grimes  * Magic number avoidance...
1505b81b6b3SRodney W. Grimes  */
1515b81b6b3SRodney W. Grimes #define SECS_PER_MIN	60	/* seconds per minute */
1525b81b6b3SRodney W. Grimes #define WHOLE_NOTE	4	/* quarter notes per whole note */
1535b81b6b3SRodney W. Grimes #define MIN_VALUE	64	/* the most we can divide a note by */
1545b81b6b3SRodney W. Grimes #define DFLT_VALUE	4	/* default value (quarter-note) */
1555b81b6b3SRodney W. Grimes #define FILLTIME	8	/* for articulation, break note in parts */
1565b81b6b3SRodney W. Grimes #define STACCATO	6	/* 6/8 = 3/4 of note is filled */
1575b81b6b3SRodney W. Grimes #define NORMAL		7	/* 7/8ths of note interval is filled */
1585b81b6b3SRodney W. Grimes #define LEGATO		8	/* all of note interval is filled */
1595b81b6b3SRodney W. Grimes #define DFLT_OCTAVE	4	/* default octave */
1605b81b6b3SRodney W. Grimes #define MIN_TEMPO	32	/* minimum tempo */
1615b81b6b3SRodney W. Grimes #define DFLT_TEMPO	120	/* default tempo */
1625b81b6b3SRodney W. Grimes #define MAX_TEMPO	255	/* max tempo */
1635b81b6b3SRodney W. Grimes #define NUM_MULT	3	/* numerator of dot multiplier */
1645b81b6b3SRodney W. Grimes #define DENOM_MULT	2	/* denominator of dot multiplier */
1655b81b6b3SRodney W. Grimes 
1665b81b6b3SRodney W. Grimes /* letter to half-tone:  A   B  C  D  E  F  G */
1675b81b6b3SRodney W. Grimes static int notetab[8] = {9, 11, 0, 2, 4, 5, 7};
1685b81b6b3SRodney W. Grimes 
1695b81b6b3SRodney W. Grimes /*
1705b81b6b3SRodney W. Grimes  * This is the American Standard A440 Equal-Tempered scale with frequencies
1715b81b6b3SRodney W. Grimes  * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook...
1725b81b6b3SRodney W. Grimes  * our octave 0 is standard octave 2.
1735b81b6b3SRodney W. Grimes  */
1745b81b6b3SRodney W. Grimes #define OCTAVE_NOTES	12	/* semitones per octave */
1755b81b6b3SRodney W. Grimes static int pitchtab[] =
1765b81b6b3SRodney W. Grimes {
1775b81b6b3SRodney W. Grimes /*        C     C#    D     D#    E     F     F#    G     G#    A     A#    B*/
1785b81b6b3SRodney W. Grimes /* 0 */   65,   69,   73,   78,   82,   87,   93,   98,  103,  110,  117,  123,
1795b81b6b3SRodney W. Grimes /* 1 */  131,  139,  147,  156,  165,  175,  185,  196,  208,  220,  233,  247,
1805b81b6b3SRodney W. Grimes /* 2 */  262,  277,  294,  311,  330,  349,  370,  392,  415,  440,  466,  494,
1815b81b6b3SRodney W. Grimes /* 3 */  523,  554,  587,  622,  659,  698,  740,  784,  831,  880,  932,  988,
1825b81b6b3SRodney W. Grimes /* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975,
1835b81b6b3SRodney W. Grimes /* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
1845b81b6b3SRodney W. Grimes /* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902,
1855b81b6b3SRodney W. Grimes };
1865b81b6b3SRodney W. Grimes 
1875b81b6b3SRodney W. Grimes static void playinit()
1885b81b6b3SRodney W. Grimes {
1895b81b6b3SRodney W. Grimes     octave = DFLT_OCTAVE;
190e614aa8aSAndrey A. Chernov     whole = (HZ * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
1915b81b6b3SRodney W. Grimes     fill = NORMAL;
1925b81b6b3SRodney W. Grimes     value = DFLT_VALUE;
1935b81b6b3SRodney W. Grimes     octtrack = FALSE;
1945b81b6b3SRodney W. Grimes     octprefix = TRUE;	/* act as though there was an initial O(n) */
1955b81b6b3SRodney W. Grimes }
1965b81b6b3SRodney W. Grimes 
1975b81b6b3SRodney W. Grimes static void playtone(pitch, value, sustain)
1985b81b6b3SRodney W. Grimes /* play tone of proper duration for current rhythm signature */
1995b81b6b3SRodney W. Grimes int	pitch, value, sustain;
2005b81b6b3SRodney W. Grimes {
2015b81b6b3SRodney W. Grimes     register int	sound, silence, snum = 1, sdenom = 1;
2025b81b6b3SRodney W. Grimes 
2035b81b6b3SRodney W. Grimes     /* this weirdness avoids floating-point arithmetic */
2045b81b6b3SRodney W. Grimes     for (; sustain; sustain--)
2055b81b6b3SRodney W. Grimes     {
206e614aa8aSAndrey A. Chernov 	/* See the BUGS section in the man page for discussion */
2075b81b6b3SRodney W. Grimes 	snum *= NUM_MULT;
2085b81b6b3SRodney W. Grimes 	sdenom *= DENOM_MULT;
2095b81b6b3SRodney W. Grimes     }
2105b81b6b3SRodney W. Grimes 
2115b81b6b3SRodney W. Grimes     if (pitch == -1)
2125b81b6b3SRodney W. Grimes 	rest(whole * snum / (value * sdenom));
2135b81b6b3SRodney W. Grimes     else
2145b81b6b3SRodney W. Grimes     {
2155b81b6b3SRodney W. Grimes 	sound = (whole * snum) / (value * sdenom)
2165b81b6b3SRodney W. Grimes 		- (whole * (FILLTIME - fill)) / (value * FILLTIME);
2175b81b6b3SRodney W. Grimes 	silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom);
2185b81b6b3SRodney W. Grimes 
2195b81b6b3SRodney W. Grimes #ifdef DEBUG
220e614aa8aSAndrey A. Chernov 	(void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n",
2215b81b6b3SRodney W. Grimes 			pitch, sound, silence);
2225b81b6b3SRodney W. Grimes #endif /* DEBUG */
2235b81b6b3SRodney W. Grimes 
2245b81b6b3SRodney W. Grimes 	tone(pitchtab[pitch], sound);
2255b81b6b3SRodney W. Grimes 	if (fill != LEGATO)
2265b81b6b3SRodney W. Grimes 	    rest(silence);
2275b81b6b3SRodney W. Grimes     }
2285b81b6b3SRodney W. Grimes }
2295b81b6b3SRodney W. Grimes 
2305b81b6b3SRodney W. Grimes static int abs(n)
2315b81b6b3SRodney W. Grimes int n;
2325b81b6b3SRodney W. Grimes {
2335b81b6b3SRodney W. Grimes     if (n < 0)
2345b81b6b3SRodney W. Grimes 	return(-n);
2355b81b6b3SRodney W. Grimes     else
2365b81b6b3SRodney W. Grimes 	return(n);
2375b81b6b3SRodney W. Grimes }
2385b81b6b3SRodney W. Grimes 
2395b81b6b3SRodney W. Grimes static void playstring(cp, slen)
2405b81b6b3SRodney W. Grimes /* interpret and play an item from a notation string */
2415b81b6b3SRodney W. Grimes char	*cp;
2425b81b6b3SRodney W. Grimes size_t	slen;
2435b81b6b3SRodney W. Grimes {
244e614aa8aSAndrey A. Chernov     int		pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
2455b81b6b3SRodney W. Grimes 
2465b81b6b3SRodney W. Grimes #define GETNUM(cp, v)	for(v=0; isdigit(cp[1]) && slen > 0; ) \
2475b81b6b3SRodney W. Grimes 				{v = v * 10 + (*++cp - '0'); slen--;}
2485b81b6b3SRodney W. Grimes     for (; slen--; cp++)
2495b81b6b3SRodney W. Grimes     {
2505b81b6b3SRodney W. Grimes 	int		sustain, timeval, tempo;
2515b81b6b3SRodney W. Grimes 	register char	c = toupper(*cp);
2525b81b6b3SRodney W. Grimes 
2535b81b6b3SRodney W. Grimes #ifdef DEBUG
254e614aa8aSAndrey A. Chernov 	(void) printf("playstring: %c (%x)\n", c, c);
2555b81b6b3SRodney W. Grimes #endif /* DEBUG */
2565b81b6b3SRodney W. Grimes 
2575b81b6b3SRodney W. Grimes 	switch (c)
2585b81b6b3SRodney W. Grimes 	{
2595b81b6b3SRodney W. Grimes 	case 'A':  case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
2605b81b6b3SRodney W. Grimes 
2615b81b6b3SRodney W. Grimes 	    /* compute pitch */
2625b81b6b3SRodney W. Grimes 	    pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES;
2635b81b6b3SRodney W. Grimes 
2645b81b6b3SRodney W. Grimes 	    /* this may be followed by an accidental sign */
2655b81b6b3SRodney W. Grimes 	    if (cp[1] == '#' || cp[1] == '+')
2665b81b6b3SRodney W. Grimes 	    {
2675b81b6b3SRodney W. Grimes 		++pitch;
2685b81b6b3SRodney W. Grimes 		++cp;
2695b81b6b3SRodney W. Grimes 		slen--;
2705b81b6b3SRodney W. Grimes 	    }
2715b81b6b3SRodney W. Grimes 	    else if (cp[1] == '-')
2725b81b6b3SRodney W. Grimes 	    {
2735b81b6b3SRodney W. Grimes 		--pitch;
2745b81b6b3SRodney W. Grimes 		++cp;
2755b81b6b3SRodney W. Grimes 		slen--;
2765b81b6b3SRodney W. Grimes 	    }
2775b81b6b3SRodney W. Grimes 
2785b81b6b3SRodney W. Grimes 	    /*
2795b81b6b3SRodney W. Grimes 	     * If octave-tracking mode is on, and there has been no octave-
2805b81b6b3SRodney W. Grimes 	     * setting prefix, find the version of the current letter note
2815b81b6b3SRodney W. Grimes 	     * closest to the last regardless of octave.
2825b81b6b3SRodney W. Grimes 	     */
2835b81b6b3SRodney W. Grimes 	    if (octtrack && !octprefix)
2845b81b6b3SRodney W. Grimes 	    {
2855b81b6b3SRodney W. Grimes 		if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch))
2865b81b6b3SRodney W. Grimes 		{
2875b81b6b3SRodney W. Grimes 		    ++octave;
2885b81b6b3SRodney W. Grimes 		    pitch += OCTAVE_NOTES;
2895b81b6b3SRodney W. Grimes 		}
2905b81b6b3SRodney W. Grimes 
2915b81b6b3SRodney W. Grimes 		if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch))
2925b81b6b3SRodney W. Grimes 		{
2935b81b6b3SRodney W. Grimes 		    --octave;
2945b81b6b3SRodney W. Grimes 		    pitch -= OCTAVE_NOTES;
2955b81b6b3SRodney W. Grimes 		}
2965b81b6b3SRodney W. Grimes 	    }
2975b81b6b3SRodney W. Grimes 	    octprefix = FALSE;
2985b81b6b3SRodney W. Grimes 	    lastpitch = pitch;
2995b81b6b3SRodney W. Grimes 
3005b81b6b3SRodney W. Grimes 	    /* ...which may in turn be followed by an override time value */
3015b81b6b3SRodney W. Grimes 	    GETNUM(cp, timeval);
3025b81b6b3SRodney W. Grimes 	    if (timeval <= 0 || timeval > MIN_VALUE)
3035b81b6b3SRodney W. Grimes 		timeval = value;
3045b81b6b3SRodney W. Grimes 
3055b81b6b3SRodney W. Grimes 	    /* ...and/or sustain dots */
3065b81b6b3SRodney W. Grimes 	    for (sustain = 0; cp[1] == '.'; cp++)
3075b81b6b3SRodney W. Grimes 	    {
3085b81b6b3SRodney W. Grimes 		slen--;
3095b81b6b3SRodney W. Grimes 		sustain++;
3105b81b6b3SRodney W. Grimes 	    }
3115b81b6b3SRodney W. Grimes 
312e614aa8aSAndrey A. Chernov 	    /* ...and/or a slur mark */
313e614aa8aSAndrey A. Chernov 	    oldfill = fill;
314e614aa8aSAndrey A. Chernov 	    if (cp[1] == '_')
315e614aa8aSAndrey A. Chernov 	    {
316e614aa8aSAndrey A. Chernov 		fill = LEGATO;
317e614aa8aSAndrey A. Chernov 		++cp;
318e614aa8aSAndrey A. Chernov 		slen--;
319e614aa8aSAndrey A. Chernov 	    }
320e614aa8aSAndrey A. Chernov 
3215b81b6b3SRodney W. Grimes 	    /* time to emit the actual tone */
3225b81b6b3SRodney W. Grimes 	    playtone(pitch, timeval, sustain);
323e614aa8aSAndrey A. Chernov 
324e614aa8aSAndrey A. Chernov 	    fill = oldfill;
3255b81b6b3SRodney W. Grimes 	    break;
3265b81b6b3SRodney W. Grimes 
3275b81b6b3SRodney W. Grimes 	case 'O':
3285b81b6b3SRodney W. Grimes 	    if (cp[1] == 'N' || cp[1] == 'n')
3295b81b6b3SRodney W. Grimes 	    {
3305b81b6b3SRodney W. Grimes 		octprefix = octtrack = FALSE;
3315b81b6b3SRodney W. Grimes 		++cp;
3325b81b6b3SRodney W. Grimes 		slen--;
3335b81b6b3SRodney W. Grimes 	    }
3345b81b6b3SRodney W. Grimes 	    else if (cp[1] == 'L' || cp[1] == 'l')
3355b81b6b3SRodney W. Grimes 	    {
3365b81b6b3SRodney W. Grimes 		octtrack = TRUE;
3375b81b6b3SRodney W. Grimes 		++cp;
3385b81b6b3SRodney W. Grimes 		slen--;
3395b81b6b3SRodney W. Grimes 	    }
3405b81b6b3SRodney W. Grimes 	    else
3415b81b6b3SRodney W. Grimes 	    {
3425b81b6b3SRodney W. Grimes 		GETNUM(cp, octave);
3435b81b6b3SRodney W. Grimes 		if (octave >= sizeof(pitchtab) / OCTAVE_NOTES)
3445b81b6b3SRodney W. Grimes 		    octave = DFLT_OCTAVE;
3455b81b6b3SRodney W. Grimes 		octprefix = TRUE;
3465b81b6b3SRodney W. Grimes 	    }
3475b81b6b3SRodney W. Grimes 	    break;
3485b81b6b3SRodney W. Grimes 
3495b81b6b3SRodney W. Grimes 	case '>':
3505b81b6b3SRodney W. Grimes 	    if (octave < sizeof(pitchtab) / OCTAVE_NOTES - 1)
3515b81b6b3SRodney W. Grimes 		octave++;
3525b81b6b3SRodney W. Grimes 	    octprefix = TRUE;
3535b81b6b3SRodney W. Grimes 	    break;
3545b81b6b3SRodney W. Grimes 
3555b81b6b3SRodney W. Grimes 	case '<':
3565b81b6b3SRodney W. Grimes 	    if (octave > 0)
3575b81b6b3SRodney W. Grimes 		octave--;
3585b81b6b3SRodney W. Grimes 	    octprefix = TRUE;
3595b81b6b3SRodney W. Grimes 	    break;
3605b81b6b3SRodney W. Grimes 
3615b81b6b3SRodney W. Grimes 	case 'N':
3625b81b6b3SRodney W. Grimes 	    GETNUM(cp, pitch);
3635b81b6b3SRodney W. Grimes 	    for (sustain = 0; cp[1] == '.'; cp++)
3645b81b6b3SRodney W. Grimes 	    {
3655b81b6b3SRodney W. Grimes 		slen--;
3665b81b6b3SRodney W. Grimes 		sustain++;
3675b81b6b3SRodney W. Grimes 	    }
368e614aa8aSAndrey A. Chernov 	    oldfill = fill;
369e614aa8aSAndrey A. Chernov 	    if (cp[1] == '_')
370e614aa8aSAndrey A. Chernov 	    {
371e614aa8aSAndrey A. Chernov 		fill = LEGATO;
372e614aa8aSAndrey A. Chernov 		++cp;
373e614aa8aSAndrey A. Chernov 		slen--;
374e614aa8aSAndrey A. Chernov 	    }
3755b81b6b3SRodney W. Grimes 	    playtone(pitch - 1, value, sustain);
376e614aa8aSAndrey A. Chernov 	    fill = oldfill;
3775b81b6b3SRodney W. Grimes 	    break;
3785b81b6b3SRodney W. Grimes 
3795b81b6b3SRodney W. Grimes 	case 'L':
3805b81b6b3SRodney W. Grimes 	    GETNUM(cp, value);
3815b81b6b3SRodney W. Grimes 	    if (value <= 0 || value > MIN_VALUE)
3825b81b6b3SRodney W. Grimes 		value = DFLT_VALUE;
3835b81b6b3SRodney W. Grimes 	    break;
3845b81b6b3SRodney W. Grimes 
3855b81b6b3SRodney W. Grimes 	case 'P':
3865b81b6b3SRodney W. Grimes 	case '~':
3875b81b6b3SRodney W. Grimes 	    /* this may be followed by an override time value */
3885b81b6b3SRodney W. Grimes 	    GETNUM(cp, timeval);
3895b81b6b3SRodney W. Grimes 	    if (timeval <= 0 || timeval > MIN_VALUE)
3905b81b6b3SRodney W. Grimes 		timeval = value;
3915b81b6b3SRodney W. Grimes 	    for (sustain = 0; cp[1] == '.'; cp++)
3925b81b6b3SRodney W. Grimes 	    {
3935b81b6b3SRodney W. Grimes 		slen--;
3945b81b6b3SRodney W. Grimes 		sustain++;
3955b81b6b3SRodney W. Grimes 	    }
3965b81b6b3SRodney W. Grimes 	    playtone(-1, timeval, sustain);
3975b81b6b3SRodney W. Grimes 	    break;
3985b81b6b3SRodney W. Grimes 
3995b81b6b3SRodney W. Grimes 	case 'T':
4005b81b6b3SRodney W. Grimes 	    GETNUM(cp, tempo);
4015b81b6b3SRodney W. Grimes 	    if (tempo < MIN_TEMPO || tempo > MAX_TEMPO)
4025b81b6b3SRodney W. Grimes 		tempo = DFLT_TEMPO;
403e614aa8aSAndrey A. Chernov 	    whole = (HZ * SECS_PER_MIN * WHOLE_NOTE) / tempo;
4045b81b6b3SRodney W. Grimes 	    break;
4055b81b6b3SRodney W. Grimes 
4065b81b6b3SRodney W. Grimes 	case 'M':
4075b81b6b3SRodney W. Grimes 	    if (cp[1] == 'N' || cp[1] == 'n')
4085b81b6b3SRodney W. Grimes 	    {
4095b81b6b3SRodney W. Grimes 		fill = NORMAL;
4105b81b6b3SRodney W. Grimes 		++cp;
4115b81b6b3SRodney W. Grimes 		slen--;
4125b81b6b3SRodney W. Grimes 	    }
4135b81b6b3SRodney W. Grimes 	    else if (cp[1] == 'L' || cp[1] == 'l')
4145b81b6b3SRodney W. Grimes 	    {
4155b81b6b3SRodney W. Grimes 		fill = LEGATO;
4165b81b6b3SRodney W. Grimes 		++cp;
4175b81b6b3SRodney W. Grimes 		slen--;
4185b81b6b3SRodney W. Grimes 	    }
4195b81b6b3SRodney W. Grimes 	    else if (cp[1] == 'S' || cp[1] == 's')
4205b81b6b3SRodney W. Grimes 	    {
4215b81b6b3SRodney W. Grimes 		fill = STACCATO;
4225b81b6b3SRodney W. Grimes 		++cp;
4235b81b6b3SRodney W. Grimes 		slen--;
4245b81b6b3SRodney W. Grimes 	    }
4255b81b6b3SRodney W. Grimes 	    break;
4265b81b6b3SRodney W. Grimes 	}
4275b81b6b3SRodney W. Grimes     }
4285b81b6b3SRodney W. Grimes }
4295b81b6b3SRodney W. Grimes 
4305b81b6b3SRodney W. Grimes /******************* UNIX DRIVER HOOKS BEGIN HERE **************************
4315b81b6b3SRodney W. Grimes  *
4325b81b6b3SRodney W. Grimes  * This section implements driver hooks to run playstring() and the tone(),
4335b81b6b3SRodney W. Grimes  * endtone(), and rest() functions defined above.
4345b81b6b3SRodney W. Grimes  */
4355b81b6b3SRodney W. Grimes 
436e614aa8aSAndrey A. Chernov static int spkr_active = FALSE; /* exclusion flag */
4375b81b6b3SRodney W. Grimes static struct buf *spkr_inbuf;  /* incoming buf */
4385b81b6b3SRodney W. Grimes 
4395b81b6b3SRodney W. Grimes int spkropen(dev)
4405b81b6b3SRodney W. Grimes dev_t	dev;
4415b81b6b3SRodney W. Grimes {
4425b81b6b3SRodney W. Grimes #ifdef DEBUG
443e614aa8aSAndrey A. Chernov     (void) printf("spkropen: entering with dev = %x\n", dev);
4445b81b6b3SRodney W. Grimes #endif /* DEBUG */
4455b81b6b3SRodney W. Grimes 
4465b81b6b3SRodney W. Grimes     if (minor(dev) != 0)
4475b81b6b3SRodney W. Grimes 	return(ENXIO);
4485b81b6b3SRodney W. Grimes     else if (spkr_active)
4495b81b6b3SRodney W. Grimes 	return(EBUSY);
4505b81b6b3SRodney W. Grimes     else
4515b81b6b3SRodney W. Grimes     {
452e614aa8aSAndrey A. Chernov #ifdef DEBUG
453e614aa8aSAndrey A. Chernov 	(void) printf("spkropen: about to perform play initialization\n");
454e614aa8aSAndrey A. Chernov #endif /* DEBUG */
4555b81b6b3SRodney W. Grimes 	playinit();
4565b81b6b3SRodney W. Grimes 	spkr_inbuf = geteblk(DEV_BSIZE);
457e614aa8aSAndrey A. Chernov 	spkr_active = TRUE;
4585b81b6b3SRodney W. Grimes 	return(0);
4595b81b6b3SRodney W. Grimes     }
460e614aa8aSAndrey A. Chernov }
4615b81b6b3SRodney W. Grimes 
4625b81b6b3SRodney W. Grimes int spkrwrite(dev, uio)
4635b81b6b3SRodney W. Grimes dev_t		dev;
4645b81b6b3SRodney W. Grimes struct uio	*uio;
4655b81b6b3SRodney W. Grimes {
4665b81b6b3SRodney W. Grimes #ifdef DEBUG
4675b81b6b3SRodney W. Grimes     printf("spkrwrite: entering with dev = %x, count = %d\n",
4685b81b6b3SRodney W. Grimes 		dev, uio->uio_resid);
4695b81b6b3SRodney W. Grimes #endif /* DEBUG */
4705b81b6b3SRodney W. Grimes 
4715b81b6b3SRodney W. Grimes     if (minor(dev) != 0)
4725b81b6b3SRodney W. Grimes 	return(ENXIO);
473e614aa8aSAndrey A. Chernov     else if (uio->uio_resid > DEV_BSIZE)     /* prevent system crashes */
474e614aa8aSAndrey A. Chernov 	return(E2BIG);
4755b81b6b3SRodney W. Grimes     else
4765b81b6b3SRodney W. Grimes     {
477e614aa8aSAndrey A. Chernov 	unsigned n;
478e614aa8aSAndrey A. Chernov 	char *cp;
479e614aa8aSAndrey A. Chernov 	int error;
480e614aa8aSAndrey A. Chernov 
481e614aa8aSAndrey A. Chernov 	n = uio->uio_resid;
4825b81b6b3SRodney W. Grimes 	cp = spkr_inbuf->b_un.b_addr;
483e614aa8aSAndrey A. Chernov 	if (!(error = uiomove(cp, n, uio)))
4845b81b6b3SRodney W. Grimes 		playstring(cp, n);
4855b81b6b3SRodney W. Grimes 	return(error);
4865b81b6b3SRodney W. Grimes     }
4875b81b6b3SRodney W. Grimes }
4885b81b6b3SRodney W. Grimes 
4895b81b6b3SRodney W. Grimes int spkrclose(dev)
4905b81b6b3SRodney W. Grimes dev_t	dev;
4915b81b6b3SRodney W. Grimes {
4925b81b6b3SRodney W. Grimes #ifdef DEBUG
493e614aa8aSAndrey A. Chernov     (void) printf("spkrclose: entering with dev = %x\n", dev);
4945b81b6b3SRodney W. Grimes #endif /* DEBUG */
4955b81b6b3SRodney W. Grimes 
4965b81b6b3SRodney W. Grimes     if (minor(dev) != 0)
4975b81b6b3SRodney W. Grimes 	return(ENXIO);
4985b81b6b3SRodney W. Grimes     else
4995b81b6b3SRodney W. Grimes     {
5005b81b6b3SRodney W. Grimes 	endtone();
5015b81b6b3SRodney W. Grimes 	brelse(spkr_inbuf);
502e614aa8aSAndrey A. Chernov 	spkr_active = FALSE;
5035b81b6b3SRodney W. Grimes 	return(0);
5045b81b6b3SRodney W. Grimes     }
505e614aa8aSAndrey A. Chernov }
5065b81b6b3SRodney W. Grimes 
5075b81b6b3SRodney W. Grimes int spkrioctl(dev, cmd, cmdarg)
5085b81b6b3SRodney W. Grimes dev_t	dev;
5095b81b6b3SRodney W. Grimes int	cmd;
5105b81b6b3SRodney W. Grimes caddr_t	cmdarg;
5115b81b6b3SRodney W. Grimes {
5125b81b6b3SRodney W. Grimes #ifdef DEBUG
513e614aa8aSAndrey A. Chernov     (void) printf("spkrioctl: entering with dev = %x, cmd = %x\n");
5145b81b6b3SRodney W. Grimes #endif /* DEBUG */
5155b81b6b3SRodney W. Grimes 
5165b81b6b3SRodney W. Grimes     if (minor(dev) != 0)
5175b81b6b3SRodney W. Grimes 	return(ENXIO);
5185b81b6b3SRodney W. Grimes     else if (cmd == SPKRTONE)
5195b81b6b3SRodney W. Grimes     {
5205b81b6b3SRodney W. Grimes 	tone_t	*tp = (tone_t *)cmdarg;
5215b81b6b3SRodney W. Grimes 
5225b81b6b3SRodney W. Grimes 	if (tp->frequency == 0)
5235b81b6b3SRodney W. Grimes 	    rest(tp->duration);
5245b81b6b3SRodney W. Grimes 	else
5255b81b6b3SRodney W. Grimes 	    tone(tp->frequency, tp->duration);
526e614aa8aSAndrey A. Chernov 	return(0);
5275b81b6b3SRodney W. Grimes     }
5285b81b6b3SRodney W. Grimes     else if (cmd == SPKRTUNE)
5295b81b6b3SRodney W. Grimes     {
5305b81b6b3SRodney W. Grimes 	tone_t  *tp = (tone_t *)(*(caddr_t *)cmdarg);
5315b81b6b3SRodney W. Grimes 	tone_t ttp;
5325b81b6b3SRodney W. Grimes 	int error;
5335b81b6b3SRodney W. Grimes 
5345b81b6b3SRodney W. Grimes 	for (; ; tp++) {
5355b81b6b3SRodney W. Grimes 	    error = copyin(tp, &ttp, sizeof(tone_t));
5365b81b6b3SRodney W. Grimes 	    if (error)
5375b81b6b3SRodney W. Grimes 		    return(error);
5385b81b6b3SRodney W. Grimes 	    if (ttp.duration == 0)
5395b81b6b3SRodney W. Grimes 		    break;
5405b81b6b3SRodney W. Grimes 	    if (ttp.frequency == 0)
5415b81b6b3SRodney W. Grimes 		rest(ttp.duration);
5425b81b6b3SRodney W. Grimes 	    else
5435b81b6b3SRodney W. Grimes 		tone(ttp.frequency, ttp.duration);
5445b81b6b3SRodney W. Grimes 	}
5455b81b6b3SRodney W. Grimes 	return(0);
5465b81b6b3SRodney W. Grimes     }
547e614aa8aSAndrey A. Chernov     return(EINVAL);
548e614aa8aSAndrey A. Chernov }
5495b81b6b3SRodney W. Grimes 
5505b81b6b3SRodney W. Grimes #endif  /* NSPEAKER > 0 */
5515b81b6b3SRodney W. Grimes /* spkr.c ends here */
552