|
@@ -16,6 +16,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
*/
|
|
|
|
|
|
#include <math.h>
|
|
#include <math.h>
|
|
|
|
+#include <xmmintrin.h>
|
|
|
|
|
|
#include "util/threading.h"
|
|
#include "util/threading.h"
|
|
#include "util/bmem.h"
|
|
#include "util/bmem.h"
|
|
@@ -62,18 +63,20 @@ struct meter_cb {
|
|
};
|
|
};
|
|
|
|
|
|
struct obs_volmeter {
|
|
struct obs_volmeter {
|
|
- pthread_mutex_t mutex;
|
|
|
|
- obs_source_t *source;
|
|
|
|
- enum obs_fader_type type;
|
|
|
|
- float cur_db;
|
|
|
|
|
|
+ pthread_mutex_t mutex;
|
|
|
|
+ obs_source_t *source;
|
|
|
|
+ enum obs_fader_type type;
|
|
|
|
+ float cur_db;
|
|
|
|
|
|
- pthread_mutex_t callback_mutex;
|
|
|
|
- DARRAY(struct meter_cb)callbacks;
|
|
|
|
|
|
+ pthread_mutex_t callback_mutex;
|
|
|
|
+ DARRAY(struct meter_cb) callbacks;
|
|
|
|
|
|
- unsigned int update_ms;
|
|
|
|
|
|
+ enum obs_peak_meter_type peak_meter_type;
|
|
|
|
+ unsigned int update_ms;
|
|
|
|
+ float prev_samples[MAX_AUDIO_CHANNELS][4];
|
|
|
|
|
|
- float vol_magnitude[MAX_AUDIO_CHANNELS];
|
|
|
|
- float vol_peak[MAX_AUDIO_CHANNELS];
|
|
|
|
|
|
+ float magnitude[MAX_AUDIO_CHANNELS];
|
|
|
|
+ float peak[MAX_AUDIO_CHANNELS];
|
|
};
|
|
};
|
|
|
|
|
|
static float cubic_def_to_db(const float def)
|
|
static float cubic_def_to_db(const float def)
|
|
@@ -256,48 +259,256 @@ static void volmeter_source_destroyed(void *vptr, calldata_t *calldata)
|
|
obs_volmeter_detach_source(volmeter);
|
|
obs_volmeter_detach_source(volmeter);
|
|
}
|
|
}
|
|
|
|
|
|
-static void volmeter_process_audio_data(obs_volmeter_t *volmeter,
|
|
|
|
- const struct audio_data *data)
|
|
|
|
|
|
+static int get_nr_channels_from_audio_data(const struct audio_data *data)
|
|
|
|
+{
|
|
|
|
+ int nr_channels = 0;
|
|
|
|
+ for (int i = 0; i < MAX_AV_PLANES; i++) {
|
|
|
|
+ if (data->data[i])
|
|
|
|
+ nr_channels++;
|
|
|
|
+ }
|
|
|
|
+ return CLAMP(nr_channels, 0, MAX_AUDIO_CHANNELS);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* msb(h, g, f, e) lsb(d, c, b, a) --> msb(h, h, g, f) lsb(e, d, c, b)
|
|
|
|
+ */
|
|
|
|
+#define SHIFT_RIGHT_2PS(msb, lsb) {\
|
|
|
|
+ __m128 tmp = _mm_shuffle_ps(lsb, msb, _MM_SHUFFLE(0, 0, 3, 3));\
|
|
|
|
+ lsb = _mm_shuffle_ps(lsb, tmp, _MM_SHUFFLE(2, 1, 2, 1));\
|
|
|
|
+ msb = _mm_shuffle_ps(msb, msb, _MM_SHUFFLE(3, 3, 2, 1));\
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* x(d, c, b, a) --> (|d|, |c|, |b|, |a|)
|
|
|
|
+ */
|
|
|
|
+static inline __m128 abs_ps(__m128 v)
|
|
|
|
+{
|
|
|
|
+ return _mm_andnot_ps(_mm_set1_ps(-0.f), v);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Take cross product of a vector with a matrix resulting in vector.
|
|
|
|
+ */
|
|
|
|
+#define VECTOR_MATRIX_CROSS_PS(out, v, m0, m1, m2, m3) \
|
|
|
|
+{\
|
|
|
|
+ out = _mm_mul_ps(v, m0);\
|
|
|
|
+ __m128 mul1 = _mm_mul_ps(v, m1);\
|
|
|
|
+ __m128 mul2 = _mm_mul_ps(v, m2);\
|
|
|
|
+ __m128 mul3 = _mm_mul_ps(v, m3);\
|
|
|
|
+\
|
|
|
|
+ _MM_TRANSPOSE4_PS(out, mul1, mul2, mul3);\
|
|
|
|
+\
|
|
|
|
+ out = _mm_add_ps(out, mul1);\
|
|
|
|
+ out = _mm_add_ps(out, mul2);\
|
|
|
|
+ out = _mm_add_ps(out, mul3);\
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* x4(d, c, b, a) --> max(a, b, c, d)
|
|
|
|
+ */
|
|
|
|
+inline float hmax_ps(__m128 x4)
|
|
|
|
+{
|
|
|
|
+ float x4_mem[4];
|
|
|
|
+ _mm_store_ps(x4_mem, x4);
|
|
|
|
+
|
|
|
|
+ float r = x4_mem[0];
|
|
|
|
+ r = fmaxf(r, x4_mem[1]);
|
|
|
|
+ r = fmaxf(r, x4_mem[2]);
|
|
|
|
+ r = fmaxf(r, x4_mem[3]);
|
|
|
|
+ return r;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Calculate the true peak over a set of samples.
|
|
|
|
+ * The algorithm implements 5x oversampling by using Whittaker–Shannon
|
|
|
|
+ * interpolation over four samples.
|
|
|
|
+ *
|
|
|
|
+ * The four samples have location t=-1.5, -0.5, +0.5, +1.5
|
|
|
|
+ * The oversamples are taken at locations t=-0.3, -0.1, +0.1, +0.3
|
|
|
|
+ *
|
|
|
|
+ * @param previous_samples Last 4 samples from the previous iteration.
|
|
|
|
+ * @param samples The samples to find the peak in.
|
|
|
|
+ * @param nr_samples Number of sets of 4 samples.
|
|
|
|
+ * @returns 5 times oversampled true-peak from the set of samples.
|
|
|
|
+ */
|
|
|
|
+static float get_true_peak(__m128 previous_samples, const float *samples,
|
|
|
|
+ size_t nr_samples)
|
|
|
|
+{
|
|
|
|
+ /* These are normalized-sinc parameters for interpolating over sample
|
|
|
|
+ * points which are located at x-coords: -1.5, -0.5, +0.5, +1.5.
|
|
|
|
+ * And oversample points at x-coords: -0.3, -0.1, 0.1, 0.3. */
|
|
|
|
+ const __m128 m3 = _mm_set_ps(-0.155915f, 0.935489f, 0.233872f, -0.103943f);
|
|
|
|
+ const __m128 m1 = _mm_set_ps(-0.216236f, 0.756827f, 0.504551f, -0.189207f);
|
|
|
|
+ const __m128 p1 = _mm_set_ps(-0.189207f, 0.504551f, 0.756827f, -0.216236f);
|
|
|
|
+ const __m128 p3 = _mm_set_ps(-0.103943f, 0.233872f, 0.935489f, -0.155915f);
|
|
|
|
+
|
|
|
|
+ __m128 work = previous_samples;
|
|
|
|
+ __m128 peak = previous_samples;
|
|
|
|
+ for (size_t i = 0; (i + 3) < nr_samples; i += 4) {
|
|
|
|
+ __m128 new_work = _mm_load_ps(&samples[i]);
|
|
|
|
+ __m128 intrp_samples;
|
|
|
|
+
|
|
|
|
+ /* Include the actual sample values in the peak. */
|
|
|
|
+ __m128 abs_new_work = abs_ps(new_work);
|
|
|
|
+ peak = _mm_max_ps(peak, abs_new_work);
|
|
|
|
+
|
|
|
|
+ /* Shift in the next point. */
|
|
|
|
+ SHIFT_RIGHT_2PS(new_work, work);
|
|
|
|
+ VECTOR_MATRIX_CROSS_PS(intrp_samples, work, m3, m1, p1, p3);
|
|
|
|
+ peak = _mm_max_ps(peak, abs_ps(intrp_samples));
|
|
|
|
+
|
|
|
|
+ SHIFT_RIGHT_2PS(new_work, work);
|
|
|
|
+ VECTOR_MATRIX_CROSS_PS(intrp_samples, work, m3, m1, p1, p3);
|
|
|
|
+ peak = _mm_max_ps(peak, abs_ps(intrp_samples));
|
|
|
|
+
|
|
|
|
+ SHIFT_RIGHT_2PS(new_work, work);
|
|
|
|
+ VECTOR_MATRIX_CROSS_PS(intrp_samples, work, m3, m1, p1, p3);
|
|
|
|
+ peak = _mm_max_ps(peak, abs_ps(intrp_samples));
|
|
|
|
+
|
|
|
|
+ SHIFT_RIGHT_2PS(new_work, work);
|
|
|
|
+ VECTOR_MATRIX_CROSS_PS(intrp_samples, work, m3, m1, p1, p3);
|
|
|
|
+ peak = _mm_max_ps(peak, abs_ps(intrp_samples));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return hmax_ps(peak);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* points contain the first four samples to calculate the sinc interpolation
|
|
|
|
+ * over. They will have come from a previous iteration.
|
|
|
|
+ */
|
|
|
|
+static float get_sample_peak(__m128 previous_samples, const float *samples,
|
|
|
|
+ size_t nr_samples)
|
|
|
|
+{
|
|
|
|
+ __m128 peak = previous_samples;
|
|
|
|
+ for (size_t i = 0; (i + 3) < nr_samples; i += 4) {
|
|
|
|
+ __m128 new_work = _mm_load_ps(&samples[i]);
|
|
|
|
+ peak = _mm_max_ps(peak, abs_ps(new_work));
|
|
|
|
+ }
|
|
|
|
+ return hmax_ps(peak);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void volmeter_process_peak_last_samples(obs_volmeter_t *volmeter,
|
|
|
|
+ int channel_nr, float *samples, size_t nr_samples)
|
|
|
|
+{
|
|
|
|
+ /* Take the last 4 samples that need to be used for the next peak
|
|
|
|
+ * calculation. If there are less than 4 samples in total the new
|
|
|
|
+ * samples shift out the old samples. */
|
|
|
|
+
|
|
|
|
+ switch (nr_samples) {
|
|
|
|
+ case 0:
|
|
|
|
+ break;
|
|
|
|
+ case 1:
|
|
|
|
+ volmeter->prev_samples[channel_nr][0] =
|
|
|
|
+ volmeter->prev_samples[channel_nr][1];
|
|
|
|
+ volmeter->prev_samples[channel_nr][1] =
|
|
|
|
+ volmeter->prev_samples[channel_nr][2];
|
|
|
|
+ volmeter->prev_samples[channel_nr][2] =
|
|
|
|
+ volmeter->prev_samples[channel_nr][3];
|
|
|
|
+ volmeter->prev_samples[channel_nr][3] = samples[nr_samples-1];
|
|
|
|
+ break;
|
|
|
|
+ case 2:
|
|
|
|
+ volmeter->prev_samples[channel_nr][0] =
|
|
|
|
+ volmeter->prev_samples[channel_nr][2];
|
|
|
|
+ volmeter->prev_samples[channel_nr][1] =
|
|
|
|
+ volmeter->prev_samples[channel_nr][3];
|
|
|
|
+ volmeter->prev_samples[channel_nr][2] = samples[nr_samples-2];
|
|
|
|
+ volmeter->prev_samples[channel_nr][3] = samples[nr_samples-1];
|
|
|
|
+ break;
|
|
|
|
+ case 3:
|
|
|
|
+ volmeter->prev_samples[channel_nr][0] =
|
|
|
|
+ volmeter->prev_samples[channel_nr][3];
|
|
|
|
+ volmeter->prev_samples[channel_nr][1] = samples[nr_samples-3];
|
|
|
|
+ volmeter->prev_samples[channel_nr][2] = samples[nr_samples-2];
|
|
|
|
+ volmeter->prev_samples[channel_nr][3] = samples[nr_samples-1];
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ volmeter->prev_samples[channel_nr][0] = samples[nr_samples-4];
|
|
|
|
+ volmeter->prev_samples[channel_nr][1] = samples[nr_samples-3];
|
|
|
|
+ volmeter->prev_samples[channel_nr][2] = samples[nr_samples-2];
|
|
|
|
+ volmeter->prev_samples[channel_nr][3] = samples[nr_samples-1];
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void volmeter_process_peak(obs_volmeter_t *volmeter,
|
|
|
|
+ const struct audio_data *data, int nr_channels)
|
|
{
|
|
{
|
|
int nr_samples = data->frames;
|
|
int nr_samples = data->frames;
|
|
int channel_nr = 0;
|
|
int channel_nr = 0;
|
|
-
|
|
|
|
- for (size_t plane_nr = 0; plane_nr < MAX_AV_PLANES; plane_nr++) {
|
|
|
|
|
|
+ for (int plane_nr = 0; channel_nr < nr_channels; plane_nr++) {
|
|
float *samples = (float *)data->data[plane_nr];
|
|
float *samples = (float *)data->data[plane_nr];
|
|
if (!samples) {
|
|
if (!samples) {
|
|
- // This plane does not contain data.
|
|
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
+ if (((uintptr_t)samples & 0xf) > 0) {
|
|
|
|
+ printf("Audio plane %i is not aligned %p skipping "
|
|
|
|
+ "peak volume measurement.\n",
|
|
|
|
+ plane_nr, samples);
|
|
|
|
+ volmeter->peak[channel_nr] = 1.0;
|
|
|
|
+ channel_nr++;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* volmeter->prev_samples may not be aligned to 16 bytes;
|
|
|
|
+ * use unaligned load. */
|
|
|
|
+ __m128 previous_samples = _mm_loadu_ps(
|
|
|
|
+ volmeter->prev_samples[channel_nr]);
|
|
|
|
+
|
|
|
|
+ float peak;
|
|
|
|
+ switch (volmeter->peak_meter_type) {
|
|
|
|
+ case TRUE_PEAK_METER:
|
|
|
|
+ peak = get_true_peak(previous_samples, samples,
|
|
|
|
+ nr_samples);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case SAMPLE_PEAK_METER:
|
|
|
|
+ default:
|
|
|
|
+ peak = get_sample_peak(previous_samples, samples,
|
|
|
|
+ nr_samples);
|
|
|
|
+ break;
|
|
|
|
|
|
- // For each plane calculate:
|
|
|
|
- // * peak = the maximum-absolute of the sample values.
|
|
|
|
- // * magnitude = root-mean-square of the sample values.
|
|
|
|
- // A VU meter needs to integrate over 300ms, but this will
|
|
|
|
- // be handled by the ballistics of the meter itself,
|
|
|
|
- // reality. Which makes this calculation independent of
|
|
|
|
- // sample rate or update rate.
|
|
|
|
- float peak = 0.0;
|
|
|
|
- float sum_of_squares = 0.0;
|
|
|
|
- for (int sample_nr = 0; sample_nr < nr_samples; sample_nr++) {
|
|
|
|
- float sample = samples[sample_nr];
|
|
|
|
-
|
|
|
|
- peak = fmaxf(peak, fabsf(sample));
|
|
|
|
- sum_of_squares += (sample * sample);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- volmeter->vol_magnitude[channel_nr] = sqrtf(sum_of_squares /
|
|
|
|
|
|
+ volmeter_process_peak_last_samples(volmeter, channel_nr, samples,
|
|
nr_samples);
|
|
nr_samples);
|
|
- volmeter->vol_peak[channel_nr] = peak;
|
|
|
|
|
|
+
|
|
|
|
+ volmeter->peak[channel_nr] = peak;
|
|
|
|
+
|
|
channel_nr++;
|
|
channel_nr++;
|
|
}
|
|
}
|
|
|
|
|
|
- // Clear audio channels that are not in use.
|
|
|
|
|
|
+ /* Clear the peak of the channels that have not been handled. */
|
|
for (; channel_nr < MAX_AUDIO_CHANNELS; channel_nr++) {
|
|
for (; channel_nr < MAX_AUDIO_CHANNELS; channel_nr++) {
|
|
- volmeter->vol_magnitude[channel_nr] = 0.0;
|
|
|
|
- volmeter->vol_peak[channel_nr] = 0.0;
|
|
|
|
|
|
+ volmeter->peak[channel_nr] = 0.0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+static void volmeter_process_magnitude(obs_volmeter_t *volmeter,
|
|
|
|
+ const struct audio_data *data, int nr_channels)
|
|
|
|
+{
|
|
|
|
+ size_t nr_samples = data->frames;
|
|
|
|
+
|
|
|
|
+ int channel_nr = 0;
|
|
|
|
+ for (int plane_nr = 0; channel_nr < nr_channels; plane_nr++) {
|
|
|
|
+ float *samples = (float *)data->data[plane_nr];
|
|
|
|
+ if (!samples) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ float sum = 0.0;
|
|
|
|
+ for (size_t i = 0; i < nr_samples; i++) {
|
|
|
|
+ float sample = samples[i];
|
|
|
|
+ sum += sample * sample;
|
|
|
|
+ }
|
|
|
|
+ volmeter->magnitude[channel_nr] = sqrtf(sum / nr_samples);
|
|
|
|
+
|
|
|
|
+ channel_nr++;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void volmeter_process_audio_data(obs_volmeter_t *volmeter,
|
|
|
|
+ const struct audio_data *data)
|
|
|
|
+{
|
|
|
|
+ int nr_channels = get_nr_channels_from_audio_data(data);
|
|
|
|
+
|
|
|
|
+ volmeter_process_peak(volmeter, data, nr_channels);
|
|
|
|
+ volmeter_process_magnitude(volmeter, data, nr_channels);
|
|
|
|
+}
|
|
|
|
+
|
|
static void volmeter_source_data_received(void *vptr, obs_source_t *source,
|
|
static void volmeter_source_data_received(void *vptr, obs_source_t *source,
|
|
const struct audio_data *data, bool muted)
|
|
const struct audio_data *data, bool muted)
|
|
{
|
|
{
|
|
@@ -317,16 +528,16 @@ static void volmeter_source_data_received(void *vptr, obs_source_t *source,
|
|
for (int channel_nr = 0; channel_nr < MAX_AUDIO_CHANNELS;
|
|
for (int channel_nr = 0; channel_nr < MAX_AUDIO_CHANNELS;
|
|
channel_nr++) {
|
|
channel_nr++) {
|
|
magnitude[channel_nr] = mul_to_db(
|
|
magnitude[channel_nr] = mul_to_db(
|
|
- volmeter->vol_magnitude[channel_nr] * mul);
|
|
|
|
|
|
+ volmeter->magnitude[channel_nr] * mul);
|
|
peak[channel_nr] = mul_to_db(
|
|
peak[channel_nr] = mul_to_db(
|
|
- volmeter->vol_peak[channel_nr] * mul);
|
|
|
|
|
|
+ volmeter->peak[channel_nr] * mul);
|
|
|
|
+
|
|
|
|
+ /* The input-peak is NOT adjusted with volume, so that the user
|
|
|
|
+ * can check the input-gain. */
|
|
input_peak[channel_nr] = mul_to_db(
|
|
input_peak[channel_nr] = mul_to_db(
|
|
- volmeter->vol_peak[channel_nr]);
|
|
|
|
|
|
+ volmeter->peak[channel_nr]);
|
|
}
|
|
}
|
|
|
|
|
|
- // The input-peak is NOT adjusted with volume, so that the user
|
|
|
|
- // can check the input-gain.
|
|
|
|
-
|
|
|
|
pthread_mutex_unlock(&volmeter->mutex);
|
|
pthread_mutex_unlock(&volmeter->mutex);
|
|
|
|
|
|
signal_levels_updated(volmeter, magnitude, peak, input_peak);
|
|
signal_levels_updated(volmeter, magnitude, peak, input_peak);
|
|
@@ -641,6 +852,14 @@ void obs_volmeter_detach_source(obs_volmeter_t *volmeter)
|
|
volmeter_source_data_received, volmeter);
|
|
volmeter_source_data_received, volmeter);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+void obs_volmeter_set_peak_meter_type(obs_volmeter_t *volmeter,
|
|
|
|
+ enum obs_peak_meter_type peak_meter_type)
|
|
|
|
+{
|
|
|
|
+ pthread_mutex_lock(&volmeter->mutex);
|
|
|
|
+ volmeter->peak_meter_type = peak_meter_type;
|
|
|
|
+ pthread_mutex_unlock(&volmeter->mutex);
|
|
|
|
+}
|
|
|
|
+
|
|
void obs_volmeter_set_update_interval(obs_volmeter_t *volmeter,
|
|
void obs_volmeter_set_update_interval(obs_volmeter_t *volmeter,
|
|
const unsigned int ms)
|
|
const unsigned int ms)
|
|
{
|
|
{
|