Bläddra i källkod

obs-filters/expander: Various improvements

Make envelope per channel.
Move ballistics (attack/release) to log domain and after gain stage.
cf Josh Reiss, Uni. London,
http://c4dm.eecs.qmul.ac.uk/audioengineering/compressors/documents/Reiss-Tutorialondynamicrangecompression.pdf
Thanks to Dedrick for very useful feedback.
pkv 6 år sedan
förälder
incheckning
41ddf83da8
1 ändrade filer med 90 tillägg och 78 borttagningar
  1. 90 78
      plugins/obs-filters/expander-filter.c

+ 90 - 78
plugins/obs-filters/expander-filter.c

@@ -65,7 +65,7 @@
 
 struct expander_data {
 	obs_source_t *context;
-	float *envelope_buf;
+	float *envelope_buf[MAX_AUDIO_CHANNELS];
 	size_t envelope_buf_len;
 
 	float ratio;
@@ -76,15 +76,16 @@ struct expander_data {
 
 	size_t num_channels;
 	size_t sample_rate;
-	float envelope;
+	float envelope[MAX_AUDIO_CHANNELS];
 	float slope;
 	int  detector;
-	float runave;
+	float runave[MAX_AUDIO_CHANNELS];
 	bool is_gate;
-	float *runaverage;
+	float *runaverage[MAX_AUDIO_CHANNELS];
 	size_t runaverage_len;
-	float *maxspl;
-	size_t maxspl_len;
+	float *gaindB[MAX_AUDIO_CHANNELS];
+	size_t gaindB_len;
+	float gaindB_buf[MAX_AUDIO_CHANNELS];
 	float *env_in;
 	size_t env_in_len;
 };
@@ -100,19 +101,17 @@ enum {
 static void resize_env_buffer(struct expander_data *cd, size_t len)
 {
 	cd->envelope_buf_len = len;
-	cd->envelope_buf = brealloc(cd->envelope_buf, len * sizeof(float));
+	for (int i = 0; i < MAX_AUDIO_CHANNELS; i++) {
+		cd->envelope_buf[i] = brealloc(cd->envelope_buf[i], len * sizeof(float));
+	}
 }
 
 static void resize_runaverage_buffer(struct expander_data *cd, size_t len)
 {
 	cd->runaverage_len = len;
-	cd->runaverage = brealloc(cd->runaverage, len * sizeof(float));
-}
-
-static void resize_maxspl_buffer(struct expander_data *cd, size_t len)
-{
-	cd->maxspl_len = len;
-	cd->maxspl = brealloc(cd->maxspl, len * sizeof(float));
+	for (int i = 0; i < MAX_AUDIO_CHANNELS; i++) {
+		cd->runaverage[i] = brealloc(cd->runaverage[i], len * sizeof(float));
+	}
 }
 
 static void resize_env_in_buffer(struct expander_data *cd, size_t len)
@@ -121,6 +120,14 @@ static void resize_env_in_buffer(struct expander_data *cd, size_t len)
 	cd->env_in = brealloc(cd->env_in, len * sizeof(float));
 }
 
+static void resize_gaindB_buffer(struct expander_data *cd, size_t len)
+{
+	cd->gaindB_len = len;
+	for (int i = 0; i < MAX_AUDIO_CHANNELS; i++) {
+		cd->gaindB[i] = brealloc(cd->gaindB[i], len * sizeof(float));
+	}
+}
+
 static inline float gain_coefficient(uint32_t sample_rate, float time)
 {
 	return (float)exp(-1.0f / (sample_rate * time));
@@ -195,25 +202,27 @@ static void expander_update(void *data, obs_data_t *s)
 		cd->detector = RMS_DETECT;
 	if (strcmp(detect_mode, "peak") == 0)
 		cd->detector = PEAK_DETECT;
-	if (strcmp(detect_mode, "none") == 0)
-		cd->detector = NO_DETECT;
 
 	size_t sample_len = sample_rate * DEFAULT_AUDIO_BUF_MS / MS_IN_S;
 	if (cd->envelope_buf_len == 0)
 		resize_env_buffer(cd, sample_len);
 	if (cd->runaverage_len == 0)
 		resize_runaverage_buffer(cd, sample_len);
-	if (cd->maxspl_len == 0)
-		resize_maxspl_buffer(cd, sample_len);
 	if (cd->env_in_len == 0)
 		resize_env_in_buffer(cd, sample_len);
+	if (cd->gaindB_len == 0)
+		resize_gaindB_buffer(cd, sample_len);
 }
 
 static void *expander_create(obs_data_t *settings, obs_source_t *filter)
 {
 	struct expander_data *cd = bzalloc(sizeof(struct expander_data));
 	cd->context = filter;
-	cd->runave = 0;
+	for (int i = 0; i < MAX_AUDIO_CHANNELS; i++) {
+		cd->runave[i] = 0;
+		cd->envelope[i] = 0;
+		cd->gaindB_buf[i] = 0;
+	}
 	cd->is_gate = false;
 	const char *presets = obs_data_get_string(settings, S_PRESETS);
 	if (strcmp(presets, "gate") == 0)
@@ -227,13 +236,16 @@ static void expander_destroy(void *data)
 {
 	struct expander_data *cd = data;
 
-	bfree(cd->envelope_buf);
-	bfree(cd->runaverage);
-	bfree(cd->maxspl);
+	for (int i = 0; i < MAX_AUDIO_CHANNELS; i++) {
+		bfree(cd->envelope_buf[i]);
+		bfree(cd->runaverage[i]);
+		bfree(cd->gaindB[i]);
+	}
 	bfree(cd->env_in);
 	bfree(cd);
 }
 
+// detection stage
 static void analyze_envelope(struct expander_data *cd,
 	float **samples, const uint32_t num_samples)
 {
@@ -243,93 +255,94 @@ static void analyze_envelope(struct expander_data *cd,
 	if (cd->runaverage_len < num_samples) {
 		resize_runaverage_buffer(cd, num_samples);
 	}
-	if (cd->maxspl_len < num_samples) {
-		resize_maxspl_buffer(cd, num_samples);
-	}
 	if (cd->env_in_len < num_samples) {
 		resize_env_in_buffer(cd, num_samples);
 	}
 
-	const float attack_gain = cd->attack_gain;
-	const float release_gain = cd->release_gain;
 	// 10 ms RMS window
 	const float rmscoef = exp2f((float)-100.0 / (float)cd->sample_rate);
-	// 2.5 microsec Peak window
-	const float peakcoef = exp2f((float)-1000.0 /((float)0.0025 *
-			(float)cd->sample_rate));
 
-	memset(cd->envelope_buf, 0, num_samples * sizeof(cd->envelope_buf[0]));
-	memset(cd->runaverage, 0, num_samples * sizeof(cd->runaverage[0]));
-	memset(cd->maxspl, 0, num_samples * sizeof(cd->maxspl[0]));
+	for (int i = 0; i < MAX_AUDIO_CHANNELS; i++) {
+		memset(cd->envelope_buf[i], 0, num_samples * sizeof(cd->envelope_buf[i][0]));
+		memset(cd->runaverage[i], 0, num_samples * sizeof(cd->runaverage[i][0]));
+	}
 	memset(cd->env_in, 0, num_samples * sizeof(cd->env_in[0]));
 
 	for (size_t chan = 0; chan < cd->num_channels; ++chan) {
 		if (!samples[chan])
 			continue;
 
-		float *envelope_buf = cd->envelope_buf;
-		float *runave = cd->runaverage;
-		float *maxspl = cd->maxspl;
+		float *envelope_buf = cd->envelope_buf[chan];
+		float *runave = cd->runaverage[chan];
 		float *env_in = cd->env_in;
-		float env = cd->envelope;
 		float RMSdet = 0;
 
-		runave[0] = cd->runave;
-		maxspl[0] = fabsf(samples[chan][0]);
-		env_in[0] = sqrtf(fmaxf(cd->runave, 0));
-
-		if (cd->detector == RMS_DETECT)
+		if (cd->detector == RMS_DETECT) {
+			runave[0] = rmscoef * cd->runave[chan] +
+				(1 - rmscoef) * powf(samples[chan][0], 2.0);
+			env_in[0] = sqrtf(fmaxf(runave[0], 0));
 			for (uint32_t i = 1; i < num_samples; ++i) {
 				runave[i] = rmscoef * runave[i - 1] +
-						(1 - rmscoef) *
-						powf(samples[chan][i], 2.0);
-				env_in[i] = sqrtf(runave[i]);
-			}
-		else if (cd->detector == PEAK_DETECT)
-			for (uint32_t i = 1; i < num_samples; ++i) {
-				maxspl[i] = powf(fmaxf(fabsf(maxspl[i - 1]),
-						fabsf(samples[chan][i])), 2);
-				runave[i] = peakcoef * runave[i - 1] +
-						(1 - peakcoef) * maxspl[i];
+					(1 - rmscoef) *
+					powf(samples[chan][i], 2.0);
 				env_in[i] = sqrtf(runave[i]);
 			}
-		else if (cd->detector == NO_DETECT)
-			for (uint32_t i = 1; i < num_samples; ++i) {
+		} else if (cd->detector == PEAK_DETECT) {
+			for (uint32_t i = 0; i < num_samples; ++i) {
 				runave[i] = powf(samples[chan][i], 2);
 				env_in[i] = fabsf(samples[chan][i]);
 			}
-		cd->runave = runave[num_samples-1];
-
-		for (uint32_t i = 0; i < num_samples; ++i) {
-
-			if (env < env_in[i]) {
-				env = env_in[i] + attack_gain
-						* (env - env_in[i]);
-			} else {
-				env = env_in[i] + release_gain
-						* (env - env_in[i]);
-			}
-			envelope_buf[i] = fmaxf(envelope_buf[i], env);
 		}
 
+		cd->runave[chan] = runave[num_samples-1];
+		for (uint32_t i = 0; i < num_samples; ++i)
+			envelope_buf[i] = fmaxf(envelope_buf[i], env_in[i]);
+		cd->envelope[chan] = cd->envelope_buf[chan][num_samples - 1];
 	}
-	cd->envelope = cd->envelope_buf[num_samples - 1];
 }
 
-static inline void process_expansion(const struct expander_data *cd,
+// gain stage and ballistics in dB domain
+static inline void process_expansion(struct expander_data *cd,
 	float **samples, uint32_t num_samples)
 {
-	for (size_t i = 0; i < num_samples; ++i) {
-		float env_db = mul_to_db(cd->envelope_buf[i]);
-		float gain = fmaxf(cd->slope * (cd->threshold - env_db),
-				-60.0);
-		gain = db_to_mul(fminf(0, gain));
-
-		for (size_t c = 0; c < cd->num_channels; ++c) {
-			if (samples[c]) {
-				samples[c][i] *= gain * cd->output_gain;
+
+	const float attack_gain = cd->attack_gain;
+	const float release_gain = cd->release_gain;
+
+	if (cd->gaindB_len < num_samples)
+		resize_gaindB_buffer(cd, num_samples);
+	for (int i = 0; i < MAX_AUDIO_CHANNELS; i++)
+		memset(cd->gaindB[i], 0, num_samples * sizeof(cd->gaindB[i][0]));
+
+	for (size_t chan = 0; chan < cd->num_channels; chan++) {
+		for (size_t i = 0; i < num_samples; ++i) {
+			// gain stage of expansion
+			float env_db = mul_to_db(cd->envelope_buf[chan][i]);
+			float gain = cd->threshold - env_db > 0 ?
+					fmaxf(cd->slope * (cd->threshold - env_db),
+					-60.0) : 0;
+			// ballistics (attack/release)
+			if (i > 0) {
+				if (gain > cd->gaindB[chan][i - 1])
+					cd->gaindB[chan][i] = attack_gain * cd->gaindB[chan][i - 1]
+					+ (1 - attack_gain) * gain;
+				else
+					cd->gaindB[chan][i] = release_gain * cd->gaindB[chan][i - 1]
+					+ (1 - release_gain) * gain;
+			} else {
+				if (gain > cd->gaindB_buf[chan])
+					cd->gaindB[chan][i] = attack_gain * cd->gaindB_buf[chan]
+					+ (1 - attack_gain) * gain;
+				else
+					cd->gaindB[chan][i] = release_gain * cd->gaindB_buf[chan]
+					+ (1 - release_gain) * gain;
 			}
+
+			gain = db_to_mul(fminf(0, cd->gaindB[chan][i]));
+			if (samples[chan])
+				samples[chan][i] *= gain * cd->output_gain;
 		}
+		cd->gaindB_buf[chan] = cd->gaindB[chan][num_samples - 1];
 	}
 }
 
@@ -390,7 +403,6 @@ static obs_properties_t *expander_properties(void *data)
 			OBS_COMBO_FORMAT_STRING);
 	obs_property_list_add_string(detect, TEXT_RMS, "RMS");
 	obs_property_list_add_string(detect, TEXT_PEAK, "peak");
-	obs_property_list_add_string(detect, TEXT_NONE, "none");
 
 	UNUSED_PARAMETER(data);
 	return props;