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