channelmix: implement per channel volume

Implement per channel volume on channelmix. Extend control on stream to
take an array of values when possible.

Remove name argument from pw_node_new and pw_device_new. We can pass
this as a property instead.

Improve properties on nodes to more closely match what pulseaudio does.
Don't let the monitor do too much with the udev properties but let the
session manager set the description and icon-names.

Remove some change_mask flags for things that don't change in
introspect. Use the flags to mark changes in -cli and -monitor.
This commit is contained in:
Wim Taymans 2019-08-12 14:47:16 +02:00
parent 1c27f48992
commit c6a7b3eedb
45 changed files with 737 additions and 367 deletions

View file

@ -101,10 +101,90 @@ static const char *path_get_card_id(const char *path)
return e + 5;
}
static int dehex(char x)
{
if (x >= '0' && x <= '9')
return x - '0';
if (x >= 'A' && x <= 'F')
return x - 'A' + 10;
if (x >= 'a' && x <= 'f')
return x - 'a' + 10;
return -1;
}
static void unescape(const char *src, char *dst)
{
const char *s;
char *d;
int h1, h2;
enum { TEXT, BACKSLASH, EX, FIRST } state = TEXT;
for (s = src, d = dst; *s; s++) {
switch (state) {
case TEXT:
if (*s == '\\')
state = BACKSLASH;
else
*(d++) = *s;
break;
case BACKSLASH:
if (*s == 'x')
state = EX;
else {
*(d++) = '\\';
*(d++) = *s;
state = TEXT;
}
break;
case EX:
h1 = dehex(*s);
if (h1 < 0) {
*(d++) = '\\';
*(d++) = 'x';
*(d++) = *s;
state = TEXT;
} else
state = FIRST;
break;
case FIRST:
h2 = dehex(*s);
if (h2 < 0) {
*(d++) = '\\';
*(d++) = 'x';
*(d++) = *(s-1);
*(d++) = *s;
} else
*(d++) = (char) (h1 << 4) | h2;
state = TEXT;
break;
}
}
switch (state) {
case TEXT:
break;
case BACKSLASH:
*(d++) = '\\';
break;
case EX:
*(d++) = '\\';
*(d++) = 'x';
break;
case FIRST:
*(d++) = '\\';
*(d++) = 'x';
*(d++) = *(s-1);
break;
}
*d = 0;
}
static int emit_object_info(struct impl *this, uint32_t id, struct udev_device *dev)
{
struct spa_monitor_object_info info;
const char *str, *name;
const char *str;
char path[32];
struct spa_dict_item items[20];
uint32_t n_items = 0;
@ -117,17 +197,8 @@ static int emit_object_info(struct impl *this, uint32_t id, struct udev_device *
SPA_MONITOR_OBJECT_CHANGE_MASK_PROPS;
info.flags = 0;
name = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE");
if (!(name && *name)) {
name = udev_device_get_property_value(dev, "ID_MODEL_ENC");
if (!(name && *name)) {
name = udev_device_get_property_value(dev, "ID_MODEL");
}
}
if (!(name && *name))
name = "Unknown";
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_MONITOR_API, "udev");
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_API, "alsa");
if ((str = path_get_card_id(udev_device_get_property_value(dev, "DEVPATH"))) == NULL)
return 0;
@ -135,7 +206,9 @@ static int emit_object_info(struct impl *this, uint32_t id, struct udev_device *
snprintf(path, sizeof(path), "hw:%d", atoi(str));
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_API_ALSA_PATH, path);
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_API_ALSA_CARD, str);
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_NAME, name);
if ((str = udev_device_get_property_value(dev, "PULSE_NAME")) && *str)
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_NAME, str);
if ((str = udev_device_get_property_value(dev, "SOUND_CLASS")) && *str)
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_CLASS, str);
@ -169,6 +242,10 @@ static int emit_object_info(struct impl *this, uint32_t id, struct udev_device *
str = udev_device_get_property_value(dev, "ID_VENDOR_ENC");
if (!(str && *str)) {
str = udev_device_get_property_value(dev, "ID_VENDOR");
} else {
char *t = alloca(strlen(str) + 1);
unescape(str, t);
str = t;
}
}
if (str && *str) {
@ -177,7 +254,19 @@ static int emit_object_info(struct impl *this, uint32_t id, struct udev_device *
if ((str = udev_device_get_property_value(dev, "ID_MODEL_ID")) && *str) {
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PRODUCT_ID, str);
}
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PRODUCT_NAME, name);
str = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE");
if (!(str && *str)) {
str = udev_device_get_property_value(dev, "ID_MODEL_ENC");
if (!(str && *str)) {
str = udev_device_get_property_value(dev, "ID_MODEL");
} else {
char *t = alloca(strlen(str) + 1);
unescape(str, t);
str = t;
}
}
if (str && *str)
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PRODUCT_NAME, str);
if ((str = udev_device_get_property_value(dev, "ID_SERIAL")) && *str) {
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_SERIAL, str);

