]> git.proxmox.com Git - mirror_qemu.git/blame - audio/noaudio.c
Replaced get_tick_per_sec() by NANOSECONDS_PER_SECOND
[mirror_qemu.git] / audio / noaudio.c
CommitLineData
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 */
6086a565 24#include "qemu/osdep.h"
87ecb68b
PB
25#include "qemu-common.h"
26#include "audio.h"
1de7afc9 27#include "qemu/timer.h"
7372f88d 28
1d14ffa9
FB
29#define AUDIO_CAP "noaudio"
30#include "audio_int.h"
7372f88d 31
1d14ffa9
FB
32typedef struct NoVoiceOut {
33 HWVoiceOut hw;
7372f88d 34 int64_t old_ticks;
1d14ffa9 35} NoVoiceOut;
7372f88d 36
1d14ffa9
FB
37typedef struct NoVoiceIn {
38 HWVoiceIn hw;
39 int64_t old_ticks;
40} NoVoiceIn;
7372f88d 41
bdff253c 42static int no_run_out (HWVoiceOut *hw, int live)
7372f88d 43{
1d14ffa9 44 NoVoiceOut *no = (NoVoiceOut *) hw;
bdff253c 45 int decr, samples;
8ead62cf
FB
46 int64_t now;
47 int64_t ticks;
48 int64_t bytes;
7372f88d 49
bc72ad67 50 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
8ead62cf 51 ticks = now - no->old_ticks;
73bcb24d
RS
52 bytes = muldiv64(ticks, hw->info.bytes_per_second, NANOSECONDS_PER_SECOND);
53 bytes = audio_MIN(bytes, INT_MAX);
8ead62cf
FB
54 samples = bytes >> hw->info.shift;
55
7372f88d
FB
56 no->old_ticks = now;
57 decr = audio_MIN (live, samples);
1d14ffa9
FB
58 hw->rpos = (hw->rpos + decr) % hw->samples;
59 return decr;
60}
7372f88d 61
1d14ffa9
FB
62static int no_write (SWVoiceOut *sw, void *buf, int len)
63{
73bcb24d 64 return audio_pcm_sw_write(sw, buf, len);
1d14ffa9 65}
7372f88d 66
5706db1d 67static int no_init_out(HWVoiceOut *hw, struct audsettings *as, void *drv_opaque)
1d14ffa9 68{
d929eba5 69 audio_pcm_init_info (&hw->info, as);
c0fe3827 70 hw->samples = 1024;
1d14ffa9
FB
71 return 0;
72}
7372f88d 73
1d14ffa9
FB
74static void no_fini_out (HWVoiceOut *hw)
75{
76 (void) hw;
7372f88d
FB
77}
78
1d14ffa9 79static int no_ctl_out (HWVoiceOut *hw, int cmd, ...)
7372f88d 80{
1d14ffa9
FB
81 (void) hw;
82 (void) cmd;
83 return 0;
7372f88d
FB
84}
85
5706db1d 86static int no_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
7372f88d 87{
d929eba5 88 audio_pcm_init_info (&hw->info, as);
c0fe3827 89 hw->samples = 1024;
7372f88d
FB
90 return 0;
91}
92
1d14ffa9 93static void no_fini_in (HWVoiceIn *hw)
7372f88d
FB
94{
95 (void) hw;
96}
97
1d14ffa9
FB
98static int no_run_in (HWVoiceIn *hw)
99{
100 NoVoiceIn *no = (NoVoiceIn *) hw;
1d14ffa9
FB
101 int live = audio_pcm_hw_get_live_in (hw);
102 int dead = hw->samples - live;
ec36b695 103 int samples = 0;
1d14ffa9 104
8ead62cf 105 if (dead) {
bc72ad67 106 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
8ead62cf 107 int64_t ticks = now - no->old_ticks;
4f4cc0ef 108 int64_t bytes =
73bcb24d 109 muldiv64(ticks, hw->info.bytes_per_second, NANOSECONDS_PER_SECOND);
1d14ffa9 110
8ead62cf
FB
111 no->old_ticks = now;
112 bytes = audio_MIN (bytes, INT_MAX);
113 samples = bytes >> hw->info.shift;
114 samples = audio_MIN (samples, dead);
115 }
1d14ffa9
FB
116 return samples;
117}
118
119static int no_read (SWVoiceIn *sw, void *buf, int size)
120{
8a7d0890
MW
121 /* use custom code here instead of audio_pcm_sw_read() to avoid
122 * useless resampling/mixing */
1d14ffa9
FB
123 int samples = size >> sw->info.shift;
124 int total = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
125 int to_clear = audio_MIN (samples, total);
8a7d0890 126 sw->total_hw_samples_acquired += total;
1d14ffa9 127 audio_pcm_info_clear_buf (&sw->info, buf, to_clear);
85882c71 128 return to_clear << sw->info.shift;
1d14ffa9
FB
129}
130
131static int no_ctl_in (HWVoiceIn *hw, int cmd, ...)
7372f88d
FB
132{
133 (void) hw;
134 (void) cmd;
135 return 0;
136}
137
138static void *no_audio_init (void)
139{
140 return &no_audio_init;
141}
142
143static void no_audio_fini (void *opaque)
144{
1d14ffa9 145 (void) opaque;
7372f88d
FB
146}
147
35f4b58c 148static struct audio_pcm_ops no_pcm_ops = {
1dd3e4d1
JQ
149 .init_out = no_init_out,
150 .fini_out = no_fini_out,
151 .run_out = no_run_out,
152 .write = no_write,
153 .ctl_out = no_ctl_out,
154
155 .init_in = no_init_in,
156 .fini_in = no_fini_in,
157 .run_in = no_run_in,
158 .read = no_read,
159 .ctl_in = no_ctl_in
7372f88d
FB
160};
161
1d14ffa9 162struct audio_driver no_audio_driver = {
bee37f32
JQ
163 .name = "none",
164 .descr = "Timer based audio emulation",
165 .options = NULL,
166 .init = no_audio_init,
167 .fini = no_audio_fini,
168 .pcm_ops = &no_pcm_ops,
169 .can_be_default = 1,
170 .max_voices_out = INT_MAX,
171 .max_voices_in = INT_MAX,
172 .voice_size_out = sizeof (NoVoiceOut),
173 .voice_size_in = sizeof (NoVoiceIn)
7372f88d 174};