noaudio.c revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
829N/A/*
2362N/A * QEMU Timer based audio emulation
829N/A *
829N/A * Copyright (c) 2004-2005 Vassili Karpov (malc)
829N/A *
829N/A * Permission is hereby granted, free of charge, to any person obtaining a copy
2362N/A * of this software and associated documentation files (the "Software"), to deal
829N/A * in the Software without restriction, including without limitation the rights
2362N/A * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
829N/A * copies of the Software, and to permit persons to whom the Software is
829N/A * furnished to do so, subject to the following conditions:
829N/A *
829N/A * The above copyright notice and this permission notice shall be included in
829N/A * all copies or substantial portions of the Software.
829N/A *
829N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
829N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
829N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
829N/A * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
829N/A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2362N/A * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2362N/A * THE SOFTWARE.
2362N/A */
829N/A#include "Builtins.h"
829N/A#include "../../vl_vbox.h"
829N/A#include "audio.h"
829N/A#include <iprt/alloc.h>
829N/A
829N/A#define AUDIO_CAP "noaudio"
829N/A#include "audio_int.h"
829N/A
829N/Atypedef struct NoVoiceOut {
829N/A HWVoiceOut hw;
829N/A int64_t old_ticks;
829N/A} NoVoiceOut;
829N/A
829N/Atypedef struct NoVoiceIn {
829N/A HWVoiceIn hw;
829N/A int64_t old_ticks;
829N/A} NoVoiceIn;
829N/A
829N/Astatic int no_run_out (HWVoiceOut *hw)
829N/A{
829N/A NoVoiceOut *no = (NoVoiceOut *) hw;
829N/A int live, decr, samples;
829N/A int64_t now;
829N/A int64_t ticks;
829N/A int64_t bytes;
829N/A
829N/A live = audio_pcm_hw_get_live_out (&no->hw);
829N/A if (!live) {
829N/A return 0;
829N/A }
829N/A
829N/A now = audio_get_clock ();
829N/A ticks = now - no->old_ticks;
829N/A bytes = (ticks * hw->info.bytes_per_second) / audio_get_ticks_per_sec ();
829N/A bytes = audio_MIN (bytes, INT_MAX);
829N/A samples = bytes >> hw->info.shift;
829N/A
829N/A no->old_ticks = now;
829N/A decr = audio_MIN (live, samples);
829N/A hw->rpos = (hw->rpos + decr) % hw->samples;
829N/A return decr;
829N/A}
829N/A
829N/Astatic int no_write (SWVoiceOut *sw, void *buf, int len)
829N/A{
829N/A return audio_pcm_sw_write (sw, buf, len);
829N/A}
829N/A
829N/Astatic int no_init_out (HWVoiceOut *hw, audsettings_t *as)
829N/A{
829N/A audio_pcm_init_info (&hw->info, as);
829N/A hw->samples = 1024;
829N/A return 0;
829N/A}
829N/A
829N/Astatic void no_fini_out (HWVoiceOut *hw)
829N/A{
829N/A (void) hw;
829N/A}
829N/A
829N/Astatic int no_ctl_out (HWVoiceOut *hw, int cmd, ...)
829N/A{
829N/A (void) hw;
(void) cmd;
return 0;
}
static int no_init_in (HWVoiceIn *hw, audsettings_t *as)
{
audio_pcm_init_info (&hw->info, as);
hw->samples = 1024;
return 0;
}
static void no_fini_in (HWVoiceIn *hw)
{
(void) hw;
}
static int no_run_in (HWVoiceIn *hw)
{
NoVoiceIn *no = (NoVoiceIn *) hw;
int live = audio_pcm_hw_get_live_in (hw);
int dead = hw->samples - live;
int samples = 0;
if (dead) {
int64_t now = audio_get_clock ();
int64_t ticks = now - no->old_ticks;
int64_t bytes = (ticks * hw->info.bytes_per_second)
/ audio_get_ticks_per_sec ();
no->old_ticks = now;
bytes = audio_MIN (bytes, INT_MAX);
samples = bytes >> hw->info.shift;
samples = audio_MIN (samples, dead);
}
return samples;
}
static int no_read (SWVoiceIn *sw, void *buf, int size)
{
int samples = size >> sw->info.shift;
int total = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
int to_clear = audio_MIN (samples, total);
audio_pcm_info_clear_buf (&sw->info, buf, to_clear);
return to_clear;
}
static int no_ctl_in (HWVoiceIn *hw, int cmd, ...)
{
(void) hw;
(void) cmd;
return 0;
}
static void *no_audio_init (void)
{
return &no_audio_driver;
}
static void no_audio_fini (void *opaque)
{
(void) opaque;
}
static struct audio_pcm_ops no_pcm_ops = {
no_init_out,
no_fini_out,
no_run_out,
no_write,
no_ctl_out,
no_init_in,
no_fini_in,
no_run_in,
no_read,
no_ctl_in
};
struct audio_driver no_audio_driver = {
INIT_FIELD (name = ) "null",
INIT_FIELD (descr = ) "Timer based audio emulation",
INIT_FIELD (options = ) NULL,
INIT_FIELD (init = ) no_audio_init,
INIT_FIELD (fini = ) no_audio_fini,
INIT_FIELD (pcm_ops = ) &no_pcm_ops,
INIT_FIELD (can_be_default = ) 1,
INIT_FIELD (max_voices_out = ) INT_MAX,
INIT_FIELD (max_voices_in = ) INT_MAX,
INIT_FIELD (voice_size_out = ) sizeof (NoVoiceOut),
INIT_FIELD (voice_size_in = ) sizeof (NoVoiceIn)
};