View file

@ -99,6 +99,8 @@ static int impl_node_enum_params(void *object, int seq,
next:
result.index = result.next;
spa_log_debug(this->log, NAME" %p: %d id:%u", this, seq, id);
spa_pod_builder_init(&b, buffer, sizeof(buffer));
switch (id) {
@ -190,7 +192,6 @@ static void emit_node_info(struct impl *this, bool full)
if (this->info.change_mask) {
struct spa_dict_item items[1];
this->info.change_mask |= SPA_NODE_CHANGE_MASK_PROPS;
items[0] = SPA_DICT_ITEM_INIT(SPA_KEY_NODE_DRIVER, this->driver ? "1" : "0");
this->info.props = &SPA_DICT_INIT(items, 1);
@ -228,10 +229,6 @@ static int impl_node_set_param(void *object, uint32_t id, uint32_t flags,
if (this->target != this->slave) {
if ((res = spa_node_set_param(this->target, id, flags, param)) < 0)
return res;
this->info.change_mask = SPA_NODE_CHANGE_MASK_PARAMS;
this->params[2].flags ^= SPA_PARAM_INFO_SERIAL;
emit_node_info(this, false);
}
break;
default:
@ -366,8 +363,10 @@ static void slave_info(void *data, const struct spa_node_info *info)
"Input" : "Output");
if (info->props) {
if ((str = spa_dict_lookup(info->props, SPA_KEY_NODE_DRIVER)) != NULL)
if ((str = spa_dict_lookup(info->props, SPA_KEY_NODE_DRIVER)) != NULL) {
this->driver = strcmp(str, "true") == 0 || atoi(str) == 1;
this->info.change_mask |= SPA_NODE_CHANGE_MASK_PROPS;
}
}
}

View file

@ -31,7 +31,8 @@ channelmix_copy_c(struct channelmix *mix, uint32_t n_dst, void * SPA_RESTRICT ds
uint32_t i, n;
float **d = (float **)dst;
const float **s = (const float **)src;
float v = mix->volume;
const float *m = mix->matrix;
const float v = mix->volume;
if (v <= VOLUME_MIN) {
for (i = 0; i < n_dst; i++)
@ -42,9 +43,12 @@ channelmix_copy_c(struct channelmix *mix, uint32_t n_dst, void * SPA_RESTRICT ds
spa_memcpy(d[i], s[i], n_samples * sizeof(float));
}
else {
for (i = 0; i < n_dst; i++)
for (i = 0; i < n_dst; i++) {
const float vol = m[i * n_src + i];
for (n = 0; n < n_samples; n++)
d[i][n] = s[i][n] * v;
d[i][n] = s[i][n] * vol;
}
}
}
@ -58,14 +62,13 @@ channelmix_f32_n_m_c(struct channelmix *mix, uint32_t n_dst, void * SPA_RESTRICT
float **d = (float **) dst;
const float **s = (const float **) src;
const float *m = mix->matrix;
float v = mix->volume;
for (n = 0; n < n_samples; n++) {
for (i = 0; i < n_dst; i++) {
float sum = 0.0f;
for (j = 0; j < n_src; j++)
sum += s[j][n] * m[i * n_src + j];
d[i][n] = sum * v;
d[i][n] = sum;
}
}
}

View file

@ -32,8 +32,8 @@ void channelmix_copy_sse(struct channelmix *mix, uint32_t n_dst, void * SPA_REST
uint32_t i, n, unrolled;
float **d = (float **)dst;
const float **s = (const float **)src;
float *m = mix->matrix;
float v = mix->volume;
const __m128 vol = _mm_set1_ps(v);
if (v <= VOLUME_MIN) {
for (i = 0; i < n_dst; i++)
@ -48,6 +48,7 @@ void channelmix_copy_sse(struct channelmix *mix, uint32_t n_dst, void * SPA_REST
float *di = d[i];
const float *si = s[i];
__m128 t[4];
const __m128 vol = _mm_set1_ps(m[i * n_src + i]);
if (SPA_IS_ALIGNED(di, 16) &&
SPA_IS_ALIGNED(si, 16))

View file

@ -159,9 +159,7 @@ static const struct channelmix_info *find_channelmix_info(uint32_t src_chan, uin
static int make_matrix(struct channelmix *mix)
{
float matrix[NUM_CHAN][NUM_CHAN] = {{ 0.0f }};
uint32_t src_chan = mix->src_chan;
uint64_t src_mask = mix->src_mask;
uint32_t dst_chan = mix->dst_chan;
uint64_t dst_mask = mix->dst_mask;
uint64_t unassigned;
uint32_t i, j, matrix_encoding = MATRIX_NORMAL, c;
@ -335,11 +333,46 @@ static int make_matrix(struct channelmix *mix)
for (j = 0; j < NUM_CHAN; j++) {
if ((src_mask & (1UL << (j + 2))) == 0)
continue;
mix->matrix[c++] = matrix[i][j];
mix->matrix_orig[c++] = matrix[i][j];
sum += fabs(matrix[i][j]);
}
max = SPA_MAX(max, sum);
}
return 0;
}
static void impl_channelmix_set_volume(struct channelmix *mix, float volume, bool mute,
uint32_t n_channel_volumes, float *channel_volumes)
{
float volumes[SPA_AUDIO_MAX_CHANNELS];
float vol = mute ? 0.0f : volume, sum;
uint32_t i, j;
uint32_t src_chan = mix->src_chan;
uint32_t dst_chan = mix->dst_chan;
/** apply global volume to channels */
sum = 0.0;
for (i = 0; i < n_channel_volumes; i++) {
volumes[i] = channel_volumes[i] * vol;
sum += volumes[i];
}
mix->volume = sum / n_channel_volumes;
if (n_channel_volumes == src_chan) {
for (i = 0; i < dst_chan; i++) {
for (j = 0; j < src_chan; j++) {
float v = mix->matrix_orig[i * src_chan + j];
mix->matrix[i * src_chan + j] = v * volumes[j];
}
}
} else if (n_channel_volumes == dst_chan) {
for (i = 0; i < dst_chan; i++) {
for (j = 0; j < src_chan; j++) {
float v = mix->matrix_orig[i * src_chan + j];
mix->matrix[i * src_chan + j] = v * volumes[i];
}
}
}
mix->is_identity = dst_chan == src_chan;
for (i = 0; i < dst_chan; i++) {
@ -351,7 +384,7 @@ static int make_matrix(struct channelmix *mix)
mix->is_identity = false;
}
}
return 0;
spa_log_debug(mix->log, "vol:%f, identity:%d", mix->volume, mix->is_identity);
}
static void impl_channelmix_free(struct channelmix *mix)
@ -370,6 +403,7 @@ int channelmix_init(struct channelmix *mix)
mix->free = impl_channelmix_free;
mix->process = info->process;
mix->set_volume = impl_channelmix_set_volume;
mix->cpu_flags = info->cpu_flags;
return make_matrix(mix);
}

View file

@ -26,12 +26,11 @@
#include <stdio.h>
#include <spa/utils/defs.h>
#include <spa/param/audio/raw.h>
#define VOLUME_MIN 0.0f
#define VOLUME_NORM 1.0f
#define MAX_CHANNELS 64
#define _M(ch) (1UL << SPA_AUDIO_CHANNEL_ ## ch)
#define MASK_MONO _M(FC)|_M(MONO)|_M(UNKNOWN)
#define MASK_STEREO _M(FL)|_M(FR)|_M(UNKNOWN)
@ -52,10 +51,14 @@ struct channelmix {
unsigned int is_identity:1;
float volume;
float matrix[MAX_CHANNELS * MAX_CHANNELS];
float matrix_orig[SPA_AUDIO_MAX_CHANNELS * SPA_AUDIO_MAX_CHANNELS];
float matrix[SPA_AUDIO_MAX_CHANNELS * SPA_AUDIO_MAX_CHANNELS];
void (*process) (struct channelmix *mix, uint32_t n_dst, void * SPA_RESTRICT dst[n_dst],
uint32_t n_src, const void * SPA_RESTRICT src[n_src], uint32_t n_samples);
void (*set_volume) (struct channelmix *mix, float volume, bool mute,
uint32_t n_channel_volumes, float *channel_volumes);
void (*free) (struct channelmix *mix);
void *data;
@ -64,6 +67,7 @@ struct channelmix {
int channelmix_init(struct channelmix *mix);
#define channelmix_process(mix,...) (mix)->process(mix, __VA_ARGS__)
#define channelmix_set_volume(mix,...) (mix)->set_volume(mix, __VA_ARGS__)
#define channelmix_free(mix) (mix)->free(mix)
#define DEFINE_FUNCTION(name,arch) \

View file

@ -39,6 +39,8 @@
#include <spa/control/control.h>
#include <spa/debug/types.h>
#include "channelmix-ops.h"
#define NAME "channelmix"
#define DEFAULT_RATE 44100
@ -55,12 +57,18 @@ struct impl;
struct props {
float volume;
bool mute;
uint32_t n_channel_volumes;
float channel_volumes[SPA_AUDIO_MAX_CHANNELS];
};
static void props_reset(struct props *props)
{
uint32_t i;
props->mute = DEFAULT_MUTE;
props->volume = DEFAULT_VOLUME;
props->n_channel_volumes = 0;
for (i = 0; i < SPA_AUDIO_MAX_CHANNELS; i++)
props->channel_volumes[i] = 1.0;
}
struct buffer {
@ -96,8 +104,6 @@ struct port {
struct spa_list queue;
};
#include "channelmix-ops.h"
struct impl {
struct spa_handle handle;
struct spa_node node;
@ -228,11 +234,17 @@ static int setup_convert(struct impl *this,
this->mix.dst_mask = dst_mask;
this->mix.cpu_flags = this->cpu_flags;
this->mix.log = this->log;
this->mix.volume = this->props.mute ? 0.0f : this->props.volume;
if ((res = channelmix_init(&this->mix)) < 0)
return res;
this->props.n_channel_volumes = SPA_MAX(src_chan, dst_chan);
channelmix_set_volume(&this->mix, this->props.volume, this->props.mute,
this->props.n_channel_volumes, this->props.channel_volumes);
emit_params_changed(this);
spa_log_info(this->log, NAME " %p: got channelmix features %08x:%08x %d",
this, this->cpu_flags, this->mix.cpu_flags,
this->mix.is_identity);
@ -283,6 +295,13 @@ static int impl_node_enum_params(void *object, int seq,
SPA_PROP_INFO_name, SPA_POD_String("Mute"),
SPA_PROP_INFO_type, SPA_POD_CHOICE_Bool(p->mute));
break;
case 2:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_PropInfo, id,
SPA_PROP_INFO_id, SPA_POD_Id(SPA_PROP_channelVolumes),
SPA_PROP_INFO_name, SPA_POD_String("Channel Volumes"),
SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Float(p->volume, 0.0, 10.0));
break;
default:
return 0;
}
@ -296,8 +315,12 @@ static int impl_node_enum_params(void *object, int seq,
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_Props, id,
SPA_PROP_volume, SPA_POD_Float(p->volume),
SPA_PROP_mute, SPA_POD_Bool(p->mute));
SPA_PROP_volume, SPA_POD_Float(p->volume),
SPA_PROP_mute, SPA_POD_Bool(p->mute),
SPA_PROP_channelVolumes, SPA_POD_Array(sizeof(float),
SPA_TYPE_Float,
p->n_channel_volumes,
p->channel_volumes));
break;
default:
return 0;
@ -336,13 +359,19 @@ static int apply_props(struct impl *this, const struct spa_pod *param)
if (spa_pod_get_bool(&prop->value, &p->mute) == 0)
changed++;
break;
case SPA_PROP_channelVolumes:
if (spa_pod_copy_array(&prop->value, SPA_TYPE_Float,
p->channel_volumes, SPA_AUDIO_MAX_CHANNELS) > 0)
changed++;
break;
default:
break;
}
}
if (changed)
this->mix.volume = p->mute ? 0.0f : p->volume;
if (changed && this->mix.set_volume) {
channelmix_set_volume(&this->mix, p->volume, p->mute,
p->n_channel_volumes, p->channel_volumes);
}
return changed;
}

View file

@ -334,7 +334,6 @@ impl_node_add_listener(void *object,
spa_return_val_if_fail(this != NULL, -EINVAL);
spa_log_debug(this->log, NAME " %p: listener %p", this, listener);
spa_hook_list_isolate(&this->hooks, &save, listener, events, data);
emit_node_info(this, true);

View file

@ -90,10 +90,90 @@ static uint32_t get_device_id(struct impl *this, struct udev_device *dev)
return atoi(str + 6);
}
static int dehex(char x)
{
if (x >= '0' && x <= '9')
return x - '0';
if (x >= 'A' && x <= 'F')
return x - 'A' + 10;
if (x >= 'a' && x <= 'f')
return x - 'a' + 10;
return -1;
}
static void unescape(const char *src, char *dst)
{
const char *s;
char *d;
int h1, h2;
enum { TEXT, BACKSLASH, EX, FIRST } state = TEXT;
for (s = src, d = dst; *s; s++) {
switch (state) {
case TEXT:
if (*s == '\\')
state = BACKSLASH;
else
*(d++) = *s;
break;
case BACKSLASH:
if (*s == 'x')
state = EX;
else {
*(d++) = '\\';
*(d++) = *s;
state = TEXT;
}
break;
case EX:
h1 = dehex(*s);
if (h1 < 0) {
*(d++) = '\\';
*(d++) = 'x';
*(d++) = *s;
state = TEXT;
} else
state = FIRST;
break;
case FIRST:
h2 = dehex(*s);
if (h2 < 0) {
*(d++) = '\\';
*(d++) = 'x';
*(d++) = *(s-1);
*(d++) = *s;
} else
*(d++) = (char) (h1 << 4) | h2;
state = TEXT;
break;
}
}
switch (state) {
case TEXT:
break;
case BACKSLASH:
*(d++) = '\\';
break;
case EX:
*(d++) = '\\';
*(d++) = 'x';
break;
case FIRST:
*(d++) = '\\';
*(d++) = 'x';
*(d++) = *(s-1);
break;
}
*d = 0;
}
static int emit_object_info(struct impl *this, uint32_t id, struct udev_device *dev)
{
struct spa_monitor_object_info info;
const char *str, *name;
const char *str;
struct spa_dict_item items[20];
uint32_t n_items = 0;
@ -105,22 +185,9 @@ static int emit_object_info(struct impl *this, uint32_t id, struct udev_device *
SPA_MONITOR_OBJECT_CHANGE_MASK_PROPS;
info.flags = 0;
name = udev_device_get_property_value(dev, "ID_V4L_PRODUCT");
if (!(name && *name)) {
name = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE");
if (!(name && *name)) {
name = udev_device_get_property_value(dev, "ID_MODEL_ENC");
if (!(name && *name)) {
name = udev_device_get_property_value(dev, "ID_MODEL");
}
}
}
if (!(name && *name))
name = "Unknown";
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_MONITOR_API,"udev");
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_API, "v4l2");
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_NAME, name);
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_API_V4L2_PATH, udev_device_get_devnode(dev));
if ((str = udev_device_get_property_value(dev, "USEC_INITIALIZED")) && *str)
@ -152,6 +219,10 @@ static int emit_object_info(struct impl *this, uint32_t id, struct udev_device *
str = udev_device_get_property_value(dev, "ID_VENDOR_ENC");
if (!(str && *str)) {
str = udev_device_get_property_value(dev, "ID_VENDOR");
} else {
char *t = alloca(strlen(str) + 1);
unescape(str, t);
str = t;
}
}
if (str && *str) {
@ -160,7 +231,24 @@ static int emit_object_info(struct impl *this, uint32_t id, struct udev_device *
if ((str = udev_device_get_property_value(dev, "ID_MODEL_ID")) && *str) {
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PRODUCT_ID, str);
}
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PRODUCT_NAME, name);
str = udev_device_get_property_value(dev, "ID_V4L_PRODUCT");
if (!(str && *str)) {
str = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE");
if (!(str && *str)) {
str = udev_device_get_property_value(dev, "ID_MODEL_ENC");
if (!(str && *str)) {
str = udev_device_get_property_value(dev, "ID_MODEL");
} else {
char *t = alloca(strlen(str) + 1);
unescape(str, t);
str = t;
}
}
}
if (str && *str)
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_PRODUCT_NAME, str);
if ((str = udev_device_get_property_value(dev, "ID_SERIAL")) && *str) {
items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_SERIAL, str);
}