]> git.proxmox.com Git - qemu.git/blame - audio/wavcapture.c
slirp: Fix restricted mode
[qemu.git] / audio / wavcapture.c
CommitLineData
87ecb68b 1#include "hw/hw.h"
376253ec 2#include "monitor.h"
87ecb68b 3#include "audio.h"
8ead62cf
FB
4
5typedef struct {
6 QEMUFile *f;
7 int bytes;
ec36b695
FB
8 char *path;
9 int freq;
10 int bits;
11 int nchannels;
12 CaptureVoiceOut *cap;
8ead62cf
FB
13} 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
ec36b695 25static void wav_notify (void *opaque, audcnotification_e cmd)
8ead62cf 26{
ec36b695
FB
27 (void) opaque;
28 (void) cmd;
29}
8ead62cf 30
ec36b695
FB
31static void wav_destroy (void *opaque)
32{
33 WAVState *wav = opaque;
34 uint8_t rlen[4];
35 uint8_t dlen[4];
36 uint32_t datalen = wav->bytes;
37 uint32_t rifflen = datalen + 36;
8ead62cf 38
e84a4fed
FB
39 if (wav->f) {
40 le_store (rlen, rifflen, 4);
41 le_store (dlen, datalen, 4);
f941aa25 42
e84a4fed
FB
43 qemu_fseek (wav->f, 4, SEEK_SET);
44 qemu_put_buffer (wav->f, rlen, 4);
f941aa25 45
e84a4fed
FB
46 qemu_fseek (wav->f, 32, SEEK_CUR);
47 qemu_put_buffer (wav->f, dlen, 4);
48 qemu_fclose (wav->f);
8ead62cf 49 }
f941aa25 50
e84a4fed 51 qemu_free (wav->path);
8ead62cf
FB
52}
53
ec36b695 54static void wav_capture (void *opaque, void *buf, int size)
8ead62cf
FB
55{
56 WAVState *wav = opaque;
57
58 qemu_put_buffer (wav->f, buf, size);
59 wav->bytes += size;
60}
61
ec36b695
FB
62static 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
376253ec
AL
74 monitor_printf(cur_mon, "Capturing audio(%d,%d,%d) to %s: %d bytes\n",
75 wav->freq, wav->bits, wav->nchannels,
76 path ? path : "<not available>", wav->bytes);
ec36b695
FB
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)
8ead62cf 86{
376253ec 87 Monitor *mon = cur_mon;
8ead62cf
FB
88 WAVState *wav;
89 uint8_t hdr[] = {
90 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56,
91 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00,
92 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04,
93 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00
94 };
1ea879e5 95 struct audsettings as;
8ead62cf 96 struct audio_capture_ops ops;
ec36b695
FB
97 int stereo, bits16, shift;
98 CaptureVoiceOut *cap;
99
100 if (bits != 8 && bits != 16) {
376253ec 101 monitor_printf(mon, "incorrect bit count %d, must be 8 or 16\n", bits);
ec36b695
FB
102 return -1;
103 }
104
105 if (nchannels != 1 && nchannels != 2) {
376253ec
AL
106 monitor_printf(mon, "incorrect channel count %d, must be 1 or 2\n",
107 nchannels);
ec36b695
FB
108 return -1;
109 }
8ead62cf 110
ec36b695
FB
111 stereo = nchannels == 2;
112 bits16 = bits == 16;
8ead62cf
FB
113
114 as.freq = freq;
115 as.nchannels = 1 << stereo;
116 as.fmt = bits16 ? AUD_FMT_S16 : AUD_FMT_U8;
d929eba5 117 as.endianness = 0;
8ead62cf 118
ec36b695
FB
119 ops.notify = wav_notify;
120 ops.capture = wav_capture;
121 ops.destroy = wav_destroy;
8ead62cf
FB
122
123 wav = qemu_mallocz (sizeof (*wav));
8ead62cf
FB
124
125 shift = bits16 + stereo;
126 hdr[34] = bits16 ? 0x10 : 0x08;
127
128 le_store (hdr + 22, as.nchannels, 2);
129 le_store (hdr + 24, freq, 4);
130 le_store (hdr + 28, freq << shift, 4);
131 le_store (hdr + 32, 1 << shift, 2);
132
e70332b3 133 wav->f = qemu_fopen (path, "wb");
8ead62cf 134 if (!wav->f) {
376253ec
AL
135 monitor_printf(mon, "Failed to open wave file `%s'\nReason: %s\n",
136 path, strerror (errno));
8ead62cf 137 qemu_free (wav);
ec36b695 138 return -1;
8ead62cf
FB
139 }
140
ec36b695
FB
141 wav->path = qemu_strdup (path);
142 wav->bits = bits;
143 wav->nchannels = nchannels;
144 wav->freq = freq;
145
8ead62cf 146 qemu_put_buffer (wav->f, hdr, sizeof (hdr));
ec36b695 147
1a7dafce 148 cap = AUD_add_capture (&as, &ops, wav);
ec36b695 149 if (!cap) {
376253ec 150 monitor_printf(mon, "Failed to add audio capture\n");
e84a4fed
FB
151 qemu_free (wav->path);
152 qemu_fclose (wav->f);
ec36b695
FB
153 qemu_free (wav);
154 return -1;
155 }
156
157 wav->cap = cap;
158 s->opaque = wav;
159 s->ops = wav_capture_ops;
160 return 0;
8ead62cf 161}