blob: cb386620ae6f1f2105373b4c91834f0cdfc69580 [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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#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
malcbdff2532009-09-18 11:37:39 +040041static int no_run_out (HWVoiceOut *hw, int live)
bellard7372f882004-11-11 16:55:09 +000042{
bellard1d14ffa2005-10-30 18:58:22 +000043 NoVoiceOut *no = (NoVoiceOut *) hw;
malcbdff2532009-09-18 11:37:39 +040044 int 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
Alex Blighbc72ad62013-08-21 16:03:08 +010049 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
bellard8ead62c2006-07-04 16:51:32 +000050 ticks = now - no->old_ticks;
malc4f4cc0e2009-09-18 08:16:03 +040051 bytes = muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
bellard8ead62c2006-07-04 16:51:32 +000052 bytes = audio_MIN (bytes, INT_MAX);
53 samples = bytes >> hw->info.shift;
54
bellard7372f882004-11-11 16:55:09 +000055 no->old_ticks = now;
56 decr = audio_MIN (live, samples);
bellard1d14ffa2005-10-30 18:58:22 +000057 hw->rpos = (hw->rpos + decr) % hw->samples;
58 return decr;
bellard7372f882004-11-11 16:55:09 +000059}
60
bellard1d14ffa2005-10-30 18:58:22 +000061static int no_write (SWVoiceOut *sw, void *buf, int len)
bellard7372f882004-11-11 16:55:09 +000062{
bellard1d14ffa2005-10-30 18:58:22 +000063 return audio_pcm_sw_write (sw, buf, len);
bellard7372f882004-11-11 16:55:09 +000064}
65
malc1ea879e2008-12-03 22:48:44 +000066static int no_init_out (HWVoiceOut *hw, struct audsettings *as)
bellard7372f882004-11-11 16:55:09 +000067{
bellardd929eba2006-07-04 21:47:22 +000068 audio_pcm_init_info (&hw->info, as);
bellardc0fe3822005-11-05 18:55:28 +000069 hw->samples = 1024;
bellard7372f882004-11-11 16:55:09 +000070 return 0;
71}
72
bellard1d14ffa2005-10-30 18:58:22 +000073static void no_fini_out (HWVoiceOut *hw)
bellard7372f882004-11-11 16:55:09 +000074{
75 (void) hw;
76}
77
bellard1d14ffa2005-10-30 18:58:22 +000078static int no_ctl_out (HWVoiceOut *hw, int cmd, ...)
79{
80 (void) hw;
81 (void) cmd;
82 return 0;
83}
84
malc1ea879e2008-12-03 22:48:44 +000085static int no_init_in (HWVoiceIn *hw, struct audsettings *as)
bellard1d14ffa2005-10-30 18:58:22 +000086{
bellardd929eba2006-07-04 21:47:22 +000087 audio_pcm_init_info (&hw->info, as);
bellardc0fe3822005-11-05 18:55:28 +000088 hw->samples = 1024;
bellard1d14ffa2005-10-30 18:58:22 +000089 return 0;
90}
91
92static void no_fini_in (HWVoiceIn *hw)
93{
94 (void) hw;
95}
96
97static int no_run_in (HWVoiceIn *hw)
98{
99 NoVoiceIn *no = (NoVoiceIn *) hw;
bellard1d14ffa2005-10-30 18:58:22 +0000100 int live = audio_pcm_hw_get_live_in (hw);
101 int dead = hw->samples - live;
bellardec36b692006-07-16 18:57:03 +0000102 int samples = 0;
bellard1d14ffa2005-10-30 18:58:22 +0000103
bellard8ead62c2006-07-04 16:51:32 +0000104 if (dead) {
Alex Blighbc72ad62013-08-21 16:03:08 +0100105 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
bellard8ead62c2006-07-04 16:51:32 +0000106 int64_t ticks = now - no->old_ticks;
malc4f4cc0e2009-09-18 08:16:03 +0400107 int64_t bytes =
108 muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
bellard1d14ffa2005-10-30 18:58:22 +0000109
bellard8ead62c2006-07-04 16:51:32 +0000110 no->old_ticks = now;
111 bytes = audio_MIN (bytes, INT_MAX);
112 samples = bytes >> hw->info.shift;
113 samples = audio_MIN (samples, dead);
114 }
bellard1d14ffa2005-10-30 18:58:22 +0000115 return samples;
116}
117
118static int no_read (SWVoiceIn *sw, void *buf, int size)
119{
Michael Walle8a7d0892011-01-04 01:48:55 +0100120 /* use custom code here instead of audio_pcm_sw_read() to avoid
121 * useless resampling/mixing */
bellard1d14ffa2005-10-30 18:58:22 +0000122 int samples = size >> sw->info.shift;
123 int total = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
124 int to_clear = audio_MIN (samples, total);
Michael Walle8a7d0892011-01-04 01:48:55 +0100125 sw->total_hw_samples_acquired += total;
bellard1d14ffa2005-10-30 18:58:22 +0000126 audio_pcm_info_clear_buf (&sw->info, buf, to_clear);
Michael Walle85882c72010-12-09 00:34:51 +0100127 return to_clear << sw->info.shift;
bellard1d14ffa2005-10-30 18:58:22 +0000128}
129
130static int no_ctl_in (HWVoiceIn *hw, int cmd, ...)
bellard7372f882004-11-11 16:55:09 +0000131{
132 (void) hw;
133 (void) cmd;
134 return 0;
135}
136
137static void *no_audio_init (void)
138{
139 return &no_audio_init;
140}
141
142static void no_audio_fini (void *opaque)
143{
bellard1d14ffa2005-10-30 18:58:22 +0000144 (void) opaque;
bellard7372f882004-11-11 16:55:09 +0000145}
146
blueswir135f4b582008-10-06 18:08:30 +0000147static struct audio_pcm_ops no_pcm_ops = {
Juan Quintela1dd3e4d2009-08-11 02:31:15 +0200148 .init_out = no_init_out,
149 .fini_out = no_fini_out,
150 .run_out = no_run_out,
151 .write = no_write,
152 .ctl_out = no_ctl_out,
bellard1d14ffa2005-10-30 18:58:22 +0000153
Juan Quintela1dd3e4d2009-08-11 02:31:15 +0200154 .init_in = no_init_in,
155 .fini_in = no_fini_in,
156 .run_in = no_run_in,
157 .read = no_read,
158 .ctl_in = no_ctl_in
bellard7372f882004-11-11 16:55:09 +0000159};
160
bellard1d14ffa2005-10-30 18:58:22 +0000161struct audio_driver no_audio_driver = {
Juan Quintelabee37f32009-08-11 02:31:14 +0200162 .name = "none",
163 .descr = "Timer based audio emulation",
164 .options = NULL,
165 .init = no_audio_init,
166 .fini = no_audio_fini,
167 .pcm_ops = &no_pcm_ops,
168 .can_be_default = 1,
169 .max_voices_out = INT_MAX,
170 .max_voices_in = INT_MAX,
171 .voice_size_out = sizeof (NoVoiceOut),
172 .voice_size_in = sizeof (NoVoiceIn)
bellard7372f882004-11-11 16:55:09 +0000173};