audiobell.c revision 1.3 1 /* $NetBSD: audiobell.c,v 1.3 2019/06/26 06:57:45 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.3 2019/06/26 06:57:45 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 * dev is a device_t for the audio device to use.
85 * pitch is the pitch of the bell in Hz,
86 * period is the length in ms,
87 * volume is the amplitude in % of max,
88 * poll is no longer used.
89 */
90 void
91 audiobell(void *dev, u_int pitch, u_int period, u_int volume, int poll)
92 {
93 dev_t audio;
94 int16_t *buf;
95 audio_file_t *file;
96 audio_track_t *ptrack;
97 struct uio auio;
98 struct iovec aiov;
99 u_int i;
100 u_int j;
101 u_int remaincount;
102 u_int remainbytes;
103 u_int wave1count;
104 u_int wave1bytes;
105 u_int blkbytes;
106 u_int len;
107 u_int step;
108 u_int offset;
109 u_int play_sample_rate;
110 u_int mixer_sample_rate;
111
112 KASSERT(volume <= 100);
113
114 /* The audio system isn't built for polling. */
115 if (poll)
116 return;
117
118 buf = NULL;
119 audio = AUDIO_DEVICE | device_unit((device_t)dev);
120
121 /* If not configured, we can't beep. */
122 if (audiobellopen(audio, &file) != 0)
123 return;
124
125 ptrack = file->ptrack;
126 mixer_sample_rate = ptrack->mixer->track_fmt.sample_rate;
127
128 /* Limit pitch */
129 if (pitch < 20)
130 pitch = 20;
131
132 offset = 0;
133 if (pitch <= mixer_sample_rate / 16) {
134 /* 16-point sine wave */
135 step = 1;
136 } else if (pitch <= mixer_sample_rate / 8) {
137 /* 8-point sine wave */
138 step = 2;
139 } else if (pitch <= mixer_sample_rate / 4) {
140 /* 4-point sine wave, aka, triangular wave */
141 step = 4;
142 } else {
143 /* Rectangular wave */
144 if (pitch > mixer_sample_rate / 2)
145 pitch = mixer_sample_rate / 2;
146 step = 8;
147 offset = 4;
148 }
149
150 wave1count = __arraycount(sinewave) / step;
151 play_sample_rate = pitch * wave1count;
152 audiobellsetrate(file, play_sample_rate);
153
154 /* msec to sample count */
155 remaincount = play_sample_rate * period / 1000;
156 /* Roundup to full wave */
157 remaincount = roundup(remaincount, wave1count);
158 remainbytes = remaincount * sizeof(int16_t);
159 wave1bytes = wave1count * sizeof(int16_t);
160
161 blkbytes = ptrack->usrbuf_blksize;
162 blkbytes = rounddown(blkbytes, wave1bytes);
163 blkbytes = uimin(blkbytes, remainbytes);
164 buf = malloc(blkbytes, M_TEMP, M_WAITOK);
165 if (buf == NULL)
166 goto out;
167
168 /* Generate sinewave with specified volume */
169 j = offset;
170 for (i = 0; i < blkbytes / sizeof(int16_t); i++) {
171 /* XXX audio already has track volume feature though #if 0 */
172 buf[i] = AUDIO_SCALEDOWN(sinewave[j] * (int)volume, 16);
173 j += step;
174 j %= __arraycount(sinewave);
175 }
176
177 /* Write while paused to avoid inserting silence. */
178 ptrack->is_pause = true;
179 for (; remainbytes > 0; remainbytes -= len) {
180 len = uimin(remainbytes, blkbytes);
181 aiov.iov_base = (void *)buf;
182 aiov.iov_len = len;
183 auio.uio_iov = &aiov;
184 auio.uio_iovcnt = 1;
185 auio.uio_offset = 0;
186 auio.uio_resid = len;
187 auio.uio_rw = UIO_WRITE;
188 UIO_SETUP_SYSSPACE(&auio);
189 if (audiobellwrite(file, &auio) != 0)
190 goto out;
191
192 if (ptrack->usrbuf.used >= ptrack->usrbuf_blksize * NBLKHW)
193 ptrack->is_pause = false;
194 }
195 /* Here we go! */
196 ptrack->is_pause = false;
197 out:
198 if (buf != NULL)
199 free(buf, M_TEMP);
200 audiobellclose(file);
201 }
202