audiobell.c revision 1.2 1 /* $NetBSD: audiobell.c,v 1.2 2019/05/08 13:40:17 isaki Exp $ */
2
3 /*
4 * Copyright (c) 1999 Richard Earnshaw
5 * Copyright (c) 2004 Ben Harris
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the RiscBSD team.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/types.h>
34 __KERNEL_RCSID(0, "$NetBSD: audiobell.c,v 1.2 2019/05/08 13:40:17 isaki Exp $");
35
36 #include <sys/audioio.h>
37 #include <sys/conf.h>
38 #include <sys/device.h>
39 #include <sys/malloc.h>
40 #include <sys/systm.h>
41 #include <sys/uio.h>
42
43 #include <dev/audio/audio_if.h>
44 #include <dev/audio/audiovar.h>
45 #include <dev/audio/audiodef.h>
46 #include <dev/audio/audiobellvar.h>
47
48 /* 44.1 kHz should reduce hum at higher pitches. */
49 #define BELL_SAMPLE_RATE 44100
50
51 /*
52 * dev is a device_t for the audio device to use.
53 * pitch is the pitch of the bell in Hz,
54 * period is the length in ms,
55 * volume is the amplitude in % of max,
56 * poll is no longer used.
57 */
58 void
59 audiobell(void *dev, u_int pitch, u_int period, u_int volume, int poll)
60 {
61 dev_t audio;
62 int16_t *buf;
63 struct audiobell_arg bellarg;
64 audio_file_t *file;
65 audio_track_t *ptrack;
66 struct uio auio;
67 struct iovec aiov;
68 int i;
69 int remaincount;
70 int remainlen;
71 int wave1count;
72 int wave1len;
73 int len;
74 int16_t vol;
75
76 KASSERT(volume <= 100);
77
78 /* The audio system isn't built for polling. */
79 if (poll)
80 return;
81
82 /* Limit the pitch from 20Hz to Nyquist frequency. */
83 if (pitch > BELL_SAMPLE_RATE / 2)
84 pitch = BELL_SAMPLE_RATE;
85 if (pitch < 20)
86 pitch = 20;
87
88 buf = NULL;
89 audio = AUDIO_DEVICE | device_unit((device_t)dev);
90
91 memset(&bellarg, 0, sizeof(bellarg));
92 bellarg.encoding = AUDIO_ENCODING_SLINEAR_NE;
93 bellarg.precision = 16;
94 bellarg.channels = 1;
95 bellarg.sample_rate = BELL_SAMPLE_RATE;
96
97 /* If not configured, we can't beep. */
98 if (audiobellopen(audio, &bellarg) != 0)
99 return;
100
101 file = bellarg.file;
102 ptrack = file->ptrack;
103
104 /* msec to sample count. */
105 remaincount = period * BELL_SAMPLE_RATE / 1000;
106 remainlen = remaincount * sizeof(int16_t);
107
108 wave1count = BELL_SAMPLE_RATE / pitch;
109 wave1len = wave1count * sizeof(int16_t);
110
111 buf = malloc(wave1len, M_TEMP, M_WAITOK);
112 if (buf == NULL)
113 goto out;
114
115 /* Generate single square wave. It's enough to beep. */
116 vol = 32767 * volume / 100;
117 for (i = 0; i < wave1count / 2; i++) {
118 buf[i] = vol;
119 }
120 vol = -vol;
121 for (; i < wave1count; i++) {
122 buf[i] = vol;
123 }
124
125 /* Write while paused to avoid begin inserted silence. */
126 ptrack->is_pause = true;
127 for (; remainlen > 0; remainlen -= wave1len) {
128 len = uimin(remainlen, wave1len);
129 aiov.iov_base = (void *)buf;
130 aiov.iov_len = len;
131 auio.uio_iov = &aiov;
132 auio.uio_iovcnt = 1;
133 auio.uio_offset = 0;
134 auio.uio_resid = len;
135 auio.uio_rw = UIO_WRITE;
136 UIO_SETUP_SYSSPACE(&auio);
137 if (audiobellwrite(file, &auio) != 0)
138 goto out;
139
140 if (ptrack->usrbuf.used >= ptrack->usrbuf_blksize * NBLKHW)
141 ptrack->is_pause = false;
142 }
143 /* Here we go! */
144 ptrack->is_pause = false;
145 out:
146 if (buf != NULL)
147 free(buf, M_TEMP);
148 audiobellclose(file);
149 }
150