]> git.proxmox.com Git - mirror_qemu.git/blame - audio/wavaudio.c
Revert "vl: Fix to create migration object before block backends again"
[mirror_qemu.git] / audio / wavaudio.c
CommitLineData
85571bc7 1/*
1d14ffa9
FB
2 * QEMU WAV audio driver
3 *
4 * Copyright (c) 2004-2005 Vassili Karpov (malc)
5 *
85571bc7
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
6086a565 24#include "qemu/osdep.h"
87776ab7 25#include "qemu/host-utils.h"
1de7afc9 26#include "qemu/timer.h"
0927d166 27#include "qapi/opts-visitor.h"
87ecb68b 28#include "audio.h"
85571bc7 29
1d14ffa9
FB
30#define AUDIO_CAP "wav"
31#include "audio_int.h"
fb065187 32
1d14ffa9
FB
33typedef struct WAVVoiceOut {
34 HWVoiceOut hw;
27acf660 35 FILE *f;
fb065187
FB
36 int64_t old_ticks;
37 void *pcm_buf;
38 int total_samples;
1d14ffa9 39} WAVVoiceOut;
85571bc7 40
bdff253c 41static int wav_run_out (HWVoiceOut *hw, int live)
85571bc7 42{
1d14ffa9 43 WAVVoiceOut *wav = (WAVVoiceOut *) hw;
bdff253c 44 int rpos, decr, samples;
85571bc7 45 uint8_t *dst;
1ea879e5 46 struct st_sample *src;
bc72ad67 47 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
85571bc7 48 int64_t ticks = now - wav->old_ticks;
4f4cc0ef 49 int64_t bytes =
73bcb24d 50 muldiv64(ticks, hw->info.bytes_per_second, NANOSECONDS_PER_SECOND);
85571bc7 51
1d14ffa9
FB
52 if (bytes > INT_MAX) {
53 samples = INT_MAX >> hw->info.shift;
54 }
55 else {
56 samples = bytes >> hw->info.shift;
57 }
85571bc7 58
7372f88d 59 wav->old_ticks = now;
85571bc7
FB
60 decr = audio_MIN (live, samples);
61 samples = decr;
62 rpos = hw->rpos;
63 while (samples) {
64 int left_till_end_samples = hw->samples - rpos;
65 int convert_samples = audio_MIN (samples, left_till_end_samples);
66
1d14ffa9
FB
67 src = hw->mix_buf + rpos;
68 dst = advance (wav->pcm_buf, rpos << hw->info.shift);
85571bc7
FB
69
70 hw->clip (dst, src, convert_samples);
27acf660
JQ
71 if (fwrite (dst, convert_samples << hw->info.shift, 1, wav->f) != 1) {
72 dolog ("wav_run_out: fwrite of %d bytes failed\nReaons: %s\n",
73 convert_samples << hw->info.shift, strerror (errno));
74 }
85571bc7
FB
75
76 rpos = (rpos + convert_samples) % hw->samples;
77 samples -= convert_samples;
78 wav->total_samples += convert_samples;
79 }
80
85571bc7 81 hw->rpos = rpos;
1d14ffa9 82 return decr;
85571bc7
FB
83}
84
1d14ffa9 85static int wav_write_out (SWVoiceOut *sw, void *buf, int len)
85571bc7 86{
1d14ffa9 87 return audio_pcm_sw_write (sw, buf, len);
85571bc7
FB
88}
89
85571bc7
FB
90/* VICE code: Store number as little endian. */
91static void le_store (uint8_t *buf, uint32_t val, int len)
92{
93 int i;
94 for (i = 0; i < len; i++) {
95 buf[i] = (uint8_t) (val & 0xff);
96 val >>= 8;
97 }
98}
99
5706db1d
KZ
100static int wav_init_out(HWVoiceOut *hw, struct audsettings *as,
101 void *drv_opaque)
85571bc7 102{
1d14ffa9 103 WAVVoiceOut *wav = (WAVVoiceOut *) hw;
c0fe3827 104 int bits16 = 0, stereo = 0;
85571bc7
FB
105 uint8_t hdr[] = {
106 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56,
107 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00,
108 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04,
109 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00
110 };
0927d166
KZ
111 Audiodev *dev = drv_opaque;
112 AudiodevWavOptions *wopts = &dev->u.wav;
113 struct audsettings wav_as = audiodev_to_audsettings(dev->u.wav.out);
114 const char *wav_path = wopts->has_path ? wopts->path : "qemu.wav";
85571bc7 115
c0fe3827
FB
116 stereo = wav_as.nchannels == 2;
117 switch (wav_as.fmt) {
85bc5852
KZ
118 case AUDIO_FORMAT_S8:
119 case AUDIO_FORMAT_U8:
1d14ffa9 120 bits16 = 0;
85571bc7
FB
121 break;
122
85bc5852
KZ
123 case AUDIO_FORMAT_S16:
124 case AUDIO_FORMAT_U16:
85571bc7
FB
125 bits16 = 1;
126 break;
f941aa25 127
85bc5852
KZ
128 case AUDIO_FORMAT_S32:
129 case AUDIO_FORMAT_U32:
f941aa25
TS
130 dolog ("WAVE files can not handle 32bit formats\n");
131 return -1;
85bc5852
KZ
132
133 default:
134 abort();
85571bc7
FB
135 }
136
137 hdr[34] = bits16 ? 0x10 : 0x08;
c0fe3827 138
d929eba5
FB
139 wav_as.endianness = 0;
140 audio_pcm_init_info (&hw->info, &wav_as);
c0fe3827
FB
141
142 hw->samples = 1024;
470bcabd 143 wav->pcm_buf = audio_calloc(__func__, hw->samples, 1 << hw->info.shift);
1d14ffa9 144 if (!wav->pcm_buf) {
c0fe3827
FB
145 dolog ("Could not allocate buffer (%d bytes)\n",
146 hw->samples << hw->info.shift);
85571bc7 147 return -1;
1d14ffa9 148 }
85571bc7 149
1d14ffa9
FB
150 le_store (hdr + 22, hw->info.nchannels, 2);
151 le_store (hdr + 24, hw->info.freq, 4);
c0fe3827
FB
152 le_store (hdr + 28, hw->info.freq << (bits16 + stereo), 4);
153 le_store (hdr + 32, 1 << (bits16 + stereo), 2);
85571bc7 154
0927d166 155 wav->f = fopen(wav_path, "wb");
85571bc7 156 if (!wav->f) {
1d14ffa9 157 dolog ("Failed to open wave file `%s'\nReason: %s\n",
0927d166 158 wav_path, strerror(errno));
7267c094 159 g_free (wav->pcm_buf);
7372f88d 160 wav->pcm_buf = NULL;
85571bc7
FB
161 return -1;
162 }
163
27acf660
JQ
164 if (fwrite (hdr, sizeof (hdr), 1, wav->f) != 1) {
165 dolog ("wav_init_out: failed to write header\nReason: %s\n",
166 strerror(errno));
167 return -1;
168 }
85571bc7
FB
169 return 0;
170}
171
1d14ffa9 172static void wav_fini_out (HWVoiceOut *hw)
85571bc7 173{
1d14ffa9 174 WAVVoiceOut *wav = (WAVVoiceOut *) hw;
85571bc7
FB
175 uint8_t rlen[4];
176 uint8_t dlen[4];
50903530
FB
177 uint32_t datalen = wav->total_samples << hw->info.shift;
178 uint32_t rifflen = datalen + 36;
85571bc7 179
c0fe3827 180 if (!wav->f) {
85571bc7 181 return;
1d14ffa9 182 }
85571bc7
FB
183
184 le_store (rlen, rifflen, 4);
185 le_store (dlen, datalen, 4);
186
27acf660
JQ
187 if (fseek (wav->f, 4, SEEK_SET)) {
188 dolog ("wav_fini_out: fseek to rlen failed\nReason: %s\n",
189 strerror(errno));
190 goto doclose;
191 }
192 if (fwrite (rlen, 4, 1, wav->f) != 1) {
193 dolog ("wav_fini_out: failed to write rlen\nReason: %s\n",
194 strerror (errno));
195 goto doclose;
196 }
197 if (fseek (wav->f, 32, SEEK_CUR)) {
198 dolog ("wav_fini_out: fseek to dlen failed\nReason: %s\n",
199 strerror (errno));
200 goto doclose;
201 }
202 if (fwrite (dlen, 4, 1, wav->f) != 1) {
203 dolog ("wav_fini_out: failed to write dlen\nReaons: %s\n",
204 strerror (errno));
205 goto doclose;
206 }
85571bc7 207
27acf660
JQ
208 doclose:
209 if (fclose (wav->f)) {
210 dolog ("wav_fini_out: fclose %p failed\nReason: %s\n",
211 wav->f, strerror (errno));
212 }
85571bc7 213 wav->f = NULL;
7372f88d 214
7267c094 215 g_free (wav->pcm_buf);
7372f88d 216 wav->pcm_buf = NULL;
85571bc7
FB
217}
218
1d14ffa9 219static int wav_ctl_out (HWVoiceOut *hw, int cmd, ...)
85571bc7
FB
220{
221 (void) hw;
222 (void) cmd;
223 return 0;
224}
225
71830221 226static void *wav_audio_init(Audiodev *dev)
85571bc7 227{
0927d166
KZ
228 assert(dev->driver == AUDIODEV_DRIVER_WAV);
229 return dev;
85571bc7
FB
230}
231
232static void wav_audio_fini (void *opaque)
233{
234 ldebug ("wav_fini");
235}
236
35f4b58c 237static struct audio_pcm_ops wav_pcm_ops = {
1dd3e4d1
JQ
238 .init_out = wav_init_out,
239 .fini_out = wav_fini_out,
240 .run_out = wav_run_out,
241 .write = wav_write_out,
242 .ctl_out = wav_ctl_out,
85571bc7
FB
243};
244
d3893a39 245static struct audio_driver wav_audio_driver = {
bee37f32
JQ
246 .name = "wav",
247 .descr = "WAV renderer http://wikipedia.org/wiki/WAV",
bee37f32
JQ
248 .init = wav_audio_init,
249 .fini = wav_audio_fini,
98f9f48c 250 .pcm_ops = &wav_pcm_ops,
bee37f32
JQ
251 .can_be_default = 0,
252 .max_voices_out = 1,
253 .max_voices_in = 0,
254 .voice_size_out = sizeof (WAVVoiceOut),
255 .voice_size_in = 0
85571bc7 256};
d3893a39
GH
257
258static void register_audio_wav(void)
259{
260 audio_driver_register(&wav_audio_driver);
261}
262type_init(register_audio_wav);