2 * QEMU WAV audio driver
4 * Copyright (c) 2004-2005 Vassili Karpov (malc)
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu/osdep.h"
25 #include "qemu/host-utils.h"
26 #include "qemu/timer.h"
27 #include "qapi/opts-visitor.h"
30 #define AUDIO_CAP "wav"
31 #include "audio_int.h"
33 typedef struct WAVVoiceOut
{
41 static int wav_run_out (HWVoiceOut
*hw
, int live
)
43 WAVVoiceOut
*wav
= (WAVVoiceOut
*) hw
;
44 int rpos
, decr
, samples
;
46 struct st_sample
*src
;
47 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
48 int64_t ticks
= now
- wav
->old_ticks
;
50 muldiv64(ticks
, hw
->info
.bytes_per_second
, NANOSECONDS_PER_SECOND
);
52 if (bytes
> INT_MAX
) {
53 samples
= INT_MAX
>> hw
->info
.shift
;
56 samples
= bytes
>> hw
->info
.shift
;
60 decr
= audio_MIN (live
, samples
);
64 int left_till_end_samples
= hw
->samples
- rpos
;
65 int convert_samples
= audio_MIN (samples
, left_till_end_samples
);
67 src
= hw
->mix_buf
+ rpos
;
68 dst
= advance (wav
->pcm_buf
, rpos
<< hw
->info
.shift
);
70 hw
->clip (dst
, src
, convert_samples
);
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
));
76 rpos
= (rpos
+ convert_samples
) % hw
->samples
;
77 samples
-= convert_samples
;
78 wav
->total_samples
+= convert_samples
;
85 static int wav_write_out (SWVoiceOut
*sw
, void *buf
, int len
)
87 return audio_pcm_sw_write (sw
, buf
, len
);
90 /* VICE code: Store number as little endian. */
91 static void le_store (uint8_t *buf
, uint32_t val
, int len
)
94 for (i
= 0; i
< len
; i
++) {
95 buf
[i
] = (uint8_t) (val
& 0xff);
100 static int wav_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
103 WAVVoiceOut
*wav
= (WAVVoiceOut
*) hw
;
104 int bits16
= 0, stereo
= 0;
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
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";
116 stereo
= wav_as
.nchannels
== 2;
117 switch (wav_as
.fmt
) {
118 case AUDIO_FORMAT_S8
:
119 case AUDIO_FORMAT_U8
:
123 case AUDIO_FORMAT_S16
:
124 case AUDIO_FORMAT_U16
:
128 case AUDIO_FORMAT_S32
:
129 case AUDIO_FORMAT_U32
:
130 dolog ("WAVE files can not handle 32bit formats\n");
137 hdr
[34] = bits16
? 0x10 : 0x08;
139 wav_as
.endianness
= 0;
140 audio_pcm_init_info (&hw
->info
, &wav_as
);
143 wav
->pcm_buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
145 dolog ("Could not allocate buffer (%d bytes)\n",
146 hw
->samples
<< hw
->info
.shift
);
150 le_store (hdr
+ 22, hw
->info
.nchannels
, 2);
151 le_store (hdr
+ 24, hw
->info
.freq
, 4);
152 le_store (hdr
+ 28, hw
->info
.freq
<< (bits16
+ stereo
), 4);
153 le_store (hdr
+ 32, 1 << (bits16
+ stereo
), 2);
155 wav
->f
= fopen(wav_path
, "wb");
157 dolog ("Failed to open wave file `%s'\nReason: %s\n",
158 wav_path
, strerror(errno
));
159 g_free (wav
->pcm_buf
);
164 if (fwrite (hdr
, sizeof (hdr
), 1, wav
->f
) != 1) {
165 dolog ("wav_init_out: failed to write header\nReason: %s\n",
172 static void wav_fini_out (HWVoiceOut
*hw
)
174 WAVVoiceOut
*wav
= (WAVVoiceOut
*) hw
;
177 uint32_t datalen
= wav
->total_samples
<< hw
->info
.shift
;
178 uint32_t rifflen
= datalen
+ 36;
184 le_store (rlen
, rifflen
, 4);
185 le_store (dlen
, datalen
, 4);
187 if (fseek (wav
->f
, 4, SEEK_SET
)) {
188 dolog ("wav_fini_out: fseek to rlen failed\nReason: %s\n",
192 if (fwrite (rlen
, 4, 1, wav
->f
) != 1) {
193 dolog ("wav_fini_out: failed to write rlen\nReason: %s\n",
197 if (fseek (wav
->f
, 32, SEEK_CUR
)) {
198 dolog ("wav_fini_out: fseek to dlen failed\nReason: %s\n",
202 if (fwrite (dlen
, 4, 1, wav
->f
) != 1) {
203 dolog ("wav_fini_out: failed to write dlen\nReaons: %s\n",
209 if (fclose (wav
->f
)) {
210 dolog ("wav_fini_out: fclose %p failed\nReason: %s\n",
211 wav
->f
, strerror (errno
));
215 g_free (wav
->pcm_buf
);
219 static int wav_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
226 static void *wav_audio_init(Audiodev
*dev
)
228 assert(dev
->driver
== AUDIODEV_DRIVER_WAV
);
232 static void wav_audio_fini (void *opaque
)
237 static struct audio_pcm_ops wav_pcm_ops
= {
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
,
245 static struct audio_driver wav_audio_driver
= {
247 .descr
= "WAV renderer http://wikipedia.org/wiki/WAV",
248 .init
= wav_audio_init
,
249 .fini
= wav_audio_fini
,
250 .pcm_ops
= &wav_pcm_ops
,
254 .voice_size_out
= sizeof (WAVVoiceOut
),
258 static void register_audio_wav(void)
260 audio_driver_register(&wav_audio_driver
);
262 type_init(register_audio_wav
);