2004-11-07 18:04:02 +00:00
|
|
|
/*
|
|
|
|
* QEMU Audio subsystem
|
2005-10-30 18:58:22 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2005 Vassili Karpov (malc)
|
|
|
|
*
|
2004-11-07 18:04:02 +00: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.
|
|
|
|
*/
|
2019-05-23 14:35:07 +00:00
|
|
|
|
2016-01-18 17:33:52 +00:00
|
|
|
#include "qemu/osdep.h"
|
2007-11-17 17:14:51 +00:00
|
|
|
#include "hw/hw.h"
|
|
|
|
#include "audio.h"
|
2012-12-17 17:19:49 +00:00
|
|
|
#include "monitor/monitor.h"
|
2012-12-17 17:20:00 +00:00
|
|
|
#include "qemu/timer.h"
|
2019-03-08 22:34:15 +00:00
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "qapi/qobject-input-visitor.h"
|
|
|
|
#include "qapi/qapi-visit-audio.h"
|
2012-12-17 17:20:04 +00:00
|
|
|
#include "sysemu/sysemu.h"
|
2016-03-20 17:16:19 +00:00
|
|
|
#include "qemu/cutils.h"
|
2019-05-23 14:35:07 +00:00
|
|
|
#include "qemu/module.h"
|
2017-02-02 05:50:54 +00:00
|
|
|
#include "sysemu/replay.h"
|
2018-07-02 16:15:24 +00:00
|
|
|
#include "trace.h"
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
#define AUDIO_CAP "audio"
|
|
|
|
#include "audio_int.h"
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
/* #define DEBUG_LIVE */
|
|
|
|
/* #define DEBUG_OUT */
|
2006-07-04 16:51:32 +00:00
|
|
|
/* #define DEBUG_CAPTURE */
|
2009-09-11 22:28:45 +00:00
|
|
|
/* #define DEBUG_POLL */
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-11-05 18:55:28 +00:00
|
|
|
#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
|
|
|
|
|
2009-07-27 14:13:25 +00:00
|
|
|
|
|
|
|
/* Order of CONFIG_AUDIO_DRIVERS is import.
|
|
|
|
The 1st one is the one used by default, that is the reason
|
|
|
|
that we generate the list.
|
|
|
|
*/
|
2019-03-08 22:34:15 +00:00
|
|
|
const char *audio_prio_list[] = {
|
2018-03-06 07:40:47 +00:00
|
|
|
"spice",
|
2009-07-27 14:13:25 +00:00
|
|
|
CONFIG_AUDIO_DRIVERS
|
2018-03-06 07:40:47 +00:00
|
|
|
"none",
|
|
|
|
"wav",
|
2019-03-08 22:34:15 +00:00
|
|
|
NULL
|
2005-10-30 18:58:22 +00:00
|
|
|
};
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2018-03-06 07:40:47 +00:00
|
|
|
static QLIST_HEAD(, audio_driver) audio_drivers;
|
2019-03-08 22:34:15 +00:00
|
|
|
static AudiodevListHead audiodevs = QSIMPLEQ_HEAD_INITIALIZER(audiodevs);
|
2018-03-06 07:40:47 +00:00
|
|
|
|
|
|
|
void audio_driver_register(audio_driver *drv)
|
|
|
|
{
|
|
|
|
QLIST_INSERT_HEAD(&audio_drivers, drv, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
audio_driver *audio_driver_lookup(const char *name)
|
|
|
|
{
|
|
|
|
struct audio_driver *d;
|
|
|
|
|
|
|
|
QLIST_FOREACH(d, &audio_drivers, next) {
|
|
|
|
if (strcmp(name, d->name) == 0) {
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 07:40:48 +00:00
|
|
|
|
|
|
|
audio_module_load_one(name);
|
|
|
|
QLIST_FOREACH(d, &audio_drivers, next) {
|
|
|
|
if (strcmp(name, d->name) == 0) {
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-06 07:40:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-11-05 18:55:28 +00:00
|
|
|
static AudioState glob_audio_state;
|
|
|
|
|
2011-01-05 00:05:47 +00:00
|
|
|
const struct mixeng_volume nominal_volume = {
|
2009-07-20 18:57:00 +00:00
|
|
|
.mute = 0,
|
2005-10-30 18:58:22 +00:00
|
|
|
#ifdef FLOAT_MIXENG
|
2009-07-20 18:57:00 +00:00
|
|
|
.r = 1.0,
|
|
|
|
.l = 1.0,
|
2005-10-30 18:58:22 +00:00
|
|
|
#else
|
2009-07-20 18:57:00 +00:00
|
|
|
.r = 1ULL << 32,
|
|
|
|
.l = 1ULL << 32,
|
2005-10-30 18:58:22 +00:00
|
|
|
#endif
|
2004-11-07 18:04:02 +00:00
|
|
|
};
|
|
|
|
|
2005-10-30 18:58:22 +00: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-07 18:04:02 +00:00
|
|
|
{
|
2005-10-30 18:58:22 +00:00
|
|
|
if (cond) {
|
|
|
|
static int shown;
|
|
|
|
|
2006-07-04 16:51:32 +00:00
|
|
|
AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
|
2005-10-30 18:58:22 +00:00
|
|
|
if (!shown) {
|
|
|
|
shown = 1;
|
|
|
|
AUD_log (NULL, "Save all your work and restart without audio\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-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
return cond;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
#endif
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2007-02-17 22:19:29 +00: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-05 18:55:28 +00: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 00:04:19 +00:00
|
|
|
AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
|
2005-11-05 18:55:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-08-21 03:09:37 +00:00
|
|
|
return g_malloc0 (len);
|
2005-11-05 18:55:28 +00:00
|
|
|
}
|
|
|
|
|
2005-11-11 00:04:19 +00:00
|
|
|
void AUD_vlog (const char *cap, const char *fmt, va_list ap)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2015-06-12 12:33:02 +00:00
|
|
|
if (cap) {
|
|
|
|
fprintf(stderr, "%s: ", cap);
|
2005-11-11 00:04:19 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 12:33:02 +00:00
|
|
|
vfprintf(stderr, fmt, ap);
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2005-11-11 00:04:19 +00:00
|
|
|
void AUD_log (const char *cap, const char *fmt, ...)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2005-11-11 00:04:19 +00:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
AUD_vlog (cap, fmt, ap);
|
|
|
|
va_end (ap);
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 22:48:44 +00:00
|
|
|
static void audio_print_settings (struct audsettings *as)
|
2005-11-05 18:55:28 +00:00
|
|
|
{
|
|
|
|
dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
|
|
|
|
|
|
|
|
switch (as->fmt) {
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_S8:
|
2005-11-05 18:55:28 +00:00
|
|
|
AUD_log (NULL, "S8");
|
|
|
|
break;
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_U8:
|
2005-11-05 18:55:28 +00:00
|
|
|
AUD_log (NULL, "U8");
|
|
|
|
break;
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_S16:
|
2005-11-05 18:55:28 +00:00
|
|
|
AUD_log (NULL, "S16");
|
|
|
|
break;
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_U16:
|
2005-11-05 18:55:28 +00:00
|
|
|
AUD_log (NULL, "U16");
|
|
|
|
break;
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_S32:
|
2008-06-22 14:10:45 +00:00
|
|
|
AUD_log (NULL, "S32");
|
|
|
|
break;
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_U32:
|
2008-06-22 14:10:45 +00:00
|
|
|
AUD_log (NULL, "U32");
|
|
|
|
break;
|
2005-11-05 18:55:28 +00:00
|
|
|
default:
|
|
|
|
AUD_log (NULL, "invalid(%d)", as->fmt);
|
|
|
|
break;
|
|
|
|
}
|
2006-07-16 18:57:03 +00:00
|
|
|
|
|
|
|
AUD_log (NULL, " endianness=");
|
2006-07-04 21:47:22 +00: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-05 18:55:28 +00:00
|
|
|
AUD_log (NULL, "\n");
|
|
|
|
}
|
|
|
|
|
2008-12-03 22:48:44 +00:00
|
|
|
static int audio_validate_settings (struct audsettings *as)
|
2005-11-05 18:55:28 +00:00
|
|
|
{
|
|
|
|
int invalid;
|
|
|
|
|
|
|
|
invalid = as->nchannels != 1 && as->nchannels != 2;
|
2006-07-04 21:47:22 +00:00
|
|
|
invalid |= as->endianness != 0 && as->endianness != 1;
|
2005-11-05 18:55:28 +00:00
|
|
|
|
|
|
|
switch (as->fmt) {
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_S8:
|
|
|
|
case AUDIO_FORMAT_U8:
|
|
|
|
case AUDIO_FORMAT_S16:
|
|
|
|
case AUDIO_FORMAT_U16:
|
|
|
|
case AUDIO_FORMAT_S32:
|
|
|
|
case AUDIO_FORMAT_U32:
|
2005-11-05 18:55:28 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
invalid = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid |= as->freq <= 0;
|
2006-07-04 21:47:22 +00:00
|
|
|
return invalid ? -1 : 0;
|
2005-11-05 18:55:28 +00:00
|
|
|
}
|
|
|
|
|
2008-12-03 22:48:44 +00:00
|
|
|
static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2005-10-30 18:58:22 +00:00
|
|
|
int bits = 8, sign = 0;
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-11-05 18:55:28 +00:00
|
|
|
switch (as->fmt) {
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_S8:
|
2005-10-30 18:58:22 +00:00
|
|
|
sign = 1;
|
2012-02-25 13:46:55 +00:00
|
|
|
/* fall through */
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_U8:
|
2005-10-30 18:58:22 +00:00
|
|
|
break;
|
|
|
|
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_S16:
|
2005-10-30 18:58:22 +00:00
|
|
|
sign = 1;
|
2012-02-25 13:46:55 +00:00
|
|
|
/* fall through */
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_U16:
|
2005-10-30 18:58:22 +00:00
|
|
|
bits = 16;
|
|
|
|
break;
|
2007-02-17 22:19:29 +00:00
|
|
|
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_S32:
|
2007-02-17 22:19:29 +00:00
|
|
|
sign = 1;
|
2012-02-25 13:46:55 +00:00
|
|
|
/* fall through */
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_U32:
|
2007-02-17 22:19:29 +00:00
|
|
|
bits = 32;
|
|
|
|
break;
|
2019-03-08 22:34:13 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2005-11-05 18:55:28 +00:00
|
|
|
return info->freq == as->freq
|
|
|
|
&& info->nchannels == as->nchannels
|
2005-10-30 18:58:22 +00:00
|
|
|
&& info->sign == sign
|
2006-07-04 21:47:22 +00:00
|
|
|
&& info->bits == bits
|
|
|
|
&& info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2008-12-03 22:48:44 +00:00
|
|
|
void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
|
2005-10-30 18:58:22 +00:00
|
|
|
{
|
2007-02-17 22:19:29 +00:00
|
|
|
int bits = 8, sign = 0, shift = 0;
|
2005-10-30 18:58:22 +00:00
|
|
|
|
2005-11-05 18:55:28 +00:00
|
|
|
switch (as->fmt) {
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_S8:
|
2004-11-07 18:04:02 +00:00
|
|
|
sign = 1;
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_U8:
|
2004-11-07 18:04:02 +00:00
|
|
|
break;
|
|
|
|
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_S16:
|
2004-11-07 18:04:02 +00:00
|
|
|
sign = 1;
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_U16:
|
2004-11-07 18:04:02 +00:00
|
|
|
bits = 16;
|
2007-02-17 22:19:29 +00:00
|
|
|
shift = 1;
|
|
|
|
break;
|
|
|
|
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_S32:
|
2007-02-17 22:19:29 +00:00
|
|
|
sign = 1;
|
2019-03-08 22:34:13 +00:00
|
|
|
case AUDIO_FORMAT_U32:
|
2007-02-17 22:19:29 +00:00
|
|
|
bits = 32;
|
|
|
|
shift = 2;
|
2004-11-07 18:04:02 +00:00
|
|
|
break;
|
2019-03-08 22:34:13 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2005-11-05 18:55:28 +00:00
|
|
|
info->freq = as->freq;
|
2005-10-30 18:58:22 +00:00
|
|
|
info->bits = bits;
|
|
|
|
info->sign = sign;
|
2005-11-05 18:55:28 +00:00
|
|
|
info->nchannels = as->nchannels;
|
2007-02-17 22:19:29 +00:00
|
|
|
info->shift = (as->nchannels == 2) + shift;
|
2005-10-30 18:58:22 +00:00
|
|
|
info->align = (1 << info->shift) - 1;
|
|
|
|
info->bytes_per_second = info->freq << info->shift;
|
2006-07-04 21:47:22 +00:00
|
|
|
info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2005-10-30 18:58:22 +00:00
|
|
|
if (!len) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->sign) {
|
2006-08-03 20:39:40 +00:00
|
|
|
memset (buf, 0x00, len << info->shift);
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-02-17 22:19:29 +00:00
|
|
|
switch (info->bits) {
|
|
|
|
case 8:
|
2006-08-03 20:39:40 +00:00
|
|
|
memset (buf, 0x80, len << info->shift);
|
2007-02-17 22:19:29 +00:00
|
|
|
break;
|
2005-10-30 18:58:22 +00:00
|
|
|
|
2007-02-17 22:19:29 +00: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-30 18:58:22 +00:00
|
|
|
}
|
2007-02-17 22:19:29 +00: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-30 18:58:22 +00:00
|
|
|
|
2007-02-17 22:19:29 +00:00
|
|
|
for (i = 0; i < len << shift; i++) {
|
|
|
|
p[i] = s;
|
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
2007-02-17 22:19:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
|
|
|
|
info->bits);
|
|
|
|
break;
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-04 16:51:32 +00:00
|
|
|
/*
|
|
|
|
* Capture
|
|
|
|
*/
|
2011-01-05 00:05:47 +00:00
|
|
|
static void noop_conv (struct st_sample *dst, const void *src, int samples)
|
2006-07-04 16:51:32 +00:00
|
|
|
{
|
|
|
|
(void) src;
|
|
|
|
(void) dst;
|
|
|
|
(void) samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CaptureVoiceOut *audio_pcm_capture_find_specific (
|
2008-12-03 22:48:44 +00:00
|
|
|
struct audsettings *as
|
2006-07-04 16:51:32 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
CaptureVoiceOut *cap;
|
2009-05-13 23:11:35 +00:00
|
|
|
AudioState *s = &glob_audio_state;
|
2006-07-04 16:51:32 +00:00
|
|
|
|
|
|
|
for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
|
2006-07-04 21:47:22 +00:00
|
|
|
if (audio_pcm_info_eq (&cap->hw.info, as)) {
|
2006-07-04 16:51:32 +00:00
|
|
|
return cap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-07-16 18:57:03 +00:00
|
|
|
static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
|
2006-07-04 16:51:32 +00:00
|
|
|
{
|
2006-07-16 18:57:03 +00: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-04 16:51:32 +00:00
|
|
|
|
2006-07-16 18:57:03 +00:00
|
|
|
static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
|
|
|
|
{
|
|
|
|
if (cap->hw.enabled != enabled) {
|
|
|
|
audcnotification_e cmd;
|
2006-07-04 16:51:32 +00:00
|
|
|
cap->hw.enabled = enabled;
|
2006-07-16 18:57:03 +00:00
|
|
|
cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
|
|
|
|
audio_notify_capture (cap, cmd);
|
2006-07-04 16:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
|
|
|
|
{
|
|
|
|
HWVoiceOut *hw = &cap->hw;
|
|
|
|
SWVoiceOut *sw;
|
|
|
|
int enabled = 0;
|
|
|
|
|
2006-07-16 18:57:03 +00:00
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
2006-07-04 16:51:32 +00:00
|
|
|
if (sw->active) {
|
|
|
|
enabled = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-07-16 18:57:03 +00:00
|
|
|
audio_capture_maybe_changed (cap, enabled);
|
2006-07-04 16:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_detach_capture (HWVoiceOut *hw)
|
|
|
|
{
|
2006-07-16 18:57:03 +00: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-04 16:51:32 +00:00
|
|
|
|
|
|
|
if (sw->rate) {
|
|
|
|
st_rate_stop (sw->rate);
|
|
|
|
sw->rate = NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-12 07:36:22 +00:00
|
|
|
QLIST_REMOVE (sw, entries);
|
|
|
|
QLIST_REMOVE (sc, entries);
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free (sc);
|
2006-07-16 18:57:03 +00:00
|
|
|
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-04 16:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-13 23:11:35 +00:00
|
|
|
static int audio_attach_capture (HWVoiceOut *hw)
|
2006-07-04 16:51:32 +00:00
|
|
|
{
|
2009-05-13 23:11:35 +00:00
|
|
|
AudioState *s = &glob_audio_state;
|
2006-07-04 16:51:32 +00:00
|
|
|
CaptureVoiceOut *cap;
|
|
|
|
|
|
|
|
audio_detach_capture (hw);
|
|
|
|
for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
|
2006-07-16 18:57:03 +00:00
|
|
|
SWVoiceCap *sc;
|
2006-07-04 16:51:32 +00:00
|
|
|
SWVoiceOut *sw;
|
2006-07-16 18:57:03 +00:00
|
|
|
HWVoiceOut *hw_cap = &cap->hw;
|
2006-07-04 16:51:32 +00:00
|
|
|
|
2019-02-25 15:43:35 +00:00
|
|
|
sc = g_malloc0(sizeof(*sc));
|
2006-07-04 16:51:32 +00:00
|
|
|
|
2006-07-16 18:57:03 +00:00
|
|
|
sc->cap = cap;
|
|
|
|
sw = &sc->sw;
|
2006-07-04 16:51:32 +00:00
|
|
|
sw->hw = hw_cap;
|
2006-07-16 18:57:03 +00:00
|
|
|
sw->info = hw->info;
|
2006-07-04 16:51:32 +00:00
|
|
|
sw->empty = 1;
|
|
|
|
sw->active = hw->enabled;
|
|
|
|
sw->conv = noop_conv;
|
|
|
|
sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
|
2012-07-16 14:08:36 +00:00
|
|
|
sw->vol = nominal_volume;
|
2006-07-04 16:51:32 +00:00
|
|
|
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));
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free (sw);
|
2006-07-04 16:51:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-09-12 07:36:22 +00:00
|
|
|
QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
|
|
|
|
QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
|
2006-07-16 18:57:03 +00:00
|
|
|
#ifdef DEBUG_CAPTURE
|
2013-01-16 17:17:33 +00:00
|
|
|
sw->name = g_strdup_printf ("for %p %d,%d,%d",
|
|
|
|
hw, sw->info.freq, sw->info.bits,
|
|
|
|
sw->info.nchannels);
|
2006-07-16 18:57:03 +00:00
|
|
|
dolog ("Added %s active = %d\n", sw->name, sw->active);
|
|
|
|
#endif
|
2006-07-04 16:51:32 +00:00
|
|
|
if (sw->active) {
|
2006-07-16 18:57:03 +00:00
|
|
|
audio_capture_maybe_changed (cap, 1);
|
2006-07-04 16:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
/*
|
|
|
|
* Hard voice (capture)
|
|
|
|
*/
|
2005-11-05 18:55:28 +00:00
|
|
|
static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2005-10-30 18:58:22 +00: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-07 18:04:02 +00:00
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
return m;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2005-10-30 18:58:22 +00:00
|
|
|
int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
|
2018-02-03 08:43:02 +00:00
|
|
|
if (audio_bug(__func__, live < 0 || live > hw->samples)) {
|
2005-10-30 18:58:22 +00:00
|
|
|
dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
|
|
|
return 0;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
return live;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 06:59:38 +00:00
|
|
|
int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
|
|
|
|
int live, int pending)
|
|
|
|
{
|
|
|
|
int left = hw->samples - pending;
|
|
|
|
int len = audio_MIN (left, live);
|
|
|
|
int clipped = 0;
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
struct st_sample *src = hw->mix_buf + hw->rpos;
|
|
|
|
uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
|
|
|
|
int samples_till_end_of_buf = hw->samples - hw->rpos;
|
|
|
|
int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
|
|
|
|
|
|
|
|
hw->clip (dst, src, samples_to_clip);
|
|
|
|
|
|
|
|
hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
|
|
|
|
len -= samples_to_clip;
|
|
|
|
clipped += samples_to_clip;
|
|
|
|
}
|
|
|
|
return clipped;
|
|
|
|
}
|
|
|
|
|
2005-10-30 18:58:22 +00: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;
|
|
|
|
|
2018-02-03 08:43:02 +00:00
|
|
|
if (audio_bug(__func__, live < 0 || live > hw->samples)) {
|
2005-10-30 18:58:22 +00:00
|
|
|
dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpos = hw->wpos - live;
|
|
|
|
if (rpos >= 0) {
|
|
|
|
return rpos;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-10-30 18:58:22 +00:00
|
|
|
return hw->samples + rpos;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2005-10-30 18:58:22 +00:00
|
|
|
HWVoiceIn *hw = sw->hw;
|
|
|
|
int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
|
2008-12-03 22:48:44 +00:00
|
|
|
struct st_sample *src, *dst = sw->buf;
|
2005-10-30 18:58:22 +00:00
|
|
|
|
|
|
|
rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
|
|
|
|
|
|
|
|
live = hw->total_samples_captured - sw->total_hw_samples_acquired;
|
2018-02-03 08:43:02 +00:00
|
|
|
if (audio_bug(__func__, live < 0 || live > hw->samples)) {
|
2005-10-30 18:58:22 +00:00
|
|
|
dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
samples = size >> sw->info.shift;
|
|
|
|
if (!live) {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
swlim = (live * sw->ratio) >> 32;
|
|
|
|
swlim = audio_MIN (swlim, samples);
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
while (swlim) {
|
|
|
|
src = hw->conv_buf + rpos;
|
|
|
|
isamp = hw->wpos - rpos;
|
|
|
|
/* XXX: <= ? */
|
|
|
|
if (isamp <= 0) {
|
|
|
|
isamp = hw->samples - rpos;
|
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
if (!isamp) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
osamp = swlim;
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2018-02-03 08:43:02 +00:00
|
|
|
if (audio_bug(__func__, osamp < 0)) {
|
2005-10-30 18:58:22 +00:00
|
|
|
dolog ("osamp=%d\n", osamp);
|
2005-11-05 18:55:28 +00:00
|
|
|
return 0;
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00: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-07 18:04:02 +00:00
|
|
|
|
2012-04-17 12:32:36 +00:00
|
|
|
if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
|
|
|
|
mixeng_volume (sw->buf, ret, &sw->vol);
|
|
|
|
}
|
2011-01-05 00:05:47 +00:00
|
|
|
|
2005-11-20 16:24:34 +00:00
|
|
|
sw->clip (buf, sw->buf, ret);
|
2005-10-30 18:58:22 +00:00
|
|
|
sw->total_hw_samples_acquired += total;
|
|
|
|
return ret << sw->info.shift;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
/*
|
|
|
|
* Hard voice (playback)
|
|
|
|
*/
|
2005-11-05 18:55:28 +00:00
|
|
|
static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
|
2005-10-30 18:58:22 +00:00
|
|
|
{
|
2005-11-05 18:55:28 +00:00
|
|
|
SWVoiceOut *sw;
|
|
|
|
int m = INT_MAX;
|
|
|
|
int nb_live = 0;
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-11-05 18:55:28 +00: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-07 18:04:02 +00:00
|
|
|
}
|
2005-11-05 18:55:28 +00:00
|
|
|
|
|
|
|
*nb_livep = nb_live;
|
|
|
|
return m;
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2009-09-18 07:37:39 +00:00
|
|
|
static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
|
2005-10-30 18:58:22 +00:00
|
|
|
{
|
|
|
|
int smin;
|
2009-09-18 07:37:39 +00:00
|
|
|
int nb_live1;
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2009-09-18 07:37:39 +00:00
|
|
|
smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
|
|
|
|
if (nb_live) {
|
|
|
|
*nb_live = nb_live1;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2009-09-18 07:37:39 +00:00
|
|
|
|
|
|
|
if (nb_live1) {
|
2005-10-30 18:58:22 +00:00
|
|
|
int live = smin;
|
|
|
|
|
2018-02-03 08:43:02 +00:00
|
|
|
if (audio_bug(__func__, live < 0 || live > hw->samples)) {
|
2005-10-30 18:58:22 +00:00
|
|
|
dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
|
|
|
return 0;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
return live;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2009-09-18 07:37:39 +00:00
|
|
|
return 0;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
/*
|
|
|
|
* Soft voice (playback)
|
|
|
|
*/
|
|
|
|
int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2005-10-30 18:58:22 +00:00
|
|
|
int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
|
|
|
|
int ret = 0, pos = 0, total = 0;
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
if (!sw) {
|
|
|
|
return size;
|
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
hwsamples = sw->hw->samples;
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
live = sw->total_hw_samples_mixed;
|
2018-02-03 08:43:02 +00:00
|
|
|
if (audio_bug(__func__, live < 0 || live > hwsamples)) {
|
2005-10-30 18:58:22 +00:00
|
|
|
dolog ("live=%d hw->samples=%d\n", live, hwsamples);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
if (live == hwsamples) {
|
2006-07-16 18:57:03 +00:00
|
|
|
#ifdef DEBUG_OUT
|
|
|
|
dolog ("%s is full %d\n", sw->name, live);
|
|
|
|
#endif
|
2005-10-30 18:58:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
wpos = (sw->hw->rpos + live) % hwsamples;
|
|
|
|
samples = size >> sw->info.shift;
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
dead = hwsamples - live;
|
|
|
|
swlim = ((int64_t) dead << 32) / sw->ratio;
|
|
|
|
swlim = audio_MIN (swlim, samples);
|
|
|
|
if (swlim) {
|
2011-01-05 00:05:47 +00:00
|
|
|
sw->conv (sw->buf, buf, swlim);
|
2012-04-17 12:32:36 +00:00
|
|
|
|
|
|
|
if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
|
|
|
|
mixeng_volume (sw->buf, swlim, &sw->vol);
|
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05 18:55:28 +00:00
|
|
|
"%s: write size %d ret %d total sw %d\n",
|
|
|
|
SW_NAME (sw),
|
2005-10-30 18:58:22 +00:00
|
|
|
size >> sw->info.shift,
|
|
|
|
ret,
|
2005-11-05 18:55:28 +00:00
|
|
|
sw->total_hw_samples_mixed
|
2005-10-30 18:58:22 +00:00
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret << sw->info.shift;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
#ifdef DEBUG_AUDIO
|
|
|
|
static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2005-10-30 18:58:22 +00:00
|
|
|
dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
|
|
|
|
cap, info->bits, info->sign, info->freq, info->nchannels);
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
#endif
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
#define DAC
|
|
|
|
#include "audio_template.h"
|
|
|
|
#undef DAC
|
|
|
|
#include "audio_template.h"
|
|
|
|
|
2009-09-11 22:28:45 +00:00
|
|
|
/*
|
|
|
|
* Timer
|
|
|
|
*/
|
2018-07-02 16:15:24 +00:00
|
|
|
|
|
|
|
static bool audio_timer_running;
|
|
|
|
static uint64_t audio_timer_last;
|
|
|
|
|
2009-09-11 22:28:45 +00:00
|
|
|
static int audio_is_timer_needed (void)
|
|
|
|
{
|
|
|
|
HWVoiceIn *hwi = NULL;
|
|
|
|
HWVoiceOut *hwo = NULL;
|
|
|
|
|
|
|
|
while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
|
|
|
|
if (!hwo->poll_mode) return 1;
|
|
|
|
}
|
|
|
|
while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
|
|
|
|
if (!hwi->poll_mode) return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-18 11:30:12 +00:00
|
|
|
static void audio_reset_timer (AudioState *s)
|
2009-09-11 22:28:45 +00:00
|
|
|
{
|
|
|
|
if (audio_is_timer_needed ()) {
|
2017-02-14 07:15:10 +00:00
|
|
|
timer_mod_anticipate_ns(s->ts,
|
2019-03-08 22:34:15 +00:00
|
|
|
qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->period_ticks);
|
2018-07-02 16:15:24 +00:00
|
|
|
if (!audio_timer_running) {
|
|
|
|
audio_timer_running = true;
|
|
|
|
audio_timer_last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2019-03-08 22:34:15 +00:00
|
|
|
trace_audio_timer_start(s->period_ticks / SCALE_MS);
|
2018-07-02 16:15:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
timer_del(s->ts);
|
|
|
|
if (audio_timer_running) {
|
|
|
|
audio_timer_running = false;
|
|
|
|
trace_audio_timer_stop();
|
|
|
|
}
|
2009-09-11 22:28:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-18 11:30:12 +00:00
|
|
|
static void audio_timer (void *opaque)
|
|
|
|
{
|
2018-07-02 16:15:24 +00:00
|
|
|
int64_t now, diff;
|
2019-03-08 22:34:15 +00:00
|
|
|
AudioState *s = opaque;
|
2018-07-02 16:15:24 +00:00
|
|
|
|
|
|
|
now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
|
|
|
diff = now - audio_timer_last;
|
2019-03-08 22:34:15 +00:00
|
|
|
if (diff > s->period_ticks * 3 / 2) {
|
2018-07-02 16:15:24 +00:00
|
|
|
trace_audio_timer_delayed(diff / SCALE_MS);
|
|
|
|
}
|
|
|
|
audio_timer_last = now;
|
|
|
|
|
2019-03-08 22:34:15 +00:00
|
|
|
audio_run("timer");
|
|
|
|
audio_reset_timer(s);
|
2010-11-18 11:30:12 +00:00
|
|
|
}
|
|
|
|
|
2009-09-11 22:28:45 +00:00
|
|
|
/*
|
|
|
|
* Public API
|
|
|
|
*/
|
2005-10-30 18:58:22 +00:00
|
|
|
int AUD_write (SWVoiceOut *sw, void *buf, int size)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2005-10-30 18:58:22 +00:00
|
|
|
if (!sw) {
|
|
|
|
/* XXX: Consider options */
|
|
|
|
return size;
|
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
if (!sw->hw->enabled) {
|
2005-11-05 18:55:28 +00:00
|
|
|
dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
|
2004-11-07 18:04:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-13 21:57:58 +00:00
|
|
|
return sw->hw->pcm_ops->write(sw, buf, size);
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AUD_read (SWVoiceIn *sw, void *buf, int size)
|
|
|
|
{
|
|
|
|
if (!sw) {
|
|
|
|
/* XXX: Consider options */
|
|
|
|
return size;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
|
|
|
|
if (!sw->hw->enabled) {
|
2005-11-05 18:55:28 +00:00
|
|
|
dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
|
2005-10-30 18:58:22 +00:00
|
|
|
return 0;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
|
2016-06-13 21:57:58 +00:00
|
|
|
return sw->hw->pcm_ops->read(sw, buf, size);
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
int AUD_get_buffer_size_out (SWVoiceOut *sw)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2005-11-05 18:55:28 +00:00
|
|
|
return sw->hw->samples << sw->hw->info.shift;
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_set_active_out (SWVoiceOut *sw, int on)
|
|
|
|
{
|
|
|
|
HWVoiceOut *hw;
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
if (!sw) {
|
2004-11-07 18:04:02 +00:00
|
|
|
return;
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
|
|
|
|
hw = sw->hw;
|
2005-10-30 18:58:22 +00:00
|
|
|
if (sw->active != on) {
|
2009-02-18 20:44:04 +00:00
|
|
|
AudioState *s = &glob_audio_state;
|
2005-10-30 18:58:22 +00:00
|
|
|
SWVoiceOut *temp_sw;
|
2006-07-16 18:57:03 +00:00
|
|
|
SWVoiceCap *sc;
|
2005-10-30 18:58:22 +00:00
|
|
|
|
|
|
|
if (on) {
|
|
|
|
hw->pending_disable = 0;
|
|
|
|
if (!hw->enabled) {
|
|
|
|
hw->enabled = 1;
|
2009-02-18 20:44:04 +00:00
|
|
|
if (s->vm_running) {
|
2019-03-08 22:34:25 +00:00
|
|
|
hw->pcm_ops->ctl_out(hw, VOICE_ENABLE);
|
2010-11-18 11:30:12 +00:00
|
|
|
audio_reset_timer (s);
|
2009-02-18 20:44:04 +00:00
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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-07 18:04:02 +00:00
|
|
|
}
|
2006-07-16 18:57:03 +00:00
|
|
|
|
|
|
|
for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
|
|
|
sc->sw.active = hw->enabled;
|
2006-07-04 16:51:32 +00:00
|
|
|
if (hw->enabled) {
|
2006-07-16 18:57:03 +00:00
|
|
|
audio_capture_maybe_changed (sc->cap, 1);
|
2006-07-04 16:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
sw->active = on;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_set_active_in (SWVoiceIn *sw, int on)
|
|
|
|
{
|
|
|
|
HWVoiceIn *hw;
|
|
|
|
|
|
|
|
if (!sw) {
|
|
|
|
return;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
hw = sw->hw;
|
2004-11-07 18:04:02 +00:00
|
|
|
if (sw->active != on) {
|
2009-02-18 20:44:04 +00:00
|
|
|
AudioState *s = &glob_audio_state;
|
2005-10-30 18:58:22 +00:00
|
|
|
SWVoiceIn *temp_sw;
|
|
|
|
|
2004-11-07 18:04:02 +00:00
|
|
|
if (on) {
|
|
|
|
if (!hw->enabled) {
|
|
|
|
hw->enabled = 1;
|
2009-02-18 20:44:04 +00:00
|
|
|
if (s->vm_running) {
|
2019-03-08 22:34:25 +00:00
|
|
|
hw->pcm_ops->ctl_in(hw, VOICE_ENABLE);
|
2010-11-18 11:30:12 +00:00
|
|
|
audio_reset_timer (s);
|
2009-02-18 20:44:04 +00:00
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
sw->total_hw_samples_acquired = hw->total_samples_captured;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-10-30 18:58:22 +00:00
|
|
|
if (hw->enabled) {
|
2004-11-07 18:04:02 +00:00
|
|
|
int nb_active = 0;
|
2005-10-30 18:58:22 +00: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-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nb_active == 1) {
|
2005-10-30 18:58:22 +00:00
|
|
|
hw->enabled = 0;
|
|
|
|
hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sw->active = on;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-30 18:58:22 +00: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;
|
2018-02-03 08:43:02 +00:00
|
|
|
if (audio_bug(__func__, live < 0 || live > sw->hw->samples)) {
|
2005-10-30 18:58:22 +00:00
|
|
|
dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ldebug (
|
2006-06-25 18:15:32 +00:00
|
|
|
"%s: get_avail live %d ret %" PRId64 "\n",
|
2005-11-05 18:55:28 +00:00
|
|
|
SW_NAME (sw),
|
2005-10-30 18:58:22 +00: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;
|
|
|
|
|
2018-02-03 08:43:02 +00:00
|
|
|
if (audio_bug(__func__, live < 0 || live > sw->hw->samples)) {
|
2005-10-30 18:58:22 +00:00
|
|
|
dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
|
2005-11-05 18:55:28 +00:00
|
|
|
return 0;
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dead = sw->hw->samples - live;
|
|
|
|
|
|
|
|
#ifdef DEBUG_OUT
|
2006-06-25 18:15:32 +00:00
|
|
|
dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
|
2005-11-05 18:55:28 +00:00
|
|
|
SW_NAME (sw),
|
2005-10-30 18:58:22 +00:00
|
|
|
live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
|
2004-11-07 18:04:02 +00:00
|
|
|
#endif
|
2005-10-30 18:58:22 +00:00
|
|
|
|
|
|
|
return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
|
|
|
|
}
|
|
|
|
|
2006-07-04 16:51:32 +00:00
|
|
|
static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (hw->enabled) {
|
2006-07-16 18:57:03 +00:00
|
|
|
SWVoiceCap *sc;
|
2006-07-04 16:51:32 +00:00
|
|
|
|
2006-07-16 18:57:03 +00:00
|
|
|
for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
|
|
|
SWVoiceOut *sw = &sc->sw;
|
2006-07-04 16:51:32 +00: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-16 18:57:03 +00:00
|
|
|
dolog ("Could not mix %d bytes into a capture "
|
|
|
|
"buffer, mixed %d\n",
|
|
|
|
bytes, written);
|
2006-07-04 16:51:32 +00: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-05 18:55:28 +00:00
|
|
|
static void audio_run_out (AudioState *s)
|
2005-10-30 18:58:22 +00:00
|
|
|
{
|
|
|
|
HWVoiceOut *hw = NULL;
|
|
|
|
SWVoiceOut *sw;
|
|
|
|
|
2009-05-13 23:11:35 +00:00
|
|
|
while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
|
2005-10-30 18:58:22 +00:00
|
|
|
int played;
|
2006-07-04 16:51:32 +00:00
|
|
|
int live, free, nb_live, cleanup_required, prev_rpos;
|
2005-10-30 18:58:22 +00:00
|
|
|
|
2009-09-18 07:37:39 +00:00
|
|
|
live = audio_pcm_hw_get_live_out (hw, &nb_live);
|
2005-10-30 18:58:22 +00:00
|
|
|
if (!nb_live) {
|
|
|
|
live = 0;
|
|
|
|
}
|
2005-11-05 18:55:28 +00:00
|
|
|
|
2018-02-03 08:43:02 +00:00
|
|
|
if (audio_bug(__func__, live < 0 || live > hw->samples)) {
|
2005-10-30 18:58:22 +00:00
|
|
|
dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
2005-11-05 18:55:28 +00:00
|
|
|
continue;
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hw->pending_disable && !nb_live) {
|
2006-07-16 18:57:03 +00:00
|
|
|
SWVoiceCap *sc;
|
2005-10-30 18:58:22 +00:00
|
|
|
#ifdef DEBUG_OUT
|
|
|
|
dolog ("Disabling voice\n");
|
2004-11-07 18:04:02 +00:00
|
|
|
#endif
|
2005-10-30 18:58:22 +00:00
|
|
|
hw->enabled = 0;
|
|
|
|
hw->pending_disable = 0;
|
|
|
|
hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
|
2006-07-16 18:57:03 +00: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-04 16:51:32 +00:00
|
|
|
}
|
2005-10-30 18:58:22 +00: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-04 16:51:32 +00:00
|
|
|
prev_rpos = hw->rpos;
|
2009-09-18 07:37:39 +00:00
|
|
|
played = hw->pcm_ops->run_out (hw, live);
|
2017-02-02 05:50:54 +00:00
|
|
|
replay_audio_out(&played);
|
2018-02-03 08:43:02 +00:00
|
|
|
if (audio_bug(__func__, hw->rpos >= hw->samples)) {
|
2005-10-30 18:58:22 +00:00
|
|
|
dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
|
|
|
|
hw->rpos, hw->samples, played);
|
|
|
|
hw->rpos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_OUT
|
2005-11-05 18:55:28 +00:00
|
|
|
dolog ("played=%d\n", played);
|
2004-11-07 18:04:02 +00:00
|
|
|
#endif
|
2005-10-30 18:58:22 +00:00
|
|
|
|
|
|
|
if (played) {
|
|
|
|
hw->ts_helper += played;
|
2006-07-04 16:51:32 +00:00
|
|
|
audio_capture_mix_and_clear (hw, prev_rpos, played);
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
|
|
|
|
2005-11-05 18:55:28 +00:00
|
|
|
cleanup_required = 0;
|
2005-10-30 18:58:22 +00:00
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
|
|
|
if (!sw->active && sw->empty) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-03 08:43:02 +00:00
|
|
|
if (audio_bug(__func__, played > sw->total_hw_samples_mixed)) {
|
2005-10-30 18:58:22 +00:00
|
|
|
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-05 18:55:28 +00:00
|
|
|
cleanup_required |= !sw->active && !sw->callback.fn;
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sw->active) {
|
|
|
|
free = audio_get_free (sw);
|
|
|
|
if (free > 0) {
|
|
|
|
sw->callback.fn (sw->callback.opaque, free);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-11-05 18:55:28 +00:00
|
|
|
|
|
|
|
if (cleanup_required) {
|
2006-07-16 18:57:03 +00:00
|
|
|
SWVoiceOut *sw1;
|
|
|
|
|
|
|
|
sw = hw->sw_head.lh_first;
|
|
|
|
while (sw) {
|
|
|
|
sw1 = sw->entries.le_next;
|
2005-11-05 18:55:28 +00:00
|
|
|
if (!sw->active && !sw->callback.fn) {
|
2009-05-13 23:11:35 +00:00
|
|
|
audio_close_out (sw);
|
2005-11-05 18:55:28 +00:00
|
|
|
}
|
2006-07-16 18:57:03 +00:00
|
|
|
sw = sw1;
|
2005-11-05 18:55:28 +00:00
|
|
|
}
|
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-05 18:55:28 +00:00
|
|
|
static void audio_run_in (AudioState *s)
|
2005-10-30 18:58:22 +00:00
|
|
|
{
|
|
|
|
HWVoiceIn *hw = NULL;
|
|
|
|
|
2009-05-13 23:11:35 +00:00
|
|
|
while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
|
2005-10-30 18:58:22 +00:00
|
|
|
SWVoiceIn *sw;
|
2017-02-02 05:50:54 +00:00
|
|
|
int captured = 0, min;
|
2005-10-30 18:58:22 +00:00
|
|
|
|
2017-02-02 05:50:54 +00:00
|
|
|
if (replay_mode != REPLAY_MODE_PLAY) {
|
|
|
|
captured = hw->pcm_ops->run_in(hw);
|
|
|
|
}
|
|
|
|
replay_audio_in(&captured, hw->conv_buf, &hw->wpos, hw->samples);
|
2005-10-30 18:58:22 +00:00
|
|
|
|
|
|
|
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-04 16:51:32 +00: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;
|
|
|
|
|
2009-09-18 07:37:39 +00:00
|
|
|
captured = live = audio_pcm_hw_get_live_out (hw, NULL);
|
2006-07-04 16:51:32 +00:00
|
|
|
rpos = hw->rpos;
|
|
|
|
while (live) {
|
|
|
|
int left = hw->samples - rpos;
|
|
|
|
int to_capture = audio_MIN (live, left);
|
2008-12-03 22:48:44 +00:00
|
|
|
struct st_sample *src;
|
2006-07-04 16:51:32 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-03 08:43:02 +00:00
|
|
|
if (audio_bug(__func__, captured > sw->total_hw_samples_mixed)) {
|
2006-07-04 16:51:32 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-11 22:28:45 +00:00
|
|
|
void audio_run (const char *msg)
|
2005-11-20 16:24:34 +00:00
|
|
|
{
|
2009-09-11 22:28:45 +00:00
|
|
|
AudioState *s = &glob_audio_state;
|
2005-11-20 16:24:34 +00:00
|
|
|
|
|
|
|
audio_run_out (s);
|
|
|
|
audio_run_in (s);
|
2006-07-04 16:51:32 +00:00
|
|
|
audio_run_capture (s);
|
2009-09-11 22:28:45 +00:00
|
|
|
#ifdef DEBUG_POLL
|
|
|
|
{
|
|
|
|
static double prevtime;
|
|
|
|
double currtime;
|
|
|
|
struct timeval tv;
|
2005-11-20 16:24:34 +00:00
|
|
|
|
2009-09-11 22:28:45 +00:00
|
|
|
if (gettimeofday (&tv, NULL)) {
|
|
|
|
perror ("audio_run: gettimeofday");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
currtime = tv.tv_sec + tv.tv_usec * 1e-6;
|
|
|
|
dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
|
|
|
|
prevtime = currtime;
|
|
|
|
}
|
|
|
|
#endif
|
2005-11-20 16:24:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 22:34:15 +00:00
|
|
|
static int audio_driver_init(AudioState *s, struct audio_driver *drv,
|
|
|
|
bool msg, Audiodev *dev)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2019-03-08 22:34:15 +00:00
|
|
|
s->drv_opaque = drv->init(dev);
|
2005-10-30 18:58:22 +00:00
|
|
|
|
2005-11-05 18:55:28 +00:00
|
|
|
if (s->drv_opaque) {
|
2009-05-13 23:11:35 +00:00
|
|
|
audio_init_nb_voices_out (drv);
|
|
|
|
audio_init_nb_voices_in (drv);
|
2005-11-05 18:55:28 +00:00
|
|
|
s->drv = drv;
|
2005-10-30 18:58:22 +00:00
|
|
|
return 0;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-01-24 11:20:54 +00:00
|
|
|
if (msg) {
|
|
|
|
dolog("Could not init `%s' audio driver\n", drv->name);
|
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
return -1;
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-22 17:15:29 +00:00
|
|
|
static void audio_vm_change_state_handler (void *opaque, int running,
|
2011-07-29 17:26:33 +00:00
|
|
|
RunState state)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2005-11-05 18:55:28 +00:00
|
|
|
AudioState *s = opaque;
|
2005-10-30 18:58:22 +00:00
|
|
|
HWVoiceOut *hwo = NULL;
|
|
|
|
HWVoiceIn *hwi = NULL;
|
2005-11-11 00:04:19 +00:00
|
|
|
int op = running ? VOICE_ENABLE : VOICE_DISABLE;
|
2005-10-30 18:58:22 +00:00
|
|
|
|
2009-02-18 20:44:04 +00:00
|
|
|
s->vm_running = running;
|
2009-05-13 23:11:35 +00:00
|
|
|
while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
|
2019-03-08 22:34:25 +00:00
|
|
|
hwo->pcm_ops->ctl_out(hwo, op);
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2009-05-13 23:11:35 +00:00
|
|
|
while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
|
2019-03-08 22:34:25 +00:00
|
|
|
hwi->pcm_ops->ctl_in(hwi, op);
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2010-11-18 11:30:12 +00:00
|
|
|
audio_reset_timer (s);
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 11:23:43 +00:00
|
|
|
static bool is_cleaning_up;
|
|
|
|
|
|
|
|
bool audio_is_cleaning_up(void)
|
|
|
|
{
|
|
|
|
return is_cleaning_up;
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_cleanup(void)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2005-11-05 18:55:28 +00:00
|
|
|
AudioState *s = &glob_audio_state;
|
2016-08-01 11:23:43 +00:00
|
|
|
HWVoiceOut *hwo, *hwon;
|
|
|
|
HWVoiceIn *hwi, *hwin;
|
2005-10-30 18:58:22 +00:00
|
|
|
|
2016-08-01 11:23:43 +00:00
|
|
|
is_cleaning_up = true;
|
|
|
|
QLIST_FOREACH_SAFE(hwo, &glob_audio_state.hw_head_out, entries, hwon) {
|
2006-07-16 18:57:03 +00:00
|
|
|
SWVoiceCap *sc;
|
2006-07-04 16:51:32 +00:00
|
|
|
|
2012-05-24 15:05:15 +00:00
|
|
|
if (hwo->enabled) {
|
|
|
|
hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
|
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
hwo->pcm_ops->fini_out (hwo);
|
2006-07-04 16:51:32 +00:00
|
|
|
|
2006-07-16 18:57:03 +00: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-04 16:51:32 +00:00
|
|
|
}
|
2016-08-01 11:23:43 +00:00
|
|
|
QLIST_REMOVE(hwo, entries);
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2016-08-01 11:23:43 +00:00
|
|
|
QLIST_FOREACH_SAFE(hwi, &glob_audio_state.hw_head_in, entries, hwin) {
|
2012-05-24 15:05:15 +00:00
|
|
|
if (hwi->enabled) {
|
|
|
|
hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
|
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
hwi->pcm_ops->fini_in (hwi);
|
2016-08-01 11:23:43 +00:00
|
|
|
QLIST_REMOVE(hwi, entries);
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2005-11-05 18:55:28 +00:00
|
|
|
|
|
|
|
if (s->drv) {
|
|
|
|
s->drv->fini (s->drv_opaque);
|
2016-08-01 11:23:43 +00:00
|
|
|
s->drv = NULL;
|
2005-11-05 18:55:28 +00:00
|
|
|
}
|
2019-03-08 22:34:15 +00:00
|
|
|
|
|
|
|
if (s->dev) {
|
|
|
|
qapi_free_Audiodev(s->dev);
|
|
|
|
s->dev = NULL;
|
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
|
2009-12-02 10:49:34 +00:00
|
|
|
static const VMStateDescription vmstate_audio = {
|
|
|
|
.name = "audio",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-04-16 14:01:33 +00:00
|
|
|
.fields = (VMStateField[]) {
|
2009-12-02 10:49:34 +00:00
|
|
|
VMSTATE_END_OF_LIST()
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
2009-12-02 10:49:34 +00:00
|
|
|
};
|
2004-11-07 18:04:02 +00:00
|
|
|
|
2019-03-08 22:34:15 +00:00
|
|
|
static void audio_validate_opts(Audiodev *dev, Error **errp);
|
|
|
|
|
|
|
|
static AudiodevListEntry *audiodev_find(
|
|
|
|
AudiodevListHead *head, const char *drvname)
|
|
|
|
{
|
|
|
|
AudiodevListEntry *e;
|
|
|
|
QSIMPLEQ_FOREACH(e, head, next) {
|
|
|
|
if (strcmp(AudiodevDriver_str(e->dev->driver), drvname) == 0) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int audio_init(Audiodev *dev)
|
2004-11-07 18:04:02 +00:00
|
|
|
{
|
2005-10-30 18:58:22 +00:00
|
|
|
size_t i;
|
2004-11-07 18:04:02 +00:00
|
|
|
int done = 0;
|
2019-03-08 22:34:15 +00:00
|
|
|
const char *drvname = NULL;
|
2009-09-11 22:28:45 +00:00
|
|
|
VMChangeStateEntry *e;
|
2005-11-05 18:55:28 +00:00
|
|
|
AudioState *s = &glob_audio_state;
|
2018-03-06 07:40:47 +00:00
|
|
|
struct audio_driver *driver;
|
2019-03-08 22:34:15 +00:00
|
|
|
/* silence gcc warning about uninitialized variable */
|
|
|
|
AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head);
|
2005-10-30 18:58:22 +00:00
|
|
|
|
2009-05-12 11:02:38 +00:00
|
|
|
if (s->drv) {
|
2019-03-08 22:34:15 +00:00
|
|
|
if (dev) {
|
|
|
|
dolog("Cannot create more than one audio backend, sorry\n");
|
|
|
|
qapi_free_Audiodev(dev);
|
|
|
|
}
|
|
|
|
return -1;
|
2009-05-12 11:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 22:34:15 +00:00
|
|
|
if (dev) {
|
|
|
|
/* -audiodev option */
|
|
|
|
drvname = AudiodevDriver_str(dev->driver);
|
|
|
|
} else {
|
|
|
|
/* legacy implicit initialization */
|
|
|
|
head = audio_handle_legacy_opts();
|
|
|
|
/*
|
|
|
|
* In case of legacy initialization, all Audiodevs in the list will have
|
|
|
|
* the same configuration (except the driver), so it does't matter which
|
|
|
|
* one we chose. We need an Audiodev to set up AudioState before we can
|
|
|
|
* init a driver. Also note that dev at this point is still in the
|
|
|
|
* list.
|
|
|
|
*/
|
|
|
|
dev = QSIMPLEQ_FIRST(&head)->dev;
|
|
|
|
audio_validate_opts(dev, &error_abort);
|
|
|
|
}
|
|
|
|
s->dev = dev;
|
|
|
|
|
2009-09-12 07:36:22 +00:00
|
|
|
QLIST_INIT (&s->hw_head_out);
|
|
|
|
QLIST_INIT (&s->hw_head_in);
|
|
|
|
QLIST_INIT (&s->cap_head);
|
2016-08-01 11:23:43 +00:00
|
|
|
atexit(audio_cleanup);
|
2005-11-20 16:24:34 +00:00
|
|
|
|
2013-08-21 15:03:08 +00:00
|
|
|
s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
|
2005-11-20 16:24:34 +00:00
|
|
|
|
2019-03-08 22:34:15 +00:00
|
|
|
s->nb_hw_voices_out = audio_get_pdo_out(dev)->voices;
|
|
|
|
s->nb_hw_voices_in = audio_get_pdo_in(dev)->voices;
|
2005-11-05 18:55:28 +00:00
|
|
|
|
2005-10-30 18:58:22 +00:00
|
|
|
if (s->nb_hw_voices_out <= 0) {
|
2005-11-20 16:24:34 +00:00
|
|
|
dolog ("Bogus number of playback voices %d, setting to 1\n",
|
2005-10-30 18:58:22 +00:00
|
|
|
s->nb_hw_voices_out);
|
|
|
|
s->nb_hw_voices_out = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->nb_hw_voices_in <= 0) {
|
2005-11-20 16:24:34 +00:00
|
|
|
dolog ("Bogus number of capture voices %d, setting to 0\n",
|
2005-10-30 18:58:22 +00:00
|
|
|
s->nb_hw_voices_in);
|
2005-11-20 16:24:34 +00:00
|
|
|
s->nb_hw_voices_in = 0;
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
|
|
|
|
if (drvname) {
|
2018-03-06 07:40:47 +00:00
|
|
|
driver = audio_driver_lookup(drvname);
|
|
|
|
if (driver) {
|
2019-03-08 22:34:15 +00:00
|
|
|
done = !audio_driver_init(s, driver, true, dev);
|
2018-03-06 07:40:47 +00:00
|
|
|
} else {
|
2004-11-07 18:04:02 +00:00
|
|
|
dolog ("Unknown audio driver `%s'\n", drvname);
|
|
|
|
}
|
2019-03-08 22:34:15 +00:00
|
|
|
} else {
|
|
|
|
for (i = 0; audio_prio_list[i]; i++) {
|
|
|
|
AudiodevListEntry *e = audiodev_find(&head, audio_prio_list[i]);
|
2018-03-06 07:40:47 +00:00
|
|
|
driver = audio_driver_lookup(audio_prio_list[i]);
|
2019-03-08 22:34:15 +00:00
|
|
|
|
|
|
|
if (e && driver) {
|
|
|
|
s->dev = dev = e->dev;
|
|
|
|
audio_validate_opts(dev, &error_abort);
|
|
|
|
done = !audio_driver_init(s, driver, false, dev);
|
|
|
|
if (done) {
|
|
|
|
e->dev = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-08 22:34:15 +00:00
|
|
|
audio_free_audiodev_list(&head);
|
2004-11-07 18:04:02 +00:00
|
|
|
|
|
|
|
if (!done) {
|
2018-03-06 07:40:47 +00:00
|
|
|
driver = audio_driver_lookup("none");
|
2019-03-08 22:34:15 +00:00
|
|
|
done = !audio_driver_init(s, driver, false, dev);
|
2015-12-17 16:35:20 +00:00
|
|
|
assert(done);
|
|
|
|
dolog("warning: Using timer based audio emulation\n");
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2005-10-30 18:58:22 +00:00
|
|
|
|
2019-03-08 22:34:15 +00:00
|
|
|
if (dev->timer_period <= 0) {
|
|
|
|
s->period_ticks = 1;
|
2009-05-12 11:02:38 +00:00
|
|
|
} else {
|
2019-04-01 18:59:20 +00:00
|
|
|
s->period_ticks = dev->timer_period * SCALE_US;
|
2005-10-30 18:58:22 +00:00
|
|
|
}
|
2009-05-12 11:02:38 +00: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-30 18:58:22 +00:00
|
|
|
}
|
|
|
|
|
2009-09-12 07:36:22 +00:00
|
|
|
QLIST_INIT (&s->card_head);
|
2010-06-25 17:09:07 +00:00
|
|
|
vmstate_register (NULL, 0, &vmstate_audio, s);
|
2019-03-08 22:34:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_free_audiodev_list(AudiodevListHead *head)
|
|
|
|
{
|
|
|
|
AudiodevListEntry *e;
|
|
|
|
while ((e = QSIMPLEQ_FIRST(head))) {
|
|
|
|
QSIMPLEQ_REMOVE_HEAD(head, next);
|
|
|
|
qapi_free_Audiodev(e->dev);
|
|
|
|
g_free(e);
|
|
|
|
}
|
2004-11-07 18:04:02 +00:00
|
|
|
}
|
2006-07-04 16:51:32 +00:00
|
|
|
|
2009-05-13 23:11:35 +00:00
|
|
|
void AUD_register_card (const char *name, QEMUSoundCard *card)
|
|
|
|
{
|
2019-03-08 22:34:15 +00:00
|
|
|
audio_init(NULL);
|
2011-08-21 03:09:37 +00:00
|
|
|
card->name = g_strdup (name);
|
2009-05-13 23:11:35 +00:00
|
|
|
memset (&card->entries, 0, sizeof (card->entries));
|
2009-09-12 07:36:22 +00:00
|
|
|
QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
|
2009-05-13 23:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_remove_card (QEMUSoundCard *card)
|
|
|
|
{
|
2009-09-12 07:36:22 +00:00
|
|
|
QLIST_REMOVE (card, entries);
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free (card->name);
|
2009-05-13 23:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-16 18:57:03 +00:00
|
|
|
CaptureVoiceOut *AUD_add_capture (
|
2008-12-03 22:48:44 +00:00
|
|
|
struct audsettings *as,
|
2006-07-04 16:51:32 +00:00
|
|
|
struct audio_capture_ops *ops,
|
|
|
|
void *cb_opaque
|
|
|
|
)
|
|
|
|
{
|
2009-05-13 23:11:35 +00:00
|
|
|
AudioState *s = &glob_audio_state;
|
2006-07-04 16:51:32 +00:00
|
|
|
CaptureVoiceOut *cap;
|
|
|
|
struct capture_callback *cb;
|
|
|
|
|
2006-07-16 18:57:03 +00:00
|
|
|
if (audio_validate_settings (as)) {
|
2006-07-04 16:51:32 +00:00
|
|
|
dolog ("Invalid settings were passed when trying to add capture\n");
|
|
|
|
audio_print_settings (as);
|
2019-02-25 15:43:35 +00:00
|
|
|
return NULL;
|
2006-07-04 16:51:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 15:43:35 +00:00
|
|
|
cb = g_malloc0(sizeof(*cb));
|
2006-07-04 16:51:32 +00:00
|
|
|
cb->ops = *ops;
|
|
|
|
cb->opaque = cb_opaque;
|
|
|
|
|
2009-05-13 23:11:35 +00:00
|
|
|
cap = audio_pcm_capture_find_specific (as);
|
2006-07-04 16:51:32 +00:00
|
|
|
if (cap) {
|
2009-09-12 07:36:22 +00:00
|
|
|
QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
|
2006-07-16 18:57:03 +00:00
|
|
|
return cap;
|
2006-07-04 16:51:32 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
HWVoiceOut *hw;
|
|
|
|
CaptureVoiceOut *cap;
|
|
|
|
|
2019-02-25 15:43:35 +00:00
|
|
|
cap = g_malloc0(sizeof(*cap));
|
2006-07-04 16:51:32 +00:00
|
|
|
|
|
|
|
hw = &cap->hw;
|
2009-09-12 07:36:22 +00:00
|
|
|
QLIST_INIT (&hw->sw_head);
|
|
|
|
QLIST_INIT (&cap->cb_head);
|
2006-07-04 16:51:32 +00:00
|
|
|
|
|
|
|
/* XXX find a more elegant way */
|
|
|
|
hw->samples = 4096 * 4;
|
2019-02-25 15:43:35 +00:00
|
|
|
hw->mix_buf = g_new0(struct st_sample, hw->samples);
|
2006-07-04 16:51:32 +00:00
|
|
|
|
2006-07-04 21:47:22 +00:00
|
|
|
audio_pcm_init_info (&hw->info, as);
|
2006-07-04 16:51:32 +00:00
|
|
|
|
2019-02-25 15:43:35 +00:00
|
|
|
cap->buf = g_malloc0_n(hw->samples, 1 << hw->info.shift);
|
2006-07-04 16:51:32 +00:00
|
|
|
|
|
|
|
hw->clip = mixeng_clip
|
|
|
|
[hw->info.nchannels == 2]
|
|
|
|
[hw->info.sign]
|
2006-07-04 21:47:22 +00:00
|
|
|
[hw->info.swap_endianness]
|
2007-02-17 22:19:29 +00:00
|
|
|
[audio_bits_to_index (hw->info.bits)];
|
2006-07-04 16:51:32 +00:00
|
|
|
|
2009-09-12 07:36:22 +00:00
|
|
|
QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
|
|
|
|
QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
|
2006-07-04 16:51:32 +00:00
|
|
|
|
2016-08-01 11:23:43 +00:00
|
|
|
QLIST_FOREACH(hw, &glob_audio_state.hw_head_out, entries) {
|
2009-05-13 23:11:35 +00:00
|
|
|
audio_attach_capture (hw);
|
2006-07-04 16:51:32 +00:00
|
|
|
}
|
2006-07-16 18:57:03 +00:00
|
|
|
return cap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2009-09-12 07:36:22 +00:00
|
|
|
QLIST_REMOVE (cb, entries);
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free (cb);
|
2006-07-16 18:57:03 +00:00
|
|
|
|
|
|
|
if (!cap->cb_head.lh_first) {
|
|
|
|
SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
|
2006-07-18 21:09:59 +00:00
|
|
|
|
2006-07-16 18:57:03 +00:00
|
|
|
while (sw) {
|
2006-07-18 21:09:59 +00:00
|
|
|
SWVoiceCap *sc = (SWVoiceCap *) sw;
|
2006-07-16 18:57:03 +00:00
|
|
|
#ifdef DEBUG_CAPTURE
|
|
|
|
dolog ("freeing %s\n", sw->name);
|
|
|
|
#endif
|
2006-07-18 21:09:59 +00:00
|
|
|
|
2006-07-16 18:57:03 +00:00
|
|
|
sw1 = sw->entries.le_next;
|
|
|
|
if (sw->rate) {
|
|
|
|
st_rate_stop (sw->rate);
|
|
|
|
sw->rate = NULL;
|
|
|
|
}
|
2009-09-12 07:36:22 +00:00
|
|
|
QLIST_REMOVE (sw, entries);
|
|
|
|
QLIST_REMOVE (sc, entries);
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free (sc);
|
2006-07-16 18:57:03 +00:00
|
|
|
sw = sw1;
|
|
|
|
}
|
2009-09-12 07:36:22 +00:00
|
|
|
QLIST_REMOVE (cap, entries);
|
2017-04-28 07:56:12 +00:00
|
|
|
g_free (cap->hw.mix_buf);
|
|
|
|
g_free (cap->buf);
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free (cap);
|
2006-07-16 18:57:03 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2006-07-04 16:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-04 10:21:03 +00:00
|
|
|
|
|
|
|
void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
|
|
|
|
{
|
|
|
|
if (sw) {
|
2012-04-17 12:32:35 +00:00
|
|
|
HWVoiceOut *hw = sw->hw;
|
|
|
|
|
2008-05-04 10:21:03 +00:00
|
|
|
sw->vol.mute = mute;
|
|
|
|
sw->vol.l = nominal_volume.l * lvol / 255;
|
|
|
|
sw->vol.r = nominal_volume.r * rvol / 255;
|
2012-04-17 12:32:35 +00:00
|
|
|
|
|
|
|
if (hw->pcm_ops->ctl_out) {
|
|
|
|
hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
|
|
|
|
}
|
2008-05-04 10:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
|
|
|
|
{
|
|
|
|
if (sw) {
|
2012-04-17 12:32:35 +00:00
|
|
|
HWVoiceIn *hw = sw->hw;
|
|
|
|
|
2008-05-04 10:21:03 +00:00
|
|
|
sw->vol.mute = mute;
|
|
|
|
sw->vol.l = nominal_volume.l * lvol / 255;
|
|
|
|
sw->vol.r = nominal_volume.r * rvol / 255;
|
2012-04-17 12:32:35 +00:00
|
|
|
|
|
|
|
if (hw->pcm_ops->ctl_in) {
|
|
|
|
hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);
|
|
|
|
}
|
2008-05-04 10:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-08 22:34:15 +00:00
|
|
|
|
|
|
|
void audio_create_pdos(Audiodev *dev)
|
|
|
|
{
|
|
|
|
switch (dev->driver) {
|
|
|
|
#define CASE(DRIVER, driver, pdo_name) \
|
|
|
|
case AUDIODEV_DRIVER_##DRIVER: \
|
|
|
|
if (!dev->u.driver.has_in) { \
|
|
|
|
dev->u.driver.in = g_malloc0( \
|
|
|
|
sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
|
|
|
|
dev->u.driver.has_in = true; \
|
|
|
|
} \
|
|
|
|
if (!dev->u.driver.has_out) { \
|
|
|
|
dev->u.driver.out = g_malloc0( \
|
|
|
|
sizeof(AudiodevAlsaPerDirectionOptions)); \
|
|
|
|
dev->u.driver.has_out = true; \
|
|
|
|
} \
|
|
|
|
break
|
|
|
|
|
|
|
|
CASE(NONE, none, );
|
|
|
|
CASE(ALSA, alsa, Alsa);
|
|
|
|
CASE(COREAUDIO, coreaudio, Coreaudio);
|
|
|
|
CASE(DSOUND, dsound, );
|
|
|
|
CASE(OSS, oss, Oss);
|
|
|
|
CASE(PA, pa, Pa);
|
|
|
|
CASE(SDL, sdl, );
|
|
|
|
CASE(SPICE, spice, );
|
|
|
|
CASE(WAV, wav, );
|
|
|
|
|
|
|
|
case AUDIODEV_DRIVER__MAX:
|
|
|
|
abort();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_validate_per_direction_opts(
|
|
|
|
AudiodevPerDirectionOptions *pdo, Error **errp)
|
|
|
|
{
|
|
|
|
if (!pdo->has_fixed_settings) {
|
|
|
|
pdo->has_fixed_settings = true;
|
|
|
|
pdo->fixed_settings = true;
|
|
|
|
}
|
|
|
|
if (!pdo->fixed_settings &&
|
|
|
|
(pdo->has_frequency || pdo->has_channels || pdo->has_format)) {
|
|
|
|
error_setg(errp,
|
|
|
|
"You can't use frequency, channels or format with fixed-settings=off");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pdo->has_frequency) {
|
|
|
|
pdo->has_frequency = true;
|
|
|
|
pdo->frequency = 44100;
|
|
|
|
}
|
|
|
|
if (!pdo->has_channels) {
|
|
|
|
pdo->has_channels = true;
|
|
|
|
pdo->channels = 2;
|
|
|
|
}
|
|
|
|
if (!pdo->has_voices) {
|
|
|
|
pdo->has_voices = true;
|
|
|
|
pdo->voices = 1;
|
|
|
|
}
|
|
|
|
if (!pdo->has_format) {
|
|
|
|
pdo->has_format = true;
|
|
|
|
pdo->format = AUDIO_FORMAT_S16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_validate_opts(Audiodev *dev, Error **errp)
|
|
|
|
{
|
|
|
|
Error *err = NULL;
|
|
|
|
|
|
|
|
audio_create_pdos(dev);
|
|
|
|
|
|
|
|
audio_validate_per_direction_opts(audio_get_pdo_in(dev), &err);
|
|
|
|
if (err) {
|
|
|
|
error_propagate(errp, err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
audio_validate_per_direction_opts(audio_get_pdo_out(dev), &err);
|
|
|
|
if (err) {
|
|
|
|
error_propagate(errp, err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dev->has_timer_period) {
|
|
|
|
dev->has_timer_period = true;
|
|
|
|
dev->timer_period = 10000; /* 100Hz -> 10ms */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_parse_option(const char *opt)
|
|
|
|
{
|
|
|
|
AudiodevListEntry *e;
|
|
|
|
Audiodev *dev = NULL;
|
|
|
|
|
|
|
|
Visitor *v = qobject_input_visitor_new_str(opt, "driver", &error_fatal);
|
|
|
|
visit_type_Audiodev(v, NULL, &dev, &error_fatal);
|
|
|
|
visit_free(v);
|
|
|
|
|
|
|
|
audio_validate_opts(dev, &error_fatal);
|
|
|
|
|
|
|
|
e = g_malloc0(sizeof(AudiodevListEntry));
|
|
|
|
e->dev = dev;
|
|
|
|
QSIMPLEQ_INSERT_TAIL(&audiodevs, e, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_init_audiodevs(void)
|
|
|
|
{
|
|
|
|
AudiodevListEntry *e;
|
|
|
|
|
|
|
|
QSIMPLEQ_FOREACH(e, &audiodevs, next) {
|
|
|
|
audio_init(e->dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo)
|
|
|
|
{
|
|
|
|
return (audsettings) {
|
|
|
|
.freq = pdo->frequency,
|
|
|
|
.nchannels = pdo->channels,
|
|
|
|
.fmt = pdo->format,
|
|
|
|
.endianness = AUDIO_HOST_ENDIANNESS,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
int audioformat_bytes_per_sample(AudioFormat fmt)
|
|
|
|
{
|
|
|
|
switch (fmt) {
|
|
|
|
case AUDIO_FORMAT_U8:
|
|
|
|
case AUDIO_FORMAT_S8:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case AUDIO_FORMAT_U16:
|
|
|
|
case AUDIO_FORMAT_S16:
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
case AUDIO_FORMAT_U32:
|
|
|
|
case AUDIO_FORMAT_S32:
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
case AUDIO_FORMAT__MAX:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* frames = freq * usec / 1e6 */
|
|
|
|
int audio_buffer_frames(AudiodevPerDirectionOptions *pdo,
|
|
|
|
audsettings *as, int def_usecs)
|
|
|
|
{
|
|
|
|
uint64_t usecs = pdo->has_buffer_length ? pdo->buffer_length : def_usecs;
|
|
|
|
return (as->freq * usecs + 500000) / 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* samples = channels * frames = channels * freq * usec / 1e6 */
|
|
|
|
int audio_buffer_samples(AudiodevPerDirectionOptions *pdo,
|
|
|
|
audsettings *as, int def_usecs)
|
|
|
|
{
|
|
|
|
return as->nchannels * audio_buffer_frames(pdo, as, def_usecs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bytes = bytes_per_sample * samples =
|
|
|
|
* bytes_per_sample * channels * freq * usec / 1e6
|
|
|
|
*/
|
|
|
|
int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo,
|
|
|
|
audsettings *as, int def_usecs)
|
|
|
|
{
|
|
|
|
return audio_buffer_samples(pdo, as, def_usecs) *
|
|
|
|
audioformat_bytes_per_sample(as->fmt);
|
|
|
|
}
|