blob: 64695e6af9373e34affe736804ccb6a72d49efe2 [file] [log] [blame]
bellard7372f882004-11-11 16:55:09 +00001/*
bellard1d14ffa2005-10-30 18:58:22 +00002 * QEMU Timer based audio emulation
3 *
4 * Copyright (c) 2004-2005 Vassili Karpov (malc)
5 *
bellard7372f882004-11-11 16:55:09 +00006 * 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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "qemu-common.h"
25#include "audio.h"
26#include "qemu-timer.h"
bellard7372f882004-11-11 16:55:09 +000027
bellard1d14ffa2005-10-30 18:58:22 +000028#define AUDIO_CAP "noaudio"
29#include "audio_int.h"
bellard7372f882004-11-11 16:55:09 +000030
bellard1d14ffa2005-10-30 18:58:22 +000031typedef struct NoVoiceOut {
32 HWVoiceOut hw;
bellard7372f882004-11-11 16:55:09 +000033 int64_t old_ticks;
bellard1d14ffa2005-10-30 18:58:22 +000034} NoVoiceOut;
bellard7372f882004-11-11 16:55:09 +000035
bellard1d14ffa2005-10-30 18:58:22 +000036typedef struct NoVoiceIn {
37 HWVoiceIn hw;
38 int64_t old_ticks;
39} NoVoiceIn;
bellard7372f882004-11-11 16:55:09 +000040
bellard1d14ffa2005-10-30 18:58:22 +000041static int no_run_out (HWVoiceOut *hw)
bellard7372f882004-11-11 16:55:09 +000042{
bellard1d14ffa2005-10-30 18:58:22 +000043 NoVoiceOut *no = (NoVoiceOut *) hw;
44 int live, decr, samples;
bellard8ead62c2006-07-04 16:51:32 +000045 int64_t now;
46 int64_t ticks;
47 int64_t bytes;
bellard7372f882004-11-11 16:55:09 +000048
bellard1d14ffa2005-10-30 18:58:22 +000049 live = audio_pcm_hw_get_live_out (&no->hw);
50 if (!live) {
51 return 0;
52 }
bellard7372f882004-11-11 16:55:09 +000053
bellard8ead62c2006-07-04 16:51:32 +000054 now = qemu_get_clock (vm_clock);
55 ticks = now - no->old_ticks;
56 bytes = (ticks * hw->info.bytes_per_second) / ticks_per_sec;
57 bytes = audio_MIN (bytes, INT_MAX);
58 samples = bytes >> hw->info.shift;
59
bellard7372f882004-11-11 16:55:09 +000060 no->old_ticks = now;
61 decr = audio_MIN (live, samples);
bellard1d14ffa2005-10-30 18:58:22 +000062 hw->rpos = (hw->rpos + decr) % hw->samples;
63 return decr;
bellard7372f882004-11-11 16:55:09 +000064}
65
bellard1d14ffa2005-10-30 18:58:22 +000066static int no_write (SWVoiceOut *sw, void *buf, int len)
bellard7372f882004-11-11 16:55:09 +000067{
bellard1d14ffa2005-10-30 18:58:22 +000068 return audio_pcm_sw_write (sw, buf, len);
bellard7372f882004-11-11 16:55:09 +000069}
70
bellardc0fe3822005-11-05 18:55:28 +000071static int no_init_out (HWVoiceOut *hw, audsettings_t *as)
bellard7372f882004-11-11 16:55:09 +000072{
bellardd929eba2006-07-04 21:47:22 +000073 audio_pcm_init_info (&hw->info, as);
bellardc0fe3822005-11-05 18:55:28 +000074 hw->samples = 1024;
bellard7372f882004-11-11 16:55:09 +000075 return 0;
76}
77
bellard1d14ffa2005-10-30 18:58:22 +000078static void no_fini_out (HWVoiceOut *hw)
bellard7372f882004-11-11 16:55:09 +000079{
80 (void) hw;
81}
82
bellard1d14ffa2005-10-30 18:58:22 +000083static int no_ctl_out (HWVoiceOut *hw, int cmd, ...)
84{
85 (void) hw;
86 (void) cmd;
87 return 0;
88}
89
bellardc0fe3822005-11-05 18:55:28 +000090static int no_init_in (HWVoiceIn *hw, audsettings_t *as)
bellard1d14ffa2005-10-30 18:58:22 +000091{
bellardd929eba2006-07-04 21:47:22 +000092 audio_pcm_init_info (&hw->info, as);
bellardc0fe3822005-11-05 18:55:28 +000093 hw->samples = 1024;
bellard1d14ffa2005-10-30 18:58:22 +000094 return 0;
95}
96
97static void no_fini_in (HWVoiceIn *hw)
98{
99 (void) hw;
100}
101
102static int no_run_in (HWVoiceIn *hw)
103{
104 NoVoiceIn *no = (NoVoiceIn *) hw;
bellard1d14ffa2005-10-30 18:58:22 +0000105 int live = audio_pcm_hw_get_live_in (hw);
106 int dead = hw->samples - live;
bellardec36b692006-07-16 18:57:03 +0000107 int samples = 0;
bellard1d14ffa2005-10-30 18:58:22 +0000108
bellard8ead62c2006-07-04 16:51:32 +0000109 if (dead) {
110 int64_t now = qemu_get_clock (vm_clock);
111 int64_t ticks = now - no->old_ticks;
112 int64_t bytes = (ticks * hw->info.bytes_per_second) / ticks_per_sec;
bellard1d14ffa2005-10-30 18:58:22 +0000113
bellard8ead62c2006-07-04 16:51:32 +0000114 no->old_ticks = now;
115 bytes = audio_MIN (bytes, INT_MAX);
116 samples = bytes >> hw->info.shift;
117 samples = audio_MIN (samples, dead);
118 }
bellard1d14ffa2005-10-30 18:58:22 +0000119 return samples;
120}
121
122static int no_read (SWVoiceIn *sw, void *buf, int size)
123{
124 int samples = size >> sw->info.shift;
125 int total = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
126 int to_clear = audio_MIN (samples, total);
127 audio_pcm_info_clear_buf (&sw->info, buf, to_clear);
128 return to_clear;
129}
130
131static int no_ctl_in (HWVoiceIn *hw, int cmd, ...)
bellard7372f882004-11-11 16:55:09 +0000132{
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{
bellard1d14ffa2005-10-30 18:58:22 +0000145 (void) opaque;
bellard7372f882004-11-11 16:55:09 +0000146}
147
bellard1d14ffa2005-10-30 18:58:22 +0000148static struct audio_pcm_ops no_pcm_ops = {
149 no_init_out,
150 no_fini_out,
151 no_run_out,
152 no_write,
153 no_ctl_out,
154
155 no_init_in,
156 no_fini_in,
157 no_run_in,
158 no_read,
159 no_ctl_in
bellard7372f882004-11-11 16:55:09 +0000160};
161
bellard1d14ffa2005-10-30 18:58:22 +0000162struct audio_driver no_audio_driver = {
163 INIT_FIELD (name = ) "none",
164 INIT_FIELD (descr = ) "Timer based audio emulation",
165 INIT_FIELD (options = ) NULL,
166 INIT_FIELD (init = ) no_audio_init,
167 INIT_FIELD (fini = ) no_audio_fini,
168 INIT_FIELD (pcm_ops = ) &no_pcm_ops,
169 INIT_FIELD (can_be_default = ) 1,
170 INIT_FIELD (max_voices_out = ) INT_MAX,
171 INIT_FIELD (max_voices_in = ) INT_MAX,
172 INIT_FIELD (voice_size_out = ) sizeof (NoVoiceOut),
173 INIT_FIELD (voice_size_in = ) sizeof (NoVoiceIn)
bellard7372f882004-11-11 16:55:09 +0000174};