]>
Commit | Line | Data |
---|---|---|
87ecb68b | 1 | #include "hw/hw.h" |
83c9089e | 2 | #include "monitor/monitor.h" |
d49b6836 | 3 | #include "qemu/error-report.h" |
87ecb68b | 4 | #include "audio.h" |
8ead62cf FB |
5 | |
6 | typedef struct { | |
b04df2a4 | 7 | FILE *f; |
8ead62cf | 8 | int bytes; |
ec36b695 FB |
9 | char *path; |
10 | int freq; | |
11 | int bits; | |
12 | int nchannels; | |
13 | CaptureVoiceOut *cap; | |
8ead62cf FB |
14 | } WAVState; |
15 | ||
16 | /* VICE code: Store number as little endian. */ | |
17 | static void le_store (uint8_t *buf, uint32_t val, int len) | |
18 | { | |
19 | int i; | |
20 | for (i = 0; i < len; i++) { | |
21 | buf[i] = (uint8_t) (val & 0xff); | |
22 | val >>= 8; | |
23 | } | |
24 | } | |
25 | ||
ec36b695 | 26 | static void wav_notify (void *opaque, audcnotification_e cmd) |
8ead62cf | 27 | { |
ec36b695 FB |
28 | (void) opaque; |
29 | (void) cmd; | |
30 | } | |
8ead62cf | 31 | |
ec36b695 FB |
32 | static void wav_destroy (void *opaque) |
33 | { | |
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; | |
b04df2a4 | 39 | Monitor *mon = cur_mon; |
8ead62cf | 40 | |
e84a4fed FB |
41 | if (wav->f) { |
42 | le_store (rlen, rifflen, 4); | |
43 | le_store (dlen, datalen, 4); | |
f941aa25 | 44 | |
b04df2a4 JQ |
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; | |
49 | } | |
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; | |
54 | } | |
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; | |
59 | } | |
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; | |
64 | } | |
65 | doclose: | |
66 | if (fclose (wav->f)) { | |
69e99504 | 67 | error_report("wav_destroy: fclose failed: %s", strerror(errno)); |
b04df2a4 | 68 | } |
8ead62cf | 69 | } |
f941aa25 | 70 | |
7267c094 | 71 | g_free (wav->path); |
8ead62cf FB |
72 | } |
73 | ||
ec36b695 | 74 | static void wav_capture (void *opaque, void *buf, int size) |
8ead62cf FB |
75 | { |
76 | WAVState *wav = opaque; | |
77 | ||
b04df2a4 JQ |
78 | if (fwrite (buf, size, 1, wav->f) != 1) { |
79 | monitor_printf (cur_mon, "wav_capture: fwrite error\nReason: %s", | |
80 | strerror (errno)); | |
81 | } | |
8ead62cf FB |
82 | wav->bytes += size; |
83 | } | |
84 | ||
ec36b695 FB |
85 | static void wav_capture_destroy (void *opaque) |
86 | { | |
87 | WAVState *wav = opaque; | |
88 | ||
89 | AUD_del_capture (wav->cap, wav); | |
90 | } | |
91 | ||
92 | static void wav_capture_info (void *opaque) | |
93 | { | |
94 | WAVState *wav = opaque; | |
95 | char *path = wav->path; | |
96 | ||
b04df2a4 JQ |
97 | monitor_printf (cur_mon, "Capturing audio(%d,%d,%d) to %s: %d bytes\n", |
98 | wav->freq, wav->bits, wav->nchannels, | |
99 | path ? path : "<not available>", wav->bytes); | |
ec36b695 FB |
100 | } |
101 | ||
102 | static struct capture_ops wav_capture_ops = { | |
103 | .destroy = wav_capture_destroy, | |
104 | .info = wav_capture_info | |
105 | }; | |
106 | ||
107 | int wav_start_capture (CaptureState *s, const char *path, int freq, | |
108 | int bits, int nchannels) | |
8ead62cf | 109 | { |
376253ec | 110 | Monitor *mon = cur_mon; |
8ead62cf FB |
111 | WAVState *wav; |
112 | uint8_t hdr[] = { | |
113 | 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, | |
114 | 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, | |
115 | 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04, | |
116 | 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00 | |
117 | }; | |
1ea879e5 | 118 | struct audsettings as; |
8ead62cf | 119 | struct audio_capture_ops ops; |
ec36b695 FB |
120 | int stereo, bits16, shift; |
121 | CaptureVoiceOut *cap; | |
122 | ||
123 | if (bits != 8 && bits != 16) { | |
b04df2a4 | 124 | monitor_printf (mon, "incorrect bit count %d, must be 8 or 16\n", bits); |
ec36b695 FB |
125 | return -1; |
126 | } | |
127 | ||
128 | if (nchannels != 1 && nchannels != 2) { | |
b04df2a4 JQ |
129 | monitor_printf (mon, "incorrect channel count %d, must be 1 or 2\n", |
130 | nchannels); | |
ec36b695 FB |
131 | return -1; |
132 | } | |
8ead62cf | 133 | |
ec36b695 FB |
134 | stereo = nchannels == 2; |
135 | bits16 = bits == 16; | |
8ead62cf FB |
136 | |
137 | as.freq = freq; | |
138 | as.nchannels = 1 << stereo; | |
139 | as.fmt = bits16 ? AUD_FMT_S16 : AUD_FMT_U8; | |
d929eba5 | 140 | as.endianness = 0; |
8ead62cf | 141 | |
ec36b695 FB |
142 | ops.notify = wav_notify; |
143 | ops.capture = wav_capture; | |
144 | ops.destroy = wav_destroy; | |
8ead62cf | 145 | |
7267c094 | 146 | wav = g_malloc0 (sizeof (*wav)); |
8ead62cf FB |
147 | |
148 | shift = bits16 + stereo; | |
149 | hdr[34] = bits16 ? 0x10 : 0x08; | |
150 | ||
151 | le_store (hdr + 22, as.nchannels, 2); | |
152 | le_store (hdr + 24, freq, 4); | |
153 | le_store (hdr + 28, freq << shift, 4); | |
154 | le_store (hdr + 32, 1 << shift, 2); | |
155 | ||
b04df2a4 | 156 | wav->f = fopen (path, "wb"); |
8ead62cf | 157 | if (!wav->f) { |
b04df2a4 JQ |
158 | monitor_printf (mon, "Failed to open wave file `%s'\nReason: %s\n", |
159 | path, strerror (errno)); | |
7267c094 | 160 | g_free (wav); |
ec36b695 | 161 | return -1; |
8ead62cf FB |
162 | } |
163 | ||
7267c094 | 164 | wav->path = g_strdup (path); |
ec36b695 FB |
165 | wav->bits = bits; |
166 | wav->nchannels = nchannels; | |
167 | wav->freq = freq; | |
168 | ||
b04df2a4 JQ |
169 | if (fwrite (hdr, sizeof (hdr), 1, wav->f) != 1) { |
170 | monitor_printf (mon, "Failed to write header\nReason: %s\n", | |
171 | strerror (errno)); | |
172 | goto error_free; | |
173 | } | |
ec36b695 | 174 | |
1a7dafce | 175 | cap = AUD_add_capture (&as, &ops, wav); |
ec36b695 | 176 | if (!cap) { |
b04df2a4 JQ |
177 | monitor_printf (mon, "Failed to add audio capture\n"); |
178 | goto error_free; | |
ec36b695 FB |
179 | } |
180 | ||
181 | wav->cap = cap; | |
182 | s->opaque = wav; | |
183 | s->ops = wav_capture_ops; | |
184 | return 0; | |
b04df2a4 JQ |
185 | |
186 | error_free: | |
187 | g_free (wav->path); | |
188 | if (fclose (wav->f)) { | |
189 | monitor_printf (mon, "Failed to close wave file\nReason: %s\n", | |
190 | strerror (errno)); | |
191 | } | |
192 | g_free (wav); | |
193 | return -1; | |
8ead62cf | 194 | } |