From ef523065eb5e9535d0bd4b3ede820a231e547930 Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 25 Jul 2008 11:28:27 +0000 Subject: First primitive beatdetection in place. --- src/Makefile.am | 2 ++ src/beatmapper.cc | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/beatmapper.h | 54 +++++++++++++++++++++++++++++++++++++++ src/drumgizmo.cc | 75 ++++++++++++++++++++++++++++++++++++++++++------------- src/jackclient.cc | 43 ++++++++++++++++++++++++++++--- src/jackclient.h | 4 ++- src/midimapper.h | 1 - 7 files changed, 226 insertions(+), 23 deletions(-) create mode 100644 src/beatmapper.cc create mode 100644 src/beatmapper.h diff --git a/src/Makefile.am b/src/Makefile.am index 7bcc60b..6de8c3f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,6 +6,7 @@ drumgizmo_CXXFLAGS = $(JACK_CXXFLAGS) $(SNDFILE_CXXFLAGS) drumgizmo_SOURCES = \ audiofile.cc \ + beatmapper.cc \ channel.cc \ drumgizmo.cc \ drumkitparser.cc \ @@ -19,6 +20,7 @@ drumgizmo_SOURCES = \ EXTRA_DIST = \ audiofile.h \ + beatmapper.h \ channel.h \ drumkit.h \ drumkitparser.h \ diff --git a/src/beatmapper.cc b/src/beatmapper.cc new file mode 100644 index 0000000..6a7e35a --- /dev/null +++ b/src/beatmapper.cc @@ -0,0 +1,70 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * beatmapper.cc + * + * Fri Jul 25 11:17:42 CEST 2008 + * Copyright 2008 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * DrumGizmo 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "beatmapper.h" + +BeatMapper::BeatMapper(Instrument *instrument) +{ + this->instrument = instrument; + for(size_t i = 0; i < HISTORY_SIZE; i++) hist[i] = 0.0; + C = 1.3; + mindist = 2; + last = mindist; +} + + +Sample *BeatMapper::map(jack_nframes_t nframes) +{ + Sample *sample = NULL; + + jack_default_audio_sample_t *buffer; + buffer = (jack_default_audio_sample_t *)jack_port_get_buffer(instrument->port, nframes); + + float e = 0.0; + for(size_t i = 0; i < nframes; i++) { + e += buffer[i] * buffer[i]; + // buffer[i] = 0.0; + } + + float E = 0.0; + for(size_t i = 0; i < HISTORY_SIZE; i++) E += hist[i] / (float)HISTORY_SIZE; + + printf("last: %d, E: %f, e: %f - threshold: %f\n", last, E, e, 1.3 * E); + + // Shift history and save new value + for(size_t i = 0; i < HISTORY_SIZE - 1; i++) hist[i] = hist[i+1]; + hist[HISTORY_SIZE - 1] = e; + + if(e > C * E && last > mindist) { + Velocity *v = instrument->getVelocity(127); + if(v) sample = v->getSample(); + } + + last++; + if(sample) last = 0; + + return sample; +} diff --git a/src/beatmapper.h b/src/beatmapper.h new file mode 100644 index 0000000..c57164b --- /dev/null +++ b/src/beatmapper.h @@ -0,0 +1,54 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * beatmapper.h + * + * Fri Jul 25 11:17:42 CEST 2008 + * Copyright 2008 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * DrumGizmo 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __DRUMGIZMO_BEATMAPPER_H__ +#define __DRUMGIZMO_BEATMAPPER_H__ + +#include + +#include "instrument.h" +#include "event.h" + +#include + +#define HISTORY_SIZE 43 + +class BeatMapper { +public: + BeatMapper(Instrument *instrument); + + Sample *map(jack_nframes_t nframes); + +private: + Instrument *instrument; + float hist[HISTORY_SIZE]; + float C; + + size_t mindist; + size_t last; +}; + +#endif/*__DRUMGIZMO_BEATMAPPER_H__*/ diff --git a/src/drumgizmo.cc b/src/drumgizmo.cc index 649b364..b3b7473 100644 --- a/src/drumgizmo.cc +++ b/src/drumgizmo.cc @@ -28,6 +28,63 @@ #include "drumkitparser.h" +int main(int argc, char *argv[]) +{ + DrumKitParser parser("/tmp/aasimonster/aasimonster.xml"); + if(parser.parse()) return 1; + + JackClient client(parser.getDrumkit()); + + client.activate(); + + // sendMidi(); + + while(1) sleep(1); + + return 0; +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#if 0 #include #include @@ -78,20 +135,4 @@ void sendMidi() jack_connect(jack_client, "DrumGizmo:output_1", "system:playback_1"); jack_connect(jack_client, "DrumGizmo:output_2", "system:playback_2"); } - -int main(int argc, char *argv[]) -{ - DrumKitParser parser("/tmp/aasimonster/aasimonster.xml"); - if(parser.parse()) return 1; - - JackClient client(parser.getDrumkit()); - - client.activate(); - - // sendMidi(); - - while(1) sleep(1); - - return 0; -} - +#endif/*0*/ diff --git a/src/jackclient.cc b/src/jackclient.cc index 00eda52..cac1b74 100644 --- a/src/jackclient.cc +++ b/src/jackclient.cc @@ -73,7 +73,11 @@ JackClient::JackClient(DrumKit *drumkit) JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsTerminal, 0); - input_ports.push_back(instrument->port); + + BeatMapper *beatmapper = new BeatMapper(instrument); + beatmappers.push_back(beatmapper); + + // input_ports.push_back(instrument->port); ii++; } @@ -117,6 +121,9 @@ void JackClient::shutdown() int JackClient::process(jack_nframes_t nframes) { + // + // Look for midi input + // void *midibuffer = jack_port_get_buffer(midi_port, nframes); jack_nframes_t midievents = jack_midi_get_event_count(midibuffer); for(jack_nframes_t i = 0; i < midievents; i++) { @@ -140,10 +147,38 @@ int JackClient::process(jack_nframes_t nframes) } } - jack_midi_clear_buffer(midibuffer); + // + // Look for audio trigger input + // + // std::vector< BeatMapper >::iterator bi = beatmappers.begin(); + // while(bi != beatmappers.end()) { + for(size_t bi = 0; bi < beatmappers.size(); bi++) { + BeatMapper *beatmapper = beatmappers[bi];//*bi; + + Sample *sample = beatmapper->map(nframes); + if(!sample) continue; + + AudioFiles::iterator ai = sample->audiofiles.begin(); + while(ai != sample->audiofiles.end()) { + AudioFile *audiofile = ai->second; + audiofile->load(); + + if(drumkit->channels.find(audiofile->channel) != drumkit->channels.end()) { + Channel *channel = drumkit->channels[audiofile->channel]; + Event event(channel->port, audiofile, 0); + events.insert(event); + } + ai++; + } + + //bi++; + } + + // // Reset ports + // Ports::iterator pi = output_ports.begin(); while(pi != output_ports.end()) { @@ -159,9 +194,9 @@ int JackClient::process(jack_nframes_t nframes) Events nextevents; - // printf("Events %d\n", events.size()); - + // // Handle events + // Events::iterator ei = events.begin(); while(ei != events.end()) { diff --git a/src/jackclient.h b/src/jackclient.h index 8efa654..21e1615 100644 --- a/src/jackclient.h +++ b/src/jackclient.h @@ -36,6 +36,7 @@ #include "event.h" #include "sample.h" #include "midimapper.h" +#include "beatmapper.h" typedef std::vector< jack_port_t *> Ports; @@ -59,7 +60,7 @@ public: private: jack_client_t *jack_client; - Ports input_ports; + // Ports input_ports; Ports output_ports; jack_port_t *midi_port; @@ -68,6 +69,7 @@ private: DrumKit *drumkit; MidiMapper midimapper; + std::vector< BeatMapper* > beatmappers; }; #endif/*__DRUMGIZMO_JACKCLIENT_H__*/ diff --git a/src/midimapper.h b/src/midimapper.h index a967159..c600d4f 100644 --- a/src/midimapper.h +++ b/src/midimapper.h @@ -40,7 +40,6 @@ public: Sample *map(jack_midi_event_t event); private: - std::map< int, int > _map; DrumKit *drumkit; }; -- cgit v1.2.3