2004-11-07 19:04:02 +01:00
|
|
|
/*
|
|
|
|
* QEMU Audio subsystem
|
2005-10-30 19:58:22 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2005 Vassili Karpov (malc)
|
|
|
|
*
|
2004-11-07 19:04:02 +01: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 16:35:07 +02:00
|
|
|
|
2016-01-18 18:33:52 +01:00
|
|
|
#include "qemu/osdep.h"
|
2007-11-17 18:14:51 +01:00
|
|
|
#include "audio.h"
|
2019-08-12 07:23:45 +02:00
|
|
|
#include "migration/vmstate.h"
|
2012-12-17 18:19:49 +01:00
|
|
|
#include "monitor/monitor.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/timer.h"
|
2019-03-08 23:34:15 +01:00
|
|
|
#include "qapi/error.h"
|
2023-01-23 09:39:56 +01:00
|
|
|
#include "qapi/clone-visitor.h"
|
2019-03-08 23:34:15 +01:00
|
|
|
#include "qapi/qobject-input-visitor.h"
|
|
|
|
#include "qapi/qapi-visit-audio.h"
|
2023-01-23 09:39:56 +01:00
|
|
|
#include "qapi/qapi-commands-audio.h"
|
2016-03-20 18:16:19 +01:00
|
|
|
#include "qemu/cutils.h"
|
2023-01-21 10:47:25 +01:00
|
|
|
#include "qemu/log.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2022-09-08 10:14:41 +02:00
|
|
|
#include "qemu/help_option.h"
|
2022-03-23 16:57:36 +01:00
|
|
|
#include "sysemu/sysemu.h"
|
2017-02-02 06:50:54 +01:00
|
|
|
#include "sysemu/replay.h"
|
2019-08-12 07:23:59 +02:00
|
|
|
#include "sysemu/runstate.h"
|
2020-09-16 10:41:16 +02:00
|
|
|
#include "ui/qemu-spice.h"
|
2018-07-02 18:15:24 +02:00
|
|
|
#include "trace.h"
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
#define AUDIO_CAP "audio"
|
|
|
|
#include "audio_int.h"
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
/* #define DEBUG_LIVE */
|
|
|
|
/* #define DEBUG_OUT */
|
2006-07-04 18:51:32 +02:00
|
|
|
/* #define DEBUG_CAPTURE */
|
2009-09-12 00:28:45 +02:00
|
|
|
/* #define DEBUG_POLL */
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-11-05 19:55:28 +01:00
|
|
|
#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
|
|
|
|
|
2009-07-27 16:13:25 +02: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 23:34:15 +01:00
|
|
|
const char *audio_prio_list[] = {
|
2018-03-06 08:40:47 +01:00
|
|
|
"spice",
|
2009-07-27 16:13:25 +02:00
|
|
|
CONFIG_AUDIO_DRIVERS
|
2018-03-06 08:40:47 +01:00
|
|
|
"none",
|
|
|
|
"wav",
|
2019-03-08 23:34:15 +01:00
|
|
|
NULL
|
2005-10-30 19:58:22 +01:00
|
|
|
};
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2018-03-06 08:40:47 +01:00
|
|
|
static QLIST_HEAD(, audio_driver) audio_drivers;
|
2019-03-08 23:34:15 +01:00
|
|
|
static AudiodevListHead audiodevs = QSIMPLEQ_HEAD_INITIALIZER(audiodevs);
|
2018-03-06 08:40:47 +01: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;
|
module: add Error arguments to module_load and module_load_qom
improve error handling during module load, by changing:
bool module_load(const char *prefix, const char *lib_name);
void module_load_qom(const char *type);
to:
int module_load(const char *prefix, const char *name, Error **errp);
int module_load_qom(const char *type, Error **errp);
where the return value is:
-1 on module load error, and errp is set with the error
0 on module or one of its dependencies are not installed
1 on module load success
2 on module load success (module already loaded or built-in)
module_load_qom_one has been introduced in:
commit 28457744c345 ("module: qom module support"), which built on top of
module_load_one, but discarded the bool return value. Restore it.
Adapt all callers to emit errors, or ignore them, or fail hard,
as appropriate in each context.
Replace the previous emission of errors via fprintf in _some_ error
conditions with Error and error_report, so as to emit to the appropriate
target.
A memory leak is also fixed as part of the module_load changes.
audio: when attempting to load an audio module, report module load errors.
Note that still for some callers, a single issue may generate multiple
error reports, and this could be improved further.
Regarding the audio code itself, audio_add() seems to ignore errors,
and this should probably be improved.
block: when attempting to load a block module, report module load errors.
For the code paths that already use the Error API, take advantage of those
to report module load errors into the Error parameter.
For the other code paths, we currently emit the error, but this could be
improved further by adding Error parameters to all possible code paths.
console: when attempting to load a display module, report module load errors.
qdev: when creating a new qdev Device object (DeviceState), report load errors.
If a module cannot be loaded to create that device, now abort execution
(if no CONFIG_MODULE) or exit (if CONFIG_MODULE).
qom/object.c: when initializing a QOM object, or looking up class_by_name,
report module load errors.
qtest: when processing the "module_load" qtest command, report errors
in the load of the module.
Signed-off-by: Claudio Fontana <cfontana@suse.de>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20220929093035.4231-4-cfontana@suse.de>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2022-09-29 11:30:33 +02:00
|
|
|
Error *local_err = NULL;
|
|
|
|
int rv;
|
2018-03-06 08:40:47 +01:00
|
|
|
|
|
|
|
QLIST_FOREACH(d, &audio_drivers, next) {
|
|
|
|
if (strcmp(name, d->name) == 0) {
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|
module: add Error arguments to module_load and module_load_qom
improve error handling during module load, by changing:
bool module_load(const char *prefix, const char *lib_name);
void module_load_qom(const char *type);
to:
int module_load(const char *prefix, const char *name, Error **errp);
int module_load_qom(const char *type, Error **errp);
where the return value is:
-1 on module load error, and errp is set with the error
0 on module or one of its dependencies are not installed
1 on module load success
2 on module load success (module already loaded or built-in)
module_load_qom_one has been introduced in:
commit 28457744c345 ("module: qom module support"), which built on top of
module_load_one, but discarded the bool return value. Restore it.
Adapt all callers to emit errors, or ignore them, or fail hard,
as appropriate in each context.
Replace the previous emission of errors via fprintf in _some_ error
conditions with Error and error_report, so as to emit to the appropriate
target.
A memory leak is also fixed as part of the module_load changes.
audio: when attempting to load an audio module, report module load errors.
Note that still for some callers, a single issue may generate multiple
error reports, and this could be improved further.
Regarding the audio code itself, audio_add() seems to ignore errors,
and this should probably be improved.
block: when attempting to load a block module, report module load errors.
For the code paths that already use the Error API, take advantage of those
to report module load errors into the Error parameter.
For the other code paths, we currently emit the error, but this could be
improved further by adding Error parameters to all possible code paths.
console: when attempting to load a display module, report module load errors.
qdev: when creating a new qdev Device object (DeviceState), report load errors.
If a module cannot be loaded to create that device, now abort execution
(if no CONFIG_MODULE) or exit (if CONFIG_MODULE).
qom/object.c: when initializing a QOM object, or looking up class_by_name,
report module load errors.
qtest: when processing the "module_load" qtest command, report errors
in the load of the module.
Signed-off-by: Claudio Fontana <cfontana@suse.de>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20220929093035.4231-4-cfontana@suse.de>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2022-09-29 11:30:33 +02:00
|
|
|
rv = audio_module_load(name, &local_err);
|
|
|
|
if (rv > 0) {
|
|
|
|
QLIST_FOREACH(d, &audio_drivers, next) {
|
|
|
|
if (strcmp(name, d->name) == 0) {
|
|
|
|
return d;
|
|
|
|
}
|
2018-03-06 08:40:48 +01:00
|
|
|
}
|
module: add Error arguments to module_load and module_load_qom
improve error handling during module load, by changing:
bool module_load(const char *prefix, const char *lib_name);
void module_load_qom(const char *type);
to:
int module_load(const char *prefix, const char *name, Error **errp);
int module_load_qom(const char *type, Error **errp);
where the return value is:
-1 on module load error, and errp is set with the error
0 on module or one of its dependencies are not installed
1 on module load success
2 on module load success (module already loaded or built-in)
module_load_qom_one has been introduced in:
commit 28457744c345 ("module: qom module support"), which built on top of
module_load_one, but discarded the bool return value. Restore it.
Adapt all callers to emit errors, or ignore them, or fail hard,
as appropriate in each context.
Replace the previous emission of errors via fprintf in _some_ error
conditions with Error and error_report, so as to emit to the appropriate
target.
A memory leak is also fixed as part of the module_load changes.
audio: when attempting to load an audio module, report module load errors.
Note that still for some callers, a single issue may generate multiple
error reports, and this could be improved further.
Regarding the audio code itself, audio_add() seems to ignore errors,
and this should probably be improved.
block: when attempting to load a block module, report module load errors.
For the code paths that already use the Error API, take advantage of those
to report module load errors into the Error parameter.
For the other code paths, we currently emit the error, but this could be
improved further by adding Error parameters to all possible code paths.
console: when attempting to load a display module, report module load errors.
qdev: when creating a new qdev Device object (DeviceState), report load errors.
If a module cannot be loaded to create that device, now abort execution
(if no CONFIG_MODULE) or exit (if CONFIG_MODULE).
qom/object.c: when initializing a QOM object, or looking up class_by_name,
report module load errors.
qtest: when processing the "module_load" qtest command, report errors
in the load of the module.
Signed-off-by: Claudio Fontana <cfontana@suse.de>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20220929093035.4231-4-cfontana@suse.de>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2022-09-29 11:30:33 +02:00
|
|
|
} else if (rv < 0) {
|
|
|
|
error_report_err(local_err);
|
2018-03-06 08:40:48 +01:00
|
|
|
}
|
2018-03-06 08:40:47 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-19 01:06:47 +02:00
|
|
|
static QTAILQ_HEAD(AudioStateHead, AudioState) audio_states =
|
|
|
|
QTAILQ_HEAD_INITIALIZER(audio_states);
|
2005-11-05 19:55:28 +01:00
|
|
|
|
2011-01-05 01:05:47 +01:00
|
|
|
const struct mixeng_volume nominal_volume = {
|
2009-07-20 20:57:00 +02:00
|
|
|
.mute = 0,
|
2005-10-30 19:58:22 +01:00
|
|
|
#ifdef FLOAT_MIXENG
|
2009-07-20 20:57:00 +02:00
|
|
|
.r = 1.0,
|
|
|
|
.l = 1.0,
|
2005-10-30 19:58:22 +01:00
|
|
|
#else
|
2009-07-20 20:57:00 +02:00
|
|
|
.r = 1ULL << 32,
|
|
|
|
.l = 1ULL << 32,
|
2005-10-30 19:58:22 +01:00
|
|
|
#endif
|
2004-11-07 19:04:02 +01:00
|
|
|
};
|
|
|
|
|
2019-08-19 01:06:51 +02:00
|
|
|
static bool legacy_config = true;
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
int audio_bug (const char *funcname, int cond)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2005-10-30 19:58:22 +01:00
|
|
|
if (cond) {
|
|
|
|
static int shown;
|
|
|
|
|
2006-07-04 18:51:32 +02:00
|
|
|
AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
|
2005-10-30 19:58:22 +01: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");
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
return cond;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2007-02-17 23:19:29 +01: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);
|
2022-09-17 15:16:25 +02:00
|
|
|
return 0;
|
2007-02-17 23:19:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-11 01:04:19 +01:00
|
|
|
void AUD_vlog (const char *cap, const char *fmt, va_list ap)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2015-06-12 14:33:02 +02:00
|
|
|
if (cap) {
|
|
|
|
fprintf(stderr, "%s: ", cap);
|
2005-11-11 01:04:19 +01:00
|
|
|
}
|
|
|
|
|
2015-06-12 14:33:02 +02:00
|
|
|
vfprintf(stderr, fmt, ap);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-11-11 01:04:19 +01:00
|
|
|
void AUD_log (const char *cap, const char *fmt, ...)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2005-11-11 01:04:19 +01:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
AUD_vlog (cap, fmt, ap);
|
|
|
|
va_end (ap);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2008-12-03 23:48:44 +01:00
|
|
|
static void audio_print_settings (struct audsettings *as)
|
2005-11-05 19:55:28 +01:00
|
|
|
{
|
|
|
|
dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
|
|
|
|
|
|
|
|
switch (as->fmt) {
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_S8:
|
2005-11-05 19:55:28 +01:00
|
|
|
AUD_log (NULL, "S8");
|
|
|
|
break;
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_U8:
|
2005-11-05 19:55:28 +01:00
|
|
|
AUD_log (NULL, "U8");
|
|
|
|
break;
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_S16:
|
2005-11-05 19:55:28 +01:00
|
|
|
AUD_log (NULL, "S16");
|
|
|
|
break;
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_U16:
|
2005-11-05 19:55:28 +01:00
|
|
|
AUD_log (NULL, "U16");
|
|
|
|
break;
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_S32:
|
2008-06-22 16:10:45 +02:00
|
|
|
AUD_log (NULL, "S32");
|
|
|
|
break;
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_U32:
|
2008-06-22 16:10:45 +02:00
|
|
|
AUD_log (NULL, "U32");
|
|
|
|
break;
|
2020-02-02 20:38:07 +01:00
|
|
|
case AUDIO_FORMAT_F32:
|
|
|
|
AUD_log (NULL, "F32");
|
|
|
|
break;
|
2005-11-05 19:55:28 +01:00
|
|
|
default:
|
|
|
|
AUD_log (NULL, "invalid(%d)", as->fmt);
|
|
|
|
break;
|
|
|
|
}
|
2006-07-16 20:57:03 +02:00
|
|
|
|
|
|
|
AUD_log (NULL, " endianness=");
|
2006-07-04 23:47:22 +02: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 19:55:28 +01:00
|
|
|
AUD_log (NULL, "\n");
|
|
|
|
}
|
|
|
|
|
2008-12-03 23:48:44 +01:00
|
|
|
static int audio_validate_settings (struct audsettings *as)
|
2005-11-05 19:55:28 +01:00
|
|
|
{
|
|
|
|
int invalid;
|
|
|
|
|
2019-10-13 21:58:03 +02:00
|
|
|
invalid = as->nchannels < 1;
|
2006-07-04 23:47:22 +02:00
|
|
|
invalid |= as->endianness != 0 && as->endianness != 1;
|
2005-11-05 19:55:28 +01:00
|
|
|
|
|
|
|
switch (as->fmt) {
|
2019-03-08 23:34:13 +01: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:
|
2020-02-02 20:38:07 +01:00
|
|
|
case AUDIO_FORMAT_F32:
|
2005-11-05 19:55:28 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
invalid = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid |= as->freq <= 0;
|
2006-07-04 23:47:22 +02:00
|
|
|
return invalid ? -1 : 0;
|
2005-11-05 19:55:28 +01:00
|
|
|
}
|
|
|
|
|
2008-12-03 23:48:44 +01:00
|
|
|
static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2020-02-02 20:38:07 +01:00
|
|
|
int bits = 8;
|
|
|
|
bool is_signed = false, is_float = false;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-11-05 19:55:28 +01:00
|
|
|
switch (as->fmt) {
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_S8:
|
2020-02-02 20:38:07 +01:00
|
|
|
is_signed = true;
|
2012-02-25 14:46:55 +01:00
|
|
|
/* fall through */
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_U8:
|
2005-10-30 19:58:22 +01:00
|
|
|
break;
|
|
|
|
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_S16:
|
2020-02-02 20:38:07 +01:00
|
|
|
is_signed = true;
|
2012-02-25 14:46:55 +01:00
|
|
|
/* fall through */
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_U16:
|
2005-10-30 19:58:22 +01:00
|
|
|
bits = 16;
|
|
|
|
break;
|
2007-02-17 23:19:29 +01:00
|
|
|
|
2020-02-02 20:38:07 +01:00
|
|
|
case AUDIO_FORMAT_F32:
|
|
|
|
is_float = true;
|
|
|
|
/* fall through */
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_S32:
|
2020-02-02 20:38:07 +01:00
|
|
|
is_signed = true;
|
2012-02-25 14:46:55 +01:00
|
|
|
/* fall through */
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_U32:
|
2007-02-17 23:19:29 +01:00
|
|
|
bits = 32;
|
|
|
|
break;
|
2019-03-08 23:34:13 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2005-11-05 19:55:28 +01:00
|
|
|
return info->freq == as->freq
|
|
|
|
&& info->nchannels == as->nchannels
|
2020-02-02 20:38:07 +01:00
|
|
|
&& info->is_signed == is_signed
|
|
|
|
&& info->is_float == is_float
|
2006-07-04 23:47:22 +02:00
|
|
|
&& info->bits == bits
|
|
|
|
&& info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2008-12-03 23:48:44 +01:00
|
|
|
void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
|
2005-10-30 19:58:22 +01:00
|
|
|
{
|
2020-02-02 20:38:07 +01:00
|
|
|
int bits = 8, mul;
|
|
|
|
bool is_signed = false, is_float = false;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2005-11-05 19:55:28 +01:00
|
|
|
switch (as->fmt) {
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_S8:
|
2020-02-02 20:38:07 +01:00
|
|
|
is_signed = true;
|
2019-12-18 20:25:21 +01:00
|
|
|
/* fall through */
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_U8:
|
2019-10-13 21:58:02 +02:00
|
|
|
mul = 1;
|
2004-11-07 19:04:02 +01:00
|
|
|
break;
|
|
|
|
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_S16:
|
2020-02-02 20:38:07 +01:00
|
|
|
is_signed = true;
|
2019-07-18 21:34:41 +02:00
|
|
|
/* fall through */
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_U16:
|
2004-11-07 19:04:02 +01:00
|
|
|
bits = 16;
|
2019-10-13 21:58:02 +02:00
|
|
|
mul = 2;
|
2007-02-17 23:19:29 +01:00
|
|
|
break;
|
|
|
|
|
2020-02-02 20:38:07 +01:00
|
|
|
case AUDIO_FORMAT_F32:
|
|
|
|
is_float = true;
|
|
|
|
/* fall through */
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_S32:
|
2020-02-02 20:38:07 +01:00
|
|
|
is_signed = true;
|
2019-07-18 21:34:41 +02:00
|
|
|
/* fall through */
|
2019-03-08 23:34:13 +01:00
|
|
|
case AUDIO_FORMAT_U32:
|
2007-02-17 23:19:29 +01:00
|
|
|
bits = 32;
|
2019-10-13 21:58:02 +02:00
|
|
|
mul = 4;
|
2004-11-07 19:04:02 +01:00
|
|
|
break;
|
2019-03-08 23:34:13 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-11-05 19:55:28 +01:00
|
|
|
info->freq = as->freq;
|
2005-10-30 19:58:22 +01:00
|
|
|
info->bits = bits;
|
2020-02-02 20:38:07 +01:00
|
|
|
info->is_signed = is_signed;
|
|
|
|
info->is_float = is_float;
|
2005-11-05 19:55:28 +01:00
|
|
|
info->nchannels = as->nchannels;
|
2019-10-13 21:58:02 +02:00
|
|
|
info->bytes_per_frame = as->nchannels * mul;
|
|
|
|
info->bytes_per_second = info->freq * info->bytes_per_frame;
|
2006-07-04 23:47:22 +02:00
|
|
|
info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2005-10-30 19:58:22 +01:00
|
|
|
if (!len) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-02 20:38:07 +01:00
|
|
|
if (info->is_signed || info->is_float) {
|
2019-10-13 21:58:02 +02:00
|
|
|
memset(buf, 0x00, len * info->bytes_per_frame);
|
2021-01-15 02:24:25 +01:00
|
|
|
} else {
|
2007-02-17 23:19:29 +01:00
|
|
|
switch (info->bits) {
|
|
|
|
case 8:
|
2019-10-13 21:58:02 +02:00
|
|
|
memset(buf, 0x80, len * info->bytes_per_frame);
|
2007-02-17 23:19:29 +01:00
|
|
|
break;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2007-02-17 23:19:29 +01:00
|
|
|
case 16:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint16_t *p = buf;
|
|
|
|
short s = INT16_MAX;
|
|
|
|
|
|
|
|
if (info->swap_endianness) {
|
|
|
|
s = bswap16 (s);
|
|
|
|
}
|
|
|
|
|
2019-10-13 21:58:02 +02:00
|
|
|
for (i = 0; i < len * info->nchannels; i++) {
|
2007-02-17 23:19:29 +01:00
|
|
|
p[i] = s;
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2007-02-17 23:19:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 32:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint32_t *p = buf;
|
|
|
|
int32_t s = INT32_MAX;
|
|
|
|
|
|
|
|
if (info->swap_endianness) {
|
|
|
|
s = bswap32 (s);
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2019-10-13 21:58:02 +02:00
|
|
|
for (i = 0; i < len * info->nchannels; i++) {
|
2007-02-17 23:19:29 +01:00
|
|
|
p[i] = s;
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2007-02-17 23:19:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
|
|
|
|
info->bits);
|
|
|
|
break;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-04 18:51:32 +02:00
|
|
|
/*
|
|
|
|
* Capture
|
|
|
|
*/
|
2019-08-19 01:06:46 +02:00
|
|
|
static CaptureVoiceOut *audio_pcm_capture_find_specific(AudioState *s,
|
|
|
|
struct audsettings *as)
|
2006-07-04 18:51:32 +02:00
|
|
|
{
|
|
|
|
CaptureVoiceOut *cap;
|
|
|
|
|
|
|
|
for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
|
2006-07-04 23:47:22 +02:00
|
|
|
if (audio_pcm_info_eq (&cap->hw.info, as)) {
|
2006-07-04 18:51:32 +02:00
|
|
|
return cap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-07-16 20:57:03 +02:00
|
|
|
static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
|
2006-07-04 18:51:32 +02:00
|
|
|
{
|
2006-07-16 20:57:03 +02: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 18:51:32 +02:00
|
|
|
|
2006-07-16 20:57:03 +02:00
|
|
|
static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
|
|
|
|
{
|
|
|
|
if (cap->hw.enabled != enabled) {
|
|
|
|
audcnotification_e cmd;
|
2006-07-04 18:51:32 +02:00
|
|
|
cap->hw.enabled = enabled;
|
2006-07-16 20:57:03 +02:00
|
|
|
cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
|
|
|
|
audio_notify_capture (cap, cmd);
|
2006-07-04 18:51:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
|
|
|
|
{
|
|
|
|
HWVoiceOut *hw = &cap->hw;
|
|
|
|
SWVoiceOut *sw;
|
|
|
|
int enabled = 0;
|
|
|
|
|
2006-07-16 20:57:03 +02:00
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
2006-07-04 18:51:32 +02:00
|
|
|
if (sw->active) {
|
|
|
|
enabled = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-07-16 20:57:03 +02:00
|
|
|
audio_capture_maybe_changed (cap, enabled);
|
2006-07-04 18:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_detach_capture (HWVoiceOut *hw)
|
|
|
|
{
|
2006-07-16 20:57:03 +02: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 18:51:32 +02:00
|
|
|
|
|
|
|
if (sw->rate) {
|
|
|
|
st_rate_stop (sw->rate);
|
|
|
|
sw->rate = NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-12 09:36:22 +02:00
|
|
|
QLIST_REMOVE (sw, entries);
|
|
|
|
QLIST_REMOVE (sc, entries);
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free (sc);
|
2006-07-16 20:57:03 +02: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 18:51:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-14 01:11:35 +02:00
|
|
|
static int audio_attach_capture (HWVoiceOut *hw)
|
2006-07-04 18:51:32 +02:00
|
|
|
{
|
2019-08-19 01:06:46 +02:00
|
|
|
AudioState *s = hw->s;
|
2006-07-04 18:51:32 +02:00
|
|
|
CaptureVoiceOut *cap;
|
|
|
|
|
|
|
|
audio_detach_capture (hw);
|
|
|
|
for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
|
2006-07-16 20:57:03 +02:00
|
|
|
SWVoiceCap *sc;
|
2006-07-04 18:51:32 +02:00
|
|
|
SWVoiceOut *sw;
|
2006-07-16 20:57:03 +02:00
|
|
|
HWVoiceOut *hw_cap = &cap->hw;
|
2006-07-04 18:51:32 +02:00
|
|
|
|
2019-02-25 16:43:35 +01:00
|
|
|
sc = g_malloc0(sizeof(*sc));
|
2006-07-04 18:51:32 +02:00
|
|
|
|
2006-07-16 20:57:03 +02:00
|
|
|
sc->cap = cap;
|
|
|
|
sw = &sc->sw;
|
2006-07-04 18:51:32 +02:00
|
|
|
sw->hw = hw_cap;
|
2006-07-16 20:57:03 +02:00
|
|
|
sw->info = hw->info;
|
2006-07-04 18:51:32 +02:00
|
|
|
sw->empty = 1;
|
|
|
|
sw->active = hw->enabled;
|
2012-07-16 16:08:36 +02:00
|
|
|
sw->vol = nominal_volume;
|
2006-07-04 18:51:32 +02:00
|
|
|
sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
|
2009-09-12 09:36:22 +02:00
|
|
|
QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
|
|
|
|
QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
|
2006-07-16 20:57:03 +02:00
|
|
|
#ifdef DEBUG_CAPTURE
|
2013-01-16 18:17:33 +01:00
|
|
|
sw->name = g_strdup_printf ("for %p %d,%d,%d",
|
|
|
|
hw, sw->info.freq, sw->info.bits,
|
|
|
|
sw->info.nchannels);
|
2006-07-16 20:57:03 +02:00
|
|
|
dolog ("Added %s active = %d\n", sw->name, sw->active);
|
|
|
|
#endif
|
2006-07-04 18:51:32 +02:00
|
|
|
if (sw->active) {
|
2006-07-16 20:57:03 +02:00
|
|
|
audio_capture_maybe_changed (cap, 1);
|
2006-07-04 18:51:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
/*
|
|
|
|
* Hard voice (capture)
|
|
|
|
*/
|
2019-08-19 01:06:58 +02:00
|
|
|
static size_t audio_pcm_hw_find_min_in (HWVoiceIn *hw)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2005-10-30 19:58:22 +01:00
|
|
|
SWVoiceIn *sw;
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t m = hw->total_samples_captured;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
|
|
|
if (sw->active) {
|
2019-08-19 01:06:54 +02:00
|
|
|
m = MIN (m, sw->total_hw_samples_acquired);
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
return m;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2019-09-19 23:24:19 +02:00
|
|
|
static size_t audio_pcm_hw_get_live_in(HWVoiceIn *hw)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
|
2023-02-24 20:05:41 +01:00
|
|
|
if (audio_bug(__func__, live > hw->conv_buf.size)) {
|
|
|
|
dolog("live=%zu hw->conv_buf.size=%zu\n", live, hw->conv_buf.size);
|
2022-09-17 15:16:25 +02:00
|
|
|
return 0;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
return live;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 20:12:59 +01:00
|
|
|
static size_t audio_pcm_hw_conv_in(HWVoiceIn *hw, void *pcm_buf, size_t samples)
|
|
|
|
{
|
|
|
|
size_t conv = 0;
|
2023-02-24 20:05:41 +01:00
|
|
|
STSampleBuffer *conv_buf = &hw->conv_buf;
|
2022-03-01 20:12:59 +01:00
|
|
|
|
|
|
|
while (samples) {
|
|
|
|
uint8_t *src = advance(pcm_buf, conv * hw->info.bytes_per_frame);
|
|
|
|
size_t proc = MIN(samples, conv_buf->size - conv_buf->pos);
|
|
|
|
|
2023-02-24 20:05:41 +01:00
|
|
|
hw->conv(conv_buf->buffer + conv_buf->pos, src, proc);
|
2022-03-01 20:12:59 +01:00
|
|
|
conv_buf->pos = (conv_buf->pos + proc) % conv_buf->size;
|
|
|
|
samples -= proc;
|
|
|
|
conv += proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return conv;
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
/*
|
|
|
|
* Soft voice (capture)
|
|
|
|
*/
|
2023-02-24 20:05:50 +01:00
|
|
|
static void audio_pcm_sw_resample_in(SWVoiceIn *sw,
|
|
|
|
size_t frames_in_max, size_t frames_out_max,
|
|
|
|
size_t *total_in, size_t *total_out)
|
|
|
|
{
|
|
|
|
HWVoiceIn *hw = sw->hw;
|
|
|
|
struct st_sample *src, *dst;
|
|
|
|
size_t live, rpos, frames_in, frames_out;
|
|
|
|
|
|
|
|
live = hw->total_samples_captured - sw->total_hw_samples_acquired;
|
|
|
|
rpos = audio_ring_posb(hw->conv_buf.pos, live, hw->conv_buf.size);
|
|
|
|
|
|
|
|
/* resample conv_buf from rpos to end of buffer */
|
|
|
|
src = hw->conv_buf.buffer + rpos;
|
|
|
|
frames_in = MIN(frames_in_max, hw->conv_buf.size - rpos);
|
|
|
|
dst = sw->resample_buf.buffer;
|
|
|
|
frames_out = frames_out_max;
|
|
|
|
st_rate_flow(sw->rate, src, dst, &frames_in, &frames_out);
|
|
|
|
rpos += frames_in;
|
|
|
|
*total_in = frames_in;
|
|
|
|
*total_out = frames_out;
|
|
|
|
|
|
|
|
/* resample conv_buf from start of buffer if there are input frames left */
|
|
|
|
if (frames_in_max - frames_in && rpos == hw->conv_buf.size) {
|
|
|
|
src = hw->conv_buf.buffer;
|
|
|
|
frames_in = frames_in_max - frames_in;
|
|
|
|
dst += frames_out;
|
|
|
|
frames_out = frames_out_max - frames_out;
|
|
|
|
st_rate_flow(sw->rate, src, dst, &frames_in, &frames_out);
|
|
|
|
*total_in += frames_in;
|
|
|
|
*total_out += frames_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 20:05:51 +01:00
|
|
|
static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t buf_len)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2005-10-30 19:58:22 +01:00
|
|
|
HWVoiceIn *hw = sw->hw;
|
2023-02-24 20:05:52 +01:00
|
|
|
size_t live, frames_out_max, total_in, total_out;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
live = hw->total_samples_captured - sw->total_hw_samples_acquired;
|
2022-03-01 20:13:00 +01:00
|
|
|
if (!live) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-02-24 20:05:41 +01:00
|
|
|
if (audio_bug(__func__, live > hw->conv_buf.size)) {
|
|
|
|
dolog("live_in=%zu hw->conv_buf.size=%zu\n", live, hw->conv_buf.size);
|
2022-09-17 15:16:25 +02:00
|
|
|
return 0;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
|
|
|
|
2023-02-24 20:05:52 +01:00
|
|
|
frames_out_max = MIN(buf_len / sw->info.bytes_per_frame,
|
|
|
|
sw->resample_buf.size);
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2023-02-24 20:05:52 +01:00
|
|
|
audio_pcm_sw_resample_in(sw, live, frames_out_max, &total_in, &total_out);
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2022-03-01 20:13:05 +01:00
|
|
|
if (!hw->pcm_ops->volume_in) {
|
2023-02-24 20:05:51 +01:00
|
|
|
mixeng_volume(sw->resample_buf.buffer, total_out, &sw->vol);
|
2012-04-17 14:32:36 +02:00
|
|
|
}
|
2023-02-24 20:05:51 +01:00
|
|
|
sw->clip(buf, sw->resample_buf.buffer, total_out);
|
2011-01-05 01:05:47 +01:00
|
|
|
|
2023-02-24 20:05:51 +01:00
|
|
|
sw->total_hw_samples_acquired += total_in;
|
|
|
|
return total_out * sw->info.bytes_per_frame;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
/*
|
|
|
|
* Hard voice (playback)
|
|
|
|
*/
|
2019-08-19 01:06:58 +02:00
|
|
|
static size_t audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
|
2005-10-30 19:58:22 +01:00
|
|
|
{
|
2005-11-05 19:55:28 +01:00
|
|
|
SWVoiceOut *sw;
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t m = SIZE_MAX;
|
2005-11-05 19:55:28 +01:00
|
|
|
int nb_live = 0;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-11-05 19:55:28 +01:00
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
|
|
|
if (sw->active || !sw->empty) {
|
2019-08-19 01:06:54 +02:00
|
|
|
m = MIN (m, sw->total_hw_samples_mixed);
|
2005-11-05 19:55:28 +01:00
|
|
|
nb_live += 1;
|
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2005-11-05 19:55:28 +01:00
|
|
|
|
|
|
|
*nb_livep = nb_live;
|
|
|
|
return m;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2019-08-19 01:06:58 +02:00
|
|
|
static size_t audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
|
2005-10-30 19:58:22 +01:00
|
|
|
{
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t smin;
|
2009-09-18 09:37:39 +02:00
|
|
|
int nb_live1;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2009-09-18 09:37:39 +02:00
|
|
|
smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
|
|
|
|
if (nb_live) {
|
|
|
|
*nb_live = nb_live1;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2009-09-18 09:37:39 +02:00
|
|
|
|
|
|
|
if (nb_live1) {
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t live = smin;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2023-02-24 20:05:41 +01:00
|
|
|
if (audio_bug(__func__, live > hw->mix_buf.size)) {
|
|
|
|
dolog("live=%zu hw->mix_buf.size=%zu\n", live, hw->mix_buf.size);
|
2022-09-17 15:16:25 +02:00
|
|
|
return 0;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
return live;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2009-09-18 09:37:39 +02:00
|
|
|
return 0;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 20:13:06 +01:00
|
|
|
static size_t audio_pcm_hw_get_free(HWVoiceOut *hw)
|
|
|
|
{
|
|
|
|
return (hw->pcm_ops->buffer_get_free ? hw->pcm_ops->buffer_get_free(hw) :
|
|
|
|
INT_MAX) / hw->info.bytes_per_frame;
|
|
|
|
}
|
|
|
|
|
2022-03-01 20:12:58 +01:00
|
|
|
static void audio_pcm_hw_clip_out(HWVoiceOut *hw, void *pcm_buf, size_t len)
|
|
|
|
{
|
|
|
|
size_t clipped = 0;
|
2023-02-24 20:05:41 +01:00
|
|
|
size_t pos = hw->mix_buf.pos;
|
2022-03-01 20:12:58 +01:00
|
|
|
|
|
|
|
while (len) {
|
2023-02-24 20:05:41 +01:00
|
|
|
st_sample *src = hw->mix_buf.buffer + pos;
|
2022-03-01 20:12:58 +01:00
|
|
|
uint8_t *dst = advance(pcm_buf, clipped * hw->info.bytes_per_frame);
|
2023-02-24 20:05:41 +01:00
|
|
|
size_t samples_till_end_of_buf = hw->mix_buf.size - pos;
|
2022-03-01 20:12:58 +01:00
|
|
|
size_t samples_to_clip = MIN(len, samples_till_end_of_buf);
|
|
|
|
|
|
|
|
hw->clip(dst, src, samples_to_clip);
|
|
|
|
|
2023-02-24 20:05:41 +01:00
|
|
|
pos = (pos + samples_to_clip) % hw->mix_buf.size;
|
2022-03-01 20:12:58 +01:00
|
|
|
len -= samples_to_clip;
|
|
|
|
clipped += samples_to_clip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
/*
|
|
|
|
* Soft voice (playback)
|
|
|
|
*/
|
2023-02-24 20:05:44 +01:00
|
|
|
static void audio_pcm_sw_resample_out(SWVoiceOut *sw,
|
|
|
|
size_t frames_in_max, size_t frames_out_max,
|
|
|
|
size_t *total_in, size_t *total_out)
|
|
|
|
{
|
|
|
|
HWVoiceOut *hw = sw->hw;
|
|
|
|
struct st_sample *src, *dst;
|
|
|
|
size_t live, wpos, frames_in, frames_out;
|
|
|
|
|
|
|
|
live = sw->total_hw_samples_mixed;
|
|
|
|
wpos = (hw->mix_buf.pos + live) % hw->mix_buf.size;
|
|
|
|
|
|
|
|
/* write to mix_buf from wpos to end of buffer */
|
|
|
|
src = sw->resample_buf.buffer;
|
|
|
|
frames_in = frames_in_max;
|
|
|
|
dst = hw->mix_buf.buffer + wpos;
|
|
|
|
frames_out = MIN(frames_out_max, hw->mix_buf.size - wpos);
|
|
|
|
st_rate_flow_mix(sw->rate, src, dst, &frames_in, &frames_out);
|
|
|
|
wpos += frames_out;
|
|
|
|
*total_in = frames_in;
|
|
|
|
*total_out = frames_out;
|
|
|
|
|
|
|
|
/* write to mix_buf from start of buffer if there are input frames left */
|
|
|
|
if (frames_in_max - frames_in > 0 && wpos == hw->mix_buf.size) {
|
|
|
|
src += frames_in;
|
|
|
|
frames_in = frames_in_max - frames_in;
|
|
|
|
dst = hw->mix_buf.buffer;
|
|
|
|
frames_out = frames_out_max - frames_out;
|
|
|
|
st_rate_flow_mix(sw->rate, src, dst, &frames_in, &frames_out);
|
|
|
|
*total_in += frames_in;
|
|
|
|
*total_out += frames_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 20:05:46 +01:00
|
|
|
static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t buf_len)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2023-02-24 20:05:46 +01:00
|
|
|
HWVoiceOut *hw = sw->hw;
|
2023-02-24 20:05:49 +01:00
|
|
|
size_t live, dead, hw_free, sw_max, fe_max;
|
|
|
|
size_t frames_in_max, frames_out_max, total_in, total_out;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
live = sw->total_hw_samples_mixed;
|
2023-02-24 20:05:46 +01:00
|
|
|
if (audio_bug(__func__, live > hw->mix_buf.size)) {
|
|
|
|
dolog("live=%zu hw->mix_buf.size=%zu\n", live, hw->mix_buf.size);
|
2022-09-17 15:16:25 +02:00
|
|
|
return 0;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2023-02-24 20:05:46 +01:00
|
|
|
if (live == hw->mix_buf.size) {
|
2006-07-16 20:57:03 +02:00
|
|
|
#ifdef DEBUG_OUT
|
2021-06-16 16:14:11 +02:00
|
|
|
dolog ("%s is full %zu\n", sw->name, live);
|
2006-07-16 20:57:03 +02:00
|
|
|
#endif
|
2005-10-30 19:58:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2023-02-24 20:05:46 +01:00
|
|
|
dead = hw->mix_buf.size - live;
|
|
|
|
hw_free = audio_pcm_hw_get_free(hw);
|
2022-03-01 20:13:06 +01:00
|
|
|
hw_free = hw_free > live ? hw_free - live : 0;
|
2023-02-24 20:05:49 +01:00
|
|
|
frames_out_max = MIN(dead, hw_free);
|
|
|
|
sw_max = st_rate_frames_in(sw->rate, frames_out_max);
|
2023-02-24 20:05:53 +01:00
|
|
|
fe_max = MIN(buf_len / sw->info.bytes_per_frame + sw->resample_buf.pos,
|
|
|
|
sw->resample_buf.size);
|
2023-02-24 20:05:49 +01:00
|
|
|
frames_in_max = MIN(sw_max, fe_max);
|
2012-04-17 14:32:36 +02:00
|
|
|
|
2023-02-24 20:05:49 +01:00
|
|
|
if (!frames_in_max) {
|
|
|
|
return 0;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
|
|
|
|
2023-02-24 20:05:53 +01:00
|
|
|
if (frames_in_max > sw->resample_buf.pos) {
|
|
|
|
sw->conv(sw->resample_buf.buffer + sw->resample_buf.pos,
|
|
|
|
buf, frames_in_max - sw->resample_buf.pos);
|
|
|
|
if (!sw->hw->pcm_ops->volume_out) {
|
|
|
|
mixeng_volume(sw->resample_buf.buffer + sw->resample_buf.pos,
|
|
|
|
frames_in_max - sw->resample_buf.pos, &sw->vol);
|
|
|
|
}
|
2023-02-24 20:05:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
audio_pcm_sw_resample_out(sw, frames_in_max, frames_out_max,
|
2023-02-24 20:05:46 +01:00
|
|
|
&total_in, &total_out);
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2023-02-24 20:05:46 +01:00
|
|
|
sw->total_hw_samples_mixed += total_out;
|
2005-10-30 19:58:22 +01:00
|
|
|
sw->empty = sw->total_hw_samples_mixed == 0;
|
|
|
|
|
2023-02-24 20:05:53 +01:00
|
|
|
/*
|
|
|
|
* Upsampling may leave one audio frame in the resample buffer. Decrement
|
|
|
|
* total_in by one if there was a leftover frame from the previous resample
|
|
|
|
* pass in the resample buffer. Increment total_in by one if the current
|
|
|
|
* resample pass left one frame in the resample buffer.
|
|
|
|
*/
|
|
|
|
if (frames_in_max - total_in == 1) {
|
|
|
|
/* copy one leftover audio frame to the beginning of the buffer */
|
|
|
|
*sw->resample_buf.buffer = *(sw->resample_buf.buffer + total_in);
|
|
|
|
total_in += 1 - sw->resample_buf.pos;
|
|
|
|
sw->resample_buf.pos = 1;
|
|
|
|
} else if (total_in >= sw->resample_buf.pos) {
|
|
|
|
total_in -= sw->resample_buf.pos;
|
|
|
|
sw->resample_buf.pos = 0;
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
#ifdef DEBUG_OUT
|
|
|
|
dolog (
|
2023-02-24 20:05:46 +01:00
|
|
|
"%s: write size %zu written %zu total mixed %zu\n",
|
|
|
|
SW_NAME(sw),
|
|
|
|
buf_len / sw->info.bytes_per_frame,
|
|
|
|
total_in,
|
2005-11-05 19:55:28 +01:00
|
|
|
sw->total_hw_samples_mixed
|
2005-10-30 19:58:22 +01:00
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
2023-02-24 20:05:46 +01:00
|
|
|
return total_in * sw->info.bytes_per_frame;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
#ifdef DEBUG_AUDIO
|
|
|
|
static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2020-02-02 20:38:07 +01:00
|
|
|
dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n",
|
|
|
|
cap, info->bits, info->is_signed, info->is_float, info->freq,
|
|
|
|
info->nchannels);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
#endif
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
#define DAC
|
|
|
|
#include "audio_template.h"
|
|
|
|
#undef DAC
|
|
|
|
#include "audio_template.h"
|
|
|
|
|
2009-09-12 00:28:45 +02:00
|
|
|
/*
|
|
|
|
* Timer
|
|
|
|
*/
|
2019-08-19 01:06:46 +02:00
|
|
|
static int audio_is_timer_needed(AudioState *s)
|
2009-09-12 00:28:45 +02:00
|
|
|
{
|
|
|
|
HWVoiceIn *hwi = NULL;
|
|
|
|
HWVoiceOut *hwo = NULL;
|
|
|
|
|
2019-08-19 01:06:46 +02:00
|
|
|
while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
|
2021-01-15 02:24:25 +01:00
|
|
|
if (!hwo->poll_mode) {
|
|
|
|
return 1;
|
|
|
|
}
|
2009-09-12 00:28:45 +02:00
|
|
|
}
|
2019-08-19 01:06:46 +02:00
|
|
|
while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
|
2021-01-15 02:24:25 +01:00
|
|
|
if (!hwi->poll_mode) {
|
|
|
|
return 1;
|
|
|
|
}
|
2009-09-12 00:28:45 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-18 12:30:12 +01:00
|
|
|
static void audio_reset_timer (AudioState *s)
|
2009-09-12 00:28:45 +02:00
|
|
|
{
|
2019-08-19 01:06:46 +02:00
|
|
|
if (audio_is_timer_needed(s)) {
|
2017-02-14 08:15:10 +01:00
|
|
|
timer_mod_anticipate_ns(s->ts,
|
2019-03-08 23:34:15 +01:00
|
|
|
qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->period_ticks);
|
2019-08-19 01:06:46 +02:00
|
|
|
if (!s->timer_running) {
|
|
|
|
s->timer_running = true;
|
|
|
|
s->timer_last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2019-03-08 23:34:15 +01:00
|
|
|
trace_audio_timer_start(s->period_ticks / SCALE_MS);
|
2018-07-02 18:15:24 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
timer_del(s->ts);
|
2019-08-19 01:06:46 +02:00
|
|
|
if (s->timer_running) {
|
|
|
|
s->timer_running = false;
|
2018-07-02 18:15:24 +02:00
|
|
|
trace_audio_timer_stop();
|
|
|
|
}
|
2009-09-12 00:28:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-18 12:30:12 +01:00
|
|
|
static void audio_timer (void *opaque)
|
|
|
|
{
|
2018-07-02 18:15:24 +02:00
|
|
|
int64_t now, diff;
|
2019-03-08 23:34:15 +01:00
|
|
|
AudioState *s = opaque;
|
2018-07-02 18:15:24 +02:00
|
|
|
|
|
|
|
now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2019-08-19 01:06:46 +02:00
|
|
|
diff = now - s->timer_last;
|
2019-03-08 23:34:15 +01:00
|
|
|
if (diff > s->period_ticks * 3 / 2) {
|
2018-07-02 18:15:24 +02:00
|
|
|
trace_audio_timer_delayed(diff / SCALE_MS);
|
|
|
|
}
|
2019-08-19 01:06:46 +02:00
|
|
|
s->timer_last = now;
|
2018-07-02 18:15:24 +02:00
|
|
|
|
2019-08-19 01:06:55 +02:00
|
|
|
audio_run(s, "timer");
|
2019-03-08 23:34:15 +01:00
|
|
|
audio_reset_timer(s);
|
2010-11-18 12:30:12 +01:00
|
|
|
}
|
|
|
|
|
2009-09-12 00:28:45 +02:00
|
|
|
/*
|
|
|
|
* Public API
|
|
|
|
*/
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t AUD_write(SWVoiceOut *sw, void *buf, size_t size)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2019-10-13 21:57:59 +02:00
|
|
|
HWVoiceOut *hw;
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
if (!sw) {
|
|
|
|
/* XXX: Consider options */
|
|
|
|
return size;
|
|
|
|
}
|
2019-10-13 21:57:59 +02:00
|
|
|
hw = sw->hw;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2019-10-13 21:57:59 +02:00
|
|
|
if (!hw->enabled) {
|
2005-11-05 19:55:28 +01:00
|
|
|
dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
|
2004-11-07 19:04:02 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-13 21:57:59 +02:00
|
|
|
if (audio_get_pdo_out(hw->s->dev)->mixing_engine) {
|
|
|
|
return audio_pcm_sw_write(sw, buf, size);
|
|
|
|
} else {
|
|
|
|
return hw->pcm_ops->write(hw, buf, size);
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
|
|
|
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t AUD_read(SWVoiceIn *sw, void *buf, size_t size)
|
2005-10-30 19:58:22 +01:00
|
|
|
{
|
2019-10-13 21:57:59 +02:00
|
|
|
HWVoiceIn *hw;
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
if (!sw) {
|
|
|
|
/* XXX: Consider options */
|
|
|
|
return size;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2019-10-13 21:57:59 +02:00
|
|
|
hw = sw->hw;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2019-10-13 21:57:59 +02:00
|
|
|
if (!hw->enabled) {
|
2005-11-05 19:55:28 +01:00
|
|
|
dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
|
2005-10-30 19:58:22 +01:00
|
|
|
return 0;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2019-10-13 21:57:59 +02:00
|
|
|
if (audio_get_pdo_in(hw->s->dev)->mixing_engine) {
|
|
|
|
return audio_pcm_sw_read(sw, buf, size);
|
|
|
|
} else {
|
|
|
|
return hw->pcm_ops->read(hw, buf, size);
|
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 08:49:38 +01:00
|
|
|
int AUD_get_buffer_size_out(SWVoiceOut *sw)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2020-01-23 08:49:38 +01:00
|
|
|
return sw->hw->samples * sw->hw->info.bytes_per_frame;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_set_active_out (SWVoiceOut *sw, int on)
|
|
|
|
{
|
|
|
|
HWVoiceOut *hw;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
if (!sw) {
|
2004-11-07 19:04:02 +01:00
|
|
|
return;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
hw = sw->hw;
|
2005-10-30 19:58:22 +01:00
|
|
|
if (sw->active != on) {
|
2019-08-19 01:06:46 +02:00
|
|
|
AudioState *s = sw->s;
|
2005-10-30 19:58:22 +01:00
|
|
|
SWVoiceOut *temp_sw;
|
2006-07-16 20:57:03 +02:00
|
|
|
SWVoiceCap *sc;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
if (on) {
|
|
|
|
hw->pending_disable = 0;
|
|
|
|
if (!hw->enabled) {
|
|
|
|
hw->enabled = 1;
|
2009-02-18 21:44:04 +01:00
|
|
|
if (s->vm_running) {
|
2019-09-19 23:24:22 +02:00
|
|
|
if (hw->pcm_ops->enable_out) {
|
|
|
|
hw->pcm_ops->enable_out(hw, true);
|
|
|
|
}
|
2010-11-18 12:30:12 +01:00
|
|
|
audio_reset_timer (s);
|
2009-02-18 21:44:04 +01:00
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2021-01-15 02:24:25 +01:00
|
|
|
} else {
|
2005-10-30 19:58:22 +01:00
|
|
|
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 19:04:02 +01:00
|
|
|
}
|
2006-07-16 20:57:03 +02:00
|
|
|
|
|
|
|
for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
|
|
|
sc->sw.active = hw->enabled;
|
2006-07-04 18:51:32 +02:00
|
|
|
if (hw->enabled) {
|
2006-07-16 20:57:03 +02:00
|
|
|
audio_capture_maybe_changed (sc->cap, 1);
|
2006-07-04 18:51:32 +02:00
|
|
|
}
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
sw->active = on;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_set_active_in (SWVoiceIn *sw, int on)
|
|
|
|
{
|
|
|
|
HWVoiceIn *hw;
|
|
|
|
|
|
|
|
if (!sw) {
|
|
|
|
return;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
hw = sw->hw;
|
2004-11-07 19:04:02 +01:00
|
|
|
if (sw->active != on) {
|
2019-08-19 01:06:46 +02:00
|
|
|
AudioState *s = sw->s;
|
2005-10-30 19:58:22 +01:00
|
|
|
SWVoiceIn *temp_sw;
|
|
|
|
|
2004-11-07 19:04:02 +01:00
|
|
|
if (on) {
|
|
|
|
if (!hw->enabled) {
|
|
|
|
hw->enabled = 1;
|
2009-02-18 21:44:04 +01:00
|
|
|
if (s->vm_running) {
|
2019-09-19 23:24:22 +02:00
|
|
|
if (hw->pcm_ops->enable_in) {
|
|
|
|
hw->pcm_ops->enable_in(hw, true);
|
|
|
|
}
|
2010-11-18 12:30:12 +01:00
|
|
|
audio_reset_timer (s);
|
2009-02-18 21:44:04 +01:00
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
sw->total_hw_samples_acquired = hw->total_samples_captured;
|
2021-01-15 02:24:25 +01:00
|
|
|
} else {
|
2005-10-30 19:58:22 +01:00
|
|
|
if (hw->enabled) {
|
2004-11-07 19:04:02 +01:00
|
|
|
int nb_active = 0;
|
2005-10-30 19:58:22 +01: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 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nb_active == 1) {
|
2005-10-30 19:58:22 +01:00
|
|
|
hw->enabled = 0;
|
2019-09-19 23:24:22 +02:00
|
|
|
if (hw->pcm_ops->enable_in) {
|
|
|
|
hw->pcm_ops->enable_in(hw, false);
|
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sw->active = on;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 01:06:58 +02:00
|
|
|
static size_t audio_get_avail (SWVoiceIn *sw)
|
2005-10-30 19:58:22 +01:00
|
|
|
{
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t live;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
if (!sw) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
|
2023-02-24 20:05:41 +01:00
|
|
|
if (audio_bug(__func__, live > sw->hw->conv_buf.size)) {
|
|
|
|
dolog("live=%zu sw->hw->conv_buf.size=%zu\n", live,
|
|
|
|
sw->hw->conv_buf.size);
|
2022-09-17 15:16:25 +02:00
|
|
|
return 0;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ldebug (
|
2023-02-24 20:05:52 +01:00
|
|
|
"%s: get_avail live %zu frontend frames %u\n",
|
2005-11-05 19:55:28 +01:00
|
|
|
SW_NAME (sw),
|
2023-02-24 20:05:52 +01:00
|
|
|
live, st_rate_frames_out(sw->rate, live)
|
2005-10-30 19:58:22 +01:00
|
|
|
);
|
|
|
|
|
2022-09-23 20:36:38 +02:00
|
|
|
return live;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
|
|
|
|
2019-08-19 01:06:58 +02:00
|
|
|
static size_t audio_get_free(SWVoiceOut *sw)
|
2005-10-30 19:58:22 +01:00
|
|
|
{
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t live, dead;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
if (!sw) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
live = sw->total_hw_samples_mixed;
|
|
|
|
|
2023-02-24 20:05:41 +01:00
|
|
|
if (audio_bug(__func__, live > sw->hw->mix_buf.size)) {
|
|
|
|
dolog("live=%zu sw->hw->mix_buf.size=%zu\n", live,
|
|
|
|
sw->hw->mix_buf.size);
|
2022-09-17 15:16:25 +02:00
|
|
|
return 0;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
|
|
|
|
2023-02-24 20:05:41 +01:00
|
|
|
dead = sw->hw->mix_buf.size - live;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG_OUT
|
2023-02-24 20:05:49 +01:00
|
|
|
dolog("%s: get_free live %zu dead %zu frontend frames %u\n",
|
|
|
|
SW_NAME(sw), live, dead, st_rate_frames_in(sw->rate, dead));
|
2004-11-07 19:04:02 +01:00
|
|
|
#endif
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2022-03-01 20:13:06 +01:00
|
|
|
return dead;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
|
|
|
|
2019-08-19 01:06:58 +02:00
|
|
|
static void audio_capture_mix_and_clear(HWVoiceOut *hw, size_t rpos,
|
|
|
|
size_t samples)
|
2006-07-04 18:51:32 +02:00
|
|
|
{
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t n;
|
2006-07-04 18:51:32 +02:00
|
|
|
|
|
|
|
if (hw->enabled) {
|
2006-07-16 20:57:03 +02:00
|
|
|
SWVoiceCap *sc;
|
2006-07-04 18:51:32 +02:00
|
|
|
|
2006-07-16 20:57:03 +02:00
|
|
|
for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
|
|
|
SWVoiceOut *sw = &sc->sw;
|
2023-02-24 20:05:47 +01:00
|
|
|
size_t rpos2 = rpos;
|
2006-07-04 18:51:32 +02:00
|
|
|
|
|
|
|
n = samples;
|
|
|
|
while (n) {
|
2023-02-24 20:05:41 +01:00
|
|
|
size_t till_end_of_hw = hw->mix_buf.size - rpos2;
|
2023-02-24 20:05:47 +01:00
|
|
|
size_t to_read = MIN(till_end_of_hw, n);
|
|
|
|
size_t live, frames_in, frames_out;
|
2006-07-04 18:51:32 +02:00
|
|
|
|
2023-02-24 20:05:42 +01:00
|
|
|
sw->resample_buf.buffer = hw->mix_buf.buffer + rpos2;
|
2023-02-24 20:05:47 +01:00
|
|
|
sw->resample_buf.size = to_read;
|
|
|
|
live = sw->total_hw_samples_mixed;
|
|
|
|
|
|
|
|
audio_pcm_sw_resample_out(sw,
|
|
|
|
to_read, sw->hw->mix_buf.size - live,
|
|
|
|
&frames_in, &frames_out);
|
|
|
|
|
|
|
|
sw->total_hw_samples_mixed += frames_out;
|
|
|
|
sw->empty = sw->total_hw_samples_mixed == 0;
|
|
|
|
|
|
|
|
if (to_read - frames_in) {
|
|
|
|
dolog("Could not mix %zu frames into a capture "
|
2019-08-19 01:06:58 +02:00
|
|
|
"buffer, mixed %zu\n",
|
2023-02-24 20:05:47 +01:00
|
|
|
to_read, frames_in);
|
2006-07-04 18:51:32 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-02-24 20:05:47 +01:00
|
|
|
n -= to_read;
|
|
|
|
rpos2 = (rpos2 + to_read) % hw->mix_buf.size;
|
2006-07-04 18:51:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 20:05:41 +01:00
|
|
|
n = MIN(samples, hw->mix_buf.size - rpos);
|
|
|
|
mixeng_clear(hw->mix_buf.buffer + rpos, n);
|
|
|
|
mixeng_clear(hw->mix_buf.buffer, samples - n);
|
2006-07-04 18:51:32 +02:00
|
|
|
}
|
|
|
|
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
static size_t audio_pcm_hw_run_out(HWVoiceOut *hw, size_t live)
|
|
|
|
{
|
|
|
|
size_t clipped = 0;
|
|
|
|
|
|
|
|
while (live) {
|
2020-09-20 19:17:23 +02:00
|
|
|
size_t size = live * hw->info.bytes_per_frame;
|
|
|
|
size_t decr, proc;
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
void *buf = hw->pcm_ops->get_buffer_out(hw, &size);
|
2020-09-20 19:17:22 +02:00
|
|
|
|
|
|
|
if (size == 0) {
|
2020-02-03 00:02:23 +01:00
|
|
|
break;
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
}
|
|
|
|
|
2019-10-13 21:58:02 +02:00
|
|
|
decr = MIN(size / hw->info.bytes_per_frame, live);
|
2020-09-20 19:17:22 +02:00
|
|
|
if (buf) {
|
|
|
|
audio_pcm_hw_clip_out(hw, buf, decr);
|
|
|
|
}
|
2019-10-13 21:58:02 +02:00
|
|
|
proc = hw->pcm_ops->put_buffer_out(hw, buf,
|
|
|
|
decr * hw->info.bytes_per_frame) /
|
|
|
|
hw->info.bytes_per_frame;
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
|
|
|
|
live -= proc;
|
|
|
|
clipped += proc;
|
2023-02-24 20:05:41 +01:00
|
|
|
hw->mix_buf.pos = (hw->mix_buf.pos + proc) % hw->mix_buf.size;
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
|
|
|
|
if (proc == 0 || proc < decr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 08:49:39 +01:00
|
|
|
if (hw->pcm_ops->run_buffer_out) {
|
|
|
|
hw->pcm_ops->run_buffer_out(hw);
|
|
|
|
}
|
|
|
|
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
return clipped;
|
|
|
|
}
|
|
|
|
|
2005-11-05 19:55:28 +01:00
|
|
|
static void audio_run_out (AudioState *s)
|
2005-10-30 19:58:22 +01:00
|
|
|
{
|
|
|
|
HWVoiceOut *hw = NULL;
|
|
|
|
SWVoiceOut *sw;
|
|
|
|
|
2022-09-23 20:36:29 +02:00
|
|
|
while ((hw = audio_pcm_hw_find_any_enabled_out(s, hw))) {
|
|
|
|
size_t played, live, prev_rpos;
|
|
|
|
size_t hw_free = audio_pcm_hw_get_free(hw);
|
|
|
|
int nb_live;
|
|
|
|
|
|
|
|
if (!audio_get_pdo_out(s->dev)->mixing_engine) {
|
2019-10-13 21:57:59 +02:00
|
|
|
/* there is exactly 1 sw for each hw with no mixeng */
|
|
|
|
sw = hw->sw_head.lh_first;
|
|
|
|
|
|
|
|
if (hw->pending_disable) {
|
|
|
|
hw->enabled = 0;
|
|
|
|
hw->pending_disable = 0;
|
|
|
|
if (hw->pcm_ops->enable_out) {
|
|
|
|
hw->pcm_ops->enable_out(hw, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sw->active) {
|
2022-09-23 20:36:30 +02:00
|
|
|
sw->callback.fn(sw->callback.opaque,
|
|
|
|
hw_free * sw->info.bytes_per_frame);
|
2019-10-13 21:57:59 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 20:36:31 +02:00
|
|
|
if (hw->pcm_ops->run_buffer_out) {
|
|
|
|
hw->pcm_ops->run_buffer_out(hw);
|
|
|
|
}
|
|
|
|
|
2022-09-23 20:36:29 +02:00
|
|
|
continue;
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2022-03-01 20:13:03 +01:00
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
|
|
|
if (sw->active) {
|
2022-03-01 20:13:06 +01:00
|
|
|
size_t sw_free = audio_get_free(sw);
|
|
|
|
size_t free;
|
|
|
|
|
|
|
|
if (hw_free > sw->total_hw_samples_mixed) {
|
2023-02-24 20:05:49 +01:00
|
|
|
free = st_rate_frames_in(sw->rate,
|
2022-03-01 20:13:06 +01:00
|
|
|
MIN(sw_free, hw_free - sw->total_hw_samples_mixed));
|
|
|
|
} else {
|
|
|
|
free = 0;
|
|
|
|
}
|
2023-02-24 20:05:53 +01:00
|
|
|
if (free > sw->resample_buf.pos) {
|
|
|
|
free = MIN(free, sw->resample_buf.size)
|
|
|
|
- sw->resample_buf.pos;
|
2022-09-23 20:36:37 +02:00
|
|
|
sw->callback.fn(sw->callback.opaque,
|
|
|
|
free * sw->info.bytes_per_frame);
|
2022-03-01 20:13:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-18 09:37:39 +02:00
|
|
|
live = audio_pcm_hw_get_live_out (hw, &nb_live);
|
2005-10-30 19:58:22 +01:00
|
|
|
if (!nb_live) {
|
|
|
|
live = 0;
|
|
|
|
}
|
2005-11-05 19:55:28 +01:00
|
|
|
|
2023-02-24 20:05:41 +01:00
|
|
|
if (audio_bug(__func__, live > hw->mix_buf.size)) {
|
|
|
|
dolog("live=%zu hw->mix_buf.size=%zu\n", live, hw->mix_buf.size);
|
2022-09-17 15:16:25 +02:00
|
|
|
continue;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hw->pending_disable && !nb_live) {
|
2006-07-16 20:57:03 +02:00
|
|
|
SWVoiceCap *sc;
|
2005-10-30 19:58:22 +01:00
|
|
|
#ifdef DEBUG_OUT
|
|
|
|
dolog ("Disabling voice\n");
|
2004-11-07 19:04:02 +01:00
|
|
|
#endif
|
2005-10-30 19:58:22 +01:00
|
|
|
hw->enabled = 0;
|
|
|
|
hw->pending_disable = 0;
|
2019-09-19 23:24:22 +02:00
|
|
|
if (hw->pcm_ops->enable_out) {
|
|
|
|
hw->pcm_ops->enable_out(hw, false);
|
|
|
|
}
|
2006-07-16 20:57:03 +02: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 18:51:32 +02:00
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!live) {
|
2020-09-20 19:17:27 +02:00
|
|
|
if (hw->pcm_ops->run_buffer_out) {
|
|
|
|
hw->pcm_ops->run_buffer_out(hw);
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-24 20:05:41 +01:00
|
|
|
prev_rpos = hw->mix_buf.pos;
|
2019-09-19 23:24:19 +02:00
|
|
|
played = audio_pcm_hw_run_out(hw, live);
|
2017-02-02 06:50:54 +01:00
|
|
|
replay_audio_out(&played);
|
2023-02-24 20:05:41 +01:00
|
|
|
if (audio_bug(__func__, hw->mix_buf.pos >= hw->mix_buf.size)) {
|
|
|
|
dolog("hw->mix_buf.pos=%zu hw->mix_buf.size=%zu played=%zu\n",
|
|
|
|
hw->mix_buf.pos, hw->mix_buf.size, played);
|
|
|
|
hw->mix_buf.pos = 0;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_OUT
|
2019-08-19 01:06:58 +02:00
|
|
|
dolog("played=%zu\n", played);
|
2004-11-07 19:04:02 +01:00
|
|
|
#endif
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
if (played) {
|
|
|
|
hw->ts_helper += played;
|
2006-07-04 18:51:32 +02:00
|
|
|
audio_capture_mix_and_clear (hw, prev_rpos, played);
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
|
|
|
if (!sw->active && sw->empty) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-03 09:43:02 +01:00
|
|
|
if (audio_bug(__func__, played > sw->total_hw_samples_mixed)) {
|
2019-08-19 01:06:58 +02:00
|
|
|
dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
|
|
|
|
played, sw->total_hw_samples_mixed);
|
2022-09-17 15:16:25 +02:00
|
|
|
played = sw->total_hw_samples_mixed;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sw->total_hw_samples_mixed -= played;
|
|
|
|
|
|
|
|
if (!sw->total_hw_samples_mixed) {
|
|
|
|
sw->empty = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
static size_t audio_pcm_hw_run_in(HWVoiceIn *hw, size_t samples)
|
|
|
|
{
|
|
|
|
size_t conv = 0;
|
|
|
|
|
2021-01-10 11:02:24 +01:00
|
|
|
if (hw->pcm_ops->run_buffer_in) {
|
|
|
|
hw->pcm_ops->run_buffer_in(hw);
|
|
|
|
}
|
|
|
|
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
while (samples) {
|
|
|
|
size_t proc;
|
2019-10-13 21:58:02 +02:00
|
|
|
size_t size = samples * hw->info.bytes_per_frame;
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
void *buf = hw->pcm_ops->get_buffer_in(hw, &size);
|
|
|
|
|
2019-10-13 21:58:02 +02:00
|
|
|
assert(size % hw->info.bytes_per_frame == 0);
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
if (size == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-01 20:12:59 +01:00
|
|
|
proc = audio_pcm_hw_conv_in(hw, buf, size / hw->info.bytes_per_frame);
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
|
|
|
|
samples -= proc;
|
|
|
|
conv += proc;
|
2019-10-13 21:58:02 +02:00
|
|
|
hw->pcm_ops->put_buffer_in(hw, buf, proc * hw->info.bytes_per_frame);
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return conv;
|
|
|
|
}
|
|
|
|
|
2005-11-05 19:55:28 +01:00
|
|
|
static void audio_run_in (AudioState *s)
|
2005-10-30 19:58:22 +01:00
|
|
|
{
|
|
|
|
HWVoiceIn *hw = NULL;
|
|
|
|
|
2019-10-13 21:57:59 +02:00
|
|
|
if (!audio_get_pdo_in(s->dev)->mixing_engine) {
|
|
|
|
while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) {
|
|
|
|
/* there is exactly 1 sw for each hw with no mixeng */
|
|
|
|
SWVoiceIn *sw = hw->sw_head.lh_first;
|
|
|
|
if (sw->active) {
|
|
|
|
sw->callback.fn(sw->callback.opaque, INT_MAX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-19 01:06:46 +02:00
|
|
|
while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) {
|
2005-10-30 19:58:22 +01:00
|
|
|
SWVoiceIn *sw;
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t captured = 0, min;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2017-02-02 06:50:54 +01:00
|
|
|
if (replay_mode != REPLAY_MODE_PLAY) {
|
2019-09-19 23:24:19 +02:00
|
|
|
captured = audio_pcm_hw_run_in(
|
2023-02-24 20:05:41 +01:00
|
|
|
hw, hw->conv_buf.size - audio_pcm_hw_get_live_in(hw));
|
2017-02-02 06:50:54 +01:00
|
|
|
}
|
2023-02-24 20:05:41 +01:00
|
|
|
replay_audio_in(&captured, hw->conv_buf.buffer, &hw->conv_buf.pos,
|
|
|
|
hw->conv_buf.size);
|
2005-10-30 19:58:22 +01: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) {
|
2022-09-23 20:36:38 +02:00
|
|
|
size_t sw_avail = audio_get_avail(sw);
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t avail;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2023-02-24 20:05:52 +01:00
|
|
|
avail = st_rate_frames_out(sw->rate, sw_avail);
|
2005-10-30 19:58:22 +01:00
|
|
|
if (avail > 0) {
|
2023-02-24 20:05:52 +01:00
|
|
|
avail = MIN(avail, sw->resample_buf.size);
|
2022-09-23 20:36:38 +02:00
|
|
|
sw->callback.fn(sw->callback.opaque,
|
|
|
|
avail * sw->info.bytes_per_frame);
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-04 18:51:32 +02:00
|
|
|
static void audio_run_capture (AudioState *s)
|
|
|
|
{
|
|
|
|
CaptureVoiceOut *cap;
|
|
|
|
|
|
|
|
for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t live, rpos, captured;
|
2006-07-04 18:51:32 +02:00
|
|
|
HWVoiceOut *hw = &cap->hw;
|
|
|
|
SWVoiceOut *sw;
|
|
|
|
|
2009-09-18 09:37:39 +02:00
|
|
|
captured = live = audio_pcm_hw_get_live_out (hw, NULL);
|
2023-02-24 20:05:41 +01:00
|
|
|
rpos = hw->mix_buf.pos;
|
2006-07-04 18:51:32 +02:00
|
|
|
while (live) {
|
2023-02-24 20:05:41 +01:00
|
|
|
size_t left = hw->mix_buf.size - rpos;
|
2019-08-19 01:06:58 +02:00
|
|
|
size_t to_capture = MIN(live, left);
|
2008-12-03 23:48:44 +01:00
|
|
|
struct st_sample *src;
|
2006-07-04 18:51:32 +02:00
|
|
|
struct capture_callback *cb;
|
|
|
|
|
2023-02-24 20:05:41 +01:00
|
|
|
src = hw->mix_buf.buffer + rpos;
|
2006-07-04 18:51:32 +02:00
|
|
|
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,
|
2019-10-13 21:58:02 +02:00
|
|
|
to_capture * hw->info.bytes_per_frame);
|
2006-07-04 18:51:32 +02:00
|
|
|
}
|
2023-02-24 20:05:41 +01:00
|
|
|
rpos = (rpos + to_capture) % hw->mix_buf.size;
|
2006-07-04 18:51:32 +02:00
|
|
|
live -= to_capture;
|
|
|
|
}
|
2023-02-24 20:05:41 +01:00
|
|
|
hw->mix_buf.pos = rpos;
|
2006-07-04 18:51:32 +02:00
|
|
|
|
|
|
|
for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
|
|
|
if (!sw->active && sw->empty) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-03 09:43:02 +01:00
|
|
|
if (audio_bug(__func__, captured > sw->total_hw_samples_mixed)) {
|
2019-08-19 01:06:58 +02:00
|
|
|
dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
|
|
|
|
captured, sw->total_hw_samples_mixed);
|
2022-09-17 15:16:25 +02:00
|
|
|
captured = sw->total_hw_samples_mixed;
|
2006-07-04 18:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sw->total_hw_samples_mixed -= captured;
|
|
|
|
sw->empty = sw->total_hw_samples_mixed == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 01:06:55 +02:00
|
|
|
void audio_run(AudioState *s, const char *msg)
|
2005-11-20 17:24:34 +01:00
|
|
|
{
|
2019-08-19 01:06:55 +02:00
|
|
|
audio_run_out(s);
|
|
|
|
audio_run_in(s);
|
|
|
|
audio_run_capture(s);
|
2005-11-20 17:24:34 +01:00
|
|
|
|
2009-09-12 00:28:45 +02:00
|
|
|
#ifdef DEBUG_POLL
|
|
|
|
{
|
|
|
|
static double prevtime;
|
|
|
|
double currtime;
|
|
|
|
struct timeval tv;
|
2005-11-20 17:24:34 +01:00
|
|
|
|
2009-09-12 00:28:45 +02: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 17:24:34 +01:00
|
|
|
}
|
|
|
|
|
2021-01-10 11:02:24 +01:00
|
|
|
void audio_generic_run_buffer_in(HWVoiceIn *hw)
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
{
|
|
|
|
if (unlikely(!hw->buf_emul)) {
|
2021-01-10 11:02:26 +01:00
|
|
|
hw->size_emul = hw->samples * hw->info.bytes_per_frame;
|
|
|
|
hw->buf_emul = g_malloc(hw->size_emul);
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
hw->pos_emul = hw->pending_emul = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (hw->pending_emul < hw->size_emul) {
|
|
|
|
size_t read_len = MIN(hw->size_emul - hw->pos_emul,
|
|
|
|
hw->size_emul - hw->pending_emul);
|
|
|
|
size_t read = hw->pcm_ops->read(hw, hw->buf_emul + hw->pos_emul,
|
|
|
|
read_len);
|
|
|
|
hw->pending_emul += read;
|
2019-11-19 07:58:49 +01:00
|
|
|
hw->pos_emul = (hw->pos_emul + read) % hw->size_emul;
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
if (read < read_len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-01-10 11:02:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size)
|
|
|
|
{
|
2022-03-01 20:12:57 +01:00
|
|
|
size_t start;
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
|
2022-03-01 20:12:57 +01:00
|
|
|
start = audio_ring_posb(hw->pos_emul, hw->pending_emul, hw->size_emul);
|
|
|
|
assert(start < hw->size_emul);
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
|
2020-01-23 08:49:43 +01:00
|
|
|
*size = MIN(*size, hw->pending_emul);
|
|
|
|
*size = MIN(*size, hw->size_emul - start);
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
return hw->buf_emul + start;
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size)
|
|
|
|
{
|
|
|
|
assert(size <= hw->pending_emul);
|
|
|
|
hw->pending_emul -= size;
|
|
|
|
}
|
|
|
|
|
2022-03-01 20:13:06 +01:00
|
|
|
size_t audio_generic_buffer_get_free(HWVoiceOut *hw)
|
|
|
|
{
|
|
|
|
if (hw->buf_emul) {
|
|
|
|
return hw->size_emul - hw->pending_emul;
|
|
|
|
} else {
|
|
|
|
return hw->samples * hw->info.bytes_per_frame;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 08:49:39 +01:00
|
|
|
void audio_generic_run_buffer_out(HWVoiceOut *hw)
|
|
|
|
{
|
|
|
|
while (hw->pending_emul) {
|
2022-03-01 20:12:57 +01:00
|
|
|
size_t write_len, written, start;
|
2020-01-23 08:49:39 +01:00
|
|
|
|
2022-03-01 20:12:57 +01:00
|
|
|
start = audio_ring_posb(hw->pos_emul, hw->pending_emul, hw->size_emul);
|
|
|
|
assert(start < hw->size_emul);
|
2020-01-23 08:49:39 +01:00
|
|
|
|
|
|
|
write_len = MIN(hw->pending_emul, hw->size_emul - start);
|
|
|
|
|
|
|
|
written = hw->pcm_ops->write(hw, hw->buf_emul + start, write_len);
|
|
|
|
hw->pending_emul -= written;
|
|
|
|
|
|
|
|
if (written < write_len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size)
|
|
|
|
{
|
|
|
|
if (unlikely(!hw->buf_emul)) {
|
2021-01-10 11:02:26 +01:00
|
|
|
hw->size_emul = hw->samples * hw->info.bytes_per_frame;
|
|
|
|
hw->buf_emul = g_malloc(hw->size_emul);
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
hw->pos_emul = hw->pending_emul = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*size = MIN(hw->size_emul - hw->pending_emul,
|
|
|
|
hw->size_emul - hw->pos_emul);
|
|
|
|
return hw->buf_emul + hw->pos_emul;
|
|
|
|
}
|
|
|
|
|
2020-01-23 08:49:39 +01:00
|
|
|
size_t audio_generic_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size)
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
{
|
|
|
|
assert(buf == hw->buf_emul + hw->pos_emul &&
|
|
|
|
size + hw->pending_emul <= hw->size_emul);
|
|
|
|
|
|
|
|
hw->pending_emul += size;
|
|
|
|
hw->pos_emul = (hw->pos_emul + size) % hw->size_emul;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t audio_generic_write(HWVoiceOut *hw, void *buf, size_t size)
|
|
|
|
{
|
2020-09-20 19:17:26 +02:00
|
|
|
size_t total = 0;
|
|
|
|
|
2022-03-01 20:13:06 +01:00
|
|
|
if (hw->pcm_ops->buffer_get_free) {
|
|
|
|
size_t free = hw->pcm_ops->buffer_get_free(hw);
|
|
|
|
|
|
|
|
size = MIN(size, free);
|
|
|
|
}
|
|
|
|
|
2020-09-20 19:17:26 +02:00
|
|
|
while (total < size) {
|
|
|
|
size_t dst_size = size - total;
|
|
|
|
size_t copy_size, proc;
|
|
|
|
void *dst = hw->pcm_ops->get_buffer_out(hw, &dst_size);
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
|
2020-09-20 19:17:26 +02:00
|
|
|
if (dst_size == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_size = MIN(size - total, dst_size);
|
|
|
|
if (dst) {
|
|
|
|
memcpy(dst, (char *)buf + total, copy_size);
|
|
|
|
}
|
|
|
|
proc = hw->pcm_ops->put_buffer_out(hw, dst, copy_size);
|
|
|
|
total += proc;
|
|
|
|
|
|
|
|
if (proc == 0 || proc < copy_size) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t audio_generic_read(HWVoiceIn *hw, void *buf, size_t size)
|
|
|
|
{
|
2020-09-20 19:17:24 +02:00
|
|
|
size_t total = 0;
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
|
2021-01-10 11:02:24 +01:00
|
|
|
if (hw->pcm_ops->run_buffer_in) {
|
|
|
|
hw->pcm_ops->run_buffer_in(hw);
|
|
|
|
}
|
|
|
|
|
2020-09-20 19:17:24 +02:00
|
|
|
while (total < size) {
|
|
|
|
size_t src_size = size - total;
|
|
|
|
void *src = hw->pcm_ops->get_buffer_in(hw, &src_size);
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
|
2020-09-20 19:17:24 +02:00
|
|
|
if (src_size == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy((char *)buf + total, src, src_size);
|
|
|
|
hw->pcm_ops->put_buffer_in(hw, src, src_size);
|
|
|
|
total += src_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
2020-04-05 09:50:17 +02:00
|
|
|
}
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
|
2019-03-08 23:34:15 +01:00
|
|
|
static int audio_driver_init(AudioState *s, struct audio_driver *drv,
|
|
|
|
bool msg, Audiodev *dev)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2019-03-08 23:34:15 +01:00
|
|
|
s->drv_opaque = drv->init(dev);
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2005-11-05 19:55:28 +01:00
|
|
|
if (s->drv_opaque) {
|
audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and
recording, allowing us to free ourselves from the limitations of the
current mixeng (stereo, int64 samples only). In this case, HW and SW
voices will be essentially the same, for every SW voice we will create
a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready
write()/read() style, while others provide a buffer and expects us to
directly write/read it, so for optimal performance audio_pcm_ops provide
methods for both cases. Previously backends asked mixeng for more data
in run_out/run_it, now instead mixeng or the frontends will call the
backends, so that's why two sets of functions required. audio.c
contains glue code between the two styles, so backends only ever have to
implement one style and frontends are free to call whichever is more
convenient for them.
Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 15a33c03a62228922d851f7324c52f73cb8d2414.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2019-09-19 23:24:09 +02:00
|
|
|
if (!drv->pcm_ops->get_buffer_in) {
|
|
|
|
drv->pcm_ops->get_buffer_in = audio_generic_get_buffer_in;
|
|
|
|
drv->pcm_ops->put_buffer_in = audio_generic_put_buffer_in;
|
|
|
|
}
|
|
|
|
if (!drv->pcm_ops->get_buffer_out) {
|
|
|
|
drv->pcm_ops->get_buffer_out = audio_generic_get_buffer_out;
|
|
|
|
drv->pcm_ops->put_buffer_out = audio_generic_put_buffer_out;
|
|
|
|
}
|
|
|
|
|
2019-08-19 01:06:46 +02:00
|
|
|
audio_init_nb_voices_out(s, drv);
|
|
|
|
audio_init_nb_voices_in(s, drv);
|
2005-11-05 19:55:28 +01:00
|
|
|
s->drv = drv;
|
2005-10-30 19:58:22 +01:00
|
|
|
return 0;
|
2021-01-15 02:24:25 +01:00
|
|
|
} else {
|
2019-01-24 12:20:54 +01:00
|
|
|
if (msg) {
|
|
|
|
dolog("Could not init `%s' audio driver\n", drv->name);
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
return -1;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 16:20:20 +01:00
|
|
|
static void audio_vm_change_state_handler (void *opaque, bool running,
|
2011-07-29 19:26:33 +02:00
|
|
|
RunState state)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2005-11-05 19:55:28 +01:00
|
|
|
AudioState *s = opaque;
|
2005-10-30 19:58:22 +01:00
|
|
|
HWVoiceOut *hwo = NULL;
|
|
|
|
HWVoiceIn *hwi = NULL;
|
|
|
|
|
2009-02-18 21:44:04 +01:00
|
|
|
s->vm_running = running;
|
2019-08-19 01:06:46 +02:00
|
|
|
while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
|
2019-09-19 23:24:22 +02:00
|
|
|
if (hwo->pcm_ops->enable_out) {
|
|
|
|
hwo->pcm_ops->enable_out(hwo, running);
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2019-08-19 01:06:46 +02:00
|
|
|
while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
|
2019-09-19 23:24:22 +02:00
|
|
|
if (hwi->pcm_ops->enable_in) {
|
|
|
|
hwi->pcm_ops->enable_in(hwi, running);
|
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2010-11-18 12:30:12 +01:00
|
|
|
audio_reset_timer (s);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2019-08-19 01:06:47 +02:00
|
|
|
static void free_audio_state(AudioState *s)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2016-08-01 13:23:43 +02:00
|
|
|
HWVoiceOut *hwo, *hwon;
|
|
|
|
HWVoiceIn *hwi, *hwin;
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2019-08-19 01:06:46 +02:00
|
|
|
QLIST_FOREACH_SAFE(hwo, &s->hw_head_out, entries, hwon) {
|
2006-07-16 20:57:03 +02:00
|
|
|
SWVoiceCap *sc;
|
2006-07-04 18:51:32 +02:00
|
|
|
|
2019-09-19 23:24:22 +02:00
|
|
|
if (hwo->enabled && hwo->pcm_ops->enable_out) {
|
|
|
|
hwo->pcm_ops->enable_out(hwo, false);
|
2012-05-24 17:05:15 +02:00
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
hwo->pcm_ops->fini_out (hwo);
|
2006-07-04 18:51:32 +02:00
|
|
|
|
2006-07-16 20:57:03 +02: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 18:51:32 +02:00
|
|
|
}
|
2016-08-01 13:23:43 +02:00
|
|
|
QLIST_REMOVE(hwo, entries);
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2019-08-19 01:06:46 +02:00
|
|
|
QLIST_FOREACH_SAFE(hwi, &s->hw_head_in, entries, hwin) {
|
2019-09-19 23:24:22 +02:00
|
|
|
if (hwi->enabled && hwi->pcm_ops->enable_in) {
|
|
|
|
hwi->pcm_ops->enable_in(hwi, false);
|
2012-05-24 17:05:15 +02:00
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
hwi->pcm_ops->fini_in (hwi);
|
2016-08-01 13:23:43 +02:00
|
|
|
QLIST_REMOVE(hwi, entries);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2005-11-05 19:55:28 +01:00
|
|
|
|
|
|
|
if (s->drv) {
|
|
|
|
s->drv->fini (s->drv_opaque);
|
2016-08-01 13:23:43 +02:00
|
|
|
s->drv = NULL;
|
2005-11-05 19:55:28 +01:00
|
|
|
}
|
2019-03-08 23:34:15 +01:00
|
|
|
|
|
|
|
if (s->dev) {
|
|
|
|
qapi_free_Audiodev(s->dev);
|
|
|
|
s->dev = NULL;
|
|
|
|
}
|
2019-08-19 01:06:59 +02:00
|
|
|
|
|
|
|
if (s->ts) {
|
|
|
|
timer_free(s->ts);
|
|
|
|
s->ts = NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-19 01:06:47 +02:00
|
|
|
g_free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_cleanup(void)
|
|
|
|
{
|
|
|
|
while (!QTAILQ_EMPTY(&audio_states)) {
|
|
|
|
AudioState *s = QTAILQ_FIRST(&audio_states);
|
|
|
|
QTAILQ_REMOVE(&audio_states, s, list);
|
|
|
|
free_audio_state(s);
|
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2021-08-09 19:09:56 +02:00
|
|
|
static bool vmstate_audio_needed(void *opaque)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Never needed, this vmstate only exists in case
|
|
|
|
* an old qemu sends it to us.
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-02 11:49:34 +01:00
|
|
|
static const VMStateDescription vmstate_audio = {
|
|
|
|
.name = "audio",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2021-08-09 19:09:56 +02:00
|
|
|
.needed = vmstate_audio_needed,
|
2014-04-16 16:01:33 +02:00
|
|
|
.fields = (VMStateField[]) {
|
2009-12-02 11:49:34 +01:00
|
|
|
VMSTATE_END_OF_LIST()
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2009-12-02 11:49:34 +01:00
|
|
|
};
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2019-03-08 23:34:15 +01: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;
|
|
|
|
}
|
|
|
|
|
2019-08-19 01:06:47 +02:00
|
|
|
/*
|
|
|
|
* if we have dev, this function was called because of an -audiodev argument =>
|
|
|
|
* initialize a new state with it
|
|
|
|
* if dev == NULL => legacy implicit initialization, return the already created
|
|
|
|
* state or create a new one
|
|
|
|
*/
|
2019-08-19 01:06:51 +02:00
|
|
|
static AudioState *audio_init(Audiodev *dev, const char *name)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2019-08-19 01:06:47 +02:00
|
|
|
static bool atexit_registered;
|
2005-10-30 19:58:22 +01:00
|
|
|
size_t i;
|
2004-11-07 19:04:02 +01:00
|
|
|
int done = 0;
|
2019-03-08 23:34:15 +01:00
|
|
|
const char *drvname = NULL;
|
2009-09-12 00:28:45 +02:00
|
|
|
VMChangeStateEntry *e;
|
2019-08-19 01:06:47 +02:00
|
|
|
AudioState *s;
|
2018-03-06 08:40:47 +01:00
|
|
|
struct audio_driver *driver;
|
2019-03-08 23:34:15 +01:00
|
|
|
/* silence gcc warning about uninitialized variable */
|
|
|
|
AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head);
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2020-09-16 10:41:16 +02:00
|
|
|
if (using_spice) {
|
|
|
|
/*
|
|
|
|
* When using spice allow the spice audio driver being picked
|
|
|
|
* as default.
|
|
|
|
*
|
|
|
|
* Temporary hack. Using audio devices without explicit
|
|
|
|
* audiodev= property is already deprecated. Same goes for
|
|
|
|
* the -soundhw switch. Once this support gets finally
|
|
|
|
* removed we can also drop the concept of a default audio
|
|
|
|
* backend and this can go away.
|
|
|
|
*/
|
|
|
|
driver = audio_driver_lookup("spice");
|
2020-12-15 09:11:51 +01:00
|
|
|
if (driver) {
|
|
|
|
driver->can_be_default = 1;
|
|
|
|
}
|
2020-09-16 10:41:16 +02:00
|
|
|
}
|
|
|
|
|
2019-03-08 23:34:15 +01:00
|
|
|
if (dev) {
|
|
|
|
/* -audiodev option */
|
2019-08-19 01:06:51 +02:00
|
|
|
legacy_config = false;
|
2019-03-08 23:34:15 +01:00
|
|
|
drvname = AudiodevDriver_str(dev->driver);
|
2019-08-19 01:06:47 +02:00
|
|
|
} else if (!QTAILQ_EMPTY(&audio_states)) {
|
2019-08-19 01:06:51 +02:00
|
|
|
if (!legacy_config) {
|
2019-08-26 21:59:02 +02:00
|
|
|
dolog("Device %s: audiodev default parameter is deprecated, please "
|
|
|
|
"specify audiodev=%s\n", name,
|
|
|
|
QTAILQ_FIRST(&audio_states)->dev->id);
|
2019-08-19 01:06:51 +02:00
|
|
|
}
|
2019-08-19 01:06:47 +02:00
|
|
|
return QTAILQ_FIRST(&audio_states);
|
2019-03-08 23:34:15 +01:00
|
|
|
} else {
|
|
|
|
/* legacy implicit initialization */
|
|
|
|
head = audio_handle_legacy_opts();
|
|
|
|
/*
|
|
|
|
* In case of legacy initialization, all Audiodevs in the list will have
|
2020-09-17 09:50:20 +02:00
|
|
|
* the same configuration (except the driver), so it doesn't matter which
|
2019-03-08 23:34:15 +01:00
|
|
|
* 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);
|
|
|
|
}
|
2019-08-19 01:06:47 +02:00
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
s = g_new0(AudioState, 1);
|
2019-03-08 23:34:15 +01:00
|
|
|
s->dev = dev;
|
|
|
|
|
2009-09-12 09:36:22 +02:00
|
|
|
QLIST_INIT (&s->hw_head_out);
|
|
|
|
QLIST_INIT (&s->hw_head_in);
|
|
|
|
QLIST_INIT (&s->cap_head);
|
2019-08-19 01:06:47 +02:00
|
|
|
if (!atexit_registered) {
|
|
|
|
atexit(audio_cleanup);
|
|
|
|
atexit_registered = true;
|
|
|
|
}
|
2005-11-20 17:24:34 +01:00
|
|
|
|
2013-08-21 17:03:08 +02:00
|
|
|
s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
|
2005-11-20 17:24:34 +01:00
|
|
|
|
2019-03-08 23:34:15 +01: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 19:55:28 +01:00
|
|
|
|
2022-10-12 13:49:25 +02:00
|
|
|
if (s->nb_hw_voices_out < 1) {
|
2005-11-20 17:24:34 +01:00
|
|
|
dolog ("Bogus number of playback voices %d, setting to 1\n",
|
2005-10-30 19:58:22 +01:00
|
|
|
s->nb_hw_voices_out);
|
|
|
|
s->nb_hw_voices_out = 1;
|
|
|
|
}
|
|
|
|
|
2022-10-12 13:49:24 +02:00
|
|
|
if (s->nb_hw_voices_in < 0) {
|
2005-11-20 17:24:34 +01:00
|
|
|
dolog ("Bogus number of capture voices %d, setting to 0\n",
|
2005-10-30 19:58:22 +01:00
|
|
|
s->nb_hw_voices_in);
|
2005-11-20 17:24:34 +01:00
|
|
|
s->nb_hw_voices_in = 0;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
if (drvname) {
|
2018-03-06 08:40:47 +01:00
|
|
|
driver = audio_driver_lookup(drvname);
|
|
|
|
if (driver) {
|
2019-03-08 23:34:15 +01:00
|
|
|
done = !audio_driver_init(s, driver, true, dev);
|
2018-03-06 08:40:47 +01:00
|
|
|
} else {
|
2004-11-07 19:04:02 +01:00
|
|
|
dolog ("Unknown audio driver `%s'\n", drvname);
|
|
|
|
}
|
2022-08-22 15:10:21 +02:00
|
|
|
if (!done) {
|
|
|
|
free_audio_state(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-03-08 23:34:15 +01:00
|
|
|
} else {
|
|
|
|
for (i = 0; audio_prio_list[i]; i++) {
|
|
|
|
AudiodevListEntry *e = audiodev_find(&head, audio_prio_list[i]);
|
2018-03-06 08:40:47 +01:00
|
|
|
driver = audio_driver_lookup(audio_prio_list[i]);
|
2019-03-08 23:34:15 +01: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 19:58:22 +01:00
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-08 23:34:15 +01:00
|
|
|
audio_free_audiodev_list(&head);
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
if (!done) {
|
2018-03-06 08:40:47 +01:00
|
|
|
driver = audio_driver_lookup("none");
|
2019-03-08 23:34:15 +01:00
|
|
|
done = !audio_driver_init(s, driver, false, dev);
|
2015-12-17 17:35:20 +01:00
|
|
|
assert(done);
|
|
|
|
dolog("warning: Using timer based audio emulation\n");
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2019-03-08 23:34:15 +01:00
|
|
|
if (dev->timer_period <= 0) {
|
|
|
|
s->period_ticks = 1;
|
2009-05-12 13:02:38 +02:00
|
|
|
} else {
|
2019-12-19 21:34:05 +01:00
|
|
|
s->period_ticks = dev->timer_period * (int64_t)SCALE_US;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2009-05-12 13:02:38 +02: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 19:58:22 +01:00
|
|
|
}
|
|
|
|
|
2022-08-22 15:10:21 +02:00
|
|
|
QTAILQ_INSERT_TAIL(&audio_states, s, list);
|
2009-09-12 09:36:22 +02:00
|
|
|
QLIST_INIT (&s->card_head);
|
2010-06-25 19:09:07 +02:00
|
|
|
vmstate_register (NULL, 0, &vmstate_audio, s);
|
2019-08-19 01:06:47 +02:00
|
|
|
return s;
|
2019-03-08 23:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 19:04:02 +01:00
|
|
|
}
|
2006-07-04 18:51:32 +02:00
|
|
|
|
2009-05-14 01:11:35 +02:00
|
|
|
void AUD_register_card (const char *name, QEMUSoundCard *card)
|
|
|
|
{
|
2019-08-19 01:06:47 +02:00
|
|
|
if (!card->state) {
|
2019-08-19 01:06:51 +02:00
|
|
|
card->state = audio_init(NULL, name);
|
2019-08-19 01:06:47 +02:00
|
|
|
}
|
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
card->name = g_strdup (name);
|
2009-05-14 01:11:35 +02:00
|
|
|
memset (&card->entries, 0, sizeof (card->entries));
|
2019-08-19 01:06:47 +02:00
|
|
|
QLIST_INSERT_HEAD(&card->state->card_head, card, entries);
|
2009-05-14 01:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_remove_card (QEMUSoundCard *card)
|
|
|
|
{
|
2009-09-12 09:36:22 +02:00
|
|
|
QLIST_REMOVE (card, entries);
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free (card->name);
|
2009-05-14 01:11:35 +02:00
|
|
|
}
|
|
|
|
|
2022-03-01 20:13:04 +01:00
|
|
|
static struct audio_pcm_ops capture_pcm_ops;
|
2009-05-14 01:11:35 +02:00
|
|
|
|
2019-08-19 01:06:47 +02:00
|
|
|
CaptureVoiceOut *AUD_add_capture(
|
|
|
|
AudioState *s,
|
2008-12-03 23:48:44 +01:00
|
|
|
struct audsettings *as,
|
2006-07-04 18:51:32 +02:00
|
|
|
struct audio_capture_ops *ops,
|
|
|
|
void *cb_opaque
|
|
|
|
)
|
|
|
|
{
|
|
|
|
CaptureVoiceOut *cap;
|
|
|
|
struct capture_callback *cb;
|
|
|
|
|
2019-08-19 01:06:47 +02:00
|
|
|
if (!s) {
|
2019-08-19 01:06:51 +02:00
|
|
|
if (!legacy_config) {
|
2019-08-26 21:59:02 +02:00
|
|
|
dolog("Capturing without setting an audiodev is deprecated\n");
|
2019-08-19 01:06:51 +02:00
|
|
|
}
|
|
|
|
s = audio_init(NULL, NULL);
|
2019-08-19 01:06:47 +02:00
|
|
|
}
|
|
|
|
|
2019-10-13 21:57:59 +02:00
|
|
|
if (!audio_get_pdo_out(s->dev)->mixing_engine) {
|
|
|
|
dolog("Can't capture with mixeng disabled\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-07-16 20:57:03 +02:00
|
|
|
if (audio_validate_settings (as)) {
|
2006-07-04 18:51:32 +02:00
|
|
|
dolog ("Invalid settings were passed when trying to add capture\n");
|
|
|
|
audio_print_settings (as);
|
2019-02-25 16:43:35 +01:00
|
|
|
return NULL;
|
2006-07-04 18:51:32 +02:00
|
|
|
}
|
|
|
|
|
2019-02-25 16:43:35 +01:00
|
|
|
cb = g_malloc0(sizeof(*cb));
|
2006-07-04 18:51:32 +02:00
|
|
|
cb->ops = *ops;
|
|
|
|
cb->opaque = cb_opaque;
|
|
|
|
|
2019-08-19 01:06:46 +02:00
|
|
|
cap = audio_pcm_capture_find_specific(s, as);
|
2006-07-04 18:51:32 +02:00
|
|
|
if (cap) {
|
2009-09-12 09:36:22 +02:00
|
|
|
QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
|
2006-07-16 20:57:03 +02:00
|
|
|
return cap;
|
2021-01-15 02:24:25 +01:00
|
|
|
} else {
|
2006-07-04 18:51:32 +02:00
|
|
|
HWVoiceOut *hw;
|
|
|
|
CaptureVoiceOut *cap;
|
|
|
|
|
2019-02-25 16:43:35 +01:00
|
|
|
cap = g_malloc0(sizeof(*cap));
|
2006-07-04 18:51:32 +02:00
|
|
|
|
|
|
|
hw = &cap->hw;
|
2019-08-19 01:06:46 +02:00
|
|
|
hw->s = s;
|
2022-03-01 20:13:04 +01:00
|
|
|
hw->pcm_ops = &capture_pcm_ops;
|
2009-09-12 09:36:22 +02:00
|
|
|
QLIST_INIT (&hw->sw_head);
|
|
|
|
QLIST_INIT (&cap->cb_head);
|
2006-07-04 18:51:32 +02:00
|
|
|
|
|
|
|
/* XXX find a more elegant way */
|
|
|
|
hw->samples = 4096 * 4;
|
2019-09-19 23:24:20 +02:00
|
|
|
audio_pcm_hw_alloc_resources_out(hw);
|
2006-07-04 18:51:32 +02:00
|
|
|
|
2006-07-04 23:47:22 +02:00
|
|
|
audio_pcm_init_info (&hw->info, as);
|
2006-07-04 18:51:32 +02:00
|
|
|
|
2023-02-24 20:05:41 +01:00
|
|
|
cap->buf = g_malloc0_n(hw->mix_buf.size, hw->info.bytes_per_frame);
|
2006-07-04 18:51:32 +02:00
|
|
|
|
2020-02-02 20:38:07 +01:00
|
|
|
if (hw->info.is_float) {
|
|
|
|
hw->clip = mixeng_clip_float[hw->info.nchannels == 2];
|
|
|
|
} else {
|
|
|
|
hw->clip = mixeng_clip
|
|
|
|
[hw->info.nchannels == 2]
|
|
|
|
[hw->info.is_signed]
|
|
|
|
[hw->info.swap_endianness]
|
|
|
|
[audio_bits_to_index(hw->info.bits)];
|
|
|
|
}
|
2006-07-04 18:51:32 +02:00
|
|
|
|
2009-09-12 09:36:22 +02:00
|
|
|
QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
|
|
|
|
QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
|
2006-07-04 18:51:32 +02:00
|
|
|
|
2019-08-19 01:06:46 +02:00
|
|
|
QLIST_FOREACH(hw, &s->hw_head_out, entries) {
|
2009-05-14 01:11:35 +02:00
|
|
|
audio_attach_capture (hw);
|
2006-07-04 18:51:32 +02:00
|
|
|
}
|
2006-07-16 20:57:03 +02: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 09:36:22 +02:00
|
|
|
QLIST_REMOVE (cb, entries);
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free (cb);
|
2006-07-16 20:57:03 +02:00
|
|
|
|
|
|
|
if (!cap->cb_head.lh_first) {
|
|
|
|
SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
|
2006-07-18 23:09:59 +02:00
|
|
|
|
2006-07-16 20:57:03 +02:00
|
|
|
while (sw) {
|
2006-07-18 23:09:59 +02:00
|
|
|
SWVoiceCap *sc = (SWVoiceCap *) sw;
|
2006-07-16 20:57:03 +02:00
|
|
|
#ifdef DEBUG_CAPTURE
|
|
|
|
dolog ("freeing %s\n", sw->name);
|
|
|
|
#endif
|
2006-07-18 23:09:59 +02:00
|
|
|
|
2006-07-16 20:57:03 +02:00
|
|
|
sw1 = sw->entries.le_next;
|
|
|
|
if (sw->rate) {
|
|
|
|
st_rate_stop (sw->rate);
|
|
|
|
sw->rate = NULL;
|
|
|
|
}
|
2009-09-12 09:36:22 +02:00
|
|
|
QLIST_REMOVE (sw, entries);
|
|
|
|
QLIST_REMOVE (sc, entries);
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free (sc);
|
2006-07-16 20:57:03 +02:00
|
|
|
sw = sw1;
|
|
|
|
}
|
2009-09-12 09:36:22 +02:00
|
|
|
QLIST_REMOVE (cap, entries);
|
2023-02-24 20:05:41 +01:00
|
|
|
g_free(cap->hw.mix_buf.buffer);
|
2017-04-28 09:56:12 +02:00
|
|
|
g_free (cap->buf);
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free (cap);
|
2006-07-16 20:57:03 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2006-07-04 18:51:32 +02:00
|
|
|
}
|
|
|
|
}
|
2008-05-04 12:21:03 +02:00
|
|
|
|
|
|
|
void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
|
2019-10-13 21:58:01 +02:00
|
|
|
{
|
|
|
|
Volume vol = { .mute = mute, .channels = 2, .vol = { lvol, rvol } };
|
|
|
|
audio_set_volume_out(sw, &vol);
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_set_volume_out(SWVoiceOut *sw, Volume *vol)
|
2008-05-04 12:21:03 +02:00
|
|
|
{
|
|
|
|
if (sw) {
|
2012-04-17 14:32:35 +02:00
|
|
|
HWVoiceOut *hw = sw->hw;
|
|
|
|
|
2019-10-13 21:58:01 +02:00
|
|
|
sw->vol.mute = vol->mute;
|
|
|
|
sw->vol.l = nominal_volume.l * vol->vol[0] / 255;
|
|
|
|
sw->vol.r = nominal_volume.l * vol->vol[vol->channels > 1 ? 1 : 0] /
|
|
|
|
255;
|
2012-04-17 14:32:35 +02:00
|
|
|
|
2019-09-19 23:24:22 +02:00
|
|
|
if (hw->pcm_ops->volume_out) {
|
2019-10-13 21:58:01 +02:00
|
|
|
hw->pcm_ops->volume_out(hw, vol);
|
2012-04-17 14:32:35 +02:00
|
|
|
}
|
2008-05-04 12:21:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
|
2019-10-13 21:58:01 +02:00
|
|
|
{
|
|
|
|
Volume vol = { .mute = mute, .channels = 2, .vol = { lvol, rvol } };
|
|
|
|
audio_set_volume_in(sw, &vol);
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_set_volume_in(SWVoiceIn *sw, Volume *vol)
|
2008-05-04 12:21:03 +02:00
|
|
|
{
|
|
|
|
if (sw) {
|
2012-04-17 14:32:35 +02:00
|
|
|
HWVoiceIn *hw = sw->hw;
|
|
|
|
|
2019-10-13 21:58:01 +02:00
|
|
|
sw->vol.mute = vol->mute;
|
|
|
|
sw->vol.l = nominal_volume.l * vol->vol[0] / 255;
|
|
|
|
sw->vol.r = nominal_volume.r * vol->vol[vol->channels > 1 ? 1 : 0] /
|
|
|
|
255;
|
2012-04-17 14:32:35 +02:00
|
|
|
|
2019-09-19 23:24:22 +02:00
|
|
|
if (hw->pcm_ops->volume_in) {
|
2019-10-13 21:58:01 +02:00
|
|
|
hw->pcm_ops->volume_in(hw, vol);
|
2012-04-17 14:32:35 +02:00
|
|
|
}
|
2008-05-04 12:21:03 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-08 23:34:15 +01:00
|
|
|
|
|
|
|
void audio_create_pdos(Audiodev *dev)
|
|
|
|
{
|
|
|
|
switch (dev->driver) {
|
|
|
|
#define CASE(DRIVER, driver, pdo_name) \
|
|
|
|
case AUDIODEV_DRIVER_##DRIVER: \
|
2022-11-04 17:06:49 +01:00
|
|
|
if (!dev->u.driver.in) { \
|
2019-03-08 23:34:15 +01:00
|
|
|
dev->u.driver.in = g_malloc0( \
|
|
|
|
sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
|
|
|
|
} \
|
2022-11-04 17:06:49 +01:00
|
|
|
if (!dev->u.driver.out) { \
|
2019-03-08 23:34:15 +01:00
|
|
|
dev->u.driver.out = g_malloc0( \
|
2019-08-26 21:59:01 +02:00
|
|
|
sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
|
2019-03-08 23:34:15 +01:00
|
|
|
} \
|
|
|
|
break
|
|
|
|
|
|
|
|
CASE(NONE, none, );
|
2023-01-23 09:39:57 +01:00
|
|
|
#ifdef CONFIG_AUDIO_ALSA
|
2019-03-08 23:34:15 +01:00
|
|
|
CASE(ALSA, alsa, Alsa);
|
2023-01-23 09:39:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_AUDIO_COREAUDIO
|
2019-03-08 23:34:15 +01:00
|
|
|
CASE(COREAUDIO, coreaudio, Coreaudio);
|
2023-01-23 09:39:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_DBUS_DISPLAY
|
2021-03-09 14:15:28 +01:00
|
|
|
CASE(DBUS, dbus, );
|
2023-01-23 09:39:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_AUDIO_DSOUND
|
2019-03-08 23:34:15 +01:00
|
|
|
CASE(DSOUND, dsound, );
|
2023-01-23 09:39:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_AUDIO_JACK
|
2020-04-29 07:53:58 +02:00
|
|
|
CASE(JACK, jack, Jack);
|
2023-01-23 09:39:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_AUDIO_OSS
|
2019-03-08 23:34:15 +01:00
|
|
|
CASE(OSS, oss, Oss);
|
2023-01-23 09:39:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_AUDIO_PA
|
2019-03-08 23:34:15 +01:00
|
|
|
CASE(PA, pa, Pa);
|
2023-01-23 09:39:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_AUDIO_SDL
|
2021-01-10 11:02:19 +01:00
|
|
|
CASE(SDL, sdl, Sdl);
|
2023-01-23 09:39:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_AUDIO_SNDIO
|
2022-09-07 15:23:42 +02:00
|
|
|
CASE(SNDIO, sndio, );
|
2023-01-23 09:39:57 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SPICE
|
2019-03-08 23:34:15 +01:00
|
|
|
CASE(SPICE, spice, );
|
2023-01-23 09:39:57 +01:00
|
|
|
#endif
|
2019-03-08 23:34:15 +01:00
|
|
|
CASE(WAV, wav, );
|
|
|
|
|
|
|
|
case AUDIODEV_DRIVER__MAX:
|
|
|
|
abort();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_validate_per_direction_opts(
|
|
|
|
AudiodevPerDirectionOptions *pdo, Error **errp)
|
|
|
|
{
|
2019-10-13 21:57:59 +02:00
|
|
|
if (!pdo->has_mixing_engine) {
|
|
|
|
pdo->has_mixing_engine = true;
|
|
|
|
pdo->mixing_engine = true;
|
|
|
|
}
|
2019-03-08 23:34:15 +01:00
|
|
|
if (!pdo->has_fixed_settings) {
|
|
|
|
pdo->has_fixed_settings = true;
|
2019-10-13 21:57:59 +02:00
|
|
|
pdo->fixed_settings = pdo->mixing_engine;
|
2019-03-08 23:34:15 +01:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2019-10-13 21:57:59 +02:00
|
|
|
if (!pdo->mixing_engine && pdo->fixed_settings) {
|
|
|
|
error_setg(errp, "You can't use fixed-settings without mixeng");
|
|
|
|
return;
|
|
|
|
}
|
2019-03-08 23:34:15 +01:00
|
|
|
|
|
|
|
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;
|
2019-10-13 21:57:59 +02:00
|
|
|
pdo->voices = pdo->mixing_engine ? 1 : INT_MAX;
|
2019-03-08 23:34:15 +01:00
|
|
|
}
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-08 10:14:41 +02:00
|
|
|
void audio_help(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("Available audio drivers:\n");
|
|
|
|
|
|
|
|
for (i = 0; i < AUDIODEV_DRIVER__MAX; i++) {
|
|
|
|
audio_driver *driver = audio_driver_lookup(AudiodevDriver_str(i));
|
|
|
|
if (driver) {
|
|
|
|
printf("%s\n", driver->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 23:34:15 +01:00
|
|
|
void audio_parse_option(const char *opt)
|
|
|
|
{
|
|
|
|
Audiodev *dev = NULL;
|
|
|
|
|
2022-09-08 10:14:41 +02:00
|
|
|
if (is_help_option(opt)) {
|
|
|
|
audio_help();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
2019-03-08 23:34:15 +01:00
|
|
|
Visitor *v = qobject_input_visitor_new_str(opt, "driver", &error_fatal);
|
|
|
|
visit_type_Audiodev(v, NULL, &dev, &error_fatal);
|
|
|
|
visit_free(v);
|
|
|
|
|
2022-04-27 12:27:46 +02:00
|
|
|
audio_define(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_define(Audiodev *dev)
|
|
|
|
{
|
|
|
|
AudiodevListEntry *e;
|
|
|
|
|
2019-03-08 23:34:15 +01:00
|
|
|
audio_validate_opts(dev, &error_fatal);
|
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
e = g_new0(AudiodevListEntry, 1);
|
2019-03-08 23:34:15 +01:00
|
|
|
e->dev = dev;
|
|
|
|
QSIMPLEQ_INSERT_TAIL(&audiodevs, e, next);
|
|
|
|
}
|
|
|
|
|
2022-08-22 15:10:21 +02:00
|
|
|
bool audio_init_audiodevs(void)
|
2019-03-08 23:34:15 +01:00
|
|
|
{
|
|
|
|
AudiodevListEntry *e;
|
|
|
|
|
|
|
|
QSIMPLEQ_FOREACH(e, &audiodevs, next) {
|
2022-08-22 15:10:21 +02:00
|
|
|
if (!audio_init(e->dev, NULL)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-08 23:34:15 +01:00
|
|
|
}
|
2022-08-22 15:10:21 +02:00
|
|
|
|
|
|
|
return true;
|
2019-03-08 23:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2020-02-02 20:38:07 +01:00
|
|
|
case AUDIO_FORMAT_F32:
|
2019-03-08 23:34:15 +01:00
|
|
|
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);
|
|
|
|
}
|
2019-08-19 01:06:47 +02:00
|
|
|
|
|
|
|
AudioState *audio_state_by_name(const char *name)
|
|
|
|
{
|
|
|
|
AudioState *s;
|
|
|
|
QTAILQ_FOREACH(s, &audio_states, list) {
|
|
|
|
assert(s->dev);
|
|
|
|
if (strcmp(name, s->dev->id) == 0) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *audio_get_id(QEMUSoundCard *card)
|
|
|
|
{
|
|
|
|
if (card->state) {
|
|
|
|
assert(card->state->dev);
|
|
|
|
return card->state->dev->id;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
2019-09-19 23:24:21 +02:00
|
|
|
|
2021-05-17 21:46:03 +02:00
|
|
|
const char *audio_application_name(void)
|
|
|
|
{
|
|
|
|
const char *vm_name;
|
|
|
|
|
|
|
|
vm_name = qemu_get_vm_name();
|
|
|
|
return vm_name ? vm_name : "qemu";
|
|
|
|
}
|
|
|
|
|
2019-09-19 23:24:21 +02:00
|
|
|
void audio_rate_start(RateCtl *rate)
|
|
|
|
{
|
|
|
|
memset(rate, 0, sizeof(RateCtl));
|
|
|
|
rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
|
|
|
}
|
|
|
|
|
2022-09-23 20:36:33 +02:00
|
|
|
size_t audio_rate_peek_bytes(RateCtl *rate, struct audio_pcm_info *info)
|
2019-09-19 23:24:21 +02:00
|
|
|
{
|
|
|
|
int64_t now;
|
|
|
|
int64_t ticks;
|
|
|
|
int64_t bytes;
|
2022-09-23 20:36:33 +02:00
|
|
|
int64_t frames;
|
2019-09-19 23:24:21 +02:00
|
|
|
|
|
|
|
now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
|
|
|
ticks = now - rate->start_ticks;
|
|
|
|
bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND);
|
2022-09-23 20:36:33 +02:00
|
|
|
frames = (bytes - rate->bytes_sent) / info->bytes_per_frame;
|
|
|
|
if (frames < 0 || frames > 65536) {
|
|
|
|
AUD_log(NULL, "Resetting rate control (%" PRId64 " frames)\n", frames);
|
2019-09-19 23:24:21 +02:00
|
|
|
audio_rate_start(rate);
|
2022-09-23 20:36:33 +02:00
|
|
|
frames = 0;
|
2019-09-19 23:24:21 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 20:36:33 +02:00
|
|
|
return frames * info->bytes_per_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
void audio_rate_add_bytes(RateCtl *rate, size_t bytes_used)
|
|
|
|
{
|
|
|
|
rate->bytes_sent += bytes_used;
|
|
|
|
}
|
|
|
|
|
2022-09-23 20:36:36 +02:00
|
|
|
size_t audio_rate_get_bytes(RateCtl *rate, struct audio_pcm_info *info,
|
2022-09-23 20:36:33 +02:00
|
|
|
size_t bytes_avail)
|
|
|
|
{
|
|
|
|
size_t bytes;
|
|
|
|
|
|
|
|
bytes = audio_rate_peek_bytes(rate, info);
|
|
|
|
bytes = MIN(bytes, bytes_avail);
|
|
|
|
audio_rate_add_bytes(rate, bytes);
|
|
|
|
|
|
|
|
return bytes;
|
2019-09-19 23:24:21 +02:00
|
|
|
}
|
2023-01-23 09:39:56 +01:00
|
|
|
|
|
|
|
AudiodevList *qmp_query_audiodevs(Error **errp)
|
|
|
|
{
|
|
|
|
AudiodevList *ret = NULL;
|
|
|
|
AudiodevListEntry *e;
|
|
|
|
QSIMPLEQ_FOREACH(e, &audiodevs, next) {
|
|
|
|
QAPI_LIST_PREPEND(ret, QAPI_CLONE(Audiodev, e->dev));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|