target-arm: A64: Add saturating int ops (SQNEG/SQABS)
[qemu/ar7.git] / audio / spiceaudio.c
blobfceee50adbc13297ff9a514ba08aef6a12d933ba
1 /*
2 * Copyright (C) 2010 Red Hat, Inc.
4 * maintained by Gerd Hoffmann <kraxel@redhat.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 or
9 * (at your option) version 3 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "hw/hw.h"
21 #include "qemu/timer.h"
22 #include "ui/qemu-spice.h"
24 #define AUDIO_CAP "spice"
25 #include "audio.h"
26 #include "audio_int.h"
28 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
29 #define LINE_OUT_SAMPLES (480 * 4)
30 #else
31 #define LINE_OUT_SAMPLES (256 * 4)
32 #endif
34 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
35 #define LINE_IN_SAMPLES (480 * 4)
36 #else
37 #define LINE_IN_SAMPLES (256 * 4)
38 #endif
40 typedef struct SpiceRateCtl {
41 int64_t start_ticks;
42 int64_t bytes_sent;
43 } SpiceRateCtl;
45 typedef struct SpiceVoiceOut {
46 HWVoiceOut hw;
47 SpicePlaybackInstance sin;
48 SpiceRateCtl rate;
49 int active;
50 uint32_t *frame;
51 uint32_t *fpos;
52 uint32_t fsize;
53 } SpiceVoiceOut;
55 typedef struct SpiceVoiceIn {
56 HWVoiceIn hw;
57 SpiceRecordInstance sin;
58 SpiceRateCtl rate;
59 int active;
60 uint32_t samples[LINE_IN_SAMPLES];
61 } SpiceVoiceIn;
63 static const SpicePlaybackInterface playback_sif = {
64 .base.type = SPICE_INTERFACE_PLAYBACK,
65 .base.description = "playback",
66 .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR,
67 .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR,
70 static const SpiceRecordInterface record_sif = {
71 .base.type = SPICE_INTERFACE_RECORD,
72 .base.description = "record",
73 .base.major_version = SPICE_INTERFACE_RECORD_MAJOR,
74 .base.minor_version = SPICE_INTERFACE_RECORD_MINOR,
77 static void *spice_audio_init (void)
79 if (!using_spice) {
80 return NULL;
82 return &spice_audio_init;
85 static void spice_audio_fini (void *opaque)
87 /* nothing */
90 static void rate_start (SpiceRateCtl *rate)
92 memset (rate, 0, sizeof (*rate));
93 rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
96 static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
98 int64_t now;
99 int64_t ticks;
100 int64_t bytes;
101 int64_t samples;
103 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
104 ticks = now - rate->start_ticks;
105 bytes = muldiv64 (ticks, info->bytes_per_second, get_ticks_per_sec ());
106 samples = (bytes - rate->bytes_sent) >> info->shift;
107 if (samples < 0 || samples > 65536) {
108 fprintf (stderr, "Resetting rate control (%" PRId64 " samples)\n", samples);
109 rate_start (rate);
110 samples = 0;
112 rate->bytes_sent += samples << info->shift;
113 return samples;
116 /* playback */
118 static int line_out_init (HWVoiceOut *hw, struct audsettings *as)
120 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
121 struct audsettings settings;
123 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
124 settings.freq = spice_server_get_best_playback_rate(NULL);
125 #else
126 settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
127 #endif
128 settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
129 settings.fmt = AUD_FMT_S16;
130 settings.endianness = AUDIO_HOST_ENDIANNESS;
132 audio_pcm_init_info (&hw->info, &settings);
133 hw->samples = LINE_OUT_SAMPLES;
134 out->active = 0;
136 out->sin.base.sif = &playback_sif.base;
137 qemu_spice_add_interface (&out->sin.base);
138 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
139 spice_server_set_playback_rate(&out->sin, settings.freq);
140 #endif
141 return 0;
144 static void line_out_fini (HWVoiceOut *hw)
146 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
148 spice_server_remove_interface (&out->sin.base);
151 static int line_out_run (HWVoiceOut *hw, int live)
153 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
154 int rpos, decr;
155 int samples;
157 if (!live) {
158 return 0;
161 decr = rate_get_samples (&hw->info, &out->rate);
162 decr = audio_MIN (live, decr);
164 samples = decr;
165 rpos = hw->rpos;
166 while (samples) {
167 int left_till_end_samples = hw->samples - rpos;
168 int len = audio_MIN (samples, left_till_end_samples);
170 if (!out->frame) {
171 spice_server_playback_get_buffer (&out->sin, &out->frame, &out->fsize);
172 out->fpos = out->frame;
174 if (out->frame) {
175 len = audio_MIN (len, out->fsize);
176 hw->clip (out->fpos, hw->mix_buf + rpos, len);
177 out->fsize -= len;
178 out->fpos += len;
179 if (out->fsize == 0) {
180 spice_server_playback_put_samples (&out->sin, out->frame);
181 out->frame = out->fpos = NULL;
184 rpos = (rpos + len) % hw->samples;
185 samples -= len;
187 hw->rpos = rpos;
188 return decr;
191 static int line_out_write (SWVoiceOut *sw, void *buf, int len)
193 return audio_pcm_sw_write (sw, buf, len);
196 static int line_out_ctl (HWVoiceOut *hw, int cmd, ...)
198 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
200 switch (cmd) {
201 case VOICE_ENABLE:
202 if (out->active) {
203 break;
205 out->active = 1;
206 rate_start (&out->rate);
207 spice_server_playback_start (&out->sin);
208 break;
209 case VOICE_DISABLE:
210 if (!out->active) {
211 break;
213 out->active = 0;
214 if (out->frame) {
215 memset (out->fpos, 0, out->fsize << 2);
216 spice_server_playback_put_samples (&out->sin, out->frame);
217 out->frame = out->fpos = NULL;
219 spice_server_playback_stop (&out->sin);
220 break;
221 case VOICE_VOLUME:
223 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
224 SWVoiceOut *sw;
225 va_list ap;
226 uint16_t vol[2];
228 va_start (ap, cmd);
229 sw = va_arg (ap, SWVoiceOut *);
230 va_end (ap);
232 vol[0] = sw->vol.l / ((1ULL << 16) + 1);
233 vol[1] = sw->vol.r / ((1ULL << 16) + 1);
234 spice_server_playback_set_volume (&out->sin, 2, vol);
235 spice_server_playback_set_mute (&out->sin, sw->vol.mute);
236 #endif
237 break;
241 return 0;
244 /* record */
246 static int line_in_init (HWVoiceIn *hw, struct audsettings *as)
248 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
249 struct audsettings settings;
251 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
252 settings.freq = spice_server_get_best_record_rate(NULL);
253 #else
254 settings.freq = SPICE_INTERFACE_RECORD_FREQ;
255 #endif
256 settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
257 settings.fmt = AUD_FMT_S16;
258 settings.endianness = AUDIO_HOST_ENDIANNESS;
260 audio_pcm_init_info (&hw->info, &settings);
261 hw->samples = LINE_IN_SAMPLES;
262 in->active = 0;
264 in->sin.base.sif = &record_sif.base;
265 qemu_spice_add_interface (&in->sin.base);
266 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
267 spice_server_set_record_rate(&in->sin, settings.freq);
268 #endif
269 return 0;
272 static void line_in_fini (HWVoiceIn *hw)
274 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
276 spice_server_remove_interface (&in->sin.base);
279 static int line_in_run (HWVoiceIn *hw)
281 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
282 int num_samples;
283 int ready;
284 int len[2];
285 uint64_t delta_samp;
286 const uint32_t *samples;
288 if (!(num_samples = hw->samples - audio_pcm_hw_get_live_in (hw))) {
289 return 0;
292 delta_samp = rate_get_samples (&hw->info, &in->rate);
293 num_samples = audio_MIN (num_samples, delta_samp);
295 ready = spice_server_record_get_samples (&in->sin, in->samples, num_samples);
296 samples = in->samples;
297 if (ready == 0) {
298 static const uint32_t silence[LINE_IN_SAMPLES];
299 samples = silence;
300 ready = LINE_IN_SAMPLES;
303 num_samples = audio_MIN (ready, num_samples);
305 if (hw->wpos + num_samples > hw->samples) {
306 len[0] = hw->samples - hw->wpos;
307 len[1] = num_samples - len[0];
308 } else {
309 len[0] = num_samples;
310 len[1] = 0;
313 hw->conv (hw->conv_buf + hw->wpos, samples, len[0]);
315 if (len[1]) {
316 hw->conv (hw->conv_buf, samples + len[0], len[1]);
319 hw->wpos = (hw->wpos + num_samples) % hw->samples;
321 return num_samples;
324 static int line_in_read (SWVoiceIn *sw, void *buf, int size)
326 return audio_pcm_sw_read (sw, buf, size);
329 static int line_in_ctl (HWVoiceIn *hw, int cmd, ...)
331 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
333 switch (cmd) {
334 case VOICE_ENABLE:
335 if (in->active) {
336 break;
338 in->active = 1;
339 rate_start (&in->rate);
340 spice_server_record_start (&in->sin);
341 break;
342 case VOICE_DISABLE:
343 if (!in->active) {
344 break;
346 in->active = 0;
347 spice_server_record_stop (&in->sin);
348 break;
349 case VOICE_VOLUME:
351 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
352 SWVoiceIn *sw;
353 va_list ap;
354 uint16_t vol[2];
356 va_start (ap, cmd);
357 sw = va_arg (ap, SWVoiceIn *);
358 va_end (ap);
360 vol[0] = sw->vol.l / ((1ULL << 16) + 1);
361 vol[1] = sw->vol.r / ((1ULL << 16) + 1);
362 spice_server_record_set_volume (&in->sin, 2, vol);
363 spice_server_record_set_mute (&in->sin, sw->vol.mute);
364 #endif
365 break;
369 return 0;
372 static struct audio_option audio_options[] = {
373 { /* end of list */ },
376 static struct audio_pcm_ops audio_callbacks = {
377 .init_out = line_out_init,
378 .fini_out = line_out_fini,
379 .run_out = line_out_run,
380 .write = line_out_write,
381 .ctl_out = line_out_ctl,
383 .init_in = line_in_init,
384 .fini_in = line_in_fini,
385 .run_in = line_in_run,
386 .read = line_in_read,
387 .ctl_in = line_in_ctl,
390 struct audio_driver spice_audio_driver = {
391 .name = "spice",
392 .descr = "spice audio driver",
393 .options = audio_options,
394 .init = spice_audio_init,
395 .fini = spice_audio_fini,
396 .pcm_ops = &audio_callbacks,
397 .max_voices_out = 1,
398 .max_voices_in = 1,
399 .voice_size_out = sizeof (SpiceVoiceOut),
400 .voice_size_in = sizeof (SpiceVoiceIn),
401 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
402 .ctl_caps = VOICE_VOLUME_CAP
403 #endif
406 void qemu_spice_audio_init (void)
408 spice_audio_driver.can_be_default = 1;