13 /* VICE code: Store number as little endian. */
14 static void le_store (uint8_t *buf, uint32_t val, int len)
17 for (i = 0; i < len; i++) {
18 buf[i] = (uint8_t) (val & 0xff);
23 static void wav_notify (void *opaque, audcnotification_e cmd)
29 static void wav_destroy (void *opaque)
31 WAVState *wav = opaque;
34 uint32_t datalen = wav->bytes;
35 uint32_t rifflen = datalen + 36;
38 le_store (rlen, rifflen, 4);
39 le_store (dlen, datalen, 4);
41 qemu_fseek (wav->f, 4, SEEK_SET);
42 qemu_put_buffer (wav->f, rlen, 4);
44 qemu_fseek (wav->f, 32, SEEK_CUR);
45 qemu_put_buffer (wav->f, dlen, 4);
49 qemu_free (wav->path);
52 static void wav_capture (void *opaque, void *buf, int size)
54 WAVState *wav = opaque;
56 qemu_put_buffer (wav->f, buf, size);
60 static void wav_capture_destroy (void *opaque)
62 WAVState *wav = opaque;
64 AUD_del_capture (wav->cap, wav);
67 static void wav_capture_info (void *opaque)
69 WAVState *wav = opaque;
70 char *path = wav->path;
72 term_printf ("Capturing audio(%d,%d,%d) to %s: %d bytes\n",
73 wav->freq, wav->bits, wav->nchannels,
74 path ? path : "<not available>", wav->bytes);
77 static struct capture_ops wav_capture_ops = {
78 .destroy = wav_capture_destroy,
79 .info = wav_capture_info
82 int wav_start_capture (CaptureState *s, const char *path, int freq,
83 int bits, int nchannels)
87 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56,
88 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00,
89 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04,
90 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00
93 struct audio_capture_ops ops;
94 int stereo, bits16, shift;
97 if (bits != 8 && bits != 16) {
98 term_printf ("incorrect bit count %d, must be 8 or 16\n", bits);
102 if (nchannels != 1 && nchannels != 2) {
103 term_printf ("incorrect channel count %d, must be 1 or 2\n",
108 stereo = nchannels == 2;
112 as.nchannels = 1 << stereo;
113 as.fmt = bits16 ? AUD_FMT_S16 : AUD_FMT_U8;
116 ops.notify = wav_notify;
117 ops.capture = wav_capture;
118 ops.destroy = wav_destroy;
120 wav = qemu_mallocz (sizeof (*wav));
122 term_printf ("Could not allocate memory for wav capture (%zu bytes)",
127 shift = bits16 + stereo;
128 hdr[34] = bits16 ? 0x10 : 0x08;
130 le_store (hdr + 22, as.nchannels, 2);
131 le_store (hdr + 24, freq, 4);
132 le_store (hdr + 28, freq << shift, 4);
133 le_store (hdr + 32, 1 << shift, 2);
135 wav->f = qemu_fopen (path, "wb");
137 term_printf ("Failed to open wave file `%s'\nReason: %s\n",
138 path, strerror (errno));
143 wav->path = qemu_strdup (path);
145 wav->nchannels = nchannels;
148 qemu_put_buffer (wav->f, hdr, sizeof (hdr));
150 cap = AUD_add_capture (NULL, &as, &ops, wav);
152 term_printf ("Failed to add audio capture\n");
153 qemu_free (wav->path);
154 qemu_fclose (wav->f);
161 s->ops = wav_capture_ops;