File: banshee-bpmdetector.c

package info (click to toggle)
banshee 2.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 43,392 kB
  • sloc: cs: 137,906; xml: 90,824; sh: 11,616; ansic: 4,829; makefile: 2,910; python: 38
file content (357 lines) | stat: -rw-r--r-- 11,028 bytes parent folder | download | duplicates (4)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
// banshee-bpmdetector.c
//
// Author:
//   Gabriel Burt <gburt@novell.com>
//
// Copyright (C) 2008 Novell, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <string.h>
#include <glib/gi18n.h>

#include "banshee-gst.h"
#include "banshee-tagger.h"

typedef struct BansheeBpmDetector BansheeBpmDetector;

typedef void (* BansheeBpmDetectorFinishedCallback) ();
typedef void (* BansheeBpmDetectorProgressCallback) (double bpm);
typedef void (* BansheeBpmDetectorErrorCallback)    (const gchar *error, const gchar *debug);

// Only analyze 20 seconds of audio per song
#define BPM_DETECT_ANALYSIS_DURATION_MS 20*1000

struct BansheeBpmDetector {
    gboolean is_detecting;

    /*
     * You can run this pipeline on the cmd line with:
     * gst-launch -m filesrc location=/path/to/my.mp3 ! decodebin2 ! \
     *    audioconvert ! bpmdetect ! fakesink
     */

    GstElement *pipeline;
    GstElement *filesrc;
    GstElement *decodebin;
    GstElement *audioconvert;
    GstElement *bpmdetect;
    GstElement *fakesink;
    
    BansheeBpmDetectorProgressCallback progress_cb;
    BansheeBpmDetectorFinishedCallback finished_cb;
    BansheeBpmDetectorErrorCallback error_cb;
};

// ---------------------------------------------------------------------------
// Private Functions
// ---------------------------------------------------------------------------

static void
bbd_raise_error (BansheeBpmDetector *detector, const gchar *error, const gchar *debug)
{
    printf ("bpm_detect got error: %s %s\n", error, debug);
    g_return_if_fail (detector != NULL);

    if (detector->error_cb != NULL) {
        detector->error_cb (error, debug);
    }
}

static void
bbd_pipeline_process_tag (const GstTagList *tag_list, const gchar *tag_name, BansheeBpmDetector *detector)
{
    const GValue *value;
    gint value_count;
    double bpm;
    
    g_return_if_fail (detector != NULL);

    if (detector->progress_cb == NULL) {
        return;
    }

    if (strcmp (tag_name, GST_TAG_BEATS_PER_MINUTE)) {
        return;
    }

    value_count = gst_tag_list_get_tag_size (tag_list, tag_name);
    if (value_count < 1) {
        return;
    }
    
    value = gst_tag_list_get_value_index (tag_list, tag_name, 0);
    if (value != NULL && G_VALUE_HOLDS_DOUBLE (value)) {
        bpm = g_value_get_double (value);
        detector->progress_cb (bpm);
    }
}

static gboolean
bbd_pipeline_bus_callback (GstBus *bus, GstMessage *message, gpointer data)
{
    BansheeBpmDetector *detector = (BansheeBpmDetector *)data;

    g_return_val_if_fail (detector != NULL, FALSE);

    switch (GST_MESSAGE_TYPE (message)) {
        case GST_MESSAGE_TAG: {
            GstTagList *tags;
            gst_message_parse_tag (message, &tags);
            if (GST_IS_TAG_LIST (tags)) {
                gst_tag_list_foreach (tags, (GstTagForeachFunc)bbd_pipeline_process_tag, detector);
                gst_tag_list_free (tags);
            }
            break;
        }

        case GST_MESSAGE_ERROR: {
            GError *error;
            gchar *debug;
            
            gst_message_parse_error (message, &error, &debug);
            bbd_raise_error (detector, error->message, debug);
            g_error_free (error);
            g_free (debug);
            
            detector->is_detecting = FALSE;
            break;
        }

        case GST_MESSAGE_EOS: {
            detector->is_detecting = FALSE;
            gst_element_set_state (GST_ELEMENT (detector->pipeline), GST_STATE_NULL);

            if (detector->finished_cb != NULL) {
                detector->finished_cb ();
            }
            break;
        }
        
        default: break;
    }
    
    return TRUE;
}

static void
bbd_new_decoded_pad(GstElement *decodebin, GstPad *pad, 
    gboolean last, gpointer data)
{
    GstCaps *caps;
    GstStructure *str;
    GstPad *audiopad;
    BansheeBpmDetector *detector = (BansheeBpmDetector *)data;

    g_return_if_fail(detector != NULL);

    audiopad = gst_element_get_pad(detector->audioconvert, "sink");
    
    if(GST_PAD_IS_LINKED(audiopad)) {
        g_object_unref(audiopad);
        return;
    }

    caps = gst_pad_get_caps(pad);
    str = gst_caps_get_structure(caps, 0);
    
    if(!g_strrstr(gst_structure_get_name(str), "audio")) {
        gst_caps_unref(caps);
        gst_object_unref(audiopad);
        return;
    }
   
    gst_caps_unref(caps);
    gst_pad_link(pad, audiopad);
}

