/src/qemu/audio/wavcapture.c
Line | Count | Source |
1 | | #include "qemu/osdep.h" |
2 | | #include "qemu/audio.h" |
3 | | #include "qemu/qemu-print.h" |
4 | | #include "qemu/error-report.h" |
5 | | #include "audio_int.h" |
6 | | |
7 | | typedef struct { |
8 | | FILE *f; |
9 | | int bytes; |
10 | | char *path; |
11 | | int freq; |
12 | | int bits; |
13 | | int nchannels; |
14 | | AudioBackend *audio_be; |
15 | | CaptureVoiceOut *cap; |
16 | | } WAVState; |
17 | | |
18 | | /* VICE code: Store number as little endian. */ |
19 | | static void le_store (uint8_t *buf, uint32_t val, int len) |
20 | 0 | { |
21 | 0 | int i; |
22 | 0 | for (i = 0; i < len; i++) { |
23 | 0 | buf[i] = (uint8_t) (val & 0xff); |
24 | 0 | val >>= 8; |
25 | 0 | } |
26 | 0 | } |
27 | | |
28 | | static void wav_notify (void *opaque, audcnotification_e cmd) |
29 | 0 | { |
30 | 0 | (void) opaque; |
31 | 0 | (void) cmd; |
32 | 0 | } |
33 | | |
34 | | static void wav_destroy (void *opaque) |
35 | 0 | { |
36 | 0 | WAVState *wav = opaque; |
37 | 0 | uint8_t rlen[4]; |
38 | 0 | uint8_t dlen[4]; |
39 | 0 | uint32_t datalen = wav->bytes; |
40 | 0 | uint32_t rifflen = datalen + 36; |
41 | |
|
42 | 0 | if (wav->f) { |
43 | 0 | le_store (rlen, rifflen, 4); |
44 | 0 | le_store (dlen, datalen, 4); |
45 | |
|
46 | 0 | if (fseek (wav->f, 4, SEEK_SET)) { |
47 | 0 | error_report("wav_destroy: rlen fseek failed: %s", |
48 | 0 | strerror(errno)); |
49 | 0 | goto doclose; |
50 | 0 | } |
51 | 0 | if (fwrite (rlen, 4, 1, wav->f) != 1) { |
52 | 0 | error_report("wav_destroy: rlen fwrite failed: %s", |
53 | 0 | strerror(errno)); |
54 | 0 | goto doclose; |
55 | 0 | } |
56 | 0 | if (fseek (wav->f, 32, SEEK_CUR)) { |
57 | 0 | error_report("wav_destroy: dlen fseek failed: %s", |
58 | 0 | strerror(errno)); |
59 | 0 | goto doclose; |
60 | 0 | } |
61 | 0 | if (fwrite (dlen, 1, 4, wav->f) != 4) { |
62 | 0 | error_report("wav_destroy: dlen fwrite failed: %s", |
63 | 0 | strerror(errno)); |
64 | 0 | goto doclose; |
65 | 0 | } |
66 | 0 | doclose: |
67 | 0 | if (fclose (wav->f)) { |
68 | 0 | error_report("wav_destroy: fclose failed: %s", strerror(errno)); |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | 0 | g_free (wav->path); |
73 | 0 | } |
74 | | |
75 | | static void wav_capture(void *opaque, const void *buf, int size) |
76 | 0 | { |
77 | 0 | WAVState *wav = opaque; |
78 | |
|
79 | 0 | if (fwrite (buf, size, 1, wav->f) != 1) { |
80 | 0 | error_report("wav_capture: fwrite error: %s", strerror(errno)); |
81 | 0 | } |
82 | 0 | wav->bytes += size; |
83 | 0 | } |
84 | | |
85 | | static void wav_capture_destroy (void *opaque) |
86 | 0 | { |
87 | 0 | WAVState *wav = opaque; |
88 | |
|
89 | 0 | audio_be_del_capture(wav->audio_be, wav->cap, wav); |
90 | 0 | g_free (wav); |
91 | 0 | } |
92 | | |
93 | | static void wav_capture_info (void *opaque) |
94 | 0 | { |
95 | 0 | WAVState *wav = opaque; |
96 | 0 | char *path = wav->path; |
97 | |
|
98 | 0 | qemu_printf("Capturing audio(%d,%d,%d) to %s: %d bytes\n", |
99 | 0 | wav->freq, wav->bits, wav->nchannels, |
100 | 0 | path ? path : "<not available>", wav->bytes); |
101 | 0 | } |
102 | | |
103 | | static struct capture_ops wav_capture_ops = { |
104 | | .destroy = wav_capture_destroy, |
105 | | .info = wav_capture_info |
106 | | }; |
107 | | |
108 | | int wav_start_capture(AudioBackend *state, CaptureState *s, const char *path, |
109 | | int freq, int bits, int nchannels) |
110 | 0 | { |
111 | 0 | WAVState *wav; |
112 | 0 | uint8_t hdr[] = { |
113 | 0 | 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, |
114 | 0 | 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, |
115 | 0 | 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04, |
116 | 0 | 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00 |
117 | 0 | }; |
118 | 0 | struct audsettings as; |
119 | 0 | struct audio_capture_ops ops; |
120 | 0 | int stereo, bits16, shift; |
121 | 0 | CaptureVoiceOut *cap; |
122 | |
|
123 | 0 | if (bits != 8 && bits != 16) { |
124 | 0 | error_report("incorrect bit count %d, must be 8 or 16", bits); |
125 | 0 | return -1; |
126 | 0 | } |
127 | | |
128 | 0 | if (nchannels != 1 && nchannels != 2) { |
129 | 0 | error_report("incorrect channel count %d, must be 1 or 2", |
130 | 0 | nchannels); |
131 | 0 | return -1; |
132 | 0 | } |
133 | | |
134 | 0 | if (freq <= 0 || freq > 384000) { |
135 | 0 | error_report("invalid frequency %d, must be between 1 and 384000", |
136 | 0 | freq); |
137 | 0 | return -1; |
138 | 0 | } |
139 | | |
140 | 0 | stereo = nchannels == 2; |
141 | 0 | bits16 = bits == 16; |
142 | |
|
143 | 0 | as.freq = freq; |
144 | 0 | as.nchannels = 1 << stereo; |
145 | 0 | as.fmt = bits16 ? AUDIO_FORMAT_S16 : AUDIO_FORMAT_U8; |
146 | 0 | as.big_endian = false; |
147 | |
|
148 | 0 | ops.notify = wav_notify; |
149 | 0 | ops.capture = wav_capture; |
150 | 0 | ops.destroy = wav_destroy; |
151 | |
|
152 | 0 | wav = g_malloc0 (sizeof (*wav)); |
153 | |
|
154 | 0 | shift = bits16 + stereo; |
155 | 0 | hdr[34] = bits16 ? 0x10 : 0x08; |
156 | |
|
157 | 0 | le_store (hdr + 22, as.nchannels, 2); |
158 | 0 | le_store (hdr + 24, freq, 4); |
159 | 0 | le_store (hdr + 28, freq << shift, 4); |
160 | 0 | le_store (hdr + 32, 1 << shift, 2); |
161 | |
|
162 | 0 | wav->f = fopen (path, "wb"); |
163 | 0 | if (!wav->f) { |
164 | 0 | error_report("Failed to open wave file `%s': %s", |
165 | 0 | path, strerror(errno)); |
166 | 0 | g_free (wav); |
167 | 0 | return -1; |
168 | 0 | } |
169 | | |
170 | 0 | wav->audio_be = state; |
171 | 0 | wav->path = g_strdup (path); |
172 | 0 | wav->bits = bits; |
173 | 0 | wav->nchannels = nchannels; |
174 | 0 | wav->freq = freq; |
175 | |
|
176 | 0 | if (fwrite (hdr, sizeof (hdr), 1, wav->f) != 1) { |
177 | 0 | error_report("Failed to write header: %s", strerror(errno)); |
178 | 0 | goto error_free; |
179 | 0 | } |
180 | | |
181 | 0 | cap = audio_be_add_capture(wav->audio_be, &as, &ops, wav); |
182 | 0 | if (!cap) { |
183 | 0 | error_report("Failed to add audio capture"); |
184 | 0 | goto error_free; |
185 | 0 | } |
186 | | |
187 | 0 | wav->cap = cap; |
188 | 0 | s->opaque = wav; |
189 | 0 | s->ops = wav_capture_ops; |
190 | 0 | return 0; |
191 | | |
192 | 0 | error_free: |
193 | 0 | g_free (wav->path); |
194 | 0 | if (fclose (wav->f)) { |
195 | | error_report("Failed to close wave file: %s", strerror(errno)); |
196 | 0 | } |
197 | 0 | g_free (wav); |
198 | 0 | return -1; |
199 | 0 | } |