blob: 3281c3110d4f41454eef07ce57dfec11df79cfcd [file] [log] [blame]
pbrook87ecb682007-11-17 17:14:51 +00001#include "hw/hw.h"
2#include "console.h"
3#include "audio.h"
bellard8ead62c2006-07-04 16:51:32 +00004
5typedef struct {
6 QEMUFile *f;
7 int bytes;
bellardec36b692006-07-16 18:57:03 +00008 char *path;
9 int freq;
10 int bits;
11 int nchannels;
12 CaptureVoiceOut *cap;
bellard8ead62c2006-07-04 16:51:32 +000013} WAVState;
14
15/* VICE code: Store number as little endian. */
16static void le_store (uint8_t *buf, uint32_t val, int len)
17{
18 int i;
19 for (i = 0; i < len; i++) {
20 buf[i] = (uint8_t) (val & 0xff);
21 val >>= 8;
22 }
23}
24
bellardec36b692006-07-16 18:57:03 +000025static void wav_notify (void *opaque, audcnotification_e cmd)
26{
27 (void) opaque;
28 (void) cmd;
29}
30
31static void wav_destroy (void *opaque)
bellard8ead62c2006-07-04 16:51:32 +000032{
33 WAVState *wav = opaque;
bellardec36b692006-07-16 18:57:03 +000034 uint8_t rlen[4];
35 uint8_t dlen[4];
36 uint32_t datalen = wav->bytes;
37 uint32_t rifflen = datalen + 36;
bellard8ead62c2006-07-04 16:51:32 +000038
bellarde84a4fe2006-08-07 21:35:12 +000039 if (wav->f) {
40 le_store (rlen, rifflen, 4);
41 le_store (dlen, datalen, 4);
thsf941aa22007-02-17 22:19:29 +000042
bellarde84a4fe2006-08-07 21:35:12 +000043 qemu_fseek (wav->f, 4, SEEK_SET);
44 qemu_put_buffer (wav->f, rlen, 4);
thsf941aa22007-02-17 22:19:29 +000045
bellarde84a4fe2006-08-07 21:35:12 +000046 qemu_fseek (wav->f, 32, SEEK_CUR);
47 qemu_put_buffer (wav->f, dlen, 4);
48 qemu_fclose (wav->f);
bellard8ead62c2006-07-04 16:51:32 +000049 }
thsf941aa22007-02-17 22:19:29 +000050
bellarde84a4fe2006-08-07 21:35:12 +000051 qemu_free (wav->path);
bellard8ead62c2006-07-04 16:51:32 +000052}
53
bellardec36b692006-07-16 18:57:03 +000054static void wav_capture (void *opaque, void *buf, int size)
bellard8ead62c2006-07-04 16:51:32 +000055{
56 WAVState *wav = opaque;
57
58 qemu_put_buffer (wav->f, buf, size);
59 wav->bytes += size;
60}
61
bellardec36b692006-07-16 18:57:03 +000062static void wav_capture_destroy (void *opaque)
63{
64 WAVState *wav = opaque;
65
66 AUD_del_capture (wav->cap, wav);
67}
68
69static void wav_capture_info (void *opaque)
70{
71 WAVState *wav = opaque;
72 char *path = wav->path;
73
74 term_printf ("Capturing audio(%d,%d,%d) to %s: %d bytes\n",
75 wav->freq, wav->bits, wav->nchannels,
76 path ? path : "<not available>", wav->bytes);
77}
78
79static struct capture_ops wav_capture_ops = {
80 .destroy = wav_capture_destroy,
81 .info = wav_capture_info
82};
83
84int wav_start_capture (CaptureState *s, const char *path, int freq,
85 int bits, int nchannels)
bellard8ead62c2006-07-04 16:51:32 +000086{
87 WAVState *wav;
88 uint8_t hdr[] = {
89 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56,
90 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00,
91 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04,
92 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00
93 };
94 audsettings_t as;
95 struct audio_capture_ops ops;
bellardec36b692006-07-16 18:57:03 +000096 int stereo, bits16, shift;
97 CaptureVoiceOut *cap;
bellard8ead62c2006-07-04 16:51:32 +000098
bellardec36b692006-07-16 18:57:03 +000099 if (bits != 8 && bits != 16) {
100 term_printf ("incorrect bit count %d, must be 8 or 16\n", bits);
101 return -1;
102 }
103
104 if (nchannels != 1 && nchannels != 2) {
bellard47dbd1f2006-07-22 17:06:44 +0000105 term_printf ("incorrect channel count %d, must be 1 or 2\n",
106 nchannels);
bellardec36b692006-07-16 18:57:03 +0000107 return -1;
108 }
109
110 stereo = nchannels == 2;
111 bits16 = bits == 16;
bellard8ead62c2006-07-04 16:51:32 +0000112
113 as.freq = freq;
114 as.nchannels = 1 << stereo;
115 as.fmt = bits16 ? AUD_FMT_S16 : AUD_FMT_U8;
bellardd929eba2006-07-04 21:47:22 +0000116 as.endianness = 0;
bellard8ead62c2006-07-04 16:51:32 +0000117
bellardec36b692006-07-16 18:57:03 +0000118 ops.notify = wav_notify;
119 ops.capture = wav_capture;
120 ops.destroy = wav_destroy;
bellard8ead62c2006-07-04 16:51:32 +0000121
122 wav = qemu_mallocz (sizeof (*wav));
123 if (!wav) {
bellarda3c25992006-07-18 21:09:59 +0000124 term_printf ("Could not allocate memory for wav capture (%zu bytes)",
125 sizeof (*wav));
bellardec36b692006-07-16 18:57:03 +0000126 return -1;
bellard8ead62c2006-07-04 16:51:32 +0000127 }
128
129 shift = bits16 + stereo;
130 hdr[34] = bits16 ? 0x10 : 0x08;
131
132 le_store (hdr + 22, as.nchannels, 2);
133 le_store (hdr + 24, freq, 4);
134 le_store (hdr + 28, freq << shift, 4);
135 le_store (hdr + 32, 1 << shift, 2);
136
bellarde70332b2006-08-05 21:31:46 +0000137 wav->f = qemu_fopen (path, "wb");
bellard8ead62c2006-07-04 16:51:32 +0000138 if (!wav->f) {
bellardec36b692006-07-16 18:57:03 +0000139 term_printf ("Failed to open wave file `%s'\nReason: %s\n",
140 path, strerror (errno));
bellard8ead62c2006-07-04 16:51:32 +0000141 qemu_free (wav);
bellardec36b692006-07-16 18:57:03 +0000142 return -1;
bellard8ead62c2006-07-04 16:51:32 +0000143 }
144
bellardec36b692006-07-16 18:57:03 +0000145 wav->path = qemu_strdup (path);
146 wav->bits = bits;
147 wav->nchannels = nchannels;
148 wav->freq = freq;
149
bellard8ead62c2006-07-04 16:51:32 +0000150 qemu_put_buffer (wav->f, hdr, sizeof (hdr));
bellardec36b692006-07-16 18:57:03 +0000151
152 cap = AUD_add_capture (NULL, &as, &ops, wav);
153 if (!cap) {
154 term_printf ("Failed to add audio capture\n");
bellarde84a4fe2006-08-07 21:35:12 +0000155 qemu_free (wav->path);
156 qemu_fclose (wav->f);
bellardec36b692006-07-16 18:57:03 +0000157 qemu_free (wav);
158 return -1;
159 }
160
161 wav->cap = cap;
162 s->opaque = wav;
163 s->ops = wav_capture_ops;
164 return 0;
bellard8ead62c2006-07-04 16:51:32 +0000165}