2004-11-08 02:04:02 +08:00
|
|
|
/*
|
|
|
|
* QEMU Audio subsystem
|
2005-10-31 02:58:22 +08:00
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2005 Vassili Karpov (malc)
|
|
|
|
*
|
2004-11-08 02:04:02 +08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2007-11-18 01:14:51 +08:00
|
|
|
#include "hw/hw.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "console.h"
|
|
|
|
#include "qemu-timer.h"
|
|
|
|
#include "sysemu.h"
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
#define AUDIO_CAP "audio"
|
|
|
|
#include "audio_int.h"
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
/* #define DEBUG_PLIVE */
|
|
|
|
/* #define DEBUG_LIVE */
|
|
|
|
/* #define DEBUG_OUT */
|
2006-07-05 00:51:32 +08:00
|
|
|
/* #define DEBUG_CAPTURE */
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
static struct audio_driver *drvtab[] = {
|
2008-06-26 05:04:05 +08:00
|
|
|
AUDIO_DRIVERS
|
2005-10-31 02:58:22 +08:00
|
|
|
&no_audio_driver,
|
|
|
|
&wav_audio_driver
|
|
|
|
};
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
struct fixed_settings {
|
|
|
|
int enabled;
|
|
|
|
int nb_voices;
|
|
|
|
int greedy;
|
|
|
|
audsettings_t settings;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
struct fixed_settings fixed_out;
|
|
|
|
struct fixed_settings fixed_in;
|
|
|
|
union {
|
2008-11-13 04:36:27 +08:00
|
|
|
int hertz;
|
2005-11-06 02:55:28 +08:00
|
|
|
int64_t ticks;
|
|
|
|
} period;
|
|
|
|
int plive;
|
2005-11-11 08:04:19 +08:00
|
|
|
int log_to_monitor;
|
2005-11-06 02:55:28 +08:00
|
|
|
} conf = {
|
|
|
|
{ /* DAC fixed settings */
|
|
|
|
1, /* enabled */
|
|
|
|
1, /* nb_voices */
|
|
|
|
1, /* greedy */
|
|
|
|
{
|
|
|
|
44100, /* freq */
|
|
|
|
2, /* nchannels */
|
2007-02-18 06:19:29 +08:00
|
|
|
AUD_FMT_S16, /* fmt */
|
|
|
|
AUDIO_HOST_ENDIANNESS
|
2005-11-06 02:55:28 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{ /* ADC fixed settings */
|
|
|
|
1, /* enabled */
|
|
|
|
1, /* nb_voices */
|
|
|
|
1, /* greedy */
|
|
|
|
{
|
|
|
|
44100, /* freq */
|
|
|
|
2, /* nchannels */
|
2007-02-18 06:19:29 +08:00
|
|
|
AUD_FMT_S16, /* fmt */
|
|
|
|
AUDIO_HOST_ENDIANNESS
|
2005-11-06 02:55:28 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-11-05 03:15:37 +08:00
|
|
|
{ 250 }, /* period */
|
2005-11-11 08:04:19 +08:00
|
|
|
0, /* plive */
|
2005-11-21 00:24:34 +08:00
|
|
|
0 /* log_to_monitor */
|
2005-10-31 02:58:22 +08:00
|
|
|
};
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
static AudioState glob_audio_state;
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
volume_t nominal_volume = {
|
|
|
|
0,
|
|
|
|
#ifdef FLOAT_MIXENG
|
|
|
|
1.0,
|
|
|
|
1.0
|
|
|
|
#else
|
2008-05-04 18:21:03 +08:00
|
|
|
1ULL << 32,
|
|
|
|
1ULL << 32
|
2005-10-31 02:58:22 +08:00
|
|
|
#endif
|
2004-11-08 02:04:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* http://www.df.lth.se/~john_e/gems/gem002d.html */
|
|
|
|
/* http://www.multi-platforms.com/Tips/PopCount.htm */
|
|
|
|
uint32_t popcount (uint32_t u)
|
|
|
|
{
|
|
|
|
u = ((u&0x55555555) + ((u>>1)&0x55555555));
|
|
|
|
u = ((u&0x33333333) + ((u>>2)&0x33333333));
|
|
|
|
u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
|
|
|
|
u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
|
|
|
|
u = ( u&0x0000ffff) + (u>>16);
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t lsbindex (uint32_t u)
|
|
|
|
{
|
|
|
|
return popcount ((u&-u)-1);
|
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
|
|
|
|
#error No its not
|
|
|
|
#else
|
|
|
|
int audio_bug (const char *funcname, int cond)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
if (cond) {
|
|
|
|
static int shown;
|
|
|
|
|
2006-07-05 00:51:32 +08:00
|
|
|
AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
|
2005-10-31 02:58:22 +08:00
|
|
|
if (!shown) {
|
|
|
|
shown = 1;
|
|
|
|
AUD_log (NULL, "Save all your work and restart without audio\n");
|
|
|
|
AUD_log (NULL, "Please send bug report to malc@pulsesoft.com\n");
|
|
|
|
AUD_log (NULL, "I am sorry\n");
|
|
|
|
}
|
|
|
|
AUD_log (NULL, "Context:\n");
|
|
|
|
|
|
|
|
#if defined AUDIO_BREAKPOINT_ON_BUG
|
|
|
|
# if defined HOST_I386
|
|
|
|
# if defined __GNUC__
|
|
|
|
__asm__ ("int3");
|
|
|
|
# elif defined _MSC_VER
|
|
|
|
_asm _emit 0xcc;
|
|
|
|
# else
|
|
|
|
abort ();
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
abort ();
|
|
|
|
# endif
|
|
|
|
#endif
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
return cond;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
#endif
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2007-02-18 06:19:29 +08:00
|
|
|
static inline int audio_bits_to_index (int bits)
|
|
|
|
{
|
|
|
|
switch (bits) {
|
|
|
|
case 8:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 16:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 32:
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
default:
|
|
|
|
audio_bug ("bits_to_index", 1);
|
|
|
|
AUD_log (NULL, "invalid bits %d\n", bits);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
void *audio_calloc (const char *funcname, int nmemb, size_t size)
|
|
|
|
{
|
|
|
|
int cond;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = nmemb * size;
|
|
|
|
cond = !nmemb || !size;
|
|
|
|
cond |= nmemb < 0;
|
|
|
|
cond |= len < size;
|
|
|
|
|
|
|
|
if (audio_bug ("audio_calloc", cond)) {
|
|
|
|
AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
|
|
|
|
funcname);
|
2005-11-11 08:04:19 +08:00
|
|
|
AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
|
2005-11-06 02:55:28 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return qemu_mallocz (len);
|
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
static char *audio_alloc_prefix (const char *s)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
const char qemu_prefix[] = "QEMU_";
|
|
|
|
size_t len;
|
|
|
|
char *r;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (!s) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
len = strlen (s);
|
2008-08-28 02:43:53 +08:00
|
|
|
r = qemu_malloc (len + sizeof (qemu_prefix));
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (r) {
|
|
|
|
size_t i;
|
|
|
|
char *u = r + sizeof (qemu_prefix) - 1;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2008-08-22 01:58:08 +08:00
|
|
|
pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
|
2008-09-28 08:42:15 +08:00
|
|
|
pstrcat (r, len + sizeof (qemu_prefix), s);
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
for (i = 0; i < len; ++i) {
|
2008-11-16 21:53:32 +08:00
|
|
|
u[i] = qemu_toupper(u[i]);
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
return r;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2007-11-18 09:44:38 +08:00
|
|
|
static const char *audio_audfmt_to_string (audfmt_e fmt)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
switch (fmt) {
|
|
|
|
case AUD_FMT_U8:
|
|
|
|
return "U8";
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
case AUD_FMT_U16:
|
|
|
|
return "U16";
|
2004-11-08 02:04:02 +08:00
|
|
|
|
|
|
|
case AUD_FMT_S8:
|
2005-10-31 02:58:22 +08:00
|
|
|
return "S8";
|
2004-11-08 02:04:02 +08:00
|
|
|
|
|
|
|
case AUD_FMT_S16:
|
2005-10-31 02:58:22 +08:00
|
|
|
return "S16";
|
2007-02-18 06:19:29 +08:00
|
|
|
|
|
|
|
case AUD_FMT_U32:
|
|
|
|
return "U32";
|
|
|
|
|
|
|
|
case AUD_FMT_S32:
|
|
|
|
return "S32";
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
dolog ("Bogus audfmt %d returning S16\n", fmt);
|
|
|
|
return "S16";
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2007-11-18 09:44:38 +08:00
|
|
|
static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
|
|
|
|
int *defaultp)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
if (!strcasecmp (s, "u8")) {
|
|
|
|
*defaultp = 0;
|
|
|
|
return AUD_FMT_U8;
|
|
|
|
}
|
|
|
|
else if (!strcasecmp (s, "u16")) {
|
|
|
|
*defaultp = 0;
|
|
|
|
return AUD_FMT_U16;
|
|
|
|
}
|
2007-02-18 06:19:29 +08:00
|
|
|
else if (!strcasecmp (s, "u32")) {
|
|
|
|
*defaultp = 0;
|
|
|
|
return AUD_FMT_U32;
|
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
else if (!strcasecmp (s, "s8")) {
|
|
|
|
*defaultp = 0;
|
|
|
|
return AUD_FMT_S8;
|
|
|
|
}
|
|
|
|
else if (!strcasecmp (s, "s16")) {
|
|
|
|
*defaultp = 0;
|
|
|
|
return AUD_FMT_S16;
|
|
|
|
}
|
2007-02-18 06:19:29 +08:00
|
|
|
else if (!strcasecmp (s, "s32")) {
|
|
|
|
*defaultp = 0;
|
|
|
|
return AUD_FMT_S32;
|
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
else {
|
|
|
|
dolog ("Bogus audio format `%s' using %s\n",
|
|
|
|
s, audio_audfmt_to_string (defval));
|
|
|
|
*defaultp = 1;
|
|
|
|
return defval;
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
static audfmt_e audio_get_conf_fmt (const char *envname,
|
|
|
|
audfmt_e defval,
|
|
|
|
int *defaultp)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
const char *var = getenv (envname);
|
|
|
|
if (!var) {
|
|
|
|
*defaultp = 1;
|
|
|
|
return defval;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
return audio_string_to_audfmt (var, defval, defaultp);
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
static int audio_get_conf_int (const char *key, int defval, int *defaultp)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
int val;
|
|
|
|
char *strval;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
strval = getenv (key);
|
|
|
|
if (strval) {
|
|
|
|
*defaultp = 0;
|
|
|
|
val = atoi (strval);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*defaultp = 1;
|
|
|
|
return defval;
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
static const char *audio_get_conf_str (const char *key,
|
|
|
|
const char *defval,
|
|
|
|
int *defaultp)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
const char *val = getenv (key);
|
|
|
|
if (!val) {
|
|
|
|
*defaultp = 1;
|
|
|
|
return defval;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*defaultp = 0;
|
|
|
|
return val;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-11 08:04:19 +08:00
|
|
|
void AUD_vlog (const char *cap, const char *fmt, va_list ap)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-11-11 08:04:19 +08:00
|
|
|
if (conf.log_to_monitor) {
|
|
|
|
if (cap) {
|
|
|
|
term_printf ("%s: ", cap);
|
|
|
|
}
|
|
|
|
|
|
|
|
term_vprintf (fmt, ap);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (cap) {
|
|
|
|
fprintf (stderr, "%s: ", cap);
|
|
|
|
}
|
|
|
|
|
|
|
|
vfprintf (stderr, fmt, ap);
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-11 08:04:19 +08:00
|
|
|
void AUD_log (const char *cap, const char *fmt, ...)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-11-11 08:04:19 +08:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
AUD_vlog (cap, fmt, ap);
|
|
|
|
va_end (ap);
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
static void audio_print_options (const char *prefix,
|
|
|
|
struct audio_option *opt)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
char *uprefix;
|
|
|
|
|
|
|
|
if (!prefix) {
|
|
|
|
dolog ("No prefix specified\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opt) {
|
|
|
|
dolog ("No options\n");
|
2004-11-08 02:04:02 +08:00
|
|
|
return;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
uprefix = audio_alloc_prefix (prefix);
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
for (; opt->name; opt++) {
|
|
|
|
const char *state = "default";
|
|
|
|
printf (" %s_%s: ", uprefix, opt->name);
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2007-07-12 18:59:21 +08:00
|
|
|
if (opt->overriddenp && *opt->overriddenp) {
|
2005-10-31 02:58:22 +08:00
|
|
|
state = "current";
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
switch (opt->tag) {
|
|
|
|
case AUD_OPT_BOOL:
|
|
|
|
{
|
|
|
|
int *intp = opt->valp;
|
|
|
|
printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUD_OPT_INT:
|
|
|
|
{
|
|
|
|
int *intp = opt->valp;
|
|
|
|
printf ("integer, %s = %d\n", state, *intp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUD_OPT_FMT:
|
|
|
|
{
|
|
|
|
audfmt_e *fmtp = opt->valp;
|
|
|
|
printf (
|
2008-01-14 12:24:29 +08:00
|
|
|
"format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
|
2005-10-31 02:58:22 +08:00
|
|
|
state,
|
|
|
|
audio_audfmt_to_string (*fmtp)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUD_OPT_STR:
|
|
|
|
{
|
|
|
|
const char **strp = opt->valp;
|
|
|
|
printf ("string, %s = %s\n",
|
|
|
|
state,
|
|
|
|
*strp ? *strp : "(not set)");
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf ("???\n");
|
|
|
|
dolog ("Bad value tag for option %s_%s %d\n",
|
|
|
|
uprefix, opt->name, opt->tag);
|
|
|
|
break;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
printf (" %s\n", opt->descr);
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
qemu_free (uprefix);
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
static void audio_process_options (const char *prefix,
|
|
|
|
struct audio_option *opt)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
char *optname;
|
|
|
|
const char qemu_prefix[] = "QEMU_";
|
2008-08-22 01:58:08 +08:00
|
|
|
size_t preflen, optlen;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (audio_bug (AUDIO_FUNC, !prefix)) {
|
|
|
|
dolog ("prefix = NULL\n");
|
|
|
|
return;
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (audio_bug (AUDIO_FUNC, !opt)) {
|
|
|
|
dolog ("opt = NULL\n");
|
|
|
|
return;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
preflen = strlen (prefix);
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
for (; opt->name; opt++) {
|
|
|
|
size_t len, i;
|
|
|
|
int def;
|
|
|
|
|
|
|
|
if (!opt->valp) {
|
|
|
|
dolog ("Option value pointer for `%s' is not set\n",
|
|
|
|
opt->name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen (opt->name);
|
2005-11-06 02:55:28 +08:00
|
|
|
/* len of opt->name + len of prefix + size of qemu_prefix
|
|
|
|
* (includes trailing zero) + zero + underscore (on behalf of
|
|
|
|
* sizeof) */
|
2008-08-22 01:58:08 +08:00
|
|
|
optlen = len + preflen + sizeof (qemu_prefix) + 1;
|
|
|
|
optname = qemu_malloc (optlen);
|
2005-10-31 02:58:22 +08:00
|
|
|
if (!optname) {
|
2005-11-06 02:55:28 +08:00
|
|
|
dolog ("Could not allocate memory for option name `%s'\n",
|
2005-10-31 02:58:22 +08:00
|
|
|
opt->name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-08-22 01:58:08 +08:00
|
|
|
pstrcpy (optname, optlen, qemu_prefix);
|
2005-11-06 02:55:28 +08:00
|
|
|
|
|
|
|
/* copy while upper-casing, including trailing zero */
|
2005-10-31 02:58:22 +08:00
|
|
|
for (i = 0; i <= preflen; ++i) {
|
2008-11-16 21:53:32 +08:00
|
|
|
optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2008-08-22 01:58:08 +08:00
|
|
|
pstrcat (optname, optlen, "_");
|
|
|
|
pstrcat (optname, optlen, opt->name);
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
def = 1;
|
|
|
|
switch (opt->tag) {
|
|
|
|
case AUD_OPT_BOOL:
|
|
|
|
case AUD_OPT_INT:
|
|
|
|
{
|
|
|
|
int *intp = opt->valp;
|
|
|
|
*intp = audio_get_conf_int (optname, *intp, &def);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUD_OPT_FMT:
|
|
|
|
{
|
|
|
|
audfmt_e *fmtp = opt->valp;
|
|
|
|
*fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUD_OPT_STR:
|
|
|
|
{
|
|
|
|
const char **strp = opt->valp;
|
|
|
|
*strp = audio_get_conf_str (optname, *strp, &def);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
dolog ("Bad value tag for option `%s' - %d\n",
|
|
|
|
optname, opt->tag);
|
2004-11-08 02:04:02 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-07-12 18:59:21 +08:00
|
|
|
if (!opt->overriddenp) {
|
|
|
|
opt->overriddenp = &opt->overridden;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2007-07-12 18:59:21 +08:00
|
|
|
*opt->overriddenp = !def;
|
2005-10-31 02:58:22 +08:00
|
|
|
qemu_free (optname);
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
static void audio_print_settings (audsettings_t *as)
|
|
|
|
{
|
|
|
|
dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
|
|
|
|
|
|
|
|
switch (as->fmt) {
|
|
|
|
case AUD_FMT_S8:
|
|
|
|
AUD_log (NULL, "S8");
|
|
|
|
break;
|
|
|
|
case AUD_FMT_U8:
|
|
|
|
AUD_log (NULL, "U8");
|
|
|
|
break;
|
|
|
|
case AUD_FMT_S16:
|
|
|
|
AUD_log (NULL, "S16");
|
|
|
|
break;
|
|
|
|
case AUD_FMT_U16:
|
|
|
|
AUD_log (NULL, "U16");
|
|
|
|
break;
|
2008-06-22 22:10:45 +08:00
|
|
|
case AUD_FMT_S32:
|
|
|
|
AUD_log (NULL, "S32");
|
|
|
|
break;
|
|
|
|
case AUD_FMT_U32:
|
|
|
|
AUD_log (NULL, "U32");
|
|
|
|
break;
|
2005-11-06 02:55:28 +08:00
|
|
|
default:
|
|
|
|
AUD_log (NULL, "invalid(%d)", as->fmt);
|
|
|
|
break;
|
|
|
|
}
|
2006-07-17 02:57:03 +08:00
|
|
|
|
|
|
|
AUD_log (NULL, " endianness=");
|
2006-07-05 05:47:22 +08:00
|
|
|
switch (as->endianness) {
|
|
|
|
case 0:
|
|
|
|
AUD_log (NULL, "little");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
AUD_log (NULL, "big");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
AUD_log (NULL, "invalid");
|
|
|
|
break;
|
|
|
|
}
|
2005-11-06 02:55:28 +08:00
|
|
|
AUD_log (NULL, "\n");
|
|
|
|
}
|
|
|
|
|
2006-07-17 02:57:03 +08:00
|
|
|
static int audio_validate_settings (audsettings_t *as)
|
2005-11-06 02:55:28 +08:00
|
|
|
{
|
|
|
|
int invalid;
|
|
|
|
|
|
|
|
invalid = as->nchannels != 1 && as->nchannels != 2;
|
2006-07-05 05:47:22 +08:00
|
|
|
invalid |= as->endianness != 0 && as->endianness != 1;
|
2005-11-06 02:55:28 +08:00
|
|
|
|
|
|
|
switch (as->fmt) {
|
|
|
|
case AUD_FMT_S8:
|
|
|
|
case AUD_FMT_U8:
|
|
|
|
case AUD_FMT_S16:
|
|
|
|
case AUD_FMT_U16:
|
2007-02-18 06:19:29 +08:00
|
|
|
case AUD_FMT_S32:
|
|
|
|
case AUD_FMT_U32:
|
2005-11-06 02:55:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
invalid = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid |= as->freq <= 0;
|
2006-07-05 05:47:22 +08:00
|
|
|
return invalid ? -1 : 0;
|
2005-11-06 02:55:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
int bits = 8, sign = 0;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
switch (as->fmt) {
|
2005-10-31 02:58:22 +08:00
|
|
|
case AUD_FMT_S8:
|
|
|
|
sign = 1;
|
|
|
|
case AUD_FMT_U8:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUD_FMT_S16:
|
|
|
|
sign = 1;
|
|
|
|
case AUD_FMT_U16:
|
|
|
|
bits = 16;
|
|
|
|
break;
|
2007-02-18 06:19:29 +08:00
|
|
|
|
|
|
|
case AUD_FMT_S32:
|
|
|
|
sign = 1;
|
|
|
|
case AUD_FMT_U32:
|
|
|
|
bits = 32;
|
|
|
|
break;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-11-06 02:55:28 +08:00
|
|
|
return info->freq == as->freq
|
|
|
|
&& info->nchannels == as->nchannels
|
2005-10-31 02:58:22 +08:00
|
|
|
&& info->sign == sign
|
2006-07-05 05:47:22 +08:00
|
|
|
&& info->bits == bits
|
|
|
|
&& info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2006-07-05 05:47:22 +08:00
|
|
|
void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
2007-02-18 06:19:29 +08:00
|
|
|
int bits = 8, sign = 0, shift = 0;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
switch (as->fmt) {
|
2004-11-08 02:04:02 +08:00
|
|
|
case AUD_FMT_S8:
|
|
|
|
sign = 1;
|
|
|
|
case AUD_FMT_U8:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUD_FMT_S16:
|
|
|
|
sign = 1;
|
|
|
|
case AUD_FMT_U16:
|
|
|
|
bits = 16;
|
2007-02-18 06:19:29 +08:00
|
|
|
shift = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUD_FMT_S32:
|
|
|
|
sign = 1;
|
|
|
|
case AUD_FMT_U32:
|
|
|
|
bits = 32;
|
|
|
|
shift = 2;
|
2004-11-08 02:04:02 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
info->freq = as->freq;
|
2005-10-31 02:58:22 +08:00
|
|
|
info->bits = bits;
|
|
|
|
info->sign = sign;
|
2005-11-06 02:55:28 +08:00
|
|
|
info->nchannels = as->nchannels;
|
2007-02-18 06:19:29 +08:00
|
|
|
info->shift = (as->nchannels == 2) + shift;
|
2005-10-31 02:58:22 +08:00
|
|
|
info->align = (1 << info->shift) - 1;
|
|
|
|
info->bytes_per_second = info->freq << info->shift;
|
2006-07-05 05:47:22 +08:00
|
|
|
info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
if (!len) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->sign) {
|
2006-08-04 04:39:40 +08:00
|
|
|
memset (buf, 0x00, len << info->shift);
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
else {
|
2007-02-18 06:19:29 +08:00
|
|
|
switch (info->bits) {
|
|
|
|
case 8:
|
2006-08-04 04:39:40 +08:00
|
|
|
memset (buf, 0x80, len << info->shift);
|
2007-02-18 06:19:29 +08:00
|
|
|
break;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2007-02-18 06:19:29 +08:00
|
|
|
case 16:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint16_t *p = buf;
|
|
|
|
int shift = info->nchannels - 1;
|
|
|
|
short s = INT16_MAX;
|
|
|
|
|
|
|
|
if (info->swap_endianness) {
|
|
|
|
s = bswap16 (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < len << shift; i++) {
|
|
|
|
p[i] = s;
|
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2007-02-18 06:19:29 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 32:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint32_t *p = buf;
|
|
|
|
int shift = info->nchannels - 1;
|
|
|
|
int32_t s = INT32_MAX;
|
|
|
|
|
|
|
|
if (info->swap_endianness) {
|
|
|
|
s = bswap32 (s);
|
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2007-02-18 06:19:29 +08:00
|
|
|
for (i = 0; i < len << shift; i++) {
|
|
|
|
p[i] = s;
|
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2007-02-18 06:19:29 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
|
|
|
|
info->bits);
|
|
|
|
break;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-05 00:51:32 +08:00
|
|
|
/*
|
|
|
|
* Capture
|
|
|
|
*/
|
|
|
|
static void noop_conv (st_sample_t *dst, const void *src,
|
|
|
|
int samples, volume_t *vol)
|
|
|
|
{
|
|
|
|
(void) src;
|
|
|
|
(void) dst;
|
|
|
|
(void) samples;
|
|
|
|
(void) vol;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CaptureVoiceOut *audio_pcm_capture_find_specific (
|
|
|
|
AudioState *s,
|
2006-07-05 05:47:22 +08:00
|
|
|
audsettings_t *as
|
2006-07-05 00:51:32 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
CaptureVoiceOut *cap;
|
|
|
|
|
|
|
|
for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
|
2006-07-05 05:47:22 +08:00
|
|
|
if (audio_pcm_info_eq (&cap->hw.info, as)) {
|
2006-07-05 00:51:32 +08:00
|
|
|
return cap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-07-17 02:57:03 +08:00
|
|
|
static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
|
2006-07-05 00:51:32 +08:00
|
|
|
{
|
2006-07-17 02:57:03 +08:00
|
|
|
struct capture_callback *cb;
|
|
|
|
|
|
|
|
#ifdef DEBUG_CAPTURE
|
|
|
|
dolog ("notification %d sent\n", cmd);
|
|
|
|
#endif
|
|
|
|
for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
|
|
|
|
cb->ops.notify (cb->opaque, cmd);
|
|
|
|
}
|
|
|
|
}
|
2006-07-05 00:51:32 +08:00
|
|
|
|
2006-07-17 02:57:03 +08:00
|
|
|
static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
|
|
|
|
{
|
|
|
|
if (cap->hw.enabled != enabled) {
|
|
|
|
audcnotification_e cmd;
|
2006-07-05 00:51:32 +08:00
|
|
|
cap->hw.enabled = enabled;
|
2006-07-17 02:57:03 +08:00
|
|
|
cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
|
|
|
|
audio_notify_capture (cap, cmd);
|
2006-07-05 00:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
|
|
|
|
{
|
|
|
|
HWVoiceOut *hw = &cap->hw;
|
|
|
|
SWVoiceOut *sw;
|
|
|
|
int enabled = 0;
|
|
|
|
|
2006-07-17 02:57:03 +08:00
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
2006-07-05 00:51:32 +08:00
|
|
|
if (sw->active) {
|
|
|
|
enabled = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-07-17 02:57:03 +08:00
|
|
|
audio_capture_maybe_changed (cap, enabled);
|
2006-07-05 00:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_detach_capture (HWVoiceOut *hw)
|
|
|
|
{
|
2006-07-17 02:57:03 +08:00
|
|
|
SWVoiceCap *sc = hw->cap_head.lh_first;
|
|
|
|
|
|
|
|
while (sc) {
|
|
|
|
SWVoiceCap *sc1 = sc->entries.le_next;
|
|
|
|
SWVoiceOut *sw = &sc->sw;
|
|
|
|
CaptureVoiceOut *cap = sc->cap;
|
|
|
|
int was_active = sw->active;
|
2006-07-05 00:51:32 +08:00
|
|
|
|
|
|
|
if (sw->rate) {
|
|
|
|
st_rate_stop (sw->rate);
|
|
|
|
sw->rate = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIST_REMOVE (sw, entries);
|
2006-07-17 02:57:03 +08:00
|
|
|
LIST_REMOVE (sc, entries);
|
|
|
|
qemu_free (sc);
|
|
|
|
if (was_active) {
|
|
|
|
/* We have removed soft voice from the capture:
|
|
|
|
this might have changed the overall status of the capture
|
|
|
|
since this might have been the only active voice */
|
|
|
|
audio_recalc_and_notify_capture (cap);
|
|
|
|
}
|
|
|
|
sc = sc1;
|
2006-07-05 00:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int audio_attach_capture (AudioState *s, HWVoiceOut *hw)
|
|
|
|
{
|
|
|
|
CaptureVoiceOut *cap;
|
|
|
|
|
|
|
|
audio_detach_capture (hw);
|
|
|
|
for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
|
2006-07-17 02:57:03 +08:00
|
|
|
SWVoiceCap *sc;
|
2006-07-05 00:51:32 +08:00
|
|
|
SWVoiceOut *sw;
|
2006-07-17 02:57:03 +08:00
|
|
|
HWVoiceOut *hw_cap = &cap->hw;
|
2006-07-05 00:51:32 +08:00
|
|
|
|
2006-07-17 02:57:03 +08:00
|
|
|
sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
|
|
|
|
if (!sc) {
|
2006-07-05 00:51:32 +08:00
|
|
|
dolog ("Could not allocate soft capture voice (%zu bytes)\n",
|
2006-07-17 02:57:03 +08:00
|
|
|
sizeof (*sc));
|
2006-07-05 00:51:32 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-17 02:57:03 +08:00
|
|
|
sc->cap = cap;
|
|
|
|
sw = &sc->sw;
|
2006-07-05 00:51:32 +08:00
|
|
|
sw->hw = hw_cap;
|
2006-07-17 02:57:03 +08:00
|
|
|
sw->info = hw->info;
|
2006-07-05 00:51:32 +08:00
|
|
|
sw->empty = 1;
|
|
|
|
sw->active = hw->enabled;
|
|
|
|
sw->conv = noop_conv;
|
|
|
|
sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
|
|
|
|
sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
|
|
|
|
if (!sw->rate) {
|
|
|
|
dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
|
|
|
|
qemu_free (sw);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
LIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
|
2006-07-17 02:57:03 +08:00
|
|
|
LIST_INSERT_HEAD (&hw->cap_head, sc, entries);
|
|
|
|
#ifdef DEBUG_CAPTURE
|
|
|
|
asprintf (&sw->name, "for %p %d,%d,%d",
|
|
|
|
hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
|
|
|
|
dolog ("Added %s active = %d\n", sw->name, sw->active);
|
|
|
|
#endif
|
2006-07-05 00:51:32 +08:00
|
|
|
if (sw->active) {
|
2006-07-17 02:57:03 +08:00
|
|
|
audio_capture_maybe_changed (cap, 1);
|
2006-07-05 00:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
/*
|
|
|
|
* Hard voice (capture)
|
|
|
|
*/
|
2005-11-06 02:55:28 +08:00
|
|
|
static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
SWVoiceIn *sw;
|
|
|
|
int m = hw->total_samples_captured;
|
|
|
|
|
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
|
|
|
if (sw->active) {
|
|
|
|
m = audio_MIN (m, sw->total_hw_samples_acquired);
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
return m;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
|
|
|
|
if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
|
|
|
|
dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
|
|
|
return 0;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
return live;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
/*
|
|
|
|
* Soft voice (capture)
|
|
|
|
*/
|
|
|
|
static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
|
|
|
|
{
|
|
|
|
HWVoiceIn *hw = sw->hw;
|
|
|
|
int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
|
|
|
|
int rpos;
|
|
|
|
|
|
|
|
if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
|
|
|
|
dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpos = hw->wpos - live;
|
|
|
|
if (rpos >= 0) {
|
|
|
|
return rpos;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
else {
|
2005-10-31 02:58:22 +08:00
|
|
|
return hw->samples + rpos;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
HWVoiceIn *hw = sw->hw;
|
|
|
|
int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
|
2005-11-21 00:24:34 +08:00
|
|
|
st_sample_t *src, *dst = sw->buf;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
|
|
|
|
|
|
|
|
live = hw->total_samples_captured - sw->total_hw_samples_acquired;
|
|
|
|
if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
|
|
|
|
dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
samples = size >> sw->info.shift;
|
|
|
|
if (!live) {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
swlim = (live * sw->ratio) >> 32;
|
|
|
|
swlim = audio_MIN (swlim, samples);
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
while (swlim) {
|
|
|
|
src = hw->conv_buf + rpos;
|
|
|
|
isamp = hw->wpos - rpos;
|
|
|
|
/* XXX: <= ? */
|
|
|
|
if (isamp <= 0) {
|
|
|
|
isamp = hw->samples - rpos;
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (!isamp) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
osamp = swlim;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (audio_bug (AUDIO_FUNC, osamp < 0)) {
|
|
|
|
dolog ("osamp=%d\n", osamp);
|
2005-11-06 02:55:28 +08:00
|
|
|
return 0;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
|
|
|
|
swlim -= osamp;
|
|
|
|
rpos = (rpos + isamp) % hw->samples;
|
|
|
|
dst += osamp;
|
|
|
|
ret += osamp;
|
|
|
|
total += isamp;
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-11-21 00:24:34 +08:00
|
|
|
sw->clip (buf, sw->buf, ret);
|
2005-10-31 02:58:22 +08:00
|
|
|
sw->total_hw_samples_acquired += total;
|
|
|
|
return ret << sw->info.shift;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
/*
|
|
|
|
* Hard voice (playback)
|
|
|
|
*/
|
2005-11-06 02:55:28 +08:00
|
|
|
static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
2005-11-06 02:55:28 +08:00
|
|
|
SWVoiceOut *sw;
|
|
|
|
int m = INT_MAX;
|
|
|
|
int nb_live = 0;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
|
|
|
if (sw->active || !sw->empty) {
|
|
|
|
m = audio_MIN (m, sw->total_hw_samples_mixed);
|
|
|
|
nb_live += 1;
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-11-06 02:55:28 +08:00
|
|
|
|
|
|
|
*nb_livep = nb_live;
|
|
|
|
return m;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
|
|
|
|
{
|
|
|
|
int smin;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
smin = audio_pcm_hw_find_min_out (hw, nb_live);
|
|
|
|
|
|
|
|
if (!*nb_live) {
|
|
|
|
return 0;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
else {
|
2005-10-31 02:58:22 +08:00
|
|
|
int live = smin;
|
|
|
|
|
|
|
|
if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
|
|
|
|
dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
|
|
|
return 0;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
return live;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
|
|
|
|
{
|
|
|
|
int nb_live;
|
|
|
|
int live;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
|
|
|
|
if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
|
|
|
|
dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
|
|
|
return 0;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
return live;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
/*
|
|
|
|
* Soft voice (playback)
|
|
|
|
*/
|
|
|
|
int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
|
|
|
|
int ret = 0, pos = 0, total = 0;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (!sw) {
|
|
|
|
return size;
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
hwsamples = sw->hw->samples;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
live = sw->total_hw_samples_mixed;
|
|
|
|
if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
|
|
|
|
dolog ("live=%d hw->samples=%d\n", live, hwsamples);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (live == hwsamples) {
|
2006-07-17 02:57:03 +08:00
|
|
|
#ifdef DEBUG_OUT
|
|
|
|
dolog ("%s is full %d\n", sw->name, live);
|
|
|
|
#endif
|
2005-10-31 02:58:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
wpos = (sw->hw->rpos + live) % hwsamples;
|
|
|
|
samples = size >> sw->info.shift;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
dead = hwsamples - live;
|
|
|
|
swlim = ((int64_t) dead << 32) / sw->ratio;
|
|
|
|
swlim = audio_MIN (swlim, samples);
|
|
|
|
if (swlim) {
|
|
|
|
sw->conv (sw->buf, buf, swlim, &sw->vol);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (swlim) {
|
|
|
|
dead = hwsamples - live;
|
|
|
|
left = hwsamples - wpos;
|
|
|
|
blck = audio_MIN (dead, left);
|
|
|
|
if (!blck) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
isamp = swlim;
|
|
|
|
osamp = blck;
|
|
|
|
st_rate_flow_mix (
|
|
|
|
sw->rate,
|
|
|
|
sw->buf + pos,
|
|
|
|
sw->hw->mix_buf + wpos,
|
|
|
|
&isamp,
|
|
|
|
&osamp
|
|
|
|
);
|
|
|
|
ret += isamp;
|
|
|
|
swlim -= isamp;
|
|
|
|
pos += isamp;
|
|
|
|
live += osamp;
|
|
|
|
wpos = (wpos + osamp) % hwsamples;
|
|
|
|
total += osamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw->total_hw_samples_mixed += total;
|
|
|
|
sw->empty = sw->total_hw_samples_mixed == 0;
|
|
|
|
|
|
|
|
#ifdef DEBUG_OUT
|
|
|
|
dolog (
|
2005-11-06 02:55:28 +08:00
|
|
|
"%s: write size %d ret %d total sw %d\n",
|
|
|
|
SW_NAME (sw),
|
2005-10-31 02:58:22 +08:00
|
|
|
size >> sw->info.shift,
|
|
|
|
ret,
|
2005-11-06 02:55:28 +08:00
|
|
|
sw->total_hw_samples_mixed
|
2005-10-31 02:58:22 +08:00
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret << sw->info.shift;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
#ifdef DEBUG_AUDIO
|
|
|
|
static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
|
|
|
|
cap, info->bits, info->sign, info->freq, info->nchannels);
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
#endif
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
#define DAC
|
|
|
|
#include "audio_template.h"
|
|
|
|
#undef DAC
|
|
|
|
#include "audio_template.h"
|
|
|
|
|
|
|
|
int AUD_write (SWVoiceOut *sw, void *buf, int size)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
int bytes;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (!sw) {
|
|
|
|
/* XXX: Consider options */
|
|
|
|
return size;
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (!sw->hw->enabled) {
|
2005-11-06 02:55:28 +08:00
|
|
|
dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
|
2004-11-08 02:04:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
bytes = sw->hw->pcm_ops->write (sw, buf, size);
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AUD_read (SWVoiceIn *sw, void *buf, int size)
|
|
|
|
{
|
|
|
|
int bytes;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (!sw) {
|
|
|
|
/* XXX: Consider options */
|
|
|
|
return size;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
if (!sw->hw->enabled) {
|
2005-11-06 02:55:28 +08:00
|
|
|
dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
|
2005-10-31 02:58:22 +08:00
|
|
|
return 0;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
bytes = sw->hw->pcm_ops->read (sw, buf, size);
|
|
|
|
return bytes;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
int AUD_get_buffer_size_out (SWVoiceOut *sw)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-11-06 02:55:28 +08:00
|
|
|
return sw->hw->samples << sw->hw->info.shift;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_set_active_out (SWVoiceOut *sw, int on)
|
|
|
|
{
|
|
|
|
HWVoiceOut *hw;
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (!sw) {
|
2004-11-08 02:04:02 +08:00
|
|
|
return;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
|
|
|
hw = sw->hw;
|
2005-10-31 02:58:22 +08:00
|
|
|
if (sw->active != on) {
|
|
|
|
SWVoiceOut *temp_sw;
|
2006-07-17 02:57:03 +08:00
|
|
|
SWVoiceCap *sc;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
if (on) {
|
|
|
|
hw->pending_disable = 0;
|
|
|
|
if (!hw->enabled) {
|
|
|
|
hw->enabled = 1;
|
|
|
|
hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (hw->enabled) {
|
|
|
|
int nb_active = 0;
|
|
|
|
|
|
|
|
for (temp_sw = hw->sw_head.lh_first; temp_sw;
|
|
|
|
temp_sw = temp_sw->entries.le_next) {
|
|
|
|
nb_active += temp_sw->active != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
hw->pending_disable = nb_active == 1;
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2006-07-17 02:57:03 +08:00
|
|
|
|
|
|
|
for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
|
|
|
sc->sw.active = hw->enabled;
|
2006-07-05 00:51:32 +08:00
|
|
|
if (hw->enabled) {
|
2006-07-17 02:57:03 +08:00
|
|
|
audio_capture_maybe_changed (sc->cap, 1);
|
2006-07-05 00:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
sw->active = on;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_set_active_in (SWVoiceIn *sw, int on)
|
|
|
|
{
|
|
|
|
HWVoiceIn *hw;
|
|
|
|
|
|
|
|
if (!sw) {
|
|
|
|
return;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
hw = sw->hw;
|
2004-11-08 02:04:02 +08:00
|
|
|
if (sw->active != on) {
|
2005-10-31 02:58:22 +08:00
|
|
|
SWVoiceIn *temp_sw;
|
|
|
|
|
2004-11-08 02:04:02 +08:00
|
|
|
if (on) {
|
|
|
|
if (!hw->enabled) {
|
|
|
|
hw->enabled = 1;
|
2005-10-31 02:58:22 +08:00
|
|
|
hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
sw->total_hw_samples_acquired = hw->total_samples_captured;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
else {
|
2005-10-31 02:58:22 +08:00
|
|
|
if (hw->enabled) {
|
2004-11-08 02:04:02 +08:00
|
|
|
int nb_active = 0;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
for (temp_sw = hw->sw_head.lh_first; temp_sw;
|
|
|
|
temp_sw = temp_sw->entries.le_next) {
|
|
|
|
nb_active += temp_sw->active != 0;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nb_active == 1) {
|
2005-10-31 02:58:22 +08:00
|
|
|
hw->enabled = 0;
|
|
|
|
hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sw->active = on;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
static int audio_get_avail (SWVoiceIn *sw)
|
|
|
|
{
|
|
|
|
int live;
|
|
|
|
|
|
|
|
if (!sw) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
|
|
|
|
if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
|
|
|
|
dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ldebug (
|
2006-06-26 02:15:32 +08:00
|
|
|
"%s: get_avail live %d ret %" PRId64 "\n",
|
2005-11-06 02:55:28 +08:00
|
|
|
SW_NAME (sw),
|
2005-10-31 02:58:22 +08:00
|
|
|
live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
|
|
|
|
);
|
|
|
|
|
|
|
|
return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int audio_get_free (SWVoiceOut *sw)
|
|
|
|
{
|
|
|
|
int live, dead;
|
|
|
|
|
|
|
|
if (!sw) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
live = sw->total_hw_samples_mixed;
|
|
|
|
|
|
|
|
if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
|
|
|
|
dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
|
2005-11-06 02:55:28 +08:00
|
|
|
return 0;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dead = sw->hw->samples - live;
|
|
|
|
|
|
|
|
#ifdef DEBUG_OUT
|
2006-06-26 02:15:32 +08:00
|
|
|
dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
|
2005-11-06 02:55:28 +08:00
|
|
|
SW_NAME (sw),
|
2005-10-31 02:58:22 +08:00
|
|
|
live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
|
2004-11-08 02:04:02 +08:00
|
|
|
#endif
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
|
|
|
|
}
|
|
|
|
|
2006-07-05 00:51:32 +08:00
|
|
|
static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (hw->enabled) {
|
2006-07-17 02:57:03 +08:00
|
|
|
SWVoiceCap *sc;
|
2006-07-05 00:51:32 +08:00
|
|
|
|
2006-07-17 02:57:03 +08:00
|
|
|
for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
|
|
|
SWVoiceOut *sw = &sc->sw;
|
2006-07-05 00:51:32 +08:00
|
|
|
int rpos2 = rpos;
|
|
|
|
|
|
|
|
n = samples;
|
|
|
|
while (n) {
|
|
|
|
int till_end_of_hw = hw->samples - rpos2;
|
|
|
|
int to_write = audio_MIN (till_end_of_hw, n);
|
|
|
|
int bytes = to_write << hw->info.shift;
|
|
|
|
int written;
|
|
|
|
|
|
|
|
sw->buf = hw->mix_buf + rpos2;
|
|
|
|
written = audio_pcm_sw_write (sw, NULL, bytes);
|
|
|
|
if (written - bytes) {
|
2006-07-17 02:57:03 +08:00
|
|
|
dolog ("Could not mix %d bytes into a capture "
|
|
|
|
"buffer, mixed %d\n",
|
|
|
|
bytes, written);
|
2006-07-05 00:51:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
n -= to_write;
|
|
|
|
rpos2 = (rpos2 + to_write) % hw->samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n = audio_MIN (samples, hw->samples - rpos);
|
|
|
|
mixeng_clear (hw->mix_buf + rpos, n);
|
|
|
|
mixeng_clear (hw->mix_buf, samples - n);
|
|
|
|
}
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
static void audio_run_out (AudioState *s)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
|
|
|
HWVoiceOut *hw = NULL;
|
|
|
|
SWVoiceOut *sw;
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
|
2005-10-31 02:58:22 +08:00
|
|
|
int played;
|
2006-07-05 00:51:32 +08:00
|
|
|
int live, free, nb_live, cleanup_required, prev_rpos;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
|
|
|
|
if (!nb_live) {
|
|
|
|
live = 0;
|
|
|
|
}
|
2005-11-06 02:55:28 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
|
|
|
|
dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
2005-11-06 02:55:28 +08:00
|
|
|
continue;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hw->pending_disable && !nb_live) {
|
2006-07-17 02:57:03 +08:00
|
|
|
SWVoiceCap *sc;
|
2005-10-31 02:58:22 +08:00
|
|
|
#ifdef DEBUG_OUT
|
|
|
|
dolog ("Disabling voice\n");
|
2004-11-08 02:04:02 +08:00
|
|
|
#endif
|
2005-10-31 02:58:22 +08:00
|
|
|
hw->enabled = 0;
|
|
|
|
hw->pending_disable = 0;
|
|
|
|
hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
|
2006-07-17 02:57:03 +08:00
|
|
|
for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
|
|
|
sc->sw.active = 0;
|
|
|
|
audio_recalc_and_notify_capture (sc->cap);
|
2006-07-05 00:51:32 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!live) {
|
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
|
|
|
if (sw->active) {
|
|
|
|
free = audio_get_free (sw);
|
|
|
|
if (free > 0) {
|
|
|
|
sw->callback.fn (sw->callback.opaque, free);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-07-05 00:51:32 +08:00
|
|
|
prev_rpos = hw->rpos;
|
2005-10-31 02:58:22 +08:00
|
|
|
played = hw->pcm_ops->run_out (hw);
|
|
|
|
if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
|
|
|
|
dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
|
|
|
|
hw->rpos, hw->samples, played);
|
|
|
|
hw->rpos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_OUT
|
2005-11-06 02:55:28 +08:00
|
|
|
dolog ("played=%d\n", played);
|
2004-11-08 02:04:02 +08:00
|
|
|
#endif
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
if (played) {
|
|
|
|
hw->ts_helper += played;
|
2006-07-05 00:51:32 +08:00
|
|
|
audio_capture_mix_and_clear (hw, prev_rpos, played);
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
cleanup_required = 0;
|
2005-10-31 02:58:22 +08:00
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
|
|
|
if (!sw->active && sw->empty) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
|
|
|
|
dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
|
|
|
|
played, sw->total_hw_samples_mixed);
|
|
|
|
played = sw->total_hw_samples_mixed;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw->total_hw_samples_mixed -= played;
|
|
|
|
|
|
|
|
if (!sw->total_hw_samples_mixed) {
|
|
|
|
sw->empty = 1;
|
2005-11-06 02:55:28 +08:00
|
|
|
cleanup_required |= !sw->active && !sw->callback.fn;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sw->active) {
|
|
|
|
free = audio_get_free (sw);
|
|
|
|
if (free > 0) {
|
|
|
|
sw->callback.fn (sw->callback.opaque, free);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-11-06 02:55:28 +08:00
|
|
|
|
|
|
|
if (cleanup_required) {
|
2006-07-17 02:57:03 +08:00
|
|
|
SWVoiceOut *sw1;
|
|
|
|
|
|
|
|
sw = hw->sw_head.lh_first;
|
|
|
|
while (sw) {
|
|
|
|
sw1 = sw->entries.le_next;
|
2005-11-06 02:55:28 +08:00
|
|
|
if (!sw->active && !sw->callback.fn) {
|
|
|
|
#ifdef DEBUG_PLIVE
|
|
|
|
dolog ("Finishing with old voice\n");
|
|
|
|
#endif
|
|
|
|
audio_close_out (s, sw);
|
|
|
|
}
|
2006-07-17 02:57:03 +08:00
|
|
|
sw = sw1;
|
2005-11-06 02:55:28 +08:00
|
|
|
}
|
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
static void audio_run_in (AudioState *s)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
|
|
|
HWVoiceIn *hw = NULL;
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
|
2005-10-31 02:58:22 +08:00
|
|
|
SWVoiceIn *sw;
|
|
|
|
int captured, min;
|
|
|
|
|
|
|
|
captured = hw->pcm_ops->run_in (hw);
|
|
|
|
|
|
|
|
min = audio_pcm_hw_find_min_in (hw);
|
|
|
|
hw->total_samples_captured += captured - min;
|
|
|
|
hw->ts_helper += captured;
|
|
|
|
|
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
|
|
|
sw->total_hw_samples_acquired -= min;
|
|
|
|
|
|
|
|
if (sw->active) {
|
|
|
|
int avail;
|
|
|
|
|
|
|
|
avail = audio_get_avail (sw);
|
|
|
|
if (avail > 0) {
|
|
|
|
sw->callback.fn (sw->callback.opaque, avail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-05 00:51:32 +08:00
|
|
|
static void audio_run_capture (AudioState *s)
|
|
|
|
{
|
|
|
|
CaptureVoiceOut *cap;
|
|
|
|
|
|
|
|
for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
|
|
|
|
int live, rpos, captured;
|
|
|
|
HWVoiceOut *hw = &cap->hw;
|
|
|
|
SWVoiceOut *sw;
|
|
|
|
|
|
|
|
captured = live = audio_pcm_hw_get_live_out (hw);
|
|
|
|
rpos = hw->rpos;
|
|
|
|
while (live) {
|
|
|
|
int left = hw->samples - rpos;
|
|
|
|
int to_capture = audio_MIN (live, left);
|
|
|
|
st_sample_t *src;
|
|
|
|
struct capture_callback *cb;
|
|
|
|
|
|
|
|
src = hw->mix_buf + rpos;
|
|
|
|
hw->clip (cap->buf, src, to_capture);
|
|
|
|
mixeng_clear (src, to_capture);
|
|
|
|
|
|
|
|
for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
|
|
|
|
cb->ops.capture (cb->opaque, cap->buf,
|
|
|
|
to_capture << hw->info.shift);
|
|
|
|
}
|
|
|
|
rpos = (rpos + to_capture) % hw->samples;
|
|
|
|
live -= to_capture;
|
|
|
|
}
|
|
|
|
hw->rpos = rpos;
|
|
|
|
|
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
|
|
|
if (!sw->active && sw->empty) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
|
|
|
|
dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
|
|
|
|
captured, sw->total_hw_samples_mixed);
|
|
|
|
captured = sw->total_hw_samples_mixed;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw->total_hw_samples_mixed -= captured;
|
|
|
|
sw->empty = sw->total_hw_samples_mixed == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-21 00:24:34 +08:00
|
|
|
static void audio_timer (void *opaque)
|
|
|
|
{
|
|
|
|
AudioState *s = opaque;
|
|
|
|
|
|
|
|
audio_run_out (s);
|
|
|
|
audio_run_in (s);
|
2006-07-05 00:51:32 +08:00
|
|
|
audio_run_capture (s);
|
2005-11-21 00:24:34 +08:00
|
|
|
|
|
|
|
qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
|
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
static struct audio_option audio_options[] = {
|
|
|
|
/* DAC */
|
2005-11-06 02:55:28 +08:00
|
|
|
{"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_out.enabled,
|
2005-10-31 02:58:22 +08:00
|
|
|
"Use fixed settings for host DAC", NULL, 0},
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
{"DAC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_out.settings.freq,
|
2005-10-31 02:58:22 +08:00
|
|
|
"Frequency for fixed host DAC", NULL, 0},
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
{"DAC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_out.settings.fmt,
|
2005-10-31 02:58:22 +08:00
|
|
|
"Format for fixed host DAC", NULL, 0},
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
{"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_out.settings.nchannels,
|
2005-10-31 02:58:22 +08:00
|
|
|
"Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
{"DAC_VOICES", AUD_OPT_INT, &conf.fixed_out.nb_voices,
|
2005-10-31 02:58:22 +08:00
|
|
|
"Number of voices for DAC", NULL, 0},
|
|
|
|
|
|
|
|
/* ADC */
|
2005-11-06 02:55:28 +08:00
|
|
|
{"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_in.enabled,
|
2005-10-31 02:58:22 +08:00
|
|
|
"Use fixed settings for host ADC", NULL, 0},
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
{"ADC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_in.settings.freq,
|
|
|
|
"Frequency for fixed host ADC", NULL, 0},
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
{"ADC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_in.settings.fmt,
|
|
|
|
"Format for fixed host ADC", NULL, 0},
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
{"ADC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_in.settings.nchannels,
|
2005-10-31 02:58:22 +08:00
|
|
|
"Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
{"ADC_VOICES", AUD_OPT_INT, &conf.fixed_in.nb_voices,
|
2005-10-31 02:58:22 +08:00
|
|
|
"Number of voices for ADC", NULL, 0},
|
|
|
|
|
|
|
|
/* Misc */
|
2008-11-13 04:36:27 +08:00
|
|
|
{"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hertz,
|
2005-11-06 02:55:28 +08:00
|
|
|
"Timer period in HZ (0 - use lowest possible)", NULL, 0},
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
{"PLIVE", AUD_OPT_BOOL, &conf.plive,
|
2005-10-31 02:58:22 +08:00
|
|
|
"(undocumented)", NULL, 0},
|
|
|
|
|
2005-11-11 08:04:19 +08:00
|
|
|
{"LOG_TO_MONITOR", AUD_OPT_BOOL, &conf.log_to_monitor,
|
2008-04-08 03:46:58 +08:00
|
|
|
"print logging messages to monitor instead of stderr", NULL, 0},
|
2005-11-11 08:04:19 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
{NULL, 0, NULL, NULL, NULL, 0}
|
2004-11-08 02:04:02 +08:00
|
|
|
};
|
|
|
|
|
2005-11-21 00:24:34 +08:00
|
|
|
static void audio_pp_nb_voices (const char *typ, int nb)
|
|
|
|
{
|
|
|
|
switch (nb) {
|
|
|
|
case 0:
|
|
|
|
printf ("Does not support %s\n", typ);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
printf ("One %s voice\n", typ);
|
|
|
|
break;
|
|
|
|
case INT_MAX:
|
|
|
|
printf ("Theoretically supports many %s voices\n", typ);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf ("Theoretically supports upto %d %s voices\n", nb, typ);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
void AUD_help (void)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
audio_process_options ("AUDIO", audio_options);
|
|
|
|
for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
|
|
|
|
struct audio_driver *d = drvtab[i];
|
|
|
|
if (d->options) {
|
|
|
|
audio_process_options (d->name, d->options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("Audio options:\n");
|
|
|
|
audio_print_options ("AUDIO", audio_options);
|
|
|
|
printf ("\n");
|
|
|
|
|
|
|
|
printf ("Available drivers:\n");
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
|
|
|
|
struct audio_driver *d = drvtab[i];
|
|
|
|
|
|
|
|
printf ("Name: %s\n", d->name);
|
|
|
|
printf ("Description: %s\n", d->descr);
|
|
|
|
|
2005-11-21 00:24:34 +08:00
|
|
|
audio_pp_nb_voices ("playback", d->max_voices_out);
|
|
|
|
audio_pp_nb_voices ("capture", d->max_voices_in);
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
if (d->options) {
|
|
|
|
printf ("Options:\n");
|
|
|
|
audio_print_options (d->name, d->options);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf ("No options\n");
|
|
|
|
}
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf (
|
|
|
|
"Options are settable through environment variables.\n"
|
|
|
|
"Example:\n"
|
|
|
|
#ifdef _WIN32
|
|
|
|
" set QEMU_AUDIO_DRV=wav\n"
|
2005-11-21 00:24:34 +08:00
|
|
|
" set QEMU_WAV_PATH=c:\\tune.wav\n"
|
2005-10-31 02:58:22 +08:00
|
|
|
#else
|
|
|
|
" export QEMU_AUDIO_DRV=wav\n"
|
|
|
|
" export QEMU_WAV_PATH=$HOME/tune.wav\n"
|
|
|
|
"(for csh replace export with setenv in the above)\n"
|
|
|
|
#endif
|
|
|
|
" qemu ...\n\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
static int audio_driver_init (AudioState *s, struct audio_driver *drv)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
if (drv->options) {
|
|
|
|
audio_process_options (drv->name, drv->options);
|
|
|
|
}
|
2005-11-06 02:55:28 +08:00
|
|
|
s->drv_opaque = drv->init ();
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
if (s->drv_opaque) {
|
2005-11-21 00:24:34 +08:00
|
|
|
audio_init_nb_voices_out (s, drv);
|
|
|
|
audio_init_nb_voices_in (s, drv);
|
2005-11-06 02:55:28 +08:00
|
|
|
s->drv = drv;
|
2005-10-31 02:58:22 +08:00
|
|
|
return 0;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
else {
|
2005-10-31 02:58:22 +08:00
|
|
|
dolog ("Could not init `%s' audio driver\n", drv->name);
|
|
|
|
return -1;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-11 08:04:19 +08:00
|
|
|
static void audio_vm_change_state_handler (void *opaque, int running)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-11-06 02:55:28 +08:00
|
|
|
AudioState *s = opaque;
|
2005-10-31 02:58:22 +08:00
|
|
|
HWVoiceOut *hwo = NULL;
|
|
|
|
HWVoiceIn *hwi = NULL;
|
2005-11-11 08:04:19 +08:00
|
|
|
int op = running ? VOICE_ENABLE : VOICE_DISABLE;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2005-11-11 08:04:19 +08:00
|
|
|
while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
|
|
|
|
hwo->pcm_ops->ctl_out (hwo, op);
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-11-11 08:04:19 +08:00
|
|
|
while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
|
|
|
|
hwi->pcm_ops->ctl_in (hwi, op);
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_atexit (void)
|
|
|
|
{
|
2005-11-06 02:55:28 +08:00
|
|
|
AudioState *s = &glob_audio_state;
|
2005-10-31 02:58:22 +08:00
|
|
|
HWVoiceOut *hwo = NULL;
|
|
|
|
HWVoiceIn *hwi = NULL;
|
|
|
|
|
2005-11-21 00:24:34 +08:00
|
|
|
while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
|
2006-07-17 02:57:03 +08:00
|
|
|
SWVoiceCap *sc;
|
2006-07-05 00:51:32 +08:00
|
|
|
|
2005-11-21 00:24:34 +08:00
|
|
|
hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
|
2005-10-31 02:58:22 +08:00
|
|
|
hwo->pcm_ops->fini_out (hwo);
|
2006-07-05 00:51:32 +08:00
|
|
|
|
2006-07-17 02:57:03 +08:00
|
|
|
for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
|
|
|
CaptureVoiceOut *cap = sc->cap;
|
|
|
|
struct capture_callback *cb;
|
|
|
|
|
|
|
|
for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
|
|
|
|
cb->ops.destroy (cb->opaque);
|
|
|
|
}
|
2006-07-05 00:51:32 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-11-21 00:24:34 +08:00
|
|
|
while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
|
|
|
|
hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
|
2005-10-31 02:58:22 +08:00
|
|
|
hwi->pcm_ops->fini_in (hwi);
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-11-06 02:55:28 +08:00
|
|
|
|
|
|
|
if (s->drv) {
|
|
|
|
s->drv->fini (s->drv_opaque);
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_save (QEMUFile *f, void *opaque)
|
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
(void) f;
|
|
|
|
(void) opaque;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int audio_load (QEMUFile *f, void *opaque, int version_id)
|
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
(void) f;
|
|
|
|
(void) opaque;
|
|
|
|
|
|
|
|
if (version_id != 1) {
|
2004-11-08 02:04:02 +08:00
|
|
|
return -EINVAL;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
void AUD_register_card (AudioState *s, const char *name, QEMUSoundCard *card)
|
|
|
|
{
|
|
|
|
card->audio = s;
|
|
|
|
card->name = qemu_strdup (name);
|
|
|
|
memset (&card->entries, 0, sizeof (card->entries));
|
|
|
|
LIST_INSERT_HEAD (&s->card_head, card, entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_remove_card (QEMUSoundCard *card)
|
|
|
|
{
|
|
|
|
LIST_REMOVE (card, entries);
|
|
|
|
card->audio = NULL;
|
|
|
|
qemu_free (card->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioState *AUD_init (void)
|
2004-11-08 02:04:02 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
size_t i;
|
2004-11-08 02:04:02 +08:00
|
|
|
int done = 0;
|
|
|
|
const char *drvname;
|
2005-11-06 02:55:28 +08:00
|
|
|
AudioState *s = &glob_audio_state;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2005-11-21 00:24:34 +08:00
|
|
|
LIST_INIT (&s->hw_head_out);
|
|
|
|
LIST_INIT (&s->hw_head_in);
|
2006-07-05 00:51:32 +08:00
|
|
|
LIST_INIT (&s->cap_head);
|
2005-11-21 00:24:34 +08:00
|
|
|
atexit (audio_atexit);
|
|
|
|
|
|
|
|
s->ts = qemu_new_timer (vm_clock, audio_timer, s);
|
|
|
|
if (!s->ts) {
|
|
|
|
dolog ("Could not create audio timer\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
audio_process_options ("AUDIO", audio_options);
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
s->nb_hw_voices_out = conf.fixed_out.nb_voices;
|
|
|
|
s->nb_hw_voices_in = conf.fixed_in.nb_voices;
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
if (s->nb_hw_voices_out <= 0) {
|
2005-11-21 00:24:34 +08:00
|
|
|
dolog ("Bogus number of playback voices %d, setting to 1\n",
|
2005-10-31 02:58:22 +08:00
|
|
|
s->nb_hw_voices_out);
|
|
|
|
s->nb_hw_voices_out = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->nb_hw_voices_in <= 0) {
|
2005-11-21 00:24:34 +08:00
|
|
|
dolog ("Bogus number of capture voices %d, setting to 0\n",
|
2005-10-31 02:58:22 +08:00
|
|
|
s->nb_hw_voices_in);
|
2005-11-21 00:24:34 +08:00
|
|
|
s->nb_hw_voices_in = 0;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
|
|
|
int def;
|
|
|
|
drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
|
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
|
|
|
|
if (drvname) {
|
|
|
|
int found = 0;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2004-11-08 02:04:02 +08:00
|
|
|
for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
|
|
|
|
if (!strcmp (drvname, drvtab[i]->name)) {
|
2005-11-06 02:55:28 +08:00
|
|
|
done = !audio_driver_init (s, drvtab[i]);
|
2004-11-08 02:04:02 +08:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2004-11-08 02:04:02 +08:00
|
|
|
if (!found) {
|
|
|
|
dolog ("Unknown audio driver `%s'\n", drvname);
|
2005-10-31 02:58:22 +08:00
|
|
|
dolog ("Run with -audio-help to list available drivers\n");
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!done) {
|
|
|
|
for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
|
2005-10-31 02:58:22 +08:00
|
|
|
if (drvtab[i]->can_be_default) {
|
2005-11-06 02:55:28 +08:00
|
|
|
done = !audio_driver_init (s, drvtab[i]);
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!done) {
|
2005-11-06 02:55:28 +08:00
|
|
|
done = !audio_driver_init (s, &no_audio_driver);
|
|
|
|
if (!done) {
|
|
|
|
dolog ("Could not initialize audio subsystem\n");
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
else {
|
2005-11-06 02:55:28 +08:00
|
|
|
dolog ("warning: Using timer based audio emulation\n");
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
if (done) {
|
2005-11-21 00:24:34 +08:00
|
|
|
VMChangeStateEntry *e;
|
|
|
|
|
2008-11-13 04:36:27 +08:00
|
|
|
if (conf.period.hertz <= 0) {
|
|
|
|
if (conf.period.hertz < 0) {
|
2005-11-06 02:55:28 +08:00
|
|
|
dolog ("warning: Timer period is negative - %d "
|
|
|
|
"treating as zero\n",
|
2008-11-13 04:36:27 +08:00
|
|
|
conf.period.hertz);
|
2005-11-06 02:55:28 +08:00
|
|
|
}
|
|
|
|
conf.period.ticks = 1;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2005-11-06 02:55:28 +08:00
|
|
|
else {
|
2008-11-13 04:36:27 +08:00
|
|
|
conf.period.ticks = ticks_per_sec / conf.period.hertz;
|
2005-11-06 02:55:28 +08:00
|
|
|
}
|
|
|
|
|
2005-11-21 00:24:34 +08:00
|
|
|
e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
|
|
|
|
if (!e) {
|
|
|
|
dolog ("warning: Could not register change state handler\n"
|
|
|
|
"(Audio can continue looping even after stopping the VM)\n");
|
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
else {
|
2005-11-06 02:55:28 +08:00
|
|
|
qemu_del_timer (s->ts);
|
|
|
|
return NULL;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
LIST_INIT (&s->card_head);
|
|
|
|
register_savevm ("audio", 0, 1, audio_save, audio_load, s);
|
|
|
|
qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
|
|
|
|
return s;
|
2004-11-08 02:04:02 +08:00
|
|
|
}
|
2006-07-05 00:51:32 +08:00
|
|
|
|
2006-07-17 02:57:03 +08:00
|
|
|
CaptureVoiceOut *AUD_add_capture (
|
2006-07-05 00:51:32 +08:00
|
|
|
AudioState *s,
|
|
|
|
audsettings_t *as,
|
|
|
|
struct audio_capture_ops *ops,
|
|
|
|
void *cb_opaque
|
|
|
|
)
|
|
|
|
{
|
|
|
|
CaptureVoiceOut *cap;
|
|
|
|
struct capture_callback *cb;
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
/* XXX suppress */
|
|
|
|
s = &glob_audio_state;
|
|
|
|
}
|
|
|
|
|
2006-07-17 02:57:03 +08:00
|
|
|
if (audio_validate_settings (as)) {
|
2006-07-05 00:51:32 +08:00
|
|
|
dolog ("Invalid settings were passed when trying to add capture\n");
|
|
|
|
audio_print_settings (as);
|
2006-07-17 02:57:03 +08:00
|
|
|
goto err0;
|
2006-07-05 00:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
|
|
|
|
if (!cb) {
|
|
|
|
dolog ("Could not allocate capture callback information, size %zu\n",
|
|
|
|
sizeof (*cb));
|
|
|
|
goto err0;
|
|
|
|
}
|
|
|
|
cb->ops = *ops;
|
|
|
|
cb->opaque = cb_opaque;
|
|
|
|
|
2006-07-05 05:47:22 +08:00
|
|
|
cap = audio_pcm_capture_find_specific (s, as);
|
2006-07-05 00:51:32 +08:00
|
|
|
if (cap) {
|
|
|
|
LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
|
2006-07-17 02:57:03 +08:00
|
|
|
return cap;
|
2006-07-05 00:51:32 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
HWVoiceOut *hw;
|
|
|
|
CaptureVoiceOut *cap;
|
|
|
|
|
|
|
|
cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
|
|
|
|
if (!cap) {
|
|
|
|
dolog ("Could not allocate capture voice, size %zu\n",
|
|
|
|
sizeof (*cap));
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hw = &cap->hw;
|
|
|
|
LIST_INIT (&hw->sw_head);
|
|
|
|
LIST_INIT (&cap->cb_head);
|
|
|
|
|
|
|
|
/* XXX find a more elegant way */
|
|
|
|
hw->samples = 4096 * 4;
|
|
|
|
hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
|
|
|
|
sizeof (st_sample_t));
|
|
|
|
if (!hw->mix_buf) {
|
|
|
|
dolog ("Could not allocate capture mix buffer (%d samples)\n",
|
|
|
|
hw->samples);
|
|
|
|
goto err2;
|
|
|
|
}
|
|
|
|
|
2006-07-05 05:47:22 +08:00
|
|
|
audio_pcm_init_info (&hw->info, as);
|
2006-07-05 00:51:32 +08:00
|
|
|
|
|
|
|
cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
|
|
|
|
if (!cap->buf) {
|
|
|
|
dolog ("Could not allocate capture buffer "
|
|
|
|
"(%d samples, each %d bytes)\n",
|
|
|
|
hw->samples, 1 << hw->info.shift);
|
|
|
|
goto err3;
|
|
|
|
}
|
|
|
|
|
|
|
|
hw->clip = mixeng_clip
|
|
|
|
[hw->info.nchannels == 2]
|
|
|
|
[hw->info.sign]
|
2006-07-05 05:47:22 +08:00
|
|
|
[hw->info.swap_endianness]
|
2007-02-18 06:19:29 +08:00
|
|
|
[audio_bits_to_index (hw->info.bits)];
|
2006-07-05 00:51:32 +08:00
|
|
|
|
|
|
|
LIST_INSERT_HEAD (&s->cap_head, cap, entries);
|
|
|
|
LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
|
|
|
|
|
|
|
|
hw = NULL;
|
|
|
|
while ((hw = audio_pcm_hw_find_any_out (s, hw))) {
|
|
|
|
audio_attach_capture (s, hw);
|
|
|
|
}
|
2006-07-17 02:57:03 +08:00
|
|
|
return cap;
|
2006-07-05 00:51:32 +08:00
|
|
|
|
|
|
|
err3:
|
|
|
|
qemu_free (cap->hw.mix_buf);
|
|
|
|
err2:
|
|
|
|
qemu_free (cap);
|
|
|
|
err1:
|
|
|
|
qemu_free (cb);
|
|
|
|
err0:
|
2006-07-17 02:57:03 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
|
|
|
|
{
|
|
|
|
struct capture_callback *cb;
|
|
|
|
|
|
|
|
for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
|
|
|
|
if (cb->opaque == cb_opaque) {
|
|
|
|
cb->ops.destroy (cb_opaque);
|
|
|
|
LIST_REMOVE (cb, entries);
|
|
|
|
qemu_free (cb);
|
|
|
|
|
|
|
|
if (!cap->cb_head.lh_first) {
|
|
|
|
SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
|
2006-07-19 05:09:59 +08:00
|
|
|
|
2006-07-17 02:57:03 +08:00
|
|
|
while (sw) {
|
2006-07-19 05:09:59 +08:00
|
|
|
SWVoiceCap *sc = (SWVoiceCap *) sw;
|
2006-07-17 02:57:03 +08:00
|
|
|
#ifdef DEBUG_CAPTURE
|
|
|
|
dolog ("freeing %s\n", sw->name);
|
|
|
|
#endif
|
2006-07-19 05:09:59 +08:00
|
|
|
|
2006-07-17 02:57:03 +08:00
|
|
|
sw1 = sw->entries.le_next;
|
|
|
|
if (sw->rate) {
|
|
|
|
st_rate_stop (sw->rate);
|
|
|
|
sw->rate = NULL;
|
|
|
|
}
|
|
|
|
LIST_REMOVE (sw, entries);
|
2006-07-19 05:09:59 +08:00
|
|
|
LIST_REMOVE (sc, entries);
|
|
|
|
qemu_free (sc);
|
2006-07-17 02:57:03 +08:00
|
|
|
sw = sw1;
|
|
|
|
}
|
|
|
|
LIST_REMOVE (cap, entries);
|
|
|
|
qemu_free (cap);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2006-07-05 00:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
2008-05-04 18:21:03 +08:00
|
|
|
|
|
|
|
void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
|
|
|
|
{
|
|
|
|
if (sw) {
|
|
|
|
sw->vol.mute = mute;
|
|
|
|
sw->vol.l = nominal_volume.l * lvol / 255;
|
|
|
|
sw->vol.r = nominal_volume.r * rvol / 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
|
|
|
|
{
|
|
|
|
if (sw) {
|
|
|
|
sw->vol.mute = mute;
|
|
|
|
sw->vol.l = nominal_volume.l * lvol / 255;
|
|
|
|
sw->vol.r = nominal_volume.r * rvol / 255;
|
|
|
|
}
|
|
|
|
}
|