audiobell.c revision 1.4 1 /* $NetBSD: audiobell.c,v 1.4 2021/03/20 04:56:52 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.4 2021/03/20 04:56:52 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 /*
49 * The hexadecagon is sufficiently close to a sine wave.
50 * Audiobell always outputs this 16 points data but changes its playback
51 * frequency. In addition, audio layer does linear interpolation in the
52 * frequency conversion stage, so the waveform becomes smooth.
53 * When the playback frequency rises (or the device frequency is not enough
54 * high) and one wave cannot be expressed with 16 points, the data is thinned
55 * out by power of two, like 8 points -> 4 points (triangular wave)
56 * -> 2 points (rectangular wave).
57 */
58
59 /* Amplitude. Full scale amplitude is too loud. */
60 #define A(x) ((x) * 0.6)
61
62 /* (sin(2*pi * (x/16)) * 32767 / 100) << 16 */
63 static const int32_t sinewave[] = {
64 A( 0),
65 A( 8217813),
66 A( 15184539),
67 A( 19839556),
68 A( 21474181),
69 A( 19839556),
70 A( 15184539),
71 A( 8217813),
72 A( 0),
73 A( -8217814),
74 A(-15184540),
75 A(-19839557),
76 A(-21474182),
77 A(-19839557),
78 A(-15184540),
79 A( -8217814),
80 };
81 #undef A
82
83 /*
84 * The minimum and the maximum buffer sizes must be a multiple of 32
85 * (32 = countof(sinewave) * sizeof(uint16_t)).
86 */
87 #define MINBUFSIZE (1024)
88 #define MAXBUFSIZE (4096)
89
90 /*
91 * dev is a device_t for the audio device to use.
92 * pitch is the pitch of the bell in Hz,
93 * period is the length in ms,
94 * volume is the amplitude in % of max,
95 * poll is no longer used.
96 */
97 void
98 audiobell(void *dev, u_int pitch, u_int period, u_int volume, int poll)
99 {
100 dev_t audio;
101 int16_t *buf;
102 audio_file_t *file;
103 audio_track_t *ptrack;
104 struct uio auio;
105 struct iovec aiov;
106 u_int i;
107 u_int j;
108 u_int remaincount;
109 u_int remainbytes;
110 u_int wave1count;
111 u_int wave1bytes;
112 u_int bufbytes;
113 u_int len;
114 u_int step;
115 u_int offset;
116 u_int play_sample_rate;
117 u_int mixer_sample_rate;
118
119 KASSERT(volume <= 100);
120
121 /* Playing for 0msec does nothing. */
122 if (period == 0)
123 return;
124
125 /* The audio system isn't built for polling. */
126 if (poll)
127 return;
128
129 buf = NULL;
130 audio = AUDIO_DEVICE | device_unit((device_t)dev);
131
132 /* If not configured, we can't beep. */
133 if (audiobellopen(audio, &file) != 0)
134 return;
135
136 ptrack = file->ptrack;
137 mixer_sample_rate = ptrack->mixer->track_fmt.sample_rate;
138
139 /* Limit pitch */
140 if (pitch < 20)
141 pitch = 20;
142
143 offset = 0;
144 if (pitch <= mixer_sample_rate / 16) {
145 /* 16-point sine wave */
146 step = 1;
147 } else if (pitch <= mixer_sample_rate / 8) {
148 /* 8-point sine wave */
149 step = 2;
150 } else if (pitch <= mixer_sample_rate / 4) {
151 /* 4-point sine wave, aka, triangular wave */
152 step = 4;
153 } else {
154 /* Rectangular wave */
155 if (pitch > mixer_sample_rate / 2)
156 pitch = mixer_sample_rate / 2;
157 step = 8;
158 offset = 4;
159 }
160
161 wave1count = __arraycount(sinewave) / step;
162 play_sample_rate = pitch * wave1count;
163 audiobellsetrate(file, play_sample_rate);
164
165 /* msec to sample count */
166 remaincount = play_sample_rate * period / 1000;
167 /* Roundup to full wave */
168 remaincount = roundup(remaincount, wave1count);
169 remainbytes = remaincount * sizeof(int16_t);
170 wave1bytes = wave1count * sizeof(int16_t);
171
172 /* Based on 3*usrbuf_blksize, but not too small or too large */
173 bufbytes = ptrack->usrbuf_blksize * NBLKHW;
174 if (bufbytes < MINBUFSIZE)
175 bufbytes = MINBUFSIZE;
176 else if (bufbytes > MAXBUFSIZE)
177 bufbytes = MAXBUFSIZE;
178 else
179 bufbytes = roundup(bufbytes, wave1bytes);
180 bufbytes = uimin(bufbytes, remainbytes);
181 KASSERT(bufbytes != 0);
182 buf = malloc(bufbytes, M_TEMP, M_WAITOK);
183 if (buf == NULL)
184 goto out;
185
186 /* Generate sinewave with specified volume */
187 j = offset;
188 for (i = 0; i < bufbytes / sizeof(int16_t); i++) {
189 /* XXX audio already has track volume feature though #if 0 */
190 buf[i] = AUDIO_SCALEDOWN(sinewave[j] * (int)volume, 16);
191 j += step;
192 j %= __arraycount(sinewave);
193 }
194
195 /* Write while paused to avoid inserting silence. */
196 ptrack->is_pause = true;
197 for (; remainbytes > 0; remainbytes -= len) {
198 len = uimin(remainbytes, bufbytes);
199 aiov.iov_base = (void *)buf;
200 aiov.iov_len = len;
201 auio.uio_iov = &aiov;
202 auio.uio_iovcnt = 1;
203 auio.uio_offset = 0;
204 auio.uio_resid = len;
205 auio.uio_rw = UIO_WRITE;
206 UIO_SETUP_SYSSPACE(&auio);
207 if (audiobellwrite(file, &auio) != 0)
208 goto out;
209
210 if (ptrack->usrbuf.used >= ptrack->usrbuf_blksize * NBLKHW)
211 ptrack->is_pause = false;
212 }
213 /* Here we go! */
214 ptrack->is_pause = false;
215 out:
216 if (buf != NULL)
217 free(buf, M_TEMP);
218 audiobellclose(file);
219 }
220