static gboolean
bbd_pipeline_construct (BansheeBpmDetector *detector)
{
    g_return_val_if_fail (detector != NULL, FALSE);

    if (detector->pipeline != NULL) {
        return TRUE;
    }
        
    detector->pipeline = gst_pipeline_new ("pipeline");
    if (detector->pipeline == NULL) {
        bbd_raise_error (detector, _("Could not create pipeline"), NULL);
        return FALSE;
    }

    detector->filesrc = gst_element_factory_make ("filesrc", "filesrc");
    if (detector->filesrc == NULL) {
        bbd_raise_error (detector, _("Could not create filesrc element"), NULL);
        return FALSE;
    }
  
    detector->decodebin = gst_element_factory_make ("decodebin2", "decodebin2");
    if (detector->decodebin == NULL) {
        bbd_raise_error (detector, _("Could not create decodebin2 plugin"), NULL);
        return FALSE;
    }

    detector->audioconvert = gst_element_factory_make ("audioconvert", "audioconvert");
    if (detector->audioconvert == NULL) {
        bbd_raise_error (detector, _("Could not create audioconvert plugin"), NULL);
        return FALSE;
    }

    detector->bpmdetect = gst_element_factory_make ("bpmdetect", "bpmdetect");
    if (detector->bpmdetect == NULL) {
        bbd_raise_error (detector, _("Could not create bpmdetect plugin"), NULL);
        return FALSE;
    }
    
    detector->fakesink = gst_element_factory_make ("fakesink", "bpmfakesink");
    if (detector->fakesink == NULL) {
        bbd_raise_error (detector, _("Could not create fakesink plugin"), NULL);
        return FALSE;
    }

    gst_bin_add_many (GST_BIN (detector->pipeline),
        detector->filesrc, detector->decodebin, detector->audioconvert,
        detector->bpmdetect, detector->fakesink, NULL);

    if (!gst_element_link (detector->filesrc, detector->decodebin)) {
        bbd_raise_error (detector, _("Could not link pipeline elements"), NULL);
        return FALSE;
    }

    // decodebin and audioconvert are linked dynamically when the decodebin creates a new pad
    g_signal_connect(detector->decodebin, "new-decoded-pad", 
        G_CALLBACK(bbd_new_decoded_pad), detector);

    if (!gst_element_link_many (detector->audioconvert, detector->bpmdetect, detector->fakesink, NULL)) {
        bbd_raise_error (detector, _("Could not link pipeline elements"), NULL);
        return FALSE;
    }
        
    gst_bus_add_watch (gst_pipeline_get_bus (GST_PIPELINE (detector->pipeline)), bbd_pipeline_bus_callback, detector);

    return TRUE;
}

// ---------------------------------------------------------------------------
// Internal Functions
// ---------------------------------------------------------------------------

BansheeBpmDetector *
bbd_new ()
{
    return g_new0 (BansheeBpmDetector, 1);
}

void 
bbd_cancel (BansheeBpmDetector *detector)
{
    g_return_if_fail (detector != NULL);
    
    if (detector->pipeline != NULL && GST_IS_ELEMENT (detector->pipeline)) {
        gst_element_set_state (GST_ELEMENT (detector->pipeline), GST_STATE_NULL);
        gst_object_unref (GST_OBJECT (detector->pipeline));
        detector->pipeline = NULL;
    }
}

void
bbd_destroy (BansheeBpmDetector *detector)
{
    g_return_if_fail (detector != NULL);
    
    bbd_cancel (detector);
    
    g_free (detector);
    detector = NULL;
}

gboolean
bbd_process_file (BansheeBpmDetector *detector, const gchar *path)
{
    //static GstFormat format = GST_FORMAT_TIME;
    //gint64 duration, duration_ms, start_ms, end_ms;

    g_return_val_if_fail (detector != NULL, FALSE);

    if (!bbd_pipeline_construct (detector)) {
        return FALSE;
    }
    
    detector->is_detecting = TRUE;
    gst_element_set_state (detector->fakesink, GST_STATE_NULL);
    g_object_set (G_OBJECT (detector->filesrc), "location", path, NULL);

    // TODO listen for transition to STATE_PLAYING, then
    // Determine how long the file is, and set the detector to base its analysis off the middle 30 seconds of the song

    /*if (gst_element_query_duration (detector->fakesink, &format, &duration)) {
        duration_ms = duration / GST_MSECOND;

        start_ms = CLAMP((duration_ms / 2) - (BPM_DETECT_ANALYSIS_DURATION_MS/2), 0, duration_ms);
        end_ms   = CLAMP(start_ms + BPM_DETECT_ANALYSIS_DURATION_MS, start_ms, duration_ms);
        printf("Analyzing song %s starting at %d ending at %d\n", path, start_ms/1000, end_ms/1000);

        if (gst_element_seek (detector->fakesink, 1.0, 
            //GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_SKIP | GST_SEEK_FLAG_KEY_UNIT,
            GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT,
            GST_SEEK_TYPE_SET, start_ms * GST_MSECOND, 
            GST_SEEK_TYPE_SET, end_ms * GST_MSECOND))
        {
        }
    }*/

    gst_element_set_state (detector->pipeline, GST_STATE_PLAYING);
    return TRUE;
}

void
bbd_set_progress_callback (BansheeBpmDetector *detector, BansheeBpmDetectorProgressCallback cb)
{
    g_return_if_fail (detector != NULL);
    detector->progress_cb = cb;
}

void
bbd_set_finished_callback (BansheeBpmDetector *detector, BansheeBpmDetectorFinishedCallback cb)
{
    g_return_if_fail (detector != NULL);
    detector->finished_cb = cb;
}

void
bbd_set_error_callback (BansheeBpmDetector *detector, BansheeBpmDetectorErrorCallback cb)
{
    g_return_if_fail (detector != NULL);
    detector->error_cb = cb;
}

gboolean
bbd_get_is_detecting (BansheeBpmDetector *detector)
{
    g_return_val_if_fail (detector != NULL, FALSE);
    return detector->is_detecting;
}