]> Git Repo - qemu.git/blame - audio/wavcapture.c
Take advantage of subpage support.
[qemu.git] / audio / wavcapture.c
CommitLineData
8ead62cf
FB
1#include "vl.h"
2
3typedef struct {
4 QEMUFile *f;
5 int bytes;
ec36b695
FB
6 char *path;
7 int freq;
8 int bits;
9 int nchannels;
10 CaptureVoiceOut *cap;
8ead62cf
FB
11} WAVState;
12
13/* VICE code: Store number as little endian. */
14static void le_store (uint8_t *buf, uint32_t val, int len)
15{
16 int i;
17 for (i = 0; i < len; i++) {
18 buf[i] = (uint8_t) (val & 0xff);
19 val >>= 8;
20 }
21}
22
ec36b695 23static void wav_notify (void *opaque, audcnotification_e cmd)
8ead62cf 24{
ec36b695
FB
25 (void) opaque;
26 (void) cmd;
27}
8ead62cf 28
ec36b695
FB
29static void wav_destroy (void *opaque)
30{
31 WAVState *wav = opaque;
32 uint8_t rlen[4];
33 uint8_t dlen[4];
34 uint32_t datalen = wav->bytes;
35 uint32_t rifflen = datalen + 36;
8ead62cf 36
e84a4fed
FB
37 if (wav->f) {
38 le_store (rlen, rifflen, 4);
39 le_store (dlen, datalen, 4);
f941aa25 40
e84a4fed
FB
41 qemu_fseek (wav->f, 4, SEEK_SET);
42 qemu_put_buffer (wav->f, rlen, 4);
f941aa25 43
e84a4fed
FB
44 qemu_fseek (wav->f, 32, SEEK_CUR);
45 qemu_put_buffer (wav->f, dlen, 4);
46 qemu_fclose (wav->f);
8ead62cf 47 }
f941aa25 48
e84a4fed 49 qemu_free (wav->path);
8ead62cf
FB
50}
51
ec36b695 52static void wav_capture (void *opaque, void *buf, int size)
8ead62cf
FB
53{
54 WAVState *wav = opaque;
55
56 qemu_put_buffer (wav->f, buf, size);
57 wav->bytes += size;
58}
59
ec36b695
FB
60static void wav_capture_destroy (void *opaque)
61{
62 WAVState *wav = opaque;
63
64 AUD_del_capture (wav->cap, wav);
65}
66
67static void wav_capture_info (void *opaque)
68{
69 WAVState *wav = opaque;
70 char *path = wav->path;
71
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);
75}
76
77static struct capture_ops wav_capture_ops = {
78 .destroy = wav_capture_destroy,
79 .info = wav_capture_info
80};
81
82int wav_start_capture (CaptureState *s, const char *path, int freq,
83 int bits, int nchannels)
8ead62cf
FB
84{
85 WAVState *wav;
86 uint8_t hdr[] = {
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
91 };
92 audsettings_t as;
93 struct audio_capture_ops ops;
ec36b695
FB
94 int stereo, bits16, shift;
95 CaptureVoiceOut *cap;
96
97 if (bits != 8 && bits != 16) {
98 term_printf ("incorrect bit count %d, must be 8 or 16\n", bits);
99 return -1;
100 }
101
102 if (nchannels != 1 && nchannels != 2) {
47dbd1f3
FB
103 term_printf ("incorrect channel count %d, must be 1 or 2\n",
104 nchannels);
ec36b695
FB
105 return -1;
106 }
8ead62cf 107
ec36b695
FB
108 stereo = nchannels == 2;
109 bits16 = bits == 16;
8ead62cf
FB
110
111 as.freq = freq;
112 as.nchannels = 1 << stereo;
113 as.fmt = bits16 ? AUD_FMT_S16 : AUD_FMT_U8;
d929eba5 114 as.endianness = 0;
8ead62cf 115
ec36b695
FB
116 ops.notify = wav_notify;
117 ops.capture = wav_capture;
118 ops.destroy = wav_destroy;
8ead62cf
FB
119
120 wav = qemu_mallocz (sizeof (*wav));
121 if (!wav) {
a3c25997
FB
122 term_printf ("Could not allocate memory for wav capture (%zu bytes)",
123 sizeof (*wav));
ec36b695 124 return -1;
8ead62cf
FB
125 }
126
127 shift = bits16 + stereo;
128 hdr[34] = bits16 ? 0x10 : 0x08;
129
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);
134
e70332b3 135 wav->f = qemu_fopen (path, "wb");
8ead62cf 136 if (!wav->f) {
ec36b695
FB
137 term_printf ("Failed to open wave file `%s'\nReason: %s\n",
138 path, strerror (errno));
8ead62cf 139 qemu_free (wav);
ec36b695 140 return -1;
8ead62cf
FB
141 }
142
ec36b695
FB
143 wav->path = qemu_strdup (path);
144 wav->bits = bits;
145 wav->nchannels = nchannels;
146 wav->freq = freq;
147
8ead62cf 148 qemu_put_buffer (wav->f, hdr, sizeof (hdr));
ec36b695
FB
149
150 cap = AUD_add_capture (NULL, &as, &ops, wav);
151 if (!cap) {
152 term_printf ("Failed to add audio capture\n");
e84a4fed
FB
153 qemu_free (wav->path);
154 qemu_fclose (wav->f);
ec36b695
FB
155 qemu_free (wav);
156 return -1;
157 }
158
159 wav->cap = cap;
160 s->opaque = wav;
161 s->ops = wav_capture_ops;
162 return 0;
8ead62cf 163}
This page took 0.062906 seconds and 4 git commands to generate.