summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2009-08-19 01:35:43 +0200
committerLennart Poettering <lennart@poettering.net>2009-08-19 01:35:43 +0200
commit24e582808c18d6866d8c10f8f0320b1af0ab758b (patch)
tree2f632722d576995f19bf90c6a17272eade31387e /src
parent2838b78e59ee7c8ea42fec6880cc4c2b2a2c9485 (diff)
source: rework volume handling
- drop the 'virtual_' prefix from s->virtual_volume since we don't distuingish between reference and real volumes for sources - introduce an accuracy for source volumes: if the hardware can control the volume "close enough" don't necessarily adjust the rest in software unless it is beyond a certain threshold. This should save a little bit of CPU at the expensive of a bit of accuracy in volume handling. - other minor cleanups
Diffstat (limited to 'src')
-rw-r--r--src/modules/alsa/alsa-source.c37
-rw-r--r--src/modules/bluetooth/module-bluetooth-device.c4
-rw-r--r--src/modules/oss/module-oss.c8
-rw-r--r--src/pulsecore/source.c45
-rw-r--r--src/pulsecore/source.h2
5 files changed, 57 insertions, 39 deletions
diff --git a/src/modules/alsa/alsa-source.c b/src/modules/alsa/alsa-source.c
index 9a51f857..7da37553 100644
--- a/src/modules/alsa/alsa-source.c
+++ b/src/modules/alsa/alsa-source.c
@@ -65,6 +65,8 @@
#define TSCHED_MIN_SLEEP_USEC (10*PA_USEC_PER_MSEC) /* 10ms */
#define TSCHED_MIN_WAKEUP_USEC (4*PA_USEC_PER_MSEC) /* 4ms */
+#define VOLUME_ACCURACY (PA_VOLUME_NORM/100)
+
struct userdata {
pa_core *core;
pa_module *module;
@@ -987,15 +989,11 @@ static void source_get_volume_cb(pa_source *s) {
if (pa_cvolume_equal(&u->hardware_volume, &r))
return;
- s->virtual_volume = u->hardware_volume = r;
-
- if (u->mixer_path->has_dB) {
- pa_cvolume reset;
+ s->volume = u->hardware_volume = r;
- /* Hmm, so the hardware volume changed, let's reset our software volume */
- pa_cvolume_reset(&reset, s->sample_spec.channels);
- pa_source_set_soft_volume(s, &reset);
- }
+ /* Hmm, so the hardware volume changed, let's reset our software volume */
+ if (u->mixer_path->has_dB)
+ pa_source_set_soft_volume(s, NULL);
}
static void source_set_volume_cb(pa_source *s) {
@@ -1008,7 +1006,7 @@ static void source_set_volume_cb(pa_source *s) {
pa_assert(u->mixer_handle);
/* Shift up by the base volume */
- pa_sw_cvolume_divide_scalar(&r, &s->virtual_volume, s->base_volume);
+ pa_sw_cvolume_divide_scalar(&r, &s->volume, s->base_volume);
if (pa_alsa_path_set_volume(u->mixer_path, u->mixer_handle, &s->channel_map, &r) < 0)
return;
@@ -1019,13 +1017,26 @@ static void source_set_volume_cb(pa_source *s) {
u->hardware_volume = r;
if (u->mixer_path->has_dB) {
+ pa_cvolume new_soft_volume;
+ pa_bool_t accurate_enough;
/* Match exactly what the user requested by software */
- pa_sw_cvolume_divide(&s->soft_volume, &s->virtual_volume, &u->hardware_volume);
+ pa_sw_cvolume_divide(&new_soft_volume, &s->volume, &u->hardware_volume);
+
+ /* If the adjustment to do in software is only minimal we
+ * can skip it. That saves us CPU at the expense of a bit of
+ * accuracy */
+ accurate_enough =
+ (pa_cvolume_min(&new_soft_volume) >= (PA_VOLUME_NORM - VOLUME_ACCURACY)) &&
+ (pa_cvolume_max(&new_soft_volume) <= (PA_VOLUME_NORM + VOLUME_ACCURACY));
- pa_log_debug("Requested volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->virtual_volume));
+ pa_log_debug("Requested volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->volume));
pa_log_debug("Got hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &u->hardware_volume));
- pa_log_debug("Calculated software volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->soft_volume));
+ pa_log_debug("Calculated software volume: %s (accurate-enough=%s)", pa_cvolume_snprint(t, sizeof(t), &new_soft_volume),
+ pa_yes_no(accurate_enough));
+
+ if (!accurate_enough)
+ s->soft_volume = new_soft_volume;
} else {
pa_log_debug("Wrote hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &r));
@@ -1033,7 +1044,7 @@ static void source_set_volume_cb(pa_source *s) {
/* We can't match exactly what the user requested, hence let's
* at least tell the user about it */
- s->virtual_volume = r;
+ s->volume = r;
}
}
diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c
index d6321fc4..395ec838 100644
--- a/src/modules/bluetooth/module-bluetooth-device.c
+++ b/src/modules/bluetooth/module-bluetooth-device.c
@@ -1500,12 +1500,12 @@ static void source_set_volume_cb(pa_source *s) {
if (u->profile != PROFILE_HSP)
return;
- gain = (pa_cvolume_max(&s->virtual_volume) * 15) / PA_VOLUME_NORM;
+ gain = (pa_cvolume_max(&s->volume) * 15) / PA_VOLUME_NORM;
if (gain > 15)
gain = 15;
- pa_cvolume_set(&s->virtual_volume, u->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15));
+ pa_cvolume_set(&s->volume, u->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15));
pa_assert_se(m = dbus_message_new_method_call("org.bluez", u->path, "org.bluez.Headset", "SetMicrophoneGain"));
pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_UINT16, &gain, DBUS_TYPE_INVALID));
diff --git a/src/modules/oss/module-oss.c b/src/modules/oss/module-oss.c
index c44b882b..0848d43b 100644
--- a/src/modules/oss/module-oss.c
+++ b/src/modules/oss/module-oss.c
@@ -848,11 +848,11 @@ static void source_get_volume(pa_source *s) {
pa_assert(u->mixer_devmask & (SOUND_MASK_IGAIN|SOUND_MASK_RECLEV));
if (u->mixer_devmask & SOUND_MASK_IGAIN)
- if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_IGAIN, &s->sample_spec, &s->virtual_volume) >= 0)
+ if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_IGAIN, &s->sample_spec, &s->volume) >= 0)
return;
if (u->mixer_devmask & SOUND_MASK_RECLEV)
- if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_RECLEV, &s->sample_spec, &s->virtual_volume) >= 0)
+ if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_RECLEV, &s->sample_spec, &s->volume) >= 0)
return;
pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
@@ -866,11 +866,11 @@ static void source_set_volume(pa_source *s) {
pa_assert(u->mixer_devmask & (SOUND_MASK_IGAIN|SOUND_MASK_RECLEV));
if (u->mixer_devmask & SOUND_MASK_IGAIN)
- if (pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_IGAIN, &s->sample_spec, &s->virtual_volume) >= 0)
+ if (pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_IGAIN, &s->sample_spec, &s->volume) >= 0)
return;
if (u->mixer_devmask & SOUND_MASK_RECLEV)
- if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_RECLEV, &s->sample_spec, &s->virtual_volume) >= 0)
+ if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_RECLEV, &s->sample_spec, &s->volume) >= 0)
return;
pa_log_info("Device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
diff --git a/src/pulsecore/source.c b/src/pulsecore/source.c
index 46f049ef..8aa07f5e 100644
--- a/src/pulsecore/source.c
+++ b/src/pulsecore/source.c
@@ -181,7 +181,7 @@ pa_source* pa_source_new(
pa_cvolume_reset(&data->volume, data->sample_spec.channels);
pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
- pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
+ pa_return_null_if_fail(pa_cvolume_compatible(&data->volume, &data->sample_spec));
if (!data->muted_is_set)
data->muted = FALSE;
@@ -219,7 +219,7 @@ pa_source* pa_source_new(
s->n_corked = 0;
s->monitor_of = NULL;
- s->virtual_volume = data->volume;
+ s->volume = data->volume;
pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
s->base_volume = PA_VOLUME_NORM;
s->n_volume_steps = PA_VOLUME_NORM+1;
@@ -751,31 +751,32 @@ pa_usec_t pa_source_get_latency_within_thread(pa_source *s) {
}
/* Called from main thread */
-void pa_source_set_volume(pa_source *s, const pa_cvolume *volume, pa_bool_t save) {
- pa_cvolume old_virtual_volume;
- pa_bool_t virtual_volume_changed;
+void pa_source_set_volume(
+ pa_source *s,
+ const pa_cvolume *volume,
+ pa_bool_t save) {
+
+ pa_bool_t real_changed;
pa_source_assert_ref(s);
pa_assert_ctl_context();
pa_assert(PA_SOURCE_IS_LINKED(s->state));
- pa_assert(volume);
pa_assert(pa_cvolume_valid(volume));
pa_assert(pa_cvolume_compatible(volume, &s->sample_spec));
- old_virtual_volume = s->virtual_volume;
- s->virtual_volume = *volume;
- virtual_volume_changed = !pa_cvolume_equal(&old_virtual_volume, &s->virtual_volume);
- s->save_volume = (!virtual_volume_changed && s->save_volume) || save;
+ real_changed = !pa_cvolume_equal(volume, &s->volume);
+ s->volume = *volume;
+ s->save_volume = (!real_changed && s->save_volume) || save;
if (s->set_volume) {
pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
s->set_volume(s);
} else
- s->soft_volume = s->virtual_volume;
+ s->soft_volume = s->volume;
pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
- if (virtual_volume_changed)
+ if (real_changed)
pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
}
@@ -783,12 +784,16 @@ void pa_source_set_volume(pa_source *s, const pa_cvolume *volume, pa_bool_t save
void pa_source_set_soft_volume(pa_source *s, const pa_cvolume *volume) {
pa_source_assert_ref(s);
pa_assert_ctl_context();
- pa_assert(volume);
+
+ if (!volume)
+ pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
+ else
+ s->soft_volume = *volume;
if (PA_SOURCE_IS_LINKED(s->state))
pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
else
- s->thread_info.soft_volume = *volume;
+ s->thread_info.soft_volume = s->soft_volume;
}
/* Called from main thread */
@@ -798,20 +803,22 @@ const pa_cvolume *pa_source_get_volume(pa_source *s, pa_bool_t force_refresh) {
pa_assert(PA_SOURCE_IS_LINKED(s->state));
if (s->refresh_volume || force_refresh) {
- pa_cvolume old_virtual_volume = s->virtual_volume;
+ pa_cvolume old_volume;
+
+ old_volume = s->volume;
if (s->get_volume)
s->get_volume(s);
pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_VOLUME, NULL, 0, NULL) == 0);
- if (!pa_cvolume_equal(&old_virtual_volume, &s->virtual_volume)) {
+ if (!pa_cvolume_equal(&old_volume, &s->volume)) {
s->save_volume = TRUE;
pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
}
}
- return &s->virtual_volume;
+ return &s->volume;
}
/* Called from main thread */
@@ -822,10 +829,10 @@ void pa_source_volume_changed(pa_source *s, const pa_cvolume *new_volume) {
/* The source implementor may call this if the volume changed to make sure everyone is notified */
- if (pa_cvolume_equal(&s->virtual_volume, new_volume))
+ if (pa_cvolume_equal(&s->volume, new_volume))
return;
- s->virtual_volume = *new_volume;
+ s->volume = *new_volume;
s->save_volume = TRUE;
pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
diff --git a/src/pulsecore/source.h b/src/pulsecore/source.h
index 6f33de06..7b3e4953 100644
--- a/src/pulsecore/source.h
+++ b/src/pulsecore/source.h
@@ -79,7 +79,7 @@ struct pa_source {
pa_volume_t base_volume; /* shall be constant */
unsigned n_volume_steps; /* shall be constant */
- pa_cvolume virtual_volume, soft_volume;
+ pa_cvolume volume, soft_volume;
pa_bool_t muted:1;
pa_bool_t refresh_volume:1;