1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/* GStreamer
* Copyright (C) 2008 Sebastian Dröge <slomo@circular-chaos.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* FIXME: workaround for SoundTouch.h of version 1.3.1 defining those
* variables while it shouldn't. */
#undef VERSION
#undef PACKAGE_VERSION
#undef PACKAGE_TARNAME
#undef PACKAGE_STRING
#undef PACKAGE_NAME
#undef PACKAGE_BUGREPORT
#undef PACKAGE
#define FLOAT_SAMPLES 1
#include <soundtouch/BPMDetect.h>
#include <gst/audio/audio.h>
#include <gst/audio/gstaudiofilter.h>
#include <math.h>
#include <string.h>
#include "gstbpmdetect.hh"
GST_DEBUG_CATEGORY_STATIC (gst_bpm_detect_debug);
#define GST_CAT_DEFAULT gst_bpm_detect_debug
#define GST_BPM_DETECT_GET_PRIVATE(o) (o->priv)
struct _GstBPMDetectPrivate
{
gfloat bpm;
#ifdef HAVE_SOUNDTOUCH_1_4
soundtouch::BPMDetect * detect;
#else
BPMDetect *detect;
#endif
};
#define ALLOWED_CAPS \
"audio/x-raw, " \
" format = (string) " GST_AUDIO_NE (F32) ", " \
" rate = (int) [ 8000, MAX ], " \
" channels = (int) [ 1, 2 ]"
#define gst_bpm_detect_parent_class parent_class
G_DEFINE_TYPE (GstBPMDetect, gst_bpm_detect, GST_TYPE_AUDIO_FILTER);
static void gst_bpm_detect_finalize (GObject * object);
static gboolean gst_bpm_detect_stop (GstBaseTransform * trans);
static gboolean gst_bpm_detect_event (GstBaseTransform * trans,
GstEvent * event);
static GstFlowReturn gst_bpm_detect_transform_ip (GstBaseTransform * trans,
GstBuffer * in);
static gboolean gst_bpm_detect_setup (GstAudioFilter * filter,
const GstAudioInfo * info);
static void
gst_bpm_detect_class_init (GstBPMDetectClass * klass)
{
GstCaps *caps;
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
GstAudioFilterClass *filter_class = GST_AUDIO_FILTER_CLASS (klass);
GST_DEBUG_CATEGORY_INIT (gst_bpm_detect_debug, "bpm_detect", 0,
"audio bpm detection element");
gobject_class->finalize = gst_bpm_detect_finalize;
gst_element_class_set_details_simple (element_class, "BPM Detector",
"Filter/Analyzer/Audio", "Detect the BPM of an audio stream",
"Sebastian Dröge <slomo@circular-chaos.org>");
caps = gst_caps_from_string (ALLOWED_CAPS);
gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
caps);
gst_caps_unref (caps);
trans_class->stop = GST_DEBUG_FUNCPTR (gst_bpm_detect_stop);
trans_class->sink_event = GST_DEBUG_FUNCPTR (gst_bpm_detect_event);
trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_bpm_detect_transform_ip);
trans_class->passthrough_on_same_caps = TRUE;
filter_class->setup = GST_DEBUG_FUNCPTR (gst_bpm_detect_setup);
g_type_class_add_private (gobject_class, sizeof (GstBPMDetectPrivate));
}
static void
gst_bpm_detect_init (GstBPMDetect * bpm_detect)
{
bpm_detect->priv = G_TYPE_INSTANCE_GET_PRIVATE ((bpm_detect),
GST_TYPE_BPM_DETECT, GstBPMDetectPrivate);
bpm_detect->priv->detect = NULL;
bpm_detect->bpm = 0.0;
}
static void
gst_bpm_detect_finalize (GObject * object)
{
GstBPMDetect *bpm_detect = GST_BPM_DETECT (object);
if (bpm_detect->priv->detect) {
delete bpm_detect->priv->detect;
bpm_detect->priv->detect = NULL;
}
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static gboolean
gst_bpm_detect_stop (GstBaseTransform * trans)
{
GstBPMDetect *bpm_detect = GST_BPM_DETECT (trans);
if (bpm_detect->priv->detect) {
delete bpm_detect->priv->detect;
bpm_detect->priv->detect = NULL;
}
bpm_detect->bpm = 0.0;
return TRUE;
}
static gboolean
gst_bpm_detect_event (GstBaseTransform * trans, GstEvent * event)
{
GstBPMDetect *bpm_detect = GST_BPM_DETECT (trans);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_FLUSH_STOP:
case GST_EVENT_EOS:
case GST_EVENT_SEGMENT:
if (bpm_detect->priv->detect) {
delete bpm_detect->priv->detect;
bpm_detect->priv->detect = NULL;
}
bpm_detect->bpm = 0.0;
break;
default:
break;
}
return TRUE;
}
static gboolean
gst_bpm_detect_setup (GstAudioFilter * filter, const GstAudioInfo * info)
{
GstBPMDetect *bpm_detect = GST_BPM_DETECT (filter);
if (bpm_detect->priv->detect) {
delete bpm_detect->priv->detect;
bpm_detect->priv->detect = NULL;
}
return TRUE;
}
static GstFlowReturn
gst_bpm_detect_transform_ip (GstBaseTransform * trans, GstBuffer * in)
{
GstBPMDetect *bpm_detect = GST_BPM_DETECT (trans);
GstAudioFilter *filter = GST_AUDIO_FILTER (trans);
gint nsamples;
gfloat bpm;
GstMapInfo info;
if (G_UNLIKELY (!bpm_detect->priv->detect)) {
if (GST_AUDIO_INFO_FORMAT (&filter->info) == GST_AUDIO_FORMAT_UNKNOWN) {
GST_ERROR_OBJECT (bpm_detect, "No channels or rate set yet");
return GST_FLOW_ERROR;
}
#ifdef HAVE_SOUNDTOUCH_1_4
bpm_detect->priv->detect =
new soundtouch::BPMDetect (GST_AUDIO_INFO_CHANNELS (&filter->info),
GST_AUDIO_INFO_RATE (&filter->info));
#else
bpm_detect->priv->detect =
new BPMDetect (GST_AUDIO_INFO_CHANNELS (&filter->info),
GST_AUDIO_INFO_RATE (&filter->info));
#endif
}
gst_buffer_map (in, &info, GST_MAP_READ);
nsamples = info.size / (4 * GST_AUDIO_INFO_CHANNELS (&filter->info));
/* For stereo BPMDetect->inputSamples() does downmixing into the input
* data but our buffer data shouldn't be modified.
*/
if (GST_AUDIO_INFO_CHANNELS (&filter->info) == 1) {
gfloat *inbuf = (gfloat *) info.data;
while (nsamples > 0) {
bpm_detect->priv->detect->inputSamples (inbuf, MIN (nsamples, 2048));
nsamples -= 2048;
inbuf += 2048;
}
} else {
gfloat *inbuf, *intmp, data[2 * 2048];
inbuf = (gfloat *) info.data;
intmp = data;
while (nsamples > 0) {
memcpy (intmp, inbuf, sizeof (gfloat) * 2 * MIN (nsamples, 2048));
bpm_detect->priv->detect->inputSamples (intmp, MIN (nsamples, 2048));
nsamples -= 2048;
inbuf += 2048 * 2;
}
}
gst_buffer_unmap (in, &info);
bpm = bpm_detect->priv->detect->getBpm ();
if (bpm >= 1.0 && fabs (bpm_detect->bpm - bpm) >= 1.0) {
GstTagList *tags = gst_tag_list_new_empty ();
gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE_ALL, GST_TAG_BEATS_PER_MINUTE,
bpm, (void *) NULL);
gst_pad_push_event (trans->srcpad, gst_event_new_tag (tags));
GST_INFO_OBJECT (bpm_detect, "Detected BPM: %lf\n", bpm);
bpm_detect->bpm = bpm;
}
return GST_FLOW_OK;
}
|