]> code.delx.au - pulseaudio/commitdiff
implement pa_cvolume_{get|set}_fade
authorLennart Poettering <lennart@poettering.net>
Thu, 29 Jan 2009 23:15:40 +0000 (00:15 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 29 Jan 2009 23:22:59 +0000 (00:22 +0100)
src/map-file
src/pulse/volume.c
src/pulse/volume.h

index d6137590f659466b1914b8dfb625c528a16bad25..e076a4359b37b45d1118cf5e1f7e0aaa1df27c0e 100644 (file)
@@ -110,12 +110,14 @@ pa_cvolume_channels_equal_to;
 pa_cvolume_compatible;
 pa_cvolume_equal;
 pa_cvolume_get_balance;
+pa_cvolume_get_fade;
 pa_cvolume_init;
 pa_cvolume_max;
 pa_cvolume_remap;
 pa_cvolume_scale;
 pa_cvolume_set;
 pa_cvolume_set_balance;
+pa_cvolume_set_fade;
 pa_cvolume_snprint;
 pa_cvolume_valid;
 pa_ext_stream_restore_delete;
index 711c3a9f08573f8ede0f4fab87140b045fc5a4dd..a6abce2a7978884e2d5b00f80af690982c7b22ba 100644 (file)
@@ -341,6 +341,28 @@ static pa_bool_t on_lfe(pa_channel_position_t p) {
         p == PA_CHANNEL_POSITION_LFE;
 }
 
+static pa_bool_t on_front(pa_channel_position_t p) {
+    return
+        p == PA_CHANNEL_POSITION_FRONT_LEFT ||
+        p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
+        p == PA_CHANNEL_POSITION_FRONT_CENTER ||
+        p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
+        p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER ||
+        p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
+        p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
+        p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER;
+}
+
+static pa_bool_t on_rear(pa_channel_position_t p) {
+    return
+        p == PA_CHANNEL_POSITION_REAR_LEFT ||
+        p == PA_CHANNEL_POSITION_REAR_RIGHT ||
+        p == PA_CHANNEL_POSITION_REAR_CENTER ||
+        p == PA_CHANNEL_POSITION_TOP_REAR_LEFT ||
+        p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT ||
+        p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
+}
+
 pa_cvolume *pa_cvolume_remap(pa_cvolume *v, const pa_channel_map *from, const pa_channel_map *to) {
     int a, b;
     pa_cvolume result;
@@ -519,3 +541,92 @@ pa_cvolume* pa_cvolume_scale(pa_cvolume *v, pa_volume_t max) {
 
     return v;
 }
+
+static void get_avg_fr(const pa_channel_map *map, const pa_cvolume *v, pa_volume_t *f, pa_volume_t *r) {
+    int c;
+    pa_volume_t front = 0, rear = 0;
+    unsigned n_front = 0, n_rear = 0;
+
+    pa_assert(v);
+    pa_assert(map);
+    pa_assert(map->channels == v->channels);
+    pa_assert(f);
+    pa_assert(r);
+
+    for (c = 0; c < map->channels; c++) {
+        if (on_front(map->map[c])) {
+            front += v->values[c];
+            n_front++;
+        } else if (on_rear(map->map[c])) {
+            rear += v->values[c];
+            n_rear++;
+        }
+    }
+
+    if (n_front <= 0)
+        *f = PA_VOLUME_NORM;
+    else
+        *f = front / n_front;
+
+    if (n_rear <= 0)
+        *r = PA_VOLUME_NORM;
+    else
+        *r = rear / n_rear;
+}
+
+float pa_cvolume_get_fade(const pa_cvolume *v, const pa_channel_map *map) {
+    pa_volume_t front, rear;
+
+    pa_assert(v);
+    pa_assert(map);
+    pa_assert(map->channels == v->channels);
+
+    get_avg_fr(map, v, &front, &rear);
+
+    if (front == rear)
+        return 0.0f;
+
+    if (rear > front)
+        return -1.0f + ((float) front / (float) rear);
+    else
+        return 1.0f - ((float) rear / (float) front);
+}
+
+pa_cvolume* pa_cvolume_set_fade(pa_cvolume *v, const pa_channel_map *map, float new_fade) {
+    pa_volume_t front, nfront, rear, nrear, m;
+    unsigned c;
+
+    pa_assert(map->channels == v->channels);
+    pa_assert(map);
+    pa_assert(v);
+    pa_assert(new_fade >= -1.0f);
+    pa_assert(new_fade <= 1.0f);
+
+    get_avg_fr(map, v, &front, &rear);
+
+    m = PA_MAX(front, rear);
+
+    if (new_fade <= 0) {
+        nfront  = (new_fade + 1.0f) * m;
+        nrear = m;
+    } else  {
+        nrear = (1.0f - new_fade) * m;
+        nfront = m;
+    }
+
+    for (c = 0; c < map->channels; c++) {
+        if (on_front(map->map[c])) {
+            if (front == 0)
+                v->values[c] = nfront;
+            else
+                v->values[c] = (pa_volume_t) (((uint64_t) v->values[c] * (uint64_t) nfront) / (uint64_t) front);
+        } else if (on_rear(map->map[c])) {
+            if (rear == 0)
+                v->values[c] = nrear;
+            else
+                v->values[c] = (pa_volume_t) (((uint64_t) v->values[c] * (uint64_t) nrear) / (uint64_t) rear);
+        }
+    }
+
+    return v;
+}
index 9a883ca7c4f1f28f1621e1b207fd3fb90a7c7ac9..8eef467f539019a9bcfb6653763d0c8cd80779ff 100644 (file)
@@ -242,12 +242,27 @@ float pa_cvolume_get_balance(const pa_cvolume *v, const pa_channel_map *map) PA_
 /** Adjust the 'balance' value for the specified volume with the
  * specified channel map. v will be modified in place and
  * returned. The balance is a value between -1.0f and +1.0f. This
- * operation might not be reversable! Also, after this call
+ * operation might not be reversible! Also, after this call
  * pa_cvolume_get_balance() is not guaranteed to actually return the
- * requested balance (e.g. when the input volume was zero anyway for
+ * requested balance value (e.g. when the input volume was zero anyway for
  * all channels) \since 0.9.15 */
 pa_cvolume* pa_cvolume_set_balance(pa_cvolume *v, const pa_channel_map *map, float new_balance);
 
+/** Calculate a 'fade' value (i.e. 'balance' between front and rear)
+ * for the specified volume with the specified channel map. The return
+ * value will range from -1.0f (rear) to +1.0f (left) \since
+ * 0.9.15 */
+float pa_cvolume_get_fade(const pa_cvolume *v, const pa_channel_map *map) PA_GCC_PURE;
+
+/** Adjust the 'fade' value (i.e. 'balance' between front and rear)
+ * for the specified volume with the specified channel map. v will be
+ * modified in place and returned. The balance is a value between
+ * -1.0f and +1.0f. This operation might not be reversible! Also,
+ * after this call pa_cvolume_get_fade() is not guaranteed to
+ * actually return the requested fade value (e.g. when the input volume
+ * was zero anyway for all channels) \since 0.9.15 */
+pa_cvolume* pa_cvolume_set_fade(pa_cvolume *v, const pa_channel_map *map, float new_fade);
+
 /** Scale the passed pa_cvolume structure so that the maximum volume
  * of all channels equals max. The proportions between the channel
  * volumes are kept. \since 0.9.15 */