/src/qemu/audio/wavaudio.c
Line | Count | Source |
1 | | /* |
2 | | * QEMU WAV audio driver |
3 | | * |
4 | | * Copyright (c) 2004-2005 Vassili Karpov (malc) |
5 | | * |
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 | | */ |
24 | | |
25 | | #include "qemu/osdep.h" |
26 | | #include "qemu/module.h" |
27 | | #include "qemu/error-report.h" |
28 | | #include "qemu/audio.h" |
29 | | #include "qom/object.h" |
30 | | |
31 | | #include "audio_int.h" |
32 | | |
33 | | #define TYPE_AUDIO_WAV "audio-wav" |
34 | | OBJECT_DECLARE_SIMPLE_TYPE(AudioWav, AUDIO_WAV) |
35 | | |
36 | | struct AudioWav { |
37 | | AudioMixengBackend parent_obj; |
38 | | }; |
39 | | |
40 | | typedef struct WAVVoiceOut { |
41 | | HWVoiceOut hw; |
42 | | FILE *f; |
43 | | RateCtl rate; |
44 | | int total_samples; |
45 | | } WAVVoiceOut; |
46 | | |
47 | | static size_t wav_write_out(HWVoiceOut *hw, void *buf, size_t len) |
48 | 0 | { |
49 | 0 | WAVVoiceOut *wav = (WAVVoiceOut *) hw; |
50 | 0 | int64_t bytes = audio_rate_get_bytes(&wav->rate, &hw->info, len); |
51 | 0 | assert(bytes % hw->info.bytes_per_frame == 0); |
52 | |
|
53 | 0 | if (bytes && fwrite(buf, bytes, 1, wav->f) != 1) { |
54 | 0 | error_report("wav: fwrite of %" PRId64 " bytes failed: %s", |
55 | 0 | bytes, strerror(errno)); |
56 | 0 | } |
57 | |
|
58 | 0 | wav->total_samples += bytes / hw->info.bytes_per_frame; |
59 | 0 | return bytes; |
60 | 0 | } |
61 | | |
62 | | /* VICE code: Store number as little endian. */ |
63 | | static void le_store (uint8_t *buf, uint32_t val, int len) |
64 | 0 | { |
65 | 0 | int i; |
66 | 0 | for (i = 0; i < len; i++) { |
67 | 0 | buf[i] = (uint8_t) (val & 0xff); |
68 | 0 | val >>= 8; |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | | static int wav_init_out(HWVoiceOut *hw, struct audsettings *as) |
73 | 0 | { |
74 | 0 | WAVVoiceOut *wav = (WAVVoiceOut *) hw; |
75 | 0 | int bits16 = 0, stereo = 0; |
76 | 0 | uint8_t hdr[] = { |
77 | 0 | 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, |
78 | 0 | 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, |
79 | 0 | 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04, |
80 | 0 | 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00 |
81 | 0 | }; |
82 | 0 | Audiodev *dev = hw->s->dev; |
83 | 0 | AudiodevWavOptions *wopts = &dev->u.wav; |
84 | 0 | struct audsettings wav_as = audiodev_to_audsettings(dev->u.wav.out); |
85 | 0 | const char *wav_path = wopts->path ?: "qemu.wav"; |
86 | |
|
87 | 0 | stereo = wav_as.nchannels == 2; |
88 | 0 | switch (wav_as.fmt) { |
89 | 0 | case AUDIO_FORMAT_S8: |
90 | 0 | case AUDIO_FORMAT_U8: |
91 | 0 | bits16 = 0; |
92 | 0 | break; |
93 | | |
94 | 0 | case AUDIO_FORMAT_S16: |
95 | 0 | case AUDIO_FORMAT_U16: |
96 | 0 | bits16 = 1; |
97 | 0 | break; |
98 | | |
99 | 0 | case AUDIO_FORMAT_S32: |
100 | 0 | case AUDIO_FORMAT_U32: |
101 | 0 | error_report("wav: WAVE files cannot handle 32-bit formats"); |
102 | 0 | return -1; |
103 | | |
104 | 0 | case AUDIO_FORMAT_F32: |
105 | 0 | error_report("wav: WAVE files cannot handle float formats"); |
106 | 0 | return -1; |
107 | | |
108 | 0 | default: |
109 | 0 | abort(); |
110 | 0 | } |
111 | | |
112 | 0 | hdr[34] = bits16 ? 0x10 : 0x08; |
113 | |
|
114 | 0 | wav_as.big_endian = false; |
115 | 0 | audio_pcm_init_info (&hw->info, &wav_as); |
116 | |
|
117 | 0 | hw->samples = 1024; |
118 | 0 | le_store (hdr + 22, hw->info.nchannels, 2); |
119 | 0 | le_store (hdr + 24, hw->info.freq, 4); |
120 | 0 | le_store (hdr + 28, hw->info.freq << (bits16 + stereo), 4); |
121 | 0 | le_store (hdr + 32, 1 << (bits16 + stereo), 2); |
122 | |
|
123 | 0 | wav->f = fopen(wav_path, "wb"); |
124 | 0 | if (!wav->f) { |
125 | 0 | error_report("wav: failed to open wave file '%s': %s", |
126 | 0 | wav_path, strerror(errno)); |
127 | 0 | return -1; |
128 | 0 | } |
129 | | |
130 | 0 | if (fwrite (hdr, sizeof (hdr), 1, wav->f) != 1) { |
131 | 0 | error_report("wav: failed to write header: %s", strerror(errno)); |
132 | 0 | return -1; |
133 | 0 | } |
134 | | |
135 | 0 | audio_rate_start(&wav->rate); |
136 | 0 | return 0; |
137 | 0 | } |
138 | | |
139 | | static void wav_fini_out (HWVoiceOut *hw) |
140 | 0 | { |
141 | 0 | WAVVoiceOut *wav = (WAVVoiceOut *) hw; |
142 | 0 | uint8_t rlen[4]; |
143 | 0 | uint8_t dlen[4]; |
144 | 0 | uint32_t datalen = wav->total_samples * hw->info.bytes_per_frame; |
145 | 0 | uint32_t rifflen = datalen + 36; |
146 | |
|
147 | 0 | if (!wav->f) { |
148 | 0 | return; |
149 | 0 | } |
150 | | |
151 | 0 | le_store (rlen, rifflen, 4); |
152 | 0 | le_store (dlen, datalen, 4); |
153 | |
|
154 | 0 | if (fseek (wav->f, 4, SEEK_SET)) { |
155 | 0 | error_report("wav: fseek to rlen failed: %s", strerror(errno)); |
156 | 0 | goto doclose; |
157 | 0 | } |
158 | 0 | if (fwrite (rlen, 4, 1, wav->f) != 1) { |
159 | 0 | error_report("wav: failed to write rlen: %s", strerror(errno)); |
160 | 0 | goto doclose; |
161 | 0 | } |
162 | 0 | if (fseek (wav->f, 32, SEEK_CUR)) { |
163 | 0 | error_report("wav: fseek to dlen failed: %s", strerror(errno)); |
164 | 0 | goto doclose; |
165 | 0 | } |
166 | 0 | if (fwrite (dlen, 4, 1, wav->f) != 1) { |
167 | 0 | error_report("wav: failed to write dlen: %s", strerror(errno)); |
168 | 0 | goto doclose; |
169 | 0 | } |
170 | | |
171 | 0 | doclose: |
172 | 0 | if (fclose (wav->f)) { |
173 | 0 | error_report("wav: fclose failed: %s", strerror(errno)); |
174 | 0 | } |
175 | 0 | wav->f = NULL; |
176 | 0 | } |
177 | | |
178 | | static void wav_enable_out(HWVoiceOut *hw, bool enable) |
179 | 0 | { |
180 | 0 | WAVVoiceOut *wav = (WAVVoiceOut *) hw; |
181 | |
|
182 | 0 | if (enable) { |
183 | 0 | audio_rate_start(&wav->rate); |
184 | 0 | } |
185 | 0 | } |
186 | | |
187 | | static void audio_wav_class_init(ObjectClass *klass, const void *data) |
188 | 63 | { |
189 | 63 | AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_CLASS(klass); |
190 | | |
191 | 63 | k->max_voices_out = 1; |
192 | 63 | k->max_voices_in = 0; |
193 | 63 | k->voice_size_out = sizeof(WAVVoiceOut); |
194 | 63 | k->voice_size_in = 0; |
195 | | |
196 | 63 | k->init_out = wav_init_out; |
197 | 63 | k->fini_out = wav_fini_out; |
198 | 63 | k->write = wav_write_out; |
199 | 63 | k->buffer_get_free = audio_generic_buffer_get_free; |
200 | 63 | k->run_buffer_out = audio_generic_run_buffer_out; |
201 | 63 | k->enable_out = wav_enable_out; |
202 | 63 | } |
203 | | |
204 | | static const TypeInfo audio_types[] = { |
205 | | { |
206 | | .name = TYPE_AUDIO_WAV, |
207 | | .parent = TYPE_AUDIO_MIXENG_BACKEND, |
208 | | .instance_size = sizeof(AudioWav), |
209 | | .class_init = audio_wav_class_init, |
210 | | } |
211 | | }; |
212 | | |
213 | | DEFINE_TYPES(audio_types) |
214 | | module_obj(TYPE_AUDIO_WAV); |