static gboolean gst_audio_fx_base_fir_filter_query (GstBaseTransform * trans, GstPadDirection direction, GstQuery * query) { GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (trans); gboolean res = TRUE; switch (GST_QUERY_TYPE (query)) { case GST_QUERY_LATENCY: { GstClockTime min, max; gboolean live; guint64 latency; gint rate = GST_AUDIO_FILTER_RATE (self); if (rate == 0) { res = FALSE; } else if ((res = gst_pad_peer_query (GST_BASE_TRANSFORM (self)->sinkpad, query))) { gst_query_parse_latency (query, &live, &min, &max); GST_DEBUG_OBJECT (self, "Peer latency: min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT, GST_TIME_ARGS (min), GST_TIME_ARGS (max)); if (self->fft && !self->low_latency) latency = self->block_length - self->kernel_length + 1; else latency = self->latency; /* add our own latency */ latency = gst_util_uint64_scale_round (latency, GST_SECOND, rate); GST_DEBUG_OBJECT (self, "Our latency: %" GST_TIME_FORMAT, GST_TIME_ARGS (latency)); min += latency; if (max != GST_CLOCK_TIME_NONE) max += latency; GST_DEBUG_OBJECT (self, "Calculated total latency : min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT, GST_TIME_ARGS (min), GST_TIME_ARGS (max)); gst_query_set_latency (query, live, min, max); } break; } default: res = GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction, query); break; } return res; }
/* get notified of caps and plug in the correct process function */ static gboolean gst_audio_iir_filter_setup (GstAudioFilter * base, const GstAudioInfo * info) { GstAudioIIRFilter *self = GST_AUDIO_IIR_FILTER (base); gint new_rate = GST_AUDIO_INFO_RATE (info); if (GST_AUDIO_FILTER_RATE (self) != new_rate) { g_signal_emit (G_OBJECT (self), gst_audio_iir_filter_signals[SIGNAL_RATE_CHANGED], 0, new_rate); } return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info); }
/* GstBaseTransform vmethod implementations */ static GstFlowReturn gst_audio_echo_transform_ip (GstBaseTransform * base, GstBuffer * buf) { GstAudioEcho *self = GST_AUDIO_ECHO (base); guint num_samples; GstClockTime timestamp, stream_time; GstMapInfo map; g_mutex_lock (&self->lock); timestamp = GST_BUFFER_TIMESTAMP (buf); stream_time = gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp); GST_DEBUG_OBJECT (self, "sync to %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp)); if (GST_CLOCK_TIME_IS_VALID (stream_time)) gst_object_sync_values (GST_OBJECT (self), stream_time); if (self->buffer == NULL) { guint bpf, rate; bpf = GST_AUDIO_FILTER_BPF (self); rate = GST_AUDIO_FILTER_RATE (self); self->delay_frames = MAX (gst_util_uint64_scale (self->delay, rate, GST_SECOND), 1); self->buffer_size_frames = MAX (gst_util_uint64_scale (self->max_delay, rate, GST_SECOND), 1); self->buffer_size = self->buffer_size_frames * bpf; self->buffer = g_try_malloc0 (self->buffer_size); self->buffer_pos = 0; if (self->buffer == NULL) { g_mutex_unlock (&self->lock); GST_ERROR_OBJECT (self, "Failed to allocate %u bytes", self->buffer_size); return GST_FLOW_ERROR; } } gst_buffer_map (buf, &map, GST_MAP_READWRITE); num_samples = map.size / GST_AUDIO_FILTER_BPS (self); self->process (self, map.data, num_samples); gst_buffer_unmap (buf, &map); g_mutex_unlock (&self->lock); return GST_FLOW_OK; }
static void setup_high_shelf_filter (GstIirEqualizer * equ, GstIirEqualizerBand * band) { gint rate = GST_AUDIO_FILTER_RATE (equ); g_return_if_fail (rate); { gdouble gain, omega, bw; gdouble alpha, delta, b0; gdouble egp, egm; gain = arg_to_scale (band->gain); omega = calculate_omega (band->freq, rate); bw = calculate_bw (band, rate); if (bw == 0.0) goto out; egm = gain - 1.0; egp = gain + 1.0; alpha = tan (bw / 2.0); delta = 2.0 * sqrt (gain) * alpha; b0 = egp - egm * cos (omega) + delta; band->a0 = ((egp + egm * cos (omega) + delta) * gain) / b0; band->a1 = ((egm + egp * cos (omega)) * -2.0 * gain) / b0; band->a2 = ((egp + egm * cos (omega) - delta) * gain) / b0; band->b1 = ((egm - egp * cos (omega)) * -2.0) / b0; band->b2 = -((egp - egm * cos (omega) - delta)) / b0; out: GST_INFO ("gain = %5.1f, width= %7.2f, freq = %7.2f, a0 = %7.5g, a1 = %7.5g, a2=%7.5g b1 = %7.5g, b2 = %7.5g", band->gain, band->width, band->freq, band->a0, band->a1, band->a2, band->b1, band->b2); } }
static void setup_peak_filter (GstIirEqualizer * equ, GstIirEqualizerBand * band) { gint rate = GST_AUDIO_FILTER_RATE (equ); g_return_if_fail (rate); { gdouble gain, omega, bw; gdouble alpha, alpha1, alpha2, b0; gain = arg_to_scale (band->gain); omega = calculate_omega (band->freq, rate); bw = calculate_bw (band, rate); if (bw == 0.0) goto out; alpha = tan (bw / 2.0); alpha1 = alpha * gain; alpha2 = alpha / gain; b0 = (1.0 + alpha2); band->a0 = (1.0 + alpha1) / b0; band->a1 = (-2.0 * cos (omega)) / b0; band->a2 = (1.0 - alpha1) / b0; band->b1 = (2.0 * cos (omega)) / b0; band->b2 = -(1.0 - alpha2) / b0; out: GST_INFO ("gain = %5.1f, width= %7.2f, freq = %7.2f, a0 = %7.5g, a1 = %7.5g, a2=%7.5g b1 = %7.5g, b2 = %7.5g", band->gain, band->width, band->freq, band->a0, band->a1, band->a2, band->b1, band->b2); } }
static GstFlowReturn gst_audio_fx_base_fir_filter_transform (GstBaseTransform * base, GstBuffer * inbuf, GstBuffer * outbuf) { GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base); GstClockTime timestamp, expected_timestamp; gint channels = GST_AUDIO_FILTER_CHANNELS (self); gint rate = GST_AUDIO_FILTER_RATE (self); gint bps = GST_AUDIO_FILTER_BPS (self); GstMapInfo inmap, outmap; guint input_samples; guint output_samples; guint generated_samples; guint64 output_offset; gint64 diff = 0; GstClockTime stream_time; timestamp = GST_BUFFER_TIMESTAMP (outbuf); if (!GST_CLOCK_TIME_IS_VALID (timestamp) && !GST_CLOCK_TIME_IS_VALID (self->start_ts)) { GST_ERROR_OBJECT (self, "Invalid timestamp"); return GST_FLOW_ERROR; } g_mutex_lock (&self->lock); stream_time = gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp); GST_DEBUG_OBJECT (self, "sync to %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp)); if (GST_CLOCK_TIME_IS_VALID (stream_time)) gst_object_sync_values (GST_OBJECT (self), stream_time); g_return_val_if_fail (self->kernel != NULL, GST_FLOW_ERROR); g_return_val_if_fail (channels != 0, GST_FLOW_ERROR); if (GST_CLOCK_TIME_IS_VALID (self->start_ts)) expected_timestamp = self->start_ts + gst_util_uint64_scale_int (self->nsamples_in, GST_SECOND, rate); else expected_timestamp = GST_CLOCK_TIME_NONE; /* Reset the residue if already existing on discont buffers */ if (GST_BUFFER_IS_DISCONT (inbuf) || (GST_CLOCK_TIME_IS_VALID (expected_timestamp) && (ABS (GST_CLOCK_DIFF (timestamp, expected_timestamp) > 5 * GST_MSECOND)))) { GST_DEBUG_OBJECT (self, "Discontinuity detected - flushing"); if (GST_CLOCK_TIME_IS_VALID (expected_timestamp)) gst_audio_fx_base_fir_filter_push_residue (self); self->buffer_fill = 0; g_free (self->buffer); self->buffer = NULL; self->start_ts = timestamp; self->start_off = GST_BUFFER_OFFSET (inbuf); self->nsamples_out = 0; self->nsamples_in = 0; } else if (!GST_CLOCK_TIME_IS_VALID (self->start_ts)) { self->start_ts = timestamp; self->start_off = GST_BUFFER_OFFSET (inbuf); } gst_buffer_map (inbuf, &inmap, GST_MAP_READ); gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE); input_samples = (inmap.size / bps) / channels; output_samples = (outmap.size / bps) / channels; self->nsamples_in += input_samples; generated_samples = self->process (self, inmap.data, outmap.data, input_samples); gst_buffer_unmap (inbuf, &inmap); gst_buffer_unmap (outbuf, &outmap); g_assert (generated_samples <= output_samples); self->nsamples_out += generated_samples; if (generated_samples == 0) goto no_samples; /* Calculate the number of samples we can push out now without outputting * latency zeros in the beginning */ diff = ((gint64) self->nsamples_out) - ((gint64) self->latency); if (diff < 0) goto no_samples; if (diff < generated_samples) { gint64 tmp = diff; diff = generated_samples - diff; generated_samples = tmp; } else { diff = 0; } gst_buffer_resize (outbuf, diff * bps * channels, generated_samples * bps * channels); output_offset = self->nsamples_out - self->latency - generated_samples; GST_BUFFER_TIMESTAMP (outbuf) = self->start_ts + gst_util_uint64_scale_int (output_offset, GST_SECOND, rate); GST_BUFFER_DURATION (outbuf) = gst_util_uint64_scale_int (output_samples, GST_SECOND, rate); if (self->start_off != GST_BUFFER_OFFSET_NONE) { GST_BUFFER_OFFSET (outbuf) = self->start_off + output_offset; GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET (outbuf) + generated_samples; } else { GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE; GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE; } g_mutex_unlock (&self->lock); GST_DEBUG_OBJECT (self, "Pushing buffer of size %" G_GSIZE_FORMAT " with timestamp: %" GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %" G_GUINT64_FORMAT ", offset_end: %" G_GUINT64_FORMAT ", nsamples_out: %d", gst_buffer_get_size (outbuf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)), GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf), generated_samples); return GST_FLOW_OK; no_samples: { g_mutex_unlock (&self->lock); return GST_BASE_TRANSFORM_FLOW_DROPPED; } }
void gst_audio_fx_base_fir_filter_push_residue (GstAudioFXBaseFIRFilter * self) { GstBuffer *outbuf; GstFlowReturn res; gint rate = GST_AUDIO_FILTER_RATE (self); gint channels = GST_AUDIO_FILTER_CHANNELS (self); gint bps = GST_AUDIO_FILTER_BPS (self); gint outsize, outsamples; GstMapInfo map; guint8 *in, *out; if (channels == 0 || rate == 0 || self->nsamples_in == 0) { self->buffer_fill = 0; g_free (self->buffer); self->buffer = NULL; return; } /* Calculate the number of samples and their memory size that * should be pushed from the residue */ outsamples = self->nsamples_in - (self->nsamples_out - self->latency); if (outsamples <= 0) { self->buffer_fill = 0; g_free (self->buffer); self->buffer = NULL; return; } outsize = outsamples * channels * bps; if (!self->fft || self->low_latency) { gint64 diffsize, diffsamples; /* Process the difference between latency and residue length samples * to start at the actual data instead of starting at the zeros before * when we only got one buffer smaller than latency */ diffsamples = ((gint64) self->latency) - ((gint64) self->buffer_fill) / channels; if (diffsamples > 0) { diffsize = diffsamples * channels * bps; in = g_new0 (guint8, diffsize); out = g_new0 (guint8, diffsize); self->nsamples_out += self->process (self, in, out, diffsamples); g_free (in); g_free (out); } outbuf = gst_buffer_new_and_alloc (outsize); /* Convolve the residue with zeros to get the actual remaining data */ in = g_new0 (guint8, outsize); gst_buffer_map (outbuf, &map, GST_MAP_READWRITE); self->nsamples_out += self->process (self, in, map.data, outsamples); gst_buffer_unmap (outbuf, &map); g_free (in); } else { guint gensamples = 0; outbuf = gst_buffer_new_and_alloc (outsize); gst_buffer_map (outbuf, &map, GST_MAP_READWRITE); while (gensamples < outsamples) { guint step_insamples = self->block_length - self->buffer_fill; guint8 *zeroes = g_new0 (guint8, step_insamples * channels * bps); guint8 *out = g_new (guint8, self->block_length * channels * bps); guint step_gensamples; step_gensamples = self->process (self, zeroes, out, step_insamples); g_free (zeroes); memcpy (map.data + gensamples * bps, out, MIN (step_gensamples, outsamples - gensamples) * bps); gensamples += MIN (step_gensamples, outsamples - gensamples); g_free (out); } self->nsamples_out += gensamples; gst_buffer_unmap (outbuf, &map); } /* Set timestamp, offset, etc from the values we * saved when processing the regular buffers */ if (GST_CLOCK_TIME_IS_VALID (self->start_ts)) GST_BUFFER_TIMESTAMP (outbuf) = self->start_ts; else GST_BUFFER_TIMESTAMP (outbuf) = 0; GST_BUFFER_TIMESTAMP (outbuf) += gst_util_uint64_scale_int (self->nsamples_out - outsamples - self->latency, GST_SECOND, rate); GST_BUFFER_DURATION (outbuf) = gst_util_uint64_scale_int (outsamples, GST_SECOND, rate); if (self->start_off != GST_BUFFER_OFFSET_NONE) { GST_BUFFER_OFFSET (outbuf) = self->start_off + self->nsamples_out - outsamples - self->latency; GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET (outbuf) + outsamples; } GST_DEBUG_OBJECT (self, "Pushing residue buffer of size %" G_GSIZE_FORMAT " with timestamp: %" GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %" G_GUINT64_FORMAT ", offset_end: %" G_GUINT64_FORMAT ", nsamples_out: %d", gst_buffer_get_size (outbuf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)), GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf), outsamples); res = gst_pad_push (GST_BASE_TRANSFORM_CAST (self)->srcpad, outbuf); if (G_UNLIKELY (res != GST_FLOW_OK)) { GST_WARNING_OBJECT (self, "failed to push residue"); } self->buffer_fill = 0; }
static void gst_audio_wsinclimit_build_kernel (GstAudioWSincLimit * self, const GstAudioInfo * info) { gint i = 0; gdouble sum = 0.0; gint len = 0; gdouble w; gdouble *kernel = NULL; gint rate, channels; len = self->kernel_length; if (info) { rate = GST_AUDIO_INFO_RATE (info); channels = GST_AUDIO_INFO_CHANNELS (info); } else { rate = GST_AUDIO_FILTER_RATE (self); channels = GST_AUDIO_FILTER_CHANNELS (self); } if (rate == 0) { GST_DEBUG ("rate not set yet"); return; } if (channels == 0) { GST_DEBUG ("channels not set yet"); return; } /* Clamp cutoff frequency between 0 and the nyquist frequency */ self->cutoff = CLAMP (self->cutoff, 0.0, rate / 2); GST_DEBUG ("gst_audio_wsinclimit_: initializing filter kernel of length %d " "with cutoff %.2lf Hz " "for mode %s", len, self->cutoff, (self->mode == MODE_LOW_PASS) ? "low-pass" : "high-pass"); /* fill the kernel */ w = 2 * G_PI * (self->cutoff / rate); kernel = g_new (gdouble, len); for (i = 0; i < len; ++i) { if (i == (len - 1) / 2.0) kernel[i] = w; else kernel[i] = sin (w * (i - (len - 1) / 2)) / (i - (len - 1) / 2.0); /* windowing */ switch (self->window) { case WINDOW_HAMMING: kernel[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1))); break; case WINDOW_BLACKMAN: kernel[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) + 0.08 * cos (4 * G_PI * i / (len - 1))); break; case WINDOW_GAUSSIAN: kernel[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1)))); break; case WINDOW_COSINE: kernel[i] *= cos (G_PI * i / (len - 1) - G_PI / 2); break; case WINDOW_HANN: kernel[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1))); break; } } /* normalize for unity gain at DC */ for (i = 0; i < len; ++i) sum += kernel[i]; for (i = 0; i < len; ++i) kernel[i] /= sum; /* convert to highpass if specified */ if (self->mode == MODE_HIGH_PASS) { for (i = 0; i < len; ++i) kernel[i] = -kernel[i]; if (len % 2 == 1) { kernel[(len - 1) / 2] += 1.0; } else { kernel[len / 2 - 1] += 0.5; kernel[len / 2] += 0.5; } } gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self), kernel, self->kernel_length, (len - 1) / 2, info); }
static GstFlowReturn gst_spectrum_transform_ip (GstBaseTransform * trans, GstBuffer * buffer) { GstSpectrum *spectrum = GST_SPECTRUM (trans); guint rate = GST_AUDIO_FILTER_RATE (spectrum); guint channels = GST_AUDIO_FILTER_CHANNELS (spectrum); guint bps = GST_AUDIO_FILTER_BPS (spectrum); guint bpf = GST_AUDIO_FILTER_BPF (spectrum); guint output_channels = spectrum->multi_channel ? channels : 1; guint c; gfloat max_value = (1UL << ((bps << 3) - 1)) - 1; guint bands = spectrum->bands; guint nfft = 2 * bands - 2; guint input_pos; gfloat *input; GstMapInfo map; const guint8 *data; gsize size; guint fft_todo, msg_todo, block_size; gboolean have_full_interval; GstSpectrumChannel *cd; GstSpectrumInputData input_data; g_mutex_lock (&spectrum->lock); gst_buffer_map (buffer, &map, GST_MAP_READ); data = map.data; size = map.size; GST_LOG_OBJECT (spectrum, "input size: %" G_GSIZE_FORMAT " bytes", size); if (GST_BUFFER_IS_DISCONT (buffer)) { GST_DEBUG_OBJECT (spectrum, "Discontinuity detected -- flushing"); gst_spectrum_flush (spectrum); } /* If we don't have a FFT context yet (or it was reset due to parameter * changes) get one and allocate memory for everything */ if (spectrum->channel_data == NULL) { GST_DEBUG_OBJECT (spectrum, "allocating for bands %u", bands); gst_spectrum_alloc_channel_data (spectrum); /* number of sample frames we process before posting a message * interval is in ns */ spectrum->frames_per_interval = gst_util_uint64_scale (spectrum->interval, rate, GST_SECOND); spectrum->frames_todo = spectrum->frames_per_interval; /* rounding error for frames_per_interval in ns, * aggregated it in accumulated_error */ spectrum->error_per_interval = (spectrum->interval * rate) % GST_SECOND; if (spectrum->frames_per_interval == 0) spectrum->frames_per_interval = 1; GST_INFO_OBJECT (spectrum, "interval %" GST_TIME_FORMAT ", fpi %" G_GUINT64_FORMAT ", error %" GST_TIME_FORMAT, GST_TIME_ARGS (spectrum->interval), spectrum->frames_per_interval, GST_TIME_ARGS (spectrum->error_per_interval)); spectrum->input_pos = 0; gst_spectrum_flush (spectrum); } if (spectrum->num_frames == 0) spectrum->message_ts = GST_BUFFER_TIMESTAMP (buffer); input_pos = spectrum->input_pos; input_data = spectrum->input_data; while (size >= bpf) { /* run input_data for a chunk of data */ fft_todo = nfft - (spectrum->num_frames % nfft); msg_todo = spectrum->frames_todo - spectrum->num_frames; GST_LOG_OBJECT (spectrum, "message frames todo: %u, fft frames todo: %u, input frames %" G_GSIZE_FORMAT, msg_todo, fft_todo, (size / bpf)); block_size = msg_todo; if (block_size > (size / bpf)) block_size = (size / bpf); if (block_size > fft_todo) block_size = fft_todo; for (c = 0; c < output_channels; c++) { cd = &spectrum->channel_data[c]; input = cd->input; /* Move the current frames into our ringbuffers */ input_data (data + c * bps, input, block_size, channels, max_value, input_pos, nfft); } data += block_size * bpf; size -= block_size * bpf; input_pos = (input_pos + block_size) % nfft; spectrum->num_frames += block_size; have_full_interval = (spectrum->num_frames == spectrum->frames_todo); GST_LOG_OBJECT (spectrum, "size: %" G_GSIZE_FORMAT ", do-fft = %d, do-message = %d", size, (spectrum->num_frames % nfft == 0), have_full_interval); /* If we have enough frames for an FFT or we have all frames required for * the interval and we haven't run a FFT, then run an FFT */ if ((spectrum->num_frames % nfft == 0) || (have_full_interval && !spectrum->num_fft)) { for (c = 0; c < output_channels; c++) { cd = &spectrum->channel_data[c]; gst_spectrum_run_fft (spectrum, cd, input_pos); } spectrum->num_fft++; } /* Do we have the FFTs for one interval? */ if (have_full_interval) { GST_DEBUG_OBJECT (spectrum, "nfft: %u frames: %" G_GUINT64_FORMAT " fpi: %" G_GUINT64_FORMAT " error: %" GST_TIME_FORMAT, nfft, spectrum->num_frames, spectrum->frames_per_interval, GST_TIME_ARGS (spectrum->accumulated_error)); spectrum->frames_todo = spectrum->frames_per_interval; if (spectrum->accumulated_error >= GST_SECOND) { spectrum->accumulated_error -= GST_SECOND; spectrum->frames_todo++; } spectrum->accumulated_error += spectrum->error_per_interval; if (spectrum->post_messages) { GstMessage *m; for (c = 0; c < output_channels; c++) { cd = &spectrum->channel_data[c]; gst_spectrum_prepare_message_data (spectrum, cd); } m = gst_spectrum_message_new (spectrum, spectrum->message_ts, spectrum->interval); gst_element_post_message (GST_ELEMENT (spectrum), m); } if (GST_CLOCK_TIME_IS_VALID (spectrum->message_ts)) spectrum->message_ts += gst_util_uint64_scale (spectrum->num_frames, GST_SECOND, rate); for (c = 0; c < output_channels; c++) { cd = &spectrum->channel_data[c]; gst_spectrum_reset_message_data (spectrum, cd); } spectrum->num_frames = 0; spectrum->num_fft = 0; } } spectrum->input_pos = input_pos; gst_buffer_unmap (buffer, &map); g_mutex_unlock (&spectrum->lock); g_assert (size == 0); return GST_FLOW_OK; }
static void gst_audio_echo_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { GstAudioEcho *self = GST_AUDIO_ECHO (object); switch (prop_id) { case PROP_DELAY:{ guint64 max_delay, delay; guint rate; g_mutex_lock (&self->lock); delay = g_value_get_uint64 (value); max_delay = self->max_delay; if (delay > max_delay && GST_STATE (self) > GST_STATE_READY) { GST_WARNING_OBJECT (self, "New delay (%" GST_TIME_FORMAT ") " "is larger than maximum delay (%" GST_TIME_FORMAT ")", GST_TIME_ARGS (delay), GST_TIME_ARGS (max_delay)); self->delay = max_delay; } else { self->delay = delay; self->max_delay = MAX (delay, max_delay); if (delay > max_delay) { g_free (self->buffer); self->buffer = NULL; } } rate = GST_AUDIO_FILTER_RATE (self); if (rate > 0) self->delay_frames = MAX (gst_util_uint64_scale (self->delay, rate, GST_SECOND), 1); g_mutex_unlock (&self->lock); break; } case PROP_MAX_DELAY:{ guint64 max_delay; g_mutex_lock (&self->lock); max_delay = g_value_get_uint64 (value); if (GST_STATE (self) > GST_STATE_READY) { GST_ERROR_OBJECT (self, "Can't change maximum delay in" " PLAYING or PAUSED state"); } else { self->max_delay = max_delay; g_free (self->buffer); self->buffer = NULL; } g_mutex_unlock (&self->lock); break; } case PROP_INTENSITY:{ g_mutex_lock (&self->lock); self->intensity = g_value_get_float (value); g_mutex_unlock (&self->lock); break; } case PROP_FEEDBACK:{ g_mutex_lock (&self->lock); self->feedback = g_value_get_float (value); g_mutex_unlock (&self->lock); break; } default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static void gst_audio_wsincband_build_kernel (GstAudioWSincBand * self, const GstAudioInfo * info) { gint i = 0; gdouble sum = 0.0; gint len = 0; gdouble *kernel_lp, *kernel_hp; gdouble w; gdouble *kernel; gint rate, channels; len = self->kernel_length; if (info) { rate = GST_AUDIO_INFO_RATE (info); channels = GST_AUDIO_INFO_CHANNELS (info); } else { rate = GST_AUDIO_FILTER_RATE (self); channels = GST_AUDIO_FILTER_CHANNELS (self); } if (rate == 0) { GST_DEBUG ("rate not set yet"); return; } if (channels == 0) { GST_DEBUG ("channels not set yet"); return; } /* Clamp frequencies */ self->lower_frequency = CLAMP (self->lower_frequency, 0.0, rate / 2); self->upper_frequency = CLAMP (self->upper_frequency, 0.0, rate / 2); if (self->lower_frequency > self->upper_frequency) { gint tmp = self->lower_frequency; self->lower_frequency = self->upper_frequency; self->upper_frequency = tmp; } GST_DEBUG ("gst_audio_wsincband: initializing filter kernel of length %d " "with lower frequency %.2lf Hz " ", upper frequency %.2lf Hz for mode %s", len, self->lower_frequency, self->upper_frequency, (self->mode == MODE_BAND_PASS) ? "band-pass" : "band-reject"); /* fill the lp kernel */ w = 2 * G_PI * (self->lower_frequency / rate); kernel_lp = g_new (gdouble, len); for (i = 0; i < len; ++i) { if (i == (len - 1) / 2.0) kernel_lp[i] = w; else kernel_lp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0); /* windowing */ switch (self->window) { case WINDOW_HAMMING: kernel_lp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1))); break; case WINDOW_BLACKMAN: kernel_lp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) + 0.08 * cos (4 * G_PI * i / (len - 1))); break; case WINDOW_GAUSSIAN: kernel_lp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1)))); break; case WINDOW_COSINE: kernel_lp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2); break; case WINDOW_HANN: kernel_lp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1))); break; } } /* normalize for unity gain at DC */ sum = 0.0; for (i = 0; i < len; ++i) sum += kernel_lp[i]; for (i = 0; i < len; ++i) kernel_lp[i] /= sum; /* fill the hp kernel */ w = 2 * G_PI * (self->upper_frequency / rate); kernel_hp = g_new (gdouble, len); for (i = 0; i < len; ++i) { if (i == (len - 1) / 2.0) kernel_hp[i] = w; else kernel_hp[i] = sin (w * (i - (len - 1) / 2.0)) / (i - (len - 1) / 2.0); /* Windowing */ switch (self->window) { case WINDOW_HAMMING: kernel_hp[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / (len - 1))); break; case WINDOW_BLACKMAN: kernel_hp[i] *= (0.42 - 0.5 * cos (2 * G_PI * i / (len - 1)) + 0.08 * cos (4 * G_PI * i / (len - 1))); break; case WINDOW_GAUSSIAN: kernel_hp[i] *= exp (-0.5 * POW2 (3.0 / len * (2 * i - (len - 1)))); break; case WINDOW_COSINE: kernel_hp[i] *= cos (G_PI * i / (len - 1) - G_PI / 2); break; case WINDOW_HANN: kernel_hp[i] *= 0.5 * (1 - cos (2 * G_PI * i / (len - 1))); break; } } /* normalize for unity gain at DC */ sum = 0.0; for (i = 0; i < len; ++i) sum += kernel_hp[i]; for (i = 0; i < len; ++i) kernel_hp[i] /= sum; /* do spectral inversion to go from lowpass to highpass */ for (i = 0; i < len; ++i) kernel_hp[i] = -kernel_hp[i]; if (len % 2 == 1) { kernel_hp[(len - 1) / 2] += 1.0; } else { kernel_hp[len / 2 - 1] += 0.5; kernel_hp[len / 2] += 0.5; } /* combine the two kernels */ kernel = g_new (gdouble, len); for (i = 0; i < len; ++i) kernel[i] = kernel_lp[i] + kernel_hp[i]; /* free the helper kernels */ g_free (kernel_lp); g_free (kernel_hp); /* do spectral inversion to go from bandreject to bandpass * if specified */ if (self->mode == MODE_BAND_PASS) { for (i = 0; i < len; ++i) kernel[i] = -kernel[i]; kernel[len / 2] += 1; } gst_audio_fx_base_fir_filter_set_kernel (GST_AUDIO_FX_BASE_FIR_FILTER (self), kernel, self->kernel_length, (len - 1) / 2, info); }