From 73f1a400608d93742e57c7ea3c4433eb1a2e1852 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 20 Sep 2013 22:23:23 +0200 Subject: =?UTF-8?q?New=20resample=20feature=20(greatly=20inspired=20by=20C?= =?UTF-8?q?=E9dric=20'SxDx')=20based=20on=20libsamplerate.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Makefile.am.drumgizmo | 2 +- src/audiofile.cc | 42 ++++++++++++++++++++++++++++++++++++++---- src/configuration.cc | 2 ++ src/configuration.h | 2 ++ src/drumgizmo.cc | 10 ++++++++++ src/drumgizmo.h | 3 +++ src/instrument.cc | 3 ++- 7 files changed, 58 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/Makefile.am.drumgizmo b/src/Makefile.am.drumgizmo index 47d54a2..c48c478 100644 --- a/src/Makefile.am.drumgizmo +++ b/src/Makefile.am.drumgizmo @@ -26,4 +26,4 @@ DRUMGIZMO_SOURCES = \ $(top_srcdir)/src/velocity.cc \ $(top_srcdir)/src/versionstr.cc -DRUMGIZMO_LIBS = $(SNDFILE_LIBS) $(EXPAT_LIBS) \ No newline at end of file +DRUMGIZMO_LIBS = $(SNDFILE_LIBS) $(EXPAT_LIBS) $(SAMPLERATE_LIBS) \ No newline at end of file diff --git a/src/audiofile.cc b/src/audiofile.cc index 37fb5cc..4ac1f94 100644 --- a/src/audiofile.cc +++ b/src/audiofile.cc @@ -31,9 +31,12 @@ #include #include +#include #include +#include "configuration.h" + #define LAZYLOAD AudioFile::AudioFile(std::string filename) @@ -63,13 +66,13 @@ void AudioFile::unload() { MutexAutolock l(mutex); if(data == preloaded_data) { - delete data; + delete[] data; data = NULL; size = 0; } else { size = 0; - delete data; + delete[] data; data = NULL; delete preloaded_data; preloaded_data = NULL; @@ -98,7 +101,7 @@ void AudioFile::init() sample_t* data = new sample_t[size]; size = sf_read_float(fh, data, size); - + //DEBUG(audiofile,"Lazy loaded %d samples\n", size); sf_close(fh); @@ -191,7 +194,13 @@ void AudioFile::load(int num_samples) size = sf_info.frames; - if(num_samples != ALL_SAMPLES && (int)size > num_samples) size = num_samples; + double ratio = (double)Conf::samplerate / (double)sf_info.samplerate; + + if(num_samples != ALL_SAMPLES) { + // Make sure we read enough samples, even after conversion. + num_samples /= ratio; + if((int)size > num_samples) size = num_samples; + } sample_t* data = new sample_t[size]; size = sf_read_float(fh, data, size); @@ -200,6 +209,31 @@ void AudioFile::load(int num_samples) sf_close(fh); + if(Conf::samplerate != sf_info.samplerate) { + // Resample data... + size_t osize = size * ratio; + sample_t *odata = new sample_t[osize]; + + SRC_DATA src; + src.data_in = data; + src.input_frames = size; + + src.data_out = odata; + src.output_frames = osize; + + src.src_ratio = ratio; + + // Do the conversion + src_simple(&src, SRC_SINC_BEST_QUALITY, 1); + + delete[] data; + data = odata; + size = src.output_frames; + + DEBUG(audiofile,"Converted into %d samples %p\n", size, this); + } + + mutex.lock(); this->data = data; is_loaded = true; diff --git a/src/configuration.cc b/src/configuration.cc index b71dfac..965dcf9 100644 --- a/src/configuration.cc +++ b/src/configuration.cc @@ -33,6 +33,8 @@ float Conf::velocity_modifier_weight = 0.25; bool Conf::enable_velocity_randomiser = false; float Conf::velocity_randomiser_weight = 0.1; +int Conf::samplerate = 44100; + #ifdef TEST_CONFIGURATION //Additional dependency files //deps: diff --git a/src/configuration.h b/src/configuration.h index 2690104..c49f997 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -34,6 +34,8 @@ namespace Conf { extern bool enable_velocity_randomiser; extern float velocity_randomiser_weight; + + extern int samplerate; }; diff --git a/src/drumgizmo.cc b/src/drumgizmo.cc index 4732c27..b99687d 100644 --- a/src/drumgizmo.cc +++ b/src/drumgizmo.cc @@ -406,6 +406,16 @@ void DrumGizmo::stop() // engine.stop(); } +int DrumGizmo::samplerate() +{ + return Conf::samplerate; +} + +void DrumGizmo::setSamplerate(int samplerate) +{ + Conf::samplerate = samplerate; +} + std::string float2str(float a) { char buf[256]; diff --git a/src/drumgizmo.h b/src/drumgizmo.h index 323c991..2ef0aeb 100644 --- a/src/drumgizmo.h +++ b/src/drumgizmo.h @@ -73,6 +73,9 @@ public: void handleMessage(Message *msg); + int samplerate(); + void setSamplerate(int samplerate); + private: DrumKitLoader loader; diff --git a/src/instrument.cc b/src/instrument.cc index 1175f69..43ee4ac 100644 --- a/src/instrument.cc +++ b/src/instrument.cc @@ -83,7 +83,8 @@ Sample *Instrument::sample(level_t level, size_t pos) } if(Conf::enable_velocity_modifier) { - mod += (pos - lastpos) / (44100.0 * Conf::velocity_modifier_falloff); + mod += (pos - lastpos) / + (Conf::samplerate * Conf::velocity_modifier_falloff); if(mod > 1.0) mod = 1.0; } -- cgit v1.2.3