Statistics
| Branch: | Revision:

root / audio / wavaudio.c @ 6276c767

History | View | Annotate | Download (6.8 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 85571bc7 bellard
#include "vl.h"
25 85571bc7 bellard
26 1d14ffa9 bellard
#define AUDIO_CAP "wav"
27 1d14ffa9 bellard
#include "audio_int.h"
28 fb065187 bellard
29 1d14ffa9 bellard
typedef struct WAVVoiceOut {
30 1d14ffa9 bellard
    HWVoiceOut hw;
31 fb065187 bellard
    QEMUFile *f;
32 fb065187 bellard
    int64_t old_ticks;
33 fb065187 bellard
    void *pcm_buf;
34 fb065187 bellard
    int total_samples;
35 1d14ffa9 bellard
} WAVVoiceOut;
36 85571bc7 bellard
37 85571bc7 bellard
static struct {
38 c0fe3827 bellard
    audsettings_t settings;
39 85571bc7 bellard
    const char *wav_path;
40 85571bc7 bellard
} conf = {
41 c0fe3827 bellard
    {
42 c0fe3827 bellard
        44100,
43 c0fe3827 bellard
        2,
44 f941aa25 ths
        AUD_FMT_S16,
45 f941aa25 ths
        AUDIO_HOST_ENDIANNESS
46 c0fe3827 bellard
    },
47 c0fe3827 bellard
    "qemu.wav"
48 85571bc7 bellard
};
49 85571bc7 bellard
50 1d14ffa9 bellard
static int wav_run_out (HWVoiceOut *hw)
51 85571bc7 bellard
{
52 1d14ffa9 bellard
    WAVVoiceOut *wav = (WAVVoiceOut *) hw;
53 85571bc7 bellard
    int rpos, live, decr, samples;
54 85571bc7 bellard
    uint8_t *dst;
55 85571bc7 bellard
    st_sample_t *src;
56 85571bc7 bellard
    int64_t now = qemu_get_clock (vm_clock);
57 85571bc7 bellard
    int64_t ticks = now - wav->old_ticks;
58 1d14ffa9 bellard
    int64_t bytes = (ticks * hw->info.bytes_per_second) / ticks_per_sec;
59 85571bc7 bellard
60 1d14ffa9 bellard
    if (bytes > INT_MAX) {
61 1d14ffa9 bellard
        samples = INT_MAX >> hw->info.shift;
62 1d14ffa9 bellard
    }
63 1d14ffa9 bellard
    else {
64 1d14ffa9 bellard
        samples = bytes >> hw->info.shift;
65 1d14ffa9 bellard
    }
66 85571bc7 bellard
67 1d14ffa9 bellard
    live = audio_pcm_hw_get_live_out (hw);
68 1d14ffa9 bellard
    if (!live) {
69 1d14ffa9 bellard
        return 0;
70 1d14ffa9 bellard
    }
71 85571bc7 bellard
72 7372f88d bellard
    wav->old_ticks = now;
73 85571bc7 bellard
    decr = audio_MIN (live, samples);
74 85571bc7 bellard
    samples = decr;
75 85571bc7 bellard
    rpos = hw->rpos;
76 85571bc7 bellard
    while (samples) {
77 85571bc7 bellard
        int left_till_end_samples = hw->samples - rpos;
78 85571bc7 bellard
        int convert_samples = audio_MIN (samples, left_till_end_samples);
79 85571bc7 bellard
80 1d14ffa9 bellard
        src = hw->mix_buf + rpos;
81 1d14ffa9 bellard
        dst = advance (wav->pcm_buf, rpos << hw->info.shift);
82 85571bc7 bellard
83 85571bc7 bellard
        hw->clip (dst, src, convert_samples);
84 1d14ffa9 bellard
        qemu_put_buffer (wav->f, dst, convert_samples << hw->info.shift);
85 85571bc7 bellard
86 85571bc7 bellard
        rpos = (rpos + convert_samples) % hw->samples;
87 85571bc7 bellard
        samples -= convert_samples;
88 85571bc7 bellard
        wav->total_samples += convert_samples;
89 85571bc7 bellard
    }
90 85571bc7 bellard
91 85571bc7 bellard
    hw->rpos = rpos;
92 1d14ffa9 bellard
    return decr;
93 85571bc7 bellard
}
94 85571bc7 bellard
95 1d14ffa9 bellard
static int wav_write_out (SWVoiceOut *sw, void *buf, int len)
96 85571bc7 bellard
{
97 1d14ffa9 bellard
    return audio_pcm_sw_write (sw, buf, len);
98 85571bc7 bellard
}
99 85571bc7 bellard
100 85571bc7 bellard
/* VICE code: Store number as little endian. */
101 85571bc7 bellard
static void le_store (uint8_t *buf, uint32_t val, int len)
102 85571bc7 bellard
{
103 85571bc7 bellard
    int i;
104 85571bc7 bellard
    for (i = 0; i < len; i++) {
105 85571bc7 bellard
        buf[i] = (uint8_t) (val & 0xff);
106 85571bc7 bellard
        val >>= 8;
107 85571bc7 bellard
    }
108 85571bc7 bellard
}
109 85571bc7 bellard
110 c0fe3827 bellard
static int wav_init_out (HWVoiceOut *hw, audsettings_t *as)
111 85571bc7 bellard
{
112 1d14ffa9 bellard
    WAVVoiceOut *wav = (WAVVoiceOut *) hw;
113 c0fe3827 bellard
    int bits16 = 0, stereo = 0;
114 85571bc7 bellard
    uint8_t hdr[] = {
115 85571bc7 bellard
        0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56,
116 85571bc7 bellard
        0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00,
117 85571bc7 bellard
        0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04,
118 85571bc7 bellard
        0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00
119 85571bc7 bellard
    };
120 c0fe3827 bellard
    audsettings_t wav_as = conf.settings;
121 85571bc7 bellard
122 c0fe3827 bellard
    (void) as;
123 1d14ffa9 bellard
124 c0fe3827 bellard
    stereo = wav_as.nchannels == 2;
125 c0fe3827 bellard
    switch (wav_as.fmt) {
126 85571bc7 bellard
    case AUD_FMT_S8:
127 85571bc7 bellard
    case AUD_FMT_U8:
128 1d14ffa9 bellard
        bits16 = 0;
129 85571bc7 bellard
        break;
130 85571bc7 bellard
131 85571bc7 bellard
    case AUD_FMT_S16:
132 85571bc7 bellard
    case AUD_FMT_U16:
133 85571bc7 bellard
        bits16 = 1;
134 85571bc7 bellard
        break;
135 f941aa25 ths
136 f941aa25 ths
    case AUD_FMT_S32:
137 f941aa25 ths
    case AUD_FMT_U32:
138 f941aa25 ths
        dolog ("WAVE files can not handle 32bit formats\n");
139 f941aa25 ths
        return -1;
140 85571bc7 bellard
    }
141 85571bc7 bellard
142 85571bc7 bellard
    hdr[34] = bits16 ? 0x10 : 0x08;
143 c0fe3827 bellard
144 d929eba5 bellard
    wav_as.endianness = 0;
145 d929eba5 bellard
    audio_pcm_init_info (&hw->info, &wav_as);
146 c0fe3827 bellard
147 c0fe3827 bellard
    hw->samples = 1024;
148 c0fe3827 bellard
    wav->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
149 1d14ffa9 bellard
    if (!wav->pcm_buf) {
150 c0fe3827 bellard
        dolog ("Could not allocate buffer (%d bytes)\n",
151 c0fe3827 bellard
               hw->samples << hw->info.shift);
152 85571bc7 bellard
        return -1;
153 1d14ffa9 bellard
    }
154 85571bc7 bellard
155 1d14ffa9 bellard
    le_store (hdr + 22, hw->info.nchannels, 2);
156 1d14ffa9 bellard
    le_store (hdr + 24, hw->info.freq, 4);
157 c0fe3827 bellard
    le_store (hdr + 28, hw->info.freq << (bits16 + stereo), 4);
158 c0fe3827 bellard
    le_store (hdr + 32, 1 << (bits16 + stereo), 2);
159 85571bc7 bellard
160 e70332b3 bellard
    wav->f = qemu_fopen (conf.wav_path, "wb");
161 85571bc7 bellard
    if (!wav->f) {
162 1d14ffa9 bellard
        dolog ("Failed to open wave file `%s'\nReason: %s\n",
163 85571bc7 bellard
               conf.wav_path, strerror (errno));
164 7372f88d bellard
        qemu_free (wav->pcm_buf);
165 7372f88d bellard
        wav->pcm_buf = NULL;
166 85571bc7 bellard
        return -1;
167 85571bc7 bellard
    }
168 85571bc7 bellard
169 85571bc7 bellard
    qemu_put_buffer (wav->f, hdr, sizeof (hdr));
170 85571bc7 bellard
    return 0;
171 85571bc7 bellard
}
172 85571bc7 bellard
173 1d14ffa9 bellard
static void wav_fini_out (HWVoiceOut *hw)
174 85571bc7 bellard
{
175 1d14ffa9 bellard
    WAVVoiceOut *wav = (WAVVoiceOut *) hw;
176 85571bc7 bellard
    uint8_t rlen[4];
177 85571bc7 bellard
    uint8_t dlen[4];
178 50903530 bellard
    uint32_t datalen = wav->total_samples << hw->info.shift;
179 50903530 bellard
    uint32_t rifflen = datalen + 36;
180 85571bc7 bellard
181 c0fe3827 bellard
    if (!wav->f) {
182 85571bc7 bellard
        return;
183 1d14ffa9 bellard
    }
184 85571bc7 bellard
185 85571bc7 bellard
    le_store (rlen, rifflen, 4);
186 85571bc7 bellard
    le_store (dlen, datalen, 4);
187 85571bc7 bellard
188 85571bc7 bellard
    qemu_fseek (wav->f, 4, SEEK_SET);
189 85571bc7 bellard
    qemu_put_buffer (wav->f, rlen, 4);
190 85571bc7 bellard
191 85571bc7 bellard
    qemu_fseek (wav->f, 32, SEEK_CUR);
192 85571bc7 bellard
    qemu_put_buffer (wav->f, dlen, 4);
193 85571bc7 bellard
194 e70332b3 bellard
    qemu_fclose (wav->f);
195 85571bc7 bellard
    wav->f = NULL;
196 7372f88d bellard
197 7372f88d bellard
    qemu_free (wav->pcm_buf);
198 7372f88d bellard
    wav->pcm_buf = NULL;
199 85571bc7 bellard
}
200 85571bc7 bellard
201 1d14ffa9 bellard
static int wav_ctl_out (HWVoiceOut *hw, int cmd, ...)
202 85571bc7 bellard
{
203 85571bc7 bellard
    (void) hw;
204 85571bc7 bellard
    (void) cmd;
205 85571bc7 bellard
    return 0;
206 85571bc7 bellard
}
207 85571bc7 bellard
208 85571bc7 bellard
static void *wav_audio_init (void)
209 85571bc7 bellard
{
210 85571bc7 bellard
    return &conf;
211 85571bc7 bellard
}
212 85571bc7 bellard
213 85571bc7 bellard
static void wav_audio_fini (void *opaque)
214 85571bc7 bellard
{
215 1d14ffa9 bellard
    (void) opaque;
216 85571bc7 bellard
    ldebug ("wav_fini");
217 85571bc7 bellard
}
218 85571bc7 bellard
219 1d14ffa9 bellard
struct audio_option wav_options[] = {
220 c0fe3827 bellard
    {"FREQUENCY", AUD_OPT_INT, &conf.settings.freq,
221 c0fe3827 bellard
     "Frequency", NULL, 0},
222 c0fe3827 bellard
223 c0fe3827 bellard
    {"FORMAT", AUD_OPT_FMT, &conf.settings.fmt,
224 c0fe3827 bellard
     "Format", NULL, 0},
225 c0fe3827 bellard
226 c0fe3827 bellard
    {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.settings.nchannels,
227 c0fe3827 bellard
     "Number of channels (1 - mono, 2 - stereo)", NULL, 0},
228 c0fe3827 bellard
229 1d14ffa9 bellard
    {"PATH", AUD_OPT_STR, &conf.wav_path,
230 1d14ffa9 bellard
     "Path to wave file", NULL, 0},
231 1d14ffa9 bellard
    {NULL, 0, NULL, NULL, NULL, 0}
232 1d14ffa9 bellard
};
233 1d14ffa9 bellard
234 1d14ffa9 bellard
struct audio_pcm_ops wav_pcm_ops = {
235 1d14ffa9 bellard
    wav_init_out,
236 1d14ffa9 bellard
    wav_fini_out,
237 1d14ffa9 bellard
    wav_run_out,
238 1d14ffa9 bellard
    wav_write_out,
239 1d14ffa9 bellard
    wav_ctl_out,
240 1d14ffa9 bellard
241 1d14ffa9 bellard
    NULL,
242 1d14ffa9 bellard
    NULL,
243 1d14ffa9 bellard
    NULL,
244 1d14ffa9 bellard
    NULL,
245 1d14ffa9 bellard
    NULL
246 85571bc7 bellard
};
247 85571bc7 bellard
248 1d14ffa9 bellard
struct audio_driver wav_audio_driver = {
249 1d14ffa9 bellard
    INIT_FIELD (name           = ) "wav",
250 1d14ffa9 bellard
    INIT_FIELD (descr          = )
251 1d14ffa9 bellard
    "WAV renderer http://wikipedia.org/wiki/WAV",
252 1d14ffa9 bellard
    INIT_FIELD (options        = ) wav_options,
253 1d14ffa9 bellard
    INIT_FIELD (init           = ) wav_audio_init,
254 1d14ffa9 bellard
    INIT_FIELD (fini           = ) wav_audio_fini,
255 1d14ffa9 bellard
    INIT_FIELD (pcm_ops        = ) &wav_pcm_ops,
256 1d14ffa9 bellard
    INIT_FIELD (can_be_default = ) 0,
257 1d14ffa9 bellard
    INIT_FIELD (max_voices_out = ) 1,
258 1d14ffa9 bellard
    INIT_FIELD (max_voices_in  = ) 0,
259 1d14ffa9 bellard
    INIT_FIELD (voice_size_out = ) sizeof (WAVVoiceOut),
260 1d14ffa9 bellard
    INIT_FIELD (voice_size_in  = ) 0
261 85571bc7 bellard
};