audiobell.c revision 1.5 1 1.5 skrll /* $NetBSD: audiobell.c,v 1.5 2021/07/21 06:35:44 skrll Exp $ */
2 1.2 isaki
3 1.2 isaki /*
4 1.2 isaki * Copyright (c) 1999 Richard Earnshaw
5 1.2 isaki * Copyright (c) 2004 Ben Harris
6 1.2 isaki *
7 1.2 isaki * Redistribution and use in source and binary forms, with or without
8 1.2 isaki * modification, are permitted provided that the following conditions
9 1.2 isaki * are met:
10 1.2 isaki * 1. Redistributions of source code must retain the above copyright
11 1.2 isaki * notice, this list of conditions and the following disclaimer.
12 1.2 isaki * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 isaki * notice, this list of conditions and the following disclaimer in the
14 1.2 isaki * documentation and/or other materials provided with the distribution.
15 1.2 isaki * 3. All advertising materials mentioning features or use of this software
16 1.2 isaki * must display the following acknowledgement:
17 1.2 isaki * This product includes software developed by the RiscBSD team.
18 1.2 isaki * 4. The name of the author may not be used to endorse or promote products
19 1.2 isaki * derived from this software without specific prior written permission.
20 1.2 isaki *
21 1.2 isaki * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.2 isaki * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.2 isaki * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.2 isaki * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.2 isaki * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.2 isaki * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.2 isaki * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.2 isaki * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.2 isaki * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.2 isaki * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.2 isaki */
32 1.2 isaki
33 1.5 skrll #include <sys/cdefs.h>
34 1.5 skrll __KERNEL_RCSID(0, "$NetBSD: audiobell.c,v 1.5 2021/07/21 06:35:44 skrll Exp $");
35 1.5 skrll
36 1.5 skrll #include <sys/param.h>
37 1.2 isaki #include <sys/types.h>
38 1.2 isaki #include <sys/audioio.h>
39 1.2 isaki #include <sys/conf.h>
40 1.2 isaki #include <sys/device.h>
41 1.2 isaki #include <sys/malloc.h>
42 1.2 isaki #include <sys/systm.h>
43 1.2 isaki #include <sys/uio.h>
44 1.2 isaki
45 1.2 isaki #include <dev/audio/audio_if.h>
46 1.2 isaki #include <dev/audio/audiovar.h>
47 1.2 isaki #include <dev/audio/audiodef.h>
48 1.2 isaki #include <dev/audio/audiobellvar.h>
49 1.2 isaki
50 1.3 isaki /*
51 1.3 isaki * The hexadecagon is sufficiently close to a sine wave.
52 1.3 isaki * Audiobell always outputs this 16 points data but changes its playback
53 1.3 isaki * frequency. In addition, audio layer does linear interpolation in the
54 1.3 isaki * frequency conversion stage, so the waveform becomes smooth.
55 1.3 isaki * When the playback frequency rises (or the device frequency is not enough
56 1.3 isaki * high) and one wave cannot be expressed with 16 points, the data is thinned
57 1.3 isaki * out by power of two, like 8 points -> 4 points (triangular wave)
58 1.3 isaki * -> 2 points (rectangular wave).
59 1.3 isaki */
60 1.3 isaki
61 1.3 isaki /* Amplitude. Full scale amplitude is too loud. */
62 1.3 isaki #define A(x) ((x) * 0.6)
63 1.3 isaki
64 1.3 isaki /* (sin(2*pi * (x/16)) * 32767 / 100) << 16 */
65 1.3 isaki static const int32_t sinewave[] = {
66 1.3 isaki A( 0),
67 1.3 isaki A( 8217813),
68 1.3 isaki A( 15184539),
69 1.3 isaki A( 19839556),
70 1.3 isaki A( 21474181),
71 1.3 isaki A( 19839556),
72 1.3 isaki A( 15184539),
73 1.3 isaki A( 8217813),
74 1.3 isaki A( 0),
75 1.3 isaki A( -8217814),
76 1.3 isaki A(-15184540),
77 1.3 isaki A(-19839557),
78 1.3 isaki A(-21474182),
79 1.3 isaki A(-19839557),
80 1.3 isaki A(-15184540),
81 1.3 isaki A( -8217814),
82 1.3 isaki };
83 1.3 isaki #undef A
84 1.2 isaki
85 1.2 isaki /*
86 1.4 isaki * The minimum and the maximum buffer sizes must be a multiple of 32
87 1.4 isaki * (32 = countof(sinewave) * sizeof(uint16_t)).
88 1.4 isaki */
89 1.4 isaki #define MINBUFSIZE (1024)
90 1.4 isaki #define MAXBUFSIZE (4096)
91 1.4 isaki
92 1.4 isaki /*
93 1.2 isaki * dev is a device_t for the audio device to use.
94 1.2 isaki * pitch is the pitch of the bell in Hz,
95 1.2 isaki * period is the length in ms,
96 1.2 isaki * volume is the amplitude in % of max,
97 1.2 isaki * poll is no longer used.
98 1.2 isaki */
99 1.2 isaki void
100 1.2 isaki audiobell(void *dev, u_int pitch, u_int period, u_int volume, int poll)
101 1.2 isaki {
102 1.2 isaki dev_t audio;
103 1.2 isaki int16_t *buf;
104 1.2 isaki audio_file_t *file;
105 1.2 isaki audio_track_t *ptrack;
106 1.2 isaki struct uio auio;
107 1.2 isaki struct iovec aiov;
108 1.3 isaki u_int i;
109 1.3 isaki u_int j;
110 1.3 isaki u_int remaincount;
111 1.3 isaki u_int remainbytes;
112 1.3 isaki u_int wave1count;
113 1.3 isaki u_int wave1bytes;
114 1.4 isaki u_int bufbytes;
115 1.3 isaki u_int len;
116 1.3 isaki u_int step;
117 1.3 isaki u_int offset;
118 1.3 isaki u_int play_sample_rate;
119 1.3 isaki u_int mixer_sample_rate;
120 1.2 isaki
121 1.2 isaki KASSERT(volume <= 100);
122 1.2 isaki
123 1.4 isaki /* Playing for 0msec does nothing. */
124 1.4 isaki if (period == 0)
125 1.4 isaki return;
126 1.4 isaki
127 1.2 isaki /* The audio system isn't built for polling. */
128 1.2 isaki if (poll)
129 1.2 isaki return;
130 1.2 isaki
131 1.2 isaki buf = NULL;
132 1.2 isaki audio = AUDIO_DEVICE | device_unit((device_t)dev);
133 1.2 isaki
134 1.2 isaki /* If not configured, we can't beep. */
135 1.3 isaki if (audiobellopen(audio, &file) != 0)
136 1.2 isaki return;
137 1.2 isaki
138 1.2 isaki ptrack = file->ptrack;
139 1.3 isaki mixer_sample_rate = ptrack->mixer->track_fmt.sample_rate;
140 1.2 isaki
141 1.3 isaki /* Limit pitch */
142 1.3 isaki if (pitch < 20)
143 1.3 isaki pitch = 20;
144 1.2 isaki
145 1.3 isaki offset = 0;
146 1.3 isaki if (pitch <= mixer_sample_rate / 16) {
147 1.3 isaki /* 16-point sine wave */
148 1.3 isaki step = 1;
149 1.3 isaki } else if (pitch <= mixer_sample_rate / 8) {
150 1.3 isaki /* 8-point sine wave */
151 1.3 isaki step = 2;
152 1.3 isaki } else if (pitch <= mixer_sample_rate / 4) {
153 1.3 isaki /* 4-point sine wave, aka, triangular wave */
154 1.3 isaki step = 4;
155 1.3 isaki } else {
156 1.3 isaki /* Rectangular wave */
157 1.3 isaki if (pitch > mixer_sample_rate / 2)
158 1.3 isaki pitch = mixer_sample_rate / 2;
159 1.3 isaki step = 8;
160 1.3 isaki offset = 4;
161 1.3 isaki }
162 1.2 isaki
163 1.3 isaki wave1count = __arraycount(sinewave) / step;
164 1.3 isaki play_sample_rate = pitch * wave1count;
165 1.3 isaki audiobellsetrate(file, play_sample_rate);
166 1.3 isaki
167 1.3 isaki /* msec to sample count */
168 1.3 isaki remaincount = play_sample_rate * period / 1000;
169 1.3 isaki /* Roundup to full wave */
170 1.3 isaki remaincount = roundup(remaincount, wave1count);
171 1.3 isaki remainbytes = remaincount * sizeof(int16_t);
172 1.3 isaki wave1bytes = wave1count * sizeof(int16_t);
173 1.3 isaki
174 1.4 isaki /* Based on 3*usrbuf_blksize, but not too small or too large */
175 1.4 isaki bufbytes = ptrack->usrbuf_blksize * NBLKHW;
176 1.4 isaki if (bufbytes < MINBUFSIZE)
177 1.4 isaki bufbytes = MINBUFSIZE;
178 1.4 isaki else if (bufbytes > MAXBUFSIZE)
179 1.4 isaki bufbytes = MAXBUFSIZE;
180 1.4 isaki else
181 1.4 isaki bufbytes = roundup(bufbytes, wave1bytes);
182 1.4 isaki bufbytes = uimin(bufbytes, remainbytes);
183 1.4 isaki KASSERT(bufbytes != 0);
184 1.4 isaki buf = malloc(bufbytes, M_TEMP, M_WAITOK);
185 1.2 isaki if (buf == NULL)
186 1.2 isaki goto out;
187 1.2 isaki
188 1.3 isaki /* Generate sinewave with specified volume */
189 1.3 isaki j = offset;
190 1.4 isaki for (i = 0; i < bufbytes / sizeof(int16_t); i++) {
191 1.3 isaki /* XXX audio already has track volume feature though #if 0 */
192 1.3 isaki buf[i] = AUDIO_SCALEDOWN(sinewave[j] * (int)volume, 16);
193 1.3 isaki j += step;
194 1.3 isaki j %= __arraycount(sinewave);
195 1.2 isaki }
196 1.2 isaki
197 1.3 isaki /* Write while paused to avoid inserting silence. */
198 1.2 isaki ptrack->is_pause = true;
199 1.3 isaki for (; remainbytes > 0; remainbytes -= len) {
200 1.4 isaki len = uimin(remainbytes, bufbytes);
201 1.2 isaki aiov.iov_base = (void *)buf;
202 1.2 isaki aiov.iov_len = len;
203 1.2 isaki auio.uio_iov = &aiov;
204 1.2 isaki auio.uio_iovcnt = 1;
205 1.2 isaki auio.uio_offset = 0;
206 1.2 isaki auio.uio_resid = len;
207 1.2 isaki auio.uio_rw = UIO_WRITE;
208 1.2 isaki UIO_SETUP_SYSSPACE(&auio);
209 1.2 isaki if (audiobellwrite(file, &auio) != 0)
210 1.2 isaki goto out;
211 1.2 isaki
212 1.2 isaki if (ptrack->usrbuf.used >= ptrack->usrbuf_blksize * NBLKHW)
213 1.2 isaki ptrack->is_pause = false;
214 1.2 isaki }
215 1.2 isaki /* Here we go! */
216 1.2 isaki ptrack->is_pause = false;
217 1.2 isaki out:
218 1.2 isaki if (buf != NULL)
219 1.2 isaki free(buf, M_TEMP);
220 1.2 isaki audiobellclose(file);
221 1.2 isaki }
222