usb-redir: Don't handle interrupt output packets async
[qemu/opensuse.git] / audio / wavcapture.c
blobf73691cc9b782aa725f974dd8583729ca9deda4f
1 /* public domain */
2 #include "hw/hw.h"
3 #include "monitor.h"
4 #include "audio.h"
6 typedef struct {
7 FILE *f;
8 int bytes;
9 char *path;
10 int freq;
11 int bits;
12 int nchannels;
13 CaptureVoiceOut *cap;
14 } WAVState;
16 /* VICE code: Store number as little endian. */
17 static void le_store (uint8_t *buf, uint32_t val, int len)
19 int i;
20 for (i = 0; i < len; i++) {
21 buf[i] = (uint8_t) (val & 0xff);
22 val >>= 8;
26 static void wav_notify (void *opaque, audcnotification_e cmd)
28 (void) opaque;
29 (void) cmd;
32 static void wav_destroy (void *opaque)
34 WAVState *wav = opaque;
35 uint8_t rlen[4];
36 uint8_t dlen[4];
37 uint32_t datalen = wav->bytes;
38 uint32_t rifflen = datalen + 36;
39 Monitor *mon = cur_mon;
41 if (wav->f) {
42 le_store (rlen, rifflen, 4);
43 le_store (dlen, datalen, 4);
45 if (fseek (wav->f, 4, SEEK_SET)) {
46 monitor_printf (mon, "wav_destroy: rlen fseek failed\nReason: %s\n",
47 strerror (errno));
48 goto doclose;
50 if (fwrite (rlen, 4, 1, wav->f) != 1) {
51 monitor_printf (mon, "wav_destroy: rlen fwrite failed\nReason %s\n",
52 strerror (errno));
53 goto doclose;
55 if (fseek (wav->f, 32, SEEK_CUR)) {
56 monitor_printf (mon, "wav_destroy: dlen fseek failed\nReason %s\n",
57 strerror (errno));
58 goto doclose;
60 if (fwrite (dlen, 1, 4, wav->f) != 4) {
61 monitor_printf (mon, "wav_destroy: dlen fwrite failed\nReason %s\n",
62 strerror (errno));
63 goto doclose;
65 doclose:
66 if (fclose (wav->f)) {
67 fprintf (stderr, "wav_destroy: fclose failed: %s",
68 strerror (errno));
72 g_free (wav->path);
75 static void wav_capture (void *opaque, void *buf, int size)
77 WAVState *wav = opaque;
79 if (fwrite (buf, size, 1, wav->f) != 1) {
80 monitor_printf (cur_mon, "wav_capture: fwrite error\nReason: %s",
81 strerror (errno));
83 wav->bytes += size;
86 static void wav_capture_destroy (void *opaque)
88 WAVState *wav = opaque;
90 AUD_del_capture (wav->cap, wav);
93 static void wav_capture_info (void *opaque)
95 WAVState *wav = opaque;
96 char *path = wav->path;
98 monitor_printf (cur_mon, "Capturing audio(%d,%d,%d) to %s: %d bytes\n",
99 wav->freq, wav->bits, wav->nchannels,
100 path ? path : "<not available>", wav->bytes);
103 static struct capture_ops wav_capture_ops = {
104 .destroy = wav_capture_destroy,
105 .info = wav_capture_info
108 int wav_start_capture (CaptureState *s, const char *path, int freq,
109 int bits, int nchannels)
111 Monitor *mon = cur_mon;
112 WAVState *wav;
113 uint8_t hdr[] = {
114 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56,
115 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00,
116 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04,
117 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00
119 struct audsettings as;
120 struct audio_capture_ops ops;
121 int stereo, bits16, shift;
122 CaptureVoiceOut *cap;
124 if (bits != 8 && bits != 16) {
125 monitor_printf (mon, "incorrect bit count %d, must be 8 or 16\n", bits);
126 return -1;
129 if (nchannels != 1 && nchannels != 2) {
130 monitor_printf (mon, "incorrect channel count %d, must be 1 or 2\n",
131 nchannels);
132 return -1;
135 stereo = nchannels == 2;
136 bits16 = bits == 16;
138 as.freq = freq;
139 as.nchannels = 1 << stereo;
140 as.fmt = bits16 ? AUD_FMT_S16 : AUD_FMT_U8;
141 as.endianness = 0;
143 ops.notify = wav_notify;
144 ops.capture = wav_capture;
145 ops.destroy = wav_destroy;
147 wav = g_malloc0 (sizeof (*wav));
149 shift = bits16 + stereo;
150 hdr[34] = bits16 ? 0x10 : 0x08;
152 le_store (hdr + 22, as.nchannels, 2);
153 le_store (hdr + 24, freq, 4);
154 le_store (hdr + 28, freq << shift, 4);
155 le_store (hdr + 32, 1 << shift, 2);
157 wav->f = fopen (path, "wb");
158 if (!wav->f) {
159 monitor_printf (mon, "Failed to open wave file `%s'\nReason: %s\n",
160 path, strerror (errno));
161 g_free (wav);
162 return -1;
165 wav->path = g_strdup (path);
166 wav->bits = bits;
167 wav->nchannels = nchannels;
168 wav->freq = freq;
170 if (fwrite (hdr, sizeof (hdr), 1, wav->f) != 1) {
171 monitor_printf (mon, "Failed to write header\nReason: %s\n",
172 strerror (errno));
173 goto error_free;
176 cap = AUD_add_capture (&as, &ops, wav);
177 if (!cap) {
178 monitor_printf (mon, "Failed to add audio capture\n");
179 goto error_free;
182 wav->cap = cap;
183 s->opaque = wav;
184 s->ops = wav_capture_ops;
185 return 0;
187 error_free:
188 g_free (wav->path);
189 if (fclose (wav->f)) {
190 monitor_printf (mon, "Failed to close wave file\nReason: %s\n",
191 strerror (errno));
193 g_free (wav);
194 return -1;