Skip to content

Commit

Permalink
filter and delay
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskiefer committed May 4, 2020
1 parent 9d948f0 commit fb08bbe
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 99 deletions.
3 changes: 2 additions & 1 deletion js/purejs/module-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Module.maxiBiquad = maxiBiquad;
Module.maxiOsc = maxiOsc;
Module.maxiRatioSeq = maxiRatioSeq;
Module.maxiIndex = maxiIndex;
Module.maxiRatioSeq = maxiRatioSeq;
Module.maxiFilter = maxiFilter;
Module.maxiDelayline = maxiDelayline;

// Module.cheerpTypes = cheerpTypes;
// Module.maxiFilter = maxiFilter;
Expand Down
60 changes: 29 additions & 31 deletions src/maximilian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,9 @@ void maxiMix::ambisonic(double input,std::vector<double>&eight,double x,double y
}
// --------------------------------------------------------------------------------
// MAXI_SAMPLE
//This is the maxiSample load function. It just calls read.
bool maxiSample::load(string fileName, int channel) {
myPath = fileName;
readChannel=channel;
return read();
}


maxiSample::maxiSample():position(0), recordPosition(0), myChannels(1), mySampleRate(maxiSettings::sampleRate) {};

// This is for OGG loading
bool maxiSample::loadOgg(string fileName, int channel) {
Expand Down Expand Up @@ -605,16 +602,6 @@ bool maxiSample::isReady(){
return false;
}

void maxiSample::setSample(vector<double>& sampleData){
amplitudes = sampleData;
mySampleRate = 44100;
position=amplitudes.size()-1;
}

void maxiSample::setSample(vector<double>& sampleData, int sampleRate){
setSample(sampleData);
mySampleRate = sampleRate;
}

//void maxiSample::setSampleChar(vector<char>& temp){
// this->temp = temp.data();
Expand All @@ -629,6 +616,15 @@ void maxiSample::trigger() {
recordPosition = 0;
}

#ifndef CHEERP

//This is the maxiSample load function. It just calls read.
bool maxiSample::load(string fileName, int channel) {
myPath = fileName;
readChannel=channel;
return read();
}

//This is the main read function.
bool maxiSample::read()
{
Expand Down Expand Up @@ -712,21 +708,6 @@ bool maxiSample::read()
return result; // this should probably be something more descriptive
}

// -----------------


//This plays back at the correct speed. Always loops.
double maxiSample::play() {
position++;
if ((long) position >= amplitudes.size()) position=0;
output = amplitudes[(long)position];
return output;
}

void maxiSample::setPosition(double newPos) {
position = maxiMap::clamp(newPos, 0.0, 1.0) * amplitudes.size();
}

bool maxiSample::save() {
return save(myPath);
}
Expand Down Expand Up @@ -760,6 +741,23 @@ bool maxiSample::save(string filename)
return true;
}

#endif
// -----------------


//This plays back at the correct speed. Always loops.
double maxiSample::play() {
position++;
if ((long) position >= amplitudes.size()) position=0;
output = amplitudes[(long)position];
return output;
}

void maxiSample::setPosition(double newPos) {
position = maxiMap::clamp(newPos, 0.0, 1.0) * amplitudes.size();
}


char *maxiSample::getSummary()
{
char *summary = new char[250];
Expand Down
142 changes: 75 additions & 67 deletions src/maximilian.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class maxiEnvelope {
};


class maxiDelayline {
class CHEERP_EXPORT maxiDelayline {
double frequency;
int phase;
double startphase;
Expand All @@ -216,7 +216,7 @@ class maxiDelayline {
};


class maxiFilter {
class CHEERP_EXPORT maxiFilter {
private:
double gain;
double input;
Expand Down Expand Up @@ -249,11 +249,11 @@ class maxiFilter {
resonance = res;
}

double getCutoff() const{
double getCutoff() {
return cutoff;
}

double getResonance() const{
double getResonance() {
return resonance;
}
// ------------------------------------------------
Expand Down Expand Up @@ -385,101 +385,109 @@ class maxiSample {
// int myDataSize;
short myChannels;
int mySampleRate;
inline unsigned long getLength() const {return amplitudes.size();};
void setLength(unsigned long numSamples);
short myBitsPerSample;
inline unsigned long getLength() const {return amplitudes.size();};
void setLength(unsigned long numSamples);
short myBitsPerSample;

vector<double> amplitudes;

~maxiSample() {}
maxiSample();

maxiSample():position(0), recordPosition(0), myChannels(1), mySampleRate(maxiSettings::sampleRate) {};

maxiSample& operator=(const maxiSample &source) {
if (this == &source)
return *this;
position=0;
recordPosition = 0;
myChannels = source.myChannels;
mySampleRate = maxiSettings::sampleRate;
amplitudes.clear();
amplitudes = source.amplitudes;
return *this;
maxiSample& operator=(const maxiSample &source) {
if (this == &source)
return *this;
position=0;
recordPosition = 0;
myChannels = source.myChannels;
mySampleRate = maxiSettings::sampleRate;
amplitudes.clear();
amplitudes = source.amplitudes;
return *this;
}

#ifndef CHEERP
bool load(string fileName, int channel=0);

bool loadOgg(string filename,int channel=0);
int setSampleFromOggBlob(vector<unsigned char> &oggBlob, int channel=0);

bool save();
bool save(string filename);
// read a wav file into this class
bool read();
#endif
// -------------------------
// js bits
// as loading is currently asynchronous in js this can be useful
bool isReady();

void setSample(vector<double>& temp);
void setSample(vector<double>& temp, int sampleRate);
bool loadOgg(string filename,int channel=0);
int setSampleFromOggBlob(vector<unsigned char> &oggBlob, int channel=0);

// void setSample(vector<double>& temp);
// void setSample(vector<double>& temp, int sampleRate);
void setSample(DOUBLEARRAY _sampleData){
NORMALISE_ARRAY_TYPE(_sampleData, sampleData)
amplitudes = sampleData;
mySampleRate = 44100;
position=amplitudes.size()-1;
}

void setSample(DOUBLEARRAY _sampleData, int sampleRate){
// void setSample(vector<double>& sampleData, int sampleRate){
setSample(_sampleData);
mySampleRate = sampleRate;
}

void clear(){amplitudes.clear();}
// -------------------------

void trigger();

// read a wav file into this class
bool read();

void loopRecord(double newSample, const bool recordEnabled, const double recordMix, double start = 0.0, double end = 1.0) {
loopRecordLag.addSample(recordEnabled);
if (recordPosition < start * amplitudes.size()) recordPosition = start * amplitudes.size();
if(recordEnabled) {
double currentSample = amplitudes[(unsigned long)recordPosition] / 32767.0;
newSample = (recordMix * currentSample) + ((1.0 - recordMix) * newSample);
newSample *= loopRecordLag.value();
amplitudes[(unsigned long)recordPosition] = newSample * 32767;
}
++recordPosition;
if (recordPosition >= end * amplitudes.size())
recordPosition= start * amplitudes.size();
}

void reset();

double play();
void loopRecord(double newSample, const bool recordEnabled, const double recordMix, double start = 0.0, double end = 1.0) {
loopRecordLag.addSample(recordEnabled);
if (recordPosition < start * amplitudes.size()) recordPosition = start * amplitudes.size();
if(recordEnabled) {
double currentSample = amplitudes[(unsigned long)recordPosition] / 32767.0;
newSample = (recordMix * currentSample) + ((1.0 - recordMix) * newSample);
newSample *= loopRecordLag.value();
amplitudes[(unsigned long)recordPosition] = newSample * 32767;
}
++recordPosition;
if (recordPosition >= end * amplitudes.size())
recordPosition= start * amplitudes.size();
}

double playLoop(double start, double end); // start and end are between 0.0 and 1.0
void reset();

double playOnce();
double playOnZX(double trigger);
double playOnZX(double trig, double speed);
double playOnZX(double trig, double speed, double offset);
double play();

double loopSetPosOnZX(double trigger, double position); // position between 0 and 1.0
maxiTrigger zxTrig;
// double prevTriggerVal=1;
double playLoop(double start, double end); // start and end are between 0.0 and 1.0

double playOnce(double speed);
double playOnce();
double playOnZX(double trigger);
double playOnZX(double trig, double speed);
double playOnZX(double trig, double speed, double offset);

void setPosition(double newPos); // between 0.0 and 1.0
double loopSetPosOnZX(double trigger, double position); // position between 0 and 1.0
maxiTrigger zxTrig;
// double prevTriggerVal=1;

double playUntil(double end);
double playOnce(double speed);

double play(double speed);
void setPosition(double newPos); // between 0.0 and 1.0

double play(double frequency, double start, double end, double &pos);
double playUntil(double end);

double play(double frequency, double start, double end);
double play(double speed);

double play4(double frequency, double start, double end);
double play(double frequency, double start, double end, double &pos);

double play(double frequency, double start, double end);

bool save();
bool save(string filename);
double play4(double frequency, double start, double end);

// return a printable summary of the wav file
char *getSummary();
// return a printable summary of the wav file
char *getSummary();

void normalise(double maxLevel = 0.99); //0 < maxLevel < 1.0
void autoTrim(float alpha = 0.3, float threshold = 6000, bool trimStart = true, bool trimEnd = true); //alpha of lag filter (lower == slower reaction), threshold to mark start and end, < 32767
void normalise(double maxLevel = 0.99); //0 < maxLevel < 1.0
void autoTrim(float alpha = 0.3, float threshold = 6000, bool trimStart = true, bool trimEnd = true); //alpha of lag filter (lower == slower reaction), threshold to mark start and end, < 32767
};


Expand Down

0 comments on commit fb08bbe

Please sign in to comment.