| 1 | #include "qemu/osdep.h" |
| 2 | #include "qemu/audio.h" |
| 3 | #include "qemu/qemu-print.h" |
| 4 | #include "qemu/error-report.h" |
| 5 | #include "audio_int.h" |
| 6 | |
| 7 | typedef struct { |
| 8 | FILE *f; |
| 9 | int bytes; |
| 10 | char *path; |
| 11 | int freq; |
| 12 | int bits; |
| 13 | int nchannels; |
| 14 | AudioBackend *audio_be; |
| 15 | CaptureVoiceOut *cap; |
| 16 | } WAVState; |
| 17 | |
| 18 | /* VICE code: Store number as little endian. */ |
| 19 | static void le_store (uint8_t *buf, uint32_t val, int len) |
| 20 | { |
| 21 | int i; |
| 22 | for (i = 0; i < len; i++) { |
| 23 | buf[i] = (uint8_t) (val & 0xff); |
| 24 | val >>= 8; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | static void wav_notify (void *opaque, audcnotification_e cmd) |
| 29 | { |
| 30 | (void) opaque; |
| 31 | (void) cmd; |
| 32 | } |
| 33 | |
| 34 | static void wav_destroy (void *opaque) |
| 35 | { |
| 36 | WAVState *wav = opaque; |
| 37 | uint8_t rlen[4]; |
| 38 | uint8_t dlen[4]; |
| 39 | uint32_t datalen = wav->bytes; |
| 40 | uint32_t rifflen = datalen + 36; |
| 41 | |
| 42 | if (wav->f) { |
| 43 | le_store (rlen, rifflen, 4); |
| 44 | le_store (dlen, datalen, 4); |
| 45 | |
| 46 | if (fseek (wav->f, 4, SEEK_SET)) { |
| 47 | error_report("wav_destroy: rlen fseek failed: %s", |
| 48 | strerror(errno)); |
| 49 | goto doclose; |
| 50 | } |
| 51 | if (fwrite (rlen, 4, 1, wav->f) != 1) { |
| 52 | error_report("wav_destroy: rlen fwrite failed: %s", |
| 53 | strerror(errno)); |
| 54 | goto doclose; |
| 55 | } |
| 56 | if (fseek (wav->f, 32, SEEK_CUR)) { |
| 57 | error_report("wav_destroy: dlen fseek failed: %s", |
| 58 | strerror(errno)); |
| 59 | goto doclose; |
| 60 | } |
| 61 | if (fwrite (dlen, 1, 4, wav->f) != 4) { |
| 62 | error_report("wav_destroy: dlen fwrite failed: %s", |
| 63 | strerror(errno)); |
| 64 | goto doclose; |
| 65 | } |
| 66 | doclose: |
| 67 | if (fclose (wav->f)) { |
| 68 | error_report("wav_destroy: fclose failed: %s", strerror(errno)); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | g_free (wav->path); |
| 73 | } |
| 74 | |
| 75 | static void wav_capture(void *opaque, const void *buf, int size) |
| 76 | { |
| 77 | WAVState *wav = opaque; |
| 78 | |
| 79 | if (fwrite (buf, size, 1, wav->f) != 1) { |
| 80 | error_report("wav_capture: fwrite error: %s", strerror(errno)); |
| 81 | } |
| 82 | wav->bytes += size; |
| 83 | } |
| 84 | |
| 85 | static void wav_capture_destroy (void *opaque) |
| 86 | { |
| 87 | WAVState *wav = opaque; |
| 88 | |
| 89 | audio_be_del_capture(wav->audio_be, wav->cap, wav); |
| 90 | g_free (wav); |
| 91 | } |
| 92 | |
| 93 | static void wav_capture_info (void *opaque) |
| 94 | { |
| 95 | WAVState *wav = opaque; |
| 96 | char *path = wav->path; |
| 97 | |
| 98 | qemu_printf("Capturing audio(%d,%d,%d) to %s: %d bytes\n", |
| 99 | wav->freq, wav->bits, wav->nchannels, |
| 100 | path ? path : "<not available>", wav->bytes); |
| 101 | } |
| 102 | |
| 103 | static struct capture_ops wav_capture_ops = { |
| 104 | .destroy = wav_capture_destroy, |
| 105 | .info = wav_capture_info |
| 106 | }; |
| 107 | |
| 108 | int wav_start_capture(AudioBackend *state, CaptureState *s, const char *path, |
| 109 | int freq, int bits, int nchannels) |
| 110 | { |
| 111 | WAVState *wav; |
| 112 | uint8_t hdr[] = { |
| 113 | 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, |
| 114 | 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 115 | 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04, |
| 116 | 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00 |
| 117 | }; |
| 118 | struct audsettings as; |
| 119 | struct audio_capture_ops ops; |
| 120 | int stereo, bits16, shift; |
| 121 | CaptureVoiceOut *cap; |
| 122 | |
| 123 | if (bits != 8 && bits != 16) { |
| 124 | error_report("incorrect bit count %d, must be 8 or 16", bits); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | if (nchannels != 1 && nchannels != 2) { |
| 129 | error_report("incorrect channel count %d, must be 1 or 2", |
| 130 | nchannels); |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | stereo = nchannels == 2; |
| 135 | bits16 = bits == 16; |
| 136 | |
| 137 | as.freq = freq; |
| 138 | as.nchannels = 1 << stereo; |
| 139 | as.fmt = bits16 ? AUDIO_FORMAT_S16 : AUDIO_FORMAT_U8; |
| 140 | as.big_endian = false; |
| 141 | |
| 142 | ops.notify = wav_notify; |
| 143 | ops.capture = wav_capture; |
| 144 | ops.destroy = wav_destroy; |
| 145 | |
| 146 | wav = g_malloc0 (sizeof (*wav)); |
| 147 | |
| 148 | shift = bits16 + stereo; |
| 149 | hdr[34] = bits16 ? 0x10 : 0x08; |
| 150 | |
| 151 | le_store (hdr + 22, as.nchannels, 2); |
| 152 | le_store (hdr + 24, freq, 4); |
| 153 | le_store (hdr + 28, freq << shift, 4); |
| 154 | le_store (hdr + 32, 1 << shift, 2); |
| 155 | |
| 156 | wav->f = fopen (path, "wb"); |
| 157 | if (!wav->f) { |
| 158 | error_report("Failed to open wave file `%s': %s", |
| 159 | path, strerror(errno)); |
| 160 | g_free (wav); |
| 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | wav->audio_be = state; |
| 165 | wav->path = g_strdup (path); |
| 166 | wav->bits = bits; |
| 167 | wav->nchannels = nchannels; |
| 168 | wav->freq = freq; |
| 169 | |
| 170 | if (fwrite (hdr, sizeof (hdr), 1, wav->f) != 1) { |
| 171 | error_report("Failed to write header: %s", strerror(errno)); |
| 172 | goto error_free; |
| 173 | } |
| 174 | |
| 175 | cap = audio_be_add_capture(wav->audio_be, &as, &ops, wav); |
| 176 | if (!cap) { |
| 177 | error_report("Failed to add audio capture"); |
| 178 | goto error_free; |
| 179 | } |
| 180 | |
| 181 | wav->cap = cap; |
| 182 | s->opaque = wav; |
| 183 | s->ops = wav_capture_ops; |
| 184 | return 0; |
| 185 | |
| 186 | error_free: |
| 187 | g_free (wav->path); |
| 188 | if (fclose (wav->f)) { |
| 189 | error_report("Failed to close wave file: %s", strerror(errno)); |
| 190 | } |
| 191 | g_free (wav); |
| 192 | return -1; |
| 193 | } |