Files
BeatDetector/gst/plugin.cpp

171 lines
4.4 KiB
C++
Executable File

#include <string.h>
#include <math.h>
#include <gst/gst.h>
#include <gst/audio/audio.h>
#include <iostream>
#include "plugin.h"
#include "../lib/BeatDetection.h"
static BeatDetection<float> beatDetection;
#include "../lib/BassDetection.h"
static BassDetection<float> bassDetection;
#include "../lib/BeatDetection2.h"
static BeatDetection2<float> beatDetection2;
GST_DEBUG_CATEGORY_STATIC (beat_detector_debug);
#define GST_CAT_DEFAULT beat_detector_debug
static GstStaticPadTemplate sink_template_factory =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-raw, "
"format = (string) " GST_AUDIO_NE (S16) ", "
"rate = (int) [ 1, MAX ], "
"channels = (int) [ 1, MAX ]"
)
);
static GstStaticPadTemplate src_template_factory =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-raw, "
"format = (string) " GST_AUDIO_NE (S16) ", "
"rate = (int) [ 1, MAX ], "
"channels = (int) [ 1, MAX ]"
)
);
#define gst_beat_detector_parent_class parent_class
G_DEFINE_TYPE (GstBeatDetector, gst_beat_detector, GST_TYPE_BASE_TRANSFORM);
static void gst_beat_detector_finalize(GObject* obj);
static gboolean gst_level_set_caps (GstBaseTransform* trans, GstCaps* in, GstCaps* out) {
GstBeatDetector* filter = GST_BEAT_DETECTOR(trans);
return gst_audio_info_from_caps(&filter->info, in);
}
static GstFlowReturn gst_beat_detector_transform_ip(GstBaseTransform* trans, GstBuffer* in) {
GstBeatDetector *filter = GST_BEAT_DETECTOR(trans);
GstMapInfo map;
gst_buffer_map (in, &map, GST_MAP_READ);
gint channels = GST_AUDIO_INFO_CHANNELS (&filter->info);
gint bps = GST_AUDIO_INFO_BPS (&filter->info);
//gint rate = GST_AUDIO_INFO_RATE (&filter->info);
guint num_samples = map.size / bps;
//guint num_frames = num_samples / channels;
//std::cout << channels << " " << bps << " " << rate << std::endl;
//std::cout << filter->info.layout << std::endl;
// map signed-16-bit data
const int16_t* data = (const int16_t*) map.data;
static size_t samples = 0; ++samples;
// process 2 interleaved channels
for (guint i = 0; i < num_samples; i += 2) {
const int left = data[i+0];
const int right = data[i+1];
const int sample = (left+right)/2;
//const bool beat = beatDetection.add(sample);
//if (beat) {std::cout << "beat" << i << std::endl;}
//const float res = bassDetection.add(sample);
//if (res > 0) {std::cout << res << std::endl;}
const bool beat = beatDetection2.add(left, right);
if (beat) {std::cout << "beat" << i << std::endl << std::flush;}
}
// done
return GST_FLOW_OK;
}
static void gst_beat_detector_class_init(GstBeatDetectorClass* klass) {
std::cout << "hallo!" << std::endl;
GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
GstElementClass* element_class = GST_ELEMENT_CLASS (klass);
GstBaseTransformClass* trans_class = GST_BASE_TRANSFORM_CLASS (klass);
gobject_class->finalize = gst_beat_detector_finalize;
GST_DEBUG_CATEGORY_INIT (beat_detector_debug, "beat_detector", 0, "Beat detection");
gst_element_class_add_static_pad_template (element_class, &sink_template_factory);
gst_element_class_add_static_pad_template (element_class, &src_template_factory);
gst_element_class_set_static_metadata (element_class, "BeatDetector",
"Filter/Analyzer/Audio",
"BeatDetector operating of a stream of audio/raw",
"Unknown");
trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_beat_detector_transform_ip);
trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_level_set_caps);
}
static void gst_beat_detector_init(GstBeatDetector* filter) {
gst_audio_info_init (&filter->info);
std::cout << "Init audio info." << std::endl;
gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
// filterBass.setLowPass(150, 1.5, 44100);
// filterSnare.setBandPass(5000, 1.5, 44100);
beatDetection.setSampleRate(44100);
bassDetection.setSampleRate(44100);
beatDetection2.setSampleRate(44100);
}
static void gst_beat_detector_finalize(GObject* obj) {
//GstBeatDetector *filter = GST_BEAT_DETECTOR (obj);
}
/* REGISTER PLUGIN */
static gboolean plugin_init(GstPlugin* plugin) {
return gst_element_register(plugin, "beat_detector", GST_RANK_NONE, GST_TYPE_BEAT_DETECTOR);
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
beat_detector,
"Audio beat detecting plugin",
plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);