2 * QEMU Timer based audio emulation
4 * Copyright (c) 2004-2005 Vassili Karpov (malc)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qemu/host-utils.h"
27 #include "qemu/module.h"
29 #include "qemu/timer.h"
31 #define AUDIO_CAP "noaudio"
32 #include "audio_int.h"
34 typedef struct NoVoiceOut {
39 typedef struct NoVoiceIn {
44 static int no_run_out (HWVoiceOut *hw, int live)
46 NoVoiceOut *no = (NoVoiceOut *) hw;
52 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
53 ticks = now - no->old_ticks;
54 bytes = muldiv64(ticks, hw->info.bytes_per_second, NANOSECONDS_PER_SECOND);
55 bytes = audio_MIN(bytes, INT_MAX);
56 samples = bytes >> hw->info.shift;
59 decr = audio_MIN (live, samples);
60 hw->rpos = (hw->rpos + decr) % hw->samples;
64 static int no_write (SWVoiceOut *sw, void *buf, int len)
66 return audio_pcm_sw_write(sw, buf, len);
69 static int no_init_out(HWVoiceOut *hw, struct audsettings *as, void *drv_opaque)
71 audio_pcm_init_info (&hw->info, as);
76 static void no_fini_out (HWVoiceOut *hw)
81 static int no_ctl_out (HWVoiceOut *hw, int cmd, ...)
88 static int no_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
90 audio_pcm_init_info (&hw->info, as);
95 static void no_fini_in (HWVoiceIn *hw)
100 static int no_run_in (HWVoiceIn *hw)
102 NoVoiceIn *no = (NoVoiceIn *) hw;
103 int live = audio_pcm_hw_get_live_in (hw);
104 int dead = hw->samples - live;
108 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
109 int64_t ticks = now - no->old_ticks;
111 muldiv64(ticks, hw->info.bytes_per_second, NANOSECONDS_PER_SECOND);
114 bytes = audio_MIN (bytes, INT_MAX);
115 samples = bytes >> hw->info.shift;
116 samples = audio_MIN (samples, dead);
121 static int no_read (SWVoiceIn *sw, void *buf, int size)
123 /* use custom code here instead of audio_pcm_sw_read() to avoid
124 * useless resampling/mixing */
125 int samples = size >> sw->info.shift;
126 int total = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
127 int to_clear = audio_MIN (samples, total);
128 sw->total_hw_samples_acquired += total;
129 audio_pcm_info_clear_buf (&sw->info, buf, to_clear);
130 return to_clear << sw->info.shift;
133 static int no_ctl_in (HWVoiceIn *hw, int cmd, ...)
140 static void *no_audio_init(Audiodev *dev)
142 return &no_audio_init;
145 static void no_audio_fini (void *opaque)
150 static struct audio_pcm_ops no_pcm_ops = {
151 .init_out = no_init_out,
152 .fini_out = no_fini_out,
153 .run_out = no_run_out,
155 .ctl_out = no_ctl_out,
157 .init_in = no_init_in,
158 .fini_in = no_fini_in,
164 static struct audio_driver no_audio_driver = {
166 .descr = "Timer based audio emulation",
167 .init = no_audio_init,
168 .fini = no_audio_fini,
169 .pcm_ops = &no_pcm_ops,
171 .max_voices_out = INT_MAX,
172 .max_voices_in = INT_MAX,
173 .voice_size_out = sizeof (NoVoiceOut),
174 .voice_size_in = sizeof (NoVoiceIn)
177 static void register_audio_none(void)
179 audio_driver_register(&no_audio_driver);
181 type_init(register_audio_none);