soundtouch: Allow compilation against float and integer version of the library

https://bugzilla.gnome.org/show_bug.cgi?id=707270
This commit is contained in:
Sebastian Dröge 2013-09-02 10:29:08 +02:00
parent 47c35ee52e
commit 576b4826c8
3 changed files with 47 additions and 42 deletions

View File

@ -31,7 +31,6 @@
#undef PACKAGE_BUGREPORT #undef PACKAGE_BUGREPORT
#undef PACKAGE #undef PACKAGE
#define FLOAT_SAMPLES 1
#include <soundtouch/BPMDetect.h> #include <soundtouch/BPMDetect.h>
#include <gst/audio/audio.h> #include <gst/audio/audio.h>
@ -55,11 +54,21 @@ struct _GstBPMDetectPrivate
#endif #endif
}; };
#define ALLOWED_CAPS \ #if defined(SOUNDTOUCH_FLOAT_SAMPLES)
"audio/x-raw, " \ #define ALLOWED_CAPS \
" format = (string) " GST_AUDIO_NE (F32) ", " \ "audio/x-raw, " \
" rate = (int) [ 8000, MAX ], " \ "format = (string) " GST_AUDIO_NE (F32) ", " \
" channels = (int) [ 1, 2 ]" "rate = (int) [ 8000, MAX ], " \
"channels = (int) [ 1, 2 ]"
#elif defined(SOUNDTOUCH_INTEGER_SAMPLES)
#define ALLOWED_CAPS \
"audio/x-raw, " \
"format = (string) " GST_AUDIO_NE (S16) ", " \
"rate = (int) [ 8000, MAX ], " \
"channels = (int) [ 1, 2 ]"
#else
#error "Only integer or float samples are supported"
#endif
#define gst_bpm_detect_parent_class parent_class #define gst_bpm_detect_parent_class parent_class
G_DEFINE_TYPE (GstBPMDetect, gst_bpm_detect, GST_TYPE_AUDIO_FILTER); G_DEFINE_TYPE (GstBPMDetect, gst_bpm_detect, GST_TYPE_AUDIO_FILTER);
@ -209,13 +218,13 @@ gst_bpm_detect_transform_ip (GstBaseTransform * trans, GstBuffer * in)
gst_buffer_map (in, &info, GST_MAP_READ); gst_buffer_map (in, &info, GST_MAP_READ);
nsamples = info.size / (4 * GST_AUDIO_INFO_CHANNELS (&filter->info)); nsamples = info.size / (GST_AUDIO_INFO_BPF (&filter->info) * GST_AUDIO_INFO_CHANNELS (&filter->info));
/* For stereo BPMDetect->inputSamples() does downmixing into the input /* For stereo BPMDetect->inputSamples() does downmixing into the input
* data but our buffer data shouldn't be modified. * data but our buffer data shouldn't be modified.
*/ */
if (GST_AUDIO_INFO_CHANNELS (&filter->info) == 1) { if (GST_AUDIO_INFO_CHANNELS (&filter->info) == 1) {
gfloat *inbuf = (gfloat *) info.data; soundtouch::SAMPLETYPE *inbuf = (soundtouch::SAMPLETYPE *) info.data;
while (nsamples > 0) { while (nsamples > 0) {
bpm_detect->priv->detect->inputSamples (inbuf, MIN (nsamples, 2048)); bpm_detect->priv->detect->inputSamples (inbuf, MIN (nsamples, 2048));
@ -223,13 +232,13 @@ gst_bpm_detect_transform_ip (GstBaseTransform * trans, GstBuffer * in)
inbuf += 2048; inbuf += 2048;
} }
} else { } else {
gfloat *inbuf, *intmp, data[2 * 2048]; soundtouch::SAMPLETYPE *inbuf, *intmp, data[2 * 2048];
inbuf = (gfloat *) info.data; inbuf = (soundtouch::SAMPLETYPE *) info.data;
intmp = data; intmp = data;
while (nsamples > 0) { while (nsamples > 0) {
memcpy (intmp, inbuf, sizeof (gfloat) * 2 * MIN (nsamples, 2048)); memcpy (intmp, inbuf, sizeof (soundtouch::SAMPLETYPE) * 2 * MIN (nsamples, 2048));
bpm_detect->priv->detect->inputSamples (intmp, MIN (nsamples, 2048)); bpm_detect->priv->detect->inputSamples (intmp, MIN (nsamples, 2048));
nsamples -= 2048; nsamples -= 2048;
inbuf += 2048 * 2; inbuf += 2048 * 2;

View File

@ -31,7 +31,6 @@
#undef PACKAGE_BUGREPORT #undef PACKAGE_BUGREPORT
#undef PACKAGE #undef PACKAGE
#define FLOAT_SAMPLES 1
#include <soundtouch/SoundTouch.h> #include <soundtouch/SoundTouch.h>
#include <gst/gst.h> #include <gst/gst.h>
@ -62,11 +61,21 @@ enum
ARG_PITCH ARG_PITCH
}; };
#define SUPPORTED_CAPS \ #if defined(SOUNDTOUCH_FLOAT_SAMPLES)
"audio/x-raw, " \ #define SUPPORTED_CAPS \
"format = (string) " GST_AUDIO_NE (F32) ", " \ "audio/x-raw, " \
"rate = (int) [ 8000, MAX ], " \ "format = (string) " GST_AUDIO_NE (F32) ", " \
"channels = (int) [ 1, 2 ]" "rate = (int) [ 8000, MAX ], " \
"channels = (int) [ 1, 2 ]"
#elif defined(SOUNDTOUCH_INTEGER_SAMPLES)
#define SUPPORTED_CAPS \
"audio/x-raw, " \
"format = (string) " GST_AUDIO_NE (S16) ", " \
"rate = (int) [ 8000, MAX ], " \
"channels = (int) [ 1, 2 ]"
#else
#error "Only integer or float samples are supported"
#endif
static GstStaticPadTemplate gst_pitch_sink_template = static GstStaticPadTemplate gst_pitch_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink", GST_STATIC_PAD_TEMPLATE ("sink",
@ -294,29 +303,17 @@ static gboolean
gst_pitch_setcaps (GstPitch * pitch, GstCaps * caps) gst_pitch_setcaps (GstPitch * pitch, GstCaps * caps)
{ {
GstPitchPrivate *priv; GstPitchPrivate *priv;
GstStructure *structure;
gint rate, channels;
priv = GST_PITCH_GET_PRIVATE (pitch); priv = GST_PITCH_GET_PRIVATE (pitch);
structure = gst_caps_get_structure (caps, 0); if (gst_audio_info_from_caps (&pitch->info, caps))
if (!gst_structure_get_int (structure, "rate", &rate) ||
!gst_structure_get_int (structure, "channels", &channels)) {
return FALSE; return FALSE;
}
GST_OBJECT_LOCK (pitch); GST_OBJECT_LOCK (pitch);
pitch->samplerate = rate;
pitch->channels = channels;
/* notify the soundtouch instance of this change */ /* notify the soundtouch instance of this change */
priv->st->setSampleRate (rate); priv->st->setSampleRate (pitch->info.rate);
priv->st->setChannels (channels); priv->st->setChannels (pitch->info.channels);
/* calculate sample size */
pitch->sample_size = (sizeof (gfloat) * channels);
GST_OBJECT_UNLOCK (pitch); GST_OBJECT_UNLOCK (pitch);
@ -361,10 +358,10 @@ gst_pitch_prepare_buffer (GstPitch * pitch)
if (samples == 0) if (samples == 0)
return NULL; return NULL;
buffer = gst_buffer_new_and_alloc (samples * pitch->sample_size); buffer = gst_buffer_new_and_alloc (samples * pitch->info.bpf);
gst_buffer_map (buffer, &info, (GstMapFlags) GST_MAP_READWRITE); gst_buffer_map (buffer, &info, (GstMapFlags) GST_MAP_READWRITE);
samples = priv->st->receiveSamples ((gfloat *) info.data, samples); samples = priv->st->receiveSamples ((soundtouch::SAMPLETYPE *) info.data, samples);
gst_buffer_unmap (buffer, &info); gst_buffer_unmap (buffer, &info);
if (samples <= 0) { if (samples <= 0) {
@ -373,7 +370,7 @@ gst_pitch_prepare_buffer (GstPitch * pitch)
} }
GST_BUFFER_DURATION (buffer) = GST_BUFFER_DURATION (buffer) =
gst_util_uint64_scale (samples, GST_SECOND, pitch->samplerate); gst_util_uint64_scale (samples, GST_SECOND, pitch->info.rate);
/* temporary store samples here, to avoid having to recalculate this */ /* temporary store samples here, to avoid having to recalculate this */
GST_BUFFER_OFFSET (buffer) = (gint64) samples; GST_BUFFER_OFFSET (buffer) = (gint64) samples;
@ -471,8 +468,8 @@ gst_pitch_convert (GstPitch * pitch,
g_return_val_if_fail (dst_format && dst_value, FALSE); g_return_val_if_fail (dst_format && dst_value, FALSE);
GST_OBJECT_LOCK (pitch); GST_OBJECT_LOCK (pitch);
sample_size = pitch->sample_size; sample_size = pitch->info.bpf;
samplerate = pitch->samplerate; samplerate = pitch->info.rate;
GST_OBJECT_UNLOCK (pitch); GST_OBJECT_UNLOCK (pitch);
if (sample_size == 0 || samplerate == 0) { if (sample_size == 0 || samplerate == 0) {
@ -847,7 +844,7 @@ gst_pitch_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
/* push the received samples on the soundtouch buffer */ /* push the received samples on the soundtouch buffer */
GST_LOG_OBJECT (pitch, "incoming buffer (%d samples) %" GST_TIME_FORMAT, GST_LOG_OBJECT (pitch, "incoming buffer (%d samples) %" GST_TIME_FORMAT,
(gint) (gst_buffer_get_size (buffer) / pitch->sample_size), (gint) (gst_buffer_get_size (buffer) / pitch->info.bpf),
GST_TIME_ARGS (timestamp)); GST_TIME_ARGS (timestamp));
if (GST_PITCH_GET_PRIVATE (pitch)->pending_segment) { if (GST_PITCH_GET_PRIVATE (pitch)->pending_segment) {
@ -872,7 +869,7 @@ gst_pitch_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
} }
gst_buffer_map (buffer, &info, GST_MAP_READ); gst_buffer_map (buffer, &info, GST_MAP_READ);
priv->st->putSamples ((gfloat *) info.data, info.size / pitch->sample_size); priv->st->putSamples ((soundtouch::SAMPLETYPE *) info.data, info.size / pitch->info.bpf);
gst_buffer_unmap (buffer, &info); gst_buffer_unmap (buffer, &info);
gst_buffer_unref (buffer); gst_buffer_unref (buffer);

View File

@ -21,6 +21,7 @@
#define __GST_PITCH_H__ #define __GST_PITCH_H__
#include <gst/gst.h> #include <gst/gst.h>
#include <gst/audio/audio.h>
G_BEGIN_DECLS G_BEGIN_DECLS
@ -71,9 +72,7 @@ struct _GstPitch
gfloat seg_arate; /* Rate to apply from input segment */ gfloat seg_arate; /* Rate to apply from input segment */
/* values extracted from caps */ /* values extracted from caps */
gint samplerate; /* samplerate */ GstAudioInfo info;
gint channels; /* number of audio channels */
gsize sample_size; /* number of bytes for a single sample */
/* stream tracking */ /* stream tracking */
GstClockTime next_buffer_time; GstClockTime next_buffer_time;