root / audio / wavaudio.c @ 197bc219
History | View | Annotate | Download (7 kB)
1 | 85571bc7 | bellard | /*
|
---|---|---|---|
2 | 1d14ffa9 | bellard | * QEMU WAV audio driver
|
3 | 1d14ffa9 | bellard | *
|
4 | 1d14ffa9 | bellard | * Copyright (c) 2004-2005 Vassili Karpov (malc)
|
5 | 1d14ffa9 | bellard | *
|
6 | 85571bc7 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 85571bc7 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 85571bc7 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 85571bc7 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 85571bc7 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 85571bc7 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 85571bc7 | bellard | *
|
13 | 85571bc7 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 85571bc7 | bellard | * all copies or substantial portions of the Software.
|
15 | 85571bc7 | bellard | *
|
16 | 85571bc7 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 85571bc7 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 85571bc7 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 85571bc7 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 85571bc7 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 85571bc7 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 85571bc7 | bellard | * THE SOFTWARE.
|
23 | 85571bc7 | bellard | */
|
24 | 87ecb68b | pbrook | #include "hw/hw.h" |
25 | 87ecb68b | pbrook | #include "qemu-timer.h" |
26 | 87ecb68b | pbrook | #include "audio.h" |
27 | 85571bc7 | bellard | |
28 | 1d14ffa9 | bellard | #define AUDIO_CAP "wav" |
29 | 1d14ffa9 | bellard | #include "audio_int.h" |
30 | fb065187 | bellard | |
31 | 1d14ffa9 | bellard | typedef struct WAVVoiceOut { |
32 | 1d14ffa9 | bellard | HWVoiceOut hw; |
33 | fb065187 | bellard | QEMUFile *f; |
34 | fb065187 | bellard | int64_t old_ticks; |
35 | fb065187 | bellard | void *pcm_buf;
|
36 | fb065187 | bellard | int total_samples;
|
37 | 1d14ffa9 | bellard | } WAVVoiceOut; |
38 | 85571bc7 | bellard | |
39 | 85571bc7 | bellard | static struct { |
40 | 1ea879e5 | malc | struct audsettings settings;
|
41 | 85571bc7 | bellard | const char *wav_path; |
42 | 85571bc7 | bellard | } conf = { |
43 | 1a40d5e2 | Juan Quintela | .settings.freq = 44100,
|
44 | 1a40d5e2 | Juan Quintela | .settings.nchannels = 2,
|
45 | 1a40d5e2 | Juan Quintela | .settings.fmt = AUD_FMT_S16, |
46 | 1a40d5e2 | Juan Quintela | .wav_path = "qemu.wav"
|
47 | 85571bc7 | bellard | }; |
48 | 85571bc7 | bellard | |
49 | 1d14ffa9 | bellard | static int wav_run_out (HWVoiceOut *hw) |
50 | 85571bc7 | bellard | { |
51 | 1d14ffa9 | bellard | WAVVoiceOut *wav = (WAVVoiceOut *) hw; |
52 | 85571bc7 | bellard | int rpos, live, decr, samples;
|
53 | 85571bc7 | bellard | uint8_t *dst; |
54 | 1ea879e5 | malc | struct st_sample *src;
|
55 | 85571bc7 | bellard | int64_t now = qemu_get_clock (vm_clock); |
56 | 85571bc7 | bellard | int64_t ticks = now - wav->old_ticks; |
57 | 1d14ffa9 | bellard | int64_t bytes = (ticks * hw->info.bytes_per_second) / ticks_per_sec; |
58 | 85571bc7 | bellard | |
59 | 1d14ffa9 | bellard | if (bytes > INT_MAX) {
|
60 | 1d14ffa9 | bellard | samples = INT_MAX >> hw->info.shift; |
61 | 1d14ffa9 | bellard | } |
62 | 1d14ffa9 | bellard | else {
|
63 | 1d14ffa9 | bellard | samples = bytes >> hw->info.shift; |
64 | 1d14ffa9 | bellard | } |
65 | 85571bc7 | bellard | |
66 | 1d14ffa9 | bellard | live = audio_pcm_hw_get_live_out (hw); |
67 | 1d14ffa9 | bellard | if (!live) {
|
68 | 1d14ffa9 | bellard | return 0; |
69 | 1d14ffa9 | bellard | } |
70 | 85571bc7 | bellard | |
71 | 7372f88d | bellard | wav->old_ticks = now; |
72 | 85571bc7 | bellard | decr = audio_MIN (live, samples); |
73 | 85571bc7 | bellard | samples = decr; |
74 | 85571bc7 | bellard | rpos = hw->rpos; |
75 | 85571bc7 | bellard | while (samples) {
|
76 | 85571bc7 | bellard | int left_till_end_samples = hw->samples - rpos;
|
77 | 85571bc7 | bellard | int convert_samples = audio_MIN (samples, left_till_end_samples);
|
78 | 85571bc7 | bellard | |
79 | 1d14ffa9 | bellard | src = hw->mix_buf + rpos; |
80 | 1d14ffa9 | bellard | dst = advance (wav->pcm_buf, rpos << hw->info.shift); |
81 | 85571bc7 | bellard | |
82 | 85571bc7 | bellard | hw->clip (dst, src, convert_samples); |
83 | 1d14ffa9 | bellard | qemu_put_buffer (wav->f, dst, convert_samples << hw->info.shift); |
84 | 85571bc7 | bellard | |
85 | 85571bc7 | bellard | rpos = (rpos + convert_samples) % hw->samples; |
86 | 85571bc7 | bellard | samples -= convert_samples; |
87 | 85571bc7 | bellard | wav->total_samples += convert_samples; |
88 | 85571bc7 | bellard | } |
89 | 85571bc7 | bellard | |
90 | 85571bc7 | bellard | hw->rpos = rpos; |
91 | 1d14ffa9 | bellard | return decr;
|
92 | 85571bc7 | bellard | } |
93 | 85571bc7 | bellard | |
94 | 1d14ffa9 | bellard | static int wav_write_out (SWVoiceOut *sw, void *buf, int len) |
95 | 85571bc7 | bellard | { |
96 | 1d14ffa9 | bellard | return audio_pcm_sw_write (sw, buf, len);
|
97 | 85571bc7 | bellard | } |
98 | 85571bc7 | bellard | |
99 | 85571bc7 | bellard | /* VICE code: Store number as little endian. */
|
100 | 85571bc7 | bellard | static void le_store (uint8_t *buf, uint32_t val, int len) |
101 | 85571bc7 | bellard | { |
102 | 85571bc7 | bellard | int i;
|
103 | 85571bc7 | bellard | for (i = 0; i < len; i++) { |
104 | 85571bc7 | bellard | buf[i] = (uint8_t) (val & 0xff);
|
105 | 85571bc7 | bellard | val >>= 8;
|
106 | 85571bc7 | bellard | } |
107 | 85571bc7 | bellard | } |
108 | 85571bc7 | bellard | |
109 | 1ea879e5 | malc | static int wav_init_out (HWVoiceOut *hw, struct audsettings *as) |
110 | 85571bc7 | bellard | { |
111 | 1d14ffa9 | bellard | WAVVoiceOut *wav = (WAVVoiceOut *) hw; |
112 | c0fe3827 | bellard | int bits16 = 0, stereo = 0; |
113 | 85571bc7 | bellard | uint8_t hdr[] = { |
114 | 85571bc7 | bellard | 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, |
115 | 85571bc7 | bellard | 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, |
116 | 85571bc7 | bellard | 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04, |
117 | 85571bc7 | bellard | 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00 |
118 | 85571bc7 | bellard | }; |
119 | 1ea879e5 | malc | struct audsettings wav_as = conf.settings;
|
120 | 85571bc7 | bellard | |
121 | c0fe3827 | bellard | (void) as;
|
122 | 1d14ffa9 | bellard | |
123 | c0fe3827 | bellard | stereo = wav_as.nchannels == 2;
|
124 | c0fe3827 | bellard | switch (wav_as.fmt) {
|
125 | 85571bc7 | bellard | case AUD_FMT_S8:
|
126 | 85571bc7 | bellard | case AUD_FMT_U8:
|
127 | 1d14ffa9 | bellard | bits16 = 0;
|
128 | 85571bc7 | bellard | break;
|
129 | 85571bc7 | bellard | |
130 | 85571bc7 | bellard | case AUD_FMT_S16:
|
131 | 85571bc7 | bellard | case AUD_FMT_U16:
|
132 | 85571bc7 | bellard | bits16 = 1;
|
133 | 85571bc7 | bellard | break;
|
134 | f941aa25 | ths | |
135 | f941aa25 | ths | case AUD_FMT_S32:
|
136 | f941aa25 | ths | case AUD_FMT_U32:
|
137 | f941aa25 | ths | dolog ("WAVE files can not handle 32bit formats\n");
|
138 | f941aa25 | ths | return -1; |
139 | 85571bc7 | bellard | } |
140 | 85571bc7 | bellard | |
141 | 85571bc7 | bellard | hdr[34] = bits16 ? 0x10 : 0x08; |
142 | c0fe3827 | bellard | |
143 | d929eba5 | bellard | wav_as.endianness = 0;
|
144 | d929eba5 | bellard | audio_pcm_init_info (&hw->info, &wav_as); |
145 | c0fe3827 | bellard | |
146 | c0fe3827 | bellard | hw->samples = 1024;
|
147 | c0fe3827 | bellard | wav->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
|
148 | 1d14ffa9 | bellard | if (!wav->pcm_buf) {
|
149 | c0fe3827 | bellard | dolog ("Could not allocate buffer (%d bytes)\n",
|
150 | c0fe3827 | bellard | hw->samples << hw->info.shift); |
151 | 85571bc7 | bellard | return -1; |
152 | 1d14ffa9 | bellard | } |
153 | 85571bc7 | bellard | |
154 | 1d14ffa9 | bellard | le_store (hdr + 22, hw->info.nchannels, 2); |
155 | 1d14ffa9 | bellard | le_store (hdr + 24, hw->info.freq, 4); |
156 | c0fe3827 | bellard | le_store (hdr + 28, hw->info.freq << (bits16 + stereo), 4); |
157 | c0fe3827 | bellard | le_store (hdr + 32, 1 << (bits16 + stereo), 2); |
158 | 85571bc7 | bellard | |
159 | e70332b3 | bellard | wav->f = qemu_fopen (conf.wav_path, "wb");
|
160 | 85571bc7 | bellard | if (!wav->f) {
|
161 | 1d14ffa9 | bellard | dolog ("Failed to open wave file `%s'\nReason: %s\n",
|
162 | 85571bc7 | bellard | conf.wav_path, strerror (errno)); |
163 | 7372f88d | bellard | qemu_free (wav->pcm_buf); |
164 | 7372f88d | bellard | wav->pcm_buf = NULL;
|
165 | 85571bc7 | bellard | return -1; |
166 | 85571bc7 | bellard | } |
167 | 85571bc7 | bellard | |
168 | 85571bc7 | bellard | qemu_put_buffer (wav->f, hdr, sizeof (hdr));
|
169 | 85571bc7 | bellard | return 0; |
170 | 85571bc7 | bellard | } |
171 | 85571bc7 | bellard | |
172 | 1d14ffa9 | bellard | static void wav_fini_out (HWVoiceOut *hw) |
173 | 85571bc7 | bellard | { |
174 | 1d14ffa9 | bellard | WAVVoiceOut *wav = (WAVVoiceOut *) hw; |
175 | 85571bc7 | bellard | uint8_t rlen[4];
|
176 | 85571bc7 | bellard | uint8_t dlen[4];
|
177 | 50903530 | bellard | uint32_t datalen = wav->total_samples << hw->info.shift; |
178 | 50903530 | bellard | uint32_t rifflen = datalen + 36;
|
179 | 85571bc7 | bellard | |
180 | c0fe3827 | bellard | if (!wav->f) {
|
181 | 85571bc7 | bellard | return;
|
182 | 1d14ffa9 | bellard | } |
183 | 85571bc7 | bellard | |
184 | 85571bc7 | bellard | le_store (rlen, rifflen, 4);
|
185 | 85571bc7 | bellard | le_store (dlen, datalen, 4);
|
186 | 85571bc7 | bellard | |
187 | 85571bc7 | bellard | qemu_fseek (wav->f, 4, SEEK_SET);
|
188 | 85571bc7 | bellard | qemu_put_buffer (wav->f, rlen, 4);
|
189 | 85571bc7 | bellard | |
190 | 85571bc7 | bellard | qemu_fseek (wav->f, 32, SEEK_CUR);
|
191 | 85571bc7 | bellard | qemu_put_buffer (wav->f, dlen, 4);
|
192 | 85571bc7 | bellard | |
193 | e70332b3 | bellard | qemu_fclose (wav->f); |
194 | 85571bc7 | bellard | wav->f = NULL;
|
195 | 7372f88d | bellard | |
196 | 7372f88d | bellard | qemu_free (wav->pcm_buf); |
197 | 7372f88d | bellard | wav->pcm_buf = NULL;
|
198 | 85571bc7 | bellard | } |
199 | 85571bc7 | bellard | |
200 | 1d14ffa9 | bellard | static int wav_ctl_out (HWVoiceOut *hw, int cmd, ...) |
201 | 85571bc7 | bellard | { |
202 | 85571bc7 | bellard | (void) hw;
|
203 | 85571bc7 | bellard | (void) cmd;
|
204 | 85571bc7 | bellard | return 0; |
205 | 85571bc7 | bellard | } |
206 | 85571bc7 | bellard | |
207 | 85571bc7 | bellard | static void *wav_audio_init (void) |
208 | 85571bc7 | bellard | { |
209 | 85571bc7 | bellard | return &conf;
|
210 | 85571bc7 | bellard | } |
211 | 85571bc7 | bellard | |
212 | 85571bc7 | bellard | static void wav_audio_fini (void *opaque) |
213 | 85571bc7 | bellard | { |
214 | 1d14ffa9 | bellard | (void) opaque;
|
215 | 85571bc7 | bellard | ldebug ("wav_fini");
|
216 | 85571bc7 | bellard | } |
217 | 85571bc7 | bellard | |
218 | 8869defe | blueswir1 | static struct audio_option wav_options[] = { |
219 | 98f9f48c | malc | { |
220 | 98f9f48c | malc | .name = "FREQUENCY",
|
221 | 98f9f48c | malc | .tag = AUD_OPT_INT, |
222 | 98f9f48c | malc | .valp = &conf.settings.freq, |
223 | 98f9f48c | malc | .descr = "Frequency"
|
224 | 98f9f48c | malc | }, |
225 | 98f9f48c | malc | { |
226 | 98f9f48c | malc | .name = "FORMAT",
|
227 | 98f9f48c | malc | .tag = AUD_OPT_FMT, |
228 | 98f9f48c | malc | .valp = &conf.settings.fmt, |
229 | 98f9f48c | malc | .descr = "Format"
|
230 | 98f9f48c | malc | }, |
231 | 98f9f48c | malc | { |
232 | 98f9f48c | malc | .name = "DAC_FIXED_CHANNELS",
|
233 | 98f9f48c | malc | .tag = AUD_OPT_INT, |
234 | 98f9f48c | malc | .valp = &conf.settings.nchannels, |
235 | 98f9f48c | malc | .descr = "Number of channels (1 - mono, 2 - stereo)"
|
236 | 98f9f48c | malc | }, |
237 | 98f9f48c | malc | { |
238 | 98f9f48c | malc | .name = "PATH",
|
239 | 98f9f48c | malc | .tag = AUD_OPT_STR, |
240 | 98f9f48c | malc | .valp = &conf.wav_path, |
241 | 98f9f48c | malc | .descr = "Path to wave file"
|
242 | 98f9f48c | malc | }, |
243 | 2700efa3 | Juan Quintela | { /* End of list */ }
|
244 | 1d14ffa9 | bellard | }; |
245 | 1d14ffa9 | bellard | |
246 | 35f4b58c | blueswir1 | static struct audio_pcm_ops wav_pcm_ops = { |
247 | 1dd3e4d1 | Juan Quintela | .init_out = wav_init_out, |
248 | 1dd3e4d1 | Juan Quintela | .fini_out = wav_fini_out, |
249 | 1dd3e4d1 | Juan Quintela | .run_out = wav_run_out, |
250 | 1dd3e4d1 | Juan Quintela | .write = wav_write_out, |
251 | 1dd3e4d1 | Juan Quintela | .ctl_out = wav_ctl_out, |
252 | 85571bc7 | bellard | }; |
253 | 85571bc7 | bellard | |
254 | 1d14ffa9 | bellard | struct audio_driver wav_audio_driver = {
|
255 | bee37f32 | Juan Quintela | .name = "wav",
|
256 | bee37f32 | Juan Quintela | .descr = "WAV renderer http://wikipedia.org/wiki/WAV",
|
257 | bee37f32 | Juan Quintela | .options = wav_options, |
258 | bee37f32 | Juan Quintela | .init = wav_audio_init, |
259 | bee37f32 | Juan Quintela | .fini = wav_audio_fini, |
260 | 98f9f48c | malc | .pcm_ops = &wav_pcm_ops, |
261 | bee37f32 | Juan Quintela | .can_be_default = 0,
|
262 | bee37f32 | Juan Quintela | .max_voices_out = 1,
|
263 | bee37f32 | Juan Quintela | .max_voices_in = 0,
|
264 | bee37f32 | Juan Quintela | .voice_size_out = sizeof (WAVVoiceOut),
|
265 | bee37f32 | Juan Quintela | .voice_size_in = 0
|
266 | 85571bc7 | bellard | }; |