]>
Commit | Line | Data |
---|---|---|
7372f88d | 1 | /* |
1d14ffa9 FB |
2 | * QEMU Timer based audio emulation |
3 | * | |
4 | * Copyright (c) 2004-2005 Vassili Karpov (malc) | |
5 | * | |
7372f88d FB |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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 | |
22 | * THE SOFTWARE. | |
23 | */ | |
0b8fa32f | 24 | |
6086a565 | 25 | #include "qemu/osdep.h" |
87776ab7 | 26 | #include "qemu/host-utils.h" |
0b8fa32f | 27 | #include "qemu/module.h" |
87ecb68b | 28 | #include "audio.h" |
1de7afc9 | 29 | #include "qemu/timer.h" |
7372f88d | 30 | |
1d14ffa9 FB |
31 | #define AUDIO_CAP "noaudio" |
32 | #include "audio_int.h" | |
7372f88d | 33 | |
1d14ffa9 FB |
34 | typedef struct NoVoiceOut { |
35 | HWVoiceOut hw; | |
7372f88d | 36 | int64_t old_ticks; |
1d14ffa9 | 37 | } NoVoiceOut; |
7372f88d | 38 | |
1d14ffa9 FB |
39 | typedef struct NoVoiceIn { |
40 | HWVoiceIn hw; | |
41 | int64_t old_ticks; | |
42 | } NoVoiceIn; | |
7372f88d | 43 | |
bdff253c | 44 | static int no_run_out (HWVoiceOut *hw, int live) |
7372f88d | 45 | { |
1d14ffa9 | 46 | NoVoiceOut *no = (NoVoiceOut *) hw; |
bdff253c | 47 | int decr, samples; |
8ead62cf FB |
48 | int64_t now; |
49 | int64_t ticks; | |
50 | int64_t bytes; | |
7372f88d | 51 | |
bc72ad67 | 52 | now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
8ead62cf | 53 | ticks = now - no->old_ticks; |
73bcb24d RS |
54 | bytes = muldiv64(ticks, hw->info.bytes_per_second, NANOSECONDS_PER_SECOND); |
55 | bytes = audio_MIN(bytes, INT_MAX); | |
8ead62cf FB |
56 | samples = bytes >> hw->info.shift; |
57 | ||
7372f88d FB |
58 | no->old_ticks = now; |
59 | decr = audio_MIN (live, samples); | |
1d14ffa9 FB |
60 | hw->rpos = (hw->rpos + decr) % hw->samples; |
61 | return decr; | |
62 | } | |
7372f88d | 63 | |
1d14ffa9 FB |
64 | static int no_write (SWVoiceOut *sw, void *buf, int len) |
65 | { | |
73bcb24d | 66 | return audio_pcm_sw_write(sw, buf, len); |
1d14ffa9 | 67 | } |
7372f88d | 68 | |
5706db1d | 69 | static int no_init_out(HWVoiceOut *hw, struct audsettings *as, void *drv_opaque) |
1d14ffa9 | 70 | { |
d929eba5 | 71 | audio_pcm_init_info (&hw->info, as); |
c0fe3827 | 72 | hw->samples = 1024; |
1d14ffa9 FB |
73 | return 0; |
74 | } | |
7372f88d | 75 | |
1d14ffa9 FB |
76 | static void no_fini_out (HWVoiceOut *hw) |
77 | { | |
78 | (void) hw; | |
7372f88d FB |
79 | } |
80 | ||
1d14ffa9 | 81 | static int no_ctl_out (HWVoiceOut *hw, int cmd, ...) |
7372f88d | 82 | { |
1d14ffa9 FB |
83 | (void) hw; |
84 | (void) cmd; | |
85 | return 0; | |
7372f88d FB |
86 | } |
87 | ||
5706db1d | 88 | static int no_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque) |
7372f88d | 89 | { |
d929eba5 | 90 | audio_pcm_init_info (&hw->info, as); |
c0fe3827 | 91 | hw->samples = 1024; |
7372f88d FB |
92 | return 0; |
93 | } | |
94 | ||
1d14ffa9 | 95 | static void no_fini_in (HWVoiceIn *hw) |
7372f88d FB |
96 | { |
97 | (void) hw; | |
98 | } | |
99 | ||
1d14ffa9 FB |
100 | static int no_run_in (HWVoiceIn *hw) |
101 | { | |
102 | NoVoiceIn *no = (NoVoiceIn *) hw; | |
1d14ffa9 FB |
103 | int live = audio_pcm_hw_get_live_in (hw); |
104 | int dead = hw->samples - live; | |
ec36b695 | 105 | int samples = 0; |
1d14ffa9 | 106 | |
8ead62cf | 107 | if (dead) { |
bc72ad67 | 108 | int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
8ead62cf | 109 | int64_t ticks = now - no->old_ticks; |
4f4cc0ef | 110 | int64_t bytes = |
73bcb24d | 111 | muldiv64(ticks, hw->info.bytes_per_second, NANOSECONDS_PER_SECOND); |
1d14ffa9 | 112 | |
8ead62cf FB |
113 | no->old_ticks = now; |
114 | bytes = audio_MIN (bytes, INT_MAX); | |
115 | samples = bytes >> hw->info.shift; | |
116 | samples = audio_MIN (samples, dead); | |
117 | } | |
1d14ffa9 FB |
118 | return samples; |
119 | } | |
120 | ||
121 | static int no_read (SWVoiceIn *sw, void *buf, int size) | |
122 | { | |
8a7d0890 MW |
123 | /* use custom code here instead of audio_pcm_sw_read() to avoid |
124 | * useless resampling/mixing */ | |
1d14ffa9 FB |
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); | |
8a7d0890 | 128 | sw->total_hw_samples_acquired += total; |
1d14ffa9 | 129 | audio_pcm_info_clear_buf (&sw->info, buf, to_clear); |
85882c71 | 130 | return to_clear << sw->info.shift; |
1d14ffa9 FB |
131 | } |
132 | ||
133 | static int no_ctl_in (HWVoiceIn *hw, int cmd, ...) | |
7372f88d FB |
134 | { |
135 | (void) hw; | |
136 | (void) cmd; | |
137 | return 0; | |
138 | } | |
139 | ||
71830221 | 140 | static void *no_audio_init(Audiodev *dev) |
7372f88d FB |
141 | { |
142 | return &no_audio_init; | |
143 | } | |
144 | ||
145 | static void no_audio_fini (void *opaque) | |
146 | { | |
1d14ffa9 | 147 | (void) opaque; |
7372f88d FB |
148 | } |
149 | ||
35f4b58c | 150 | static struct audio_pcm_ops no_pcm_ops = { |
1dd3e4d1 JQ |
151 | .init_out = no_init_out, |
152 | .fini_out = no_fini_out, | |
153 | .run_out = no_run_out, | |
154 | .write = no_write, | |
155 | .ctl_out = no_ctl_out, | |
156 | ||
157 | .init_in = no_init_in, | |
158 | .fini_in = no_fini_in, | |
159 | .run_in = no_run_in, | |
160 | .read = no_read, | |
161 | .ctl_in = no_ctl_in | |
7372f88d FB |
162 | }; |
163 | ||
d3893a39 | 164 | static struct audio_driver no_audio_driver = { |
bee37f32 JQ |
165 | .name = "none", |
166 | .descr = "Timer based audio emulation", | |
bee37f32 JQ |
167 | .init = no_audio_init, |
168 | .fini = no_audio_fini, | |
169 | .pcm_ops = &no_pcm_ops, | |
170 | .can_be_default = 1, | |
171 | .max_voices_out = INT_MAX, | |
172 | .max_voices_in = INT_MAX, | |
173 | .voice_size_out = sizeof (NoVoiceOut), | |
174 | .voice_size_in = sizeof (NoVoiceIn) | |
7372f88d | 175 | }; |
d3893a39 GH |
176 | |
177 | static void register_audio_none(void) | |
178 | { | |
179 | audio_driver_register(&no_audio_driver); | |
180 | } | |
181 | type_init(register_audio_none); |