diff options
| author | mick grierson <mickgrierson@gmail.com> | 2015-08-16 14:48:19 +0100 |
|---|---|---|
| committer | mick grierson <mickgrierson@gmail.com> | 2015-08-16 14:48:19 +0100 |
| commit | b155008277cbdba3534ca392d67e2db199d9c458 (patch) | |
| tree | dcd17db2ac8c1e92c3c87d77cb6fb41bc2f84513 /maximilian.cpp | |
| parent | 18ab896876b2d7ecaf4a4eda26a14e0999a8b396 (diff) | |
updating core files
Diffstat (limited to 'maximilian.cpp')
| -rw-r--r-- | maximilian.cpp | 108 |
1 files changed, 99 insertions, 9 deletions
diff --git a/maximilian.cpp b/maximilian.cpp index 7ee5868..7ba4978 100644 --- a/maximilian.cpp +++ b/maximilian.cpp @@ -39,7 +39,7 @@ * Uncomment the following to include Sean Barrett's Ogg Vorbis decoder. * If you're on windows, make sure to add the files std_vorbis.c and std_vorbis.h to your project*/ -#define VORBIS +//#define VORBIS #ifdef VORBIS extern "C" { @@ -541,6 +541,7 @@ bool maxiSample::loadOgg(string fileName, int channel) { readChannel=channel; int channelx; // cout << fileName << endl; + free(temp); myDataSize = stb_vorbis_decode_filename(const_cast<char*>(fileName.c_str()), &channelx, &temp); result = myDataSize > 0; printf("\nchannels = %d\nlength = %d",channelx,myDataSize); @@ -565,6 +566,7 @@ bool maxiSample::loadOgg(string fileName, int channel) { //This sets the playback position to the start of a sample void maxiSample::trigger() { position = 0; + recordPosition = 0; } //This is the main read function. @@ -572,8 +574,8 @@ bool maxiSample::read() { bool result; ifstream inFile( myPath.c_str(), ios::in | ios::binary); - result = (bool) inFile; - if (inFile) { + result = inFile.is_open(); + if (result) { bool datafound = false; inFile.seekg(4, ios::beg); inFile.read( (char*) &myChunkSize, 4 ); // read the ChunkSize @@ -617,7 +619,7 @@ bool maxiSample::read() } // read the data chunk - myData = (char*) malloc(myDataSize * sizeof(char)); + char * myData = (char*) malloc(myDataSize * sizeof(char)); inFile.seekg(filePos, ios::beg); inFile.read(myData, myDataSize); length=myDataSize*(0.5/myChannels); @@ -633,6 +635,7 @@ bool maxiSample::read() position+=2; } } + free(temp); temp = (short*) malloc(myDataSize * sizeof(char)); memcpy(temp, myData, myDataSize * sizeof(char)); @@ -651,11 +654,35 @@ bool maxiSample::read() //This plays back at the correct speed. Always loops. double maxiSample::play() { position++; - if ((long) position == length) position=0; + if ((long) position >= length) position=0; output = (double) temp[(long)position]/32767.0; return output; } +void maxiSample::setPosition(double newPos) { + position = maxiMap::clamp<double>(newPos, 0.0, 1.0) * length; +} + +//start end and points are between 0 and 1 +double maxiSample::playLoop(double start, double end) { + position++; + if (position < length * start) position = length * start; + if ((long) position >= length * end) position = length * start; + output = (double) temp[(long)position]/32767.0; + return output; +} + +double maxiSample::playUntil(double end) { + position++; + if ((long) position<length * end) + output = (double) temp[(long)position]/32767.0; + else { + output=0; + } + return output; +} + + //This plays back at the correct speed. Only plays once. To retrigger, you have to manually reset the position double maxiSample::playOnce() { position++; @@ -1055,7 +1082,7 @@ double maxiSample::bufferPlay4(unsigned char &bufferin,double frequency, double void maxiSample::getLength() { - length=myDataSize*0.5; + length=myDataSize*0.5; } void maxiSample::setLength(unsigned long numSamples) { @@ -1073,13 +1100,76 @@ void maxiSample::setLength(unsigned long numSamples) { } void maxiSample::clear() { - memset(myData, 0, myDataSize); + memset(temp, 0, myDataSize); } void maxiSample::reset() { position=0; } +void maxiSample::normalise(float maxLevel) { + short maxValue = 0; + for(int i=0; i < length; i++) { + if (abs(temp[i]) > maxValue) { + maxValue = abs(temp[i]); + } + } + float scale = 32767.0 * maxLevel / (float) maxValue; + for(int i=0; i < length; i++) { + temp[i] = round(scale * (float) temp[i]); + } +} + +void maxiSample::autoTrim(float alpha, float threshold, bool trimStart, bool trimEnd) { + + int startMarker=0; + if(trimStart) { + maxiLagExp<float> startLag(alpha, 0); + while(startMarker < length) { + startLag.addSample(abs(temp[startMarker])); + if (startLag.value() > threshold) { + break; + } + startMarker++; + } + } + + int endMarker = length-1; + if(trimEnd) { + maxiLagExp<float> endLag(alpha, 0); + while(endMarker > 0) { + endLag.addSample(abs(temp[endMarker])); + if (endLag.value() > threshold) { + break; + } + endMarker--; + } + } + + cout << "Autotrim: start: " << startMarker << ", end: " << endMarker << endl; + + int newLength = endMarker - startMarker; + if (newLength > 0) { + short *newData = (short*) malloc(sizeof(short) * newLength); + for(int i=0; i < newLength; i++) { + newData[i] = temp[i+startMarker]; + } + free(temp); + temp = newData; + myDataSize = newLength * 2; + length=newLength; + position=0; + recordPosition=0; + //envelope the start + int fadeSize=min((long)100, length); + for(int i=0; i < fadeSize; i++) { + float factor = i / (float) fadeSize; + temp[i] = round(temp[i] * factor); + temp[length - 1 - i] = round(temp[length - 1 - i] * factor); + } + } +} + @@ -1381,11 +1471,11 @@ double convert::mtof(int midinote) { } -void maxiEnvelopeFollower::setAttack(double attackMS) { +template<> void maxiEnvelopeFollower::setAttack(double attackMS) { attack = pow( 0.01, 1.0 / ( attackMS * maxiSettings::sampleRate * 0.001 ) ); } -void maxiEnvelopeFollower::setRelease(double releaseMS) { +template<> void maxiEnvelopeFollower::setRelease(double releaseMS) { release = pow( 0.01, 1.0 / ( releaseMS * maxiSettings::sampleRate * 0.001 ) ); } |
