summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2013-09-20 22:23:23 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2013-09-20 22:23:23 +0200
commit73f1a400608d93742e57c7ea3c4433eb1a2e1852 (patch)
treeeb6b500127e095e5bad124927054dc73f44814dc
parent3d0ed93889afb5ae739d2998e33178689c2de965 (diff)
New resample feature (greatly inspired by Cédric 'SxDx') based on libsamplerate.
-rw-r--r--lv2/lv2.cc1
-rw-r--r--src/Makefile.am.drumgizmo2
-rw-r--r--src/audiofile.cc42
-rw-r--r--src/configuration.cc2
-rw-r--r--src/configuration.h2
-rw-r--r--src/drumgizmo.cc10
-rw-r--r--src/drumgizmo.h3
-rw-r--r--src/instrument.cc3
8 files changed, 59 insertions, 6 deletions
diff --git a/lv2/lv2.cc b/lv2/lv2.cc
index cadea72..48db06a 100644
--- a/lv2/lv2.cc
+++ b/lv2/lv2.cc
@@ -261,6 +261,7 @@ LV2_Handle instantiate(const struct _LV2_Descriptor *descriptor,
*/
dglv2->dg = new DrumGizmo(dglv2->out, dglv2->in);
+ dglv2->dg->setSamplerate(sample_rate);
// dglv2->dg->loadkit(getenv("DRUMGIZMO_DRUMKIT"));
// dglv2->dg->init(true);
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 <unistd.h>
#include <sndfile.h>
+#include <samplerate.h>
#include <hugin.hpp>
+#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;
}