diff options
author | Chris Johnson <jinx6568@sover.net> | 2019-01-27 21:13:54 -0500 |
---|---|---|
committer | Chris Johnson <jinx6568@sover.net> | 2019-01-27 21:13:54 -0500 |
commit | 966f2d253cd2ee6ce140ad68095a20a9d2b63052 (patch) | |
tree | b0400d95bd06512531ade6ddf55190a58b6a5623 /plugins/MacVST/DitherFloat/source | |
parent | 0887543349dbbec0721a1fc8b1c7deba9afefa8b (diff) | |
download | airwindows-lv2-port-966f2d253cd2ee6ce140ad68095a20a9d2b63052.tar.gz airwindows-lv2-port-966f2d253cd2ee6ce140ad68095a20a9d2b63052.tar.bz2 airwindows-lv2-port-966f2d253cd2ee6ce140ad68095a20a9d2b63052.zip |
Floating Point Dither For All
Diffstat (limited to 'plugins/MacVST/DitherFloat/source')
-rwxr-xr-x | plugins/MacVST/DitherFloat/source/DitherFloat.cpp | 127 | ||||
-rwxr-xr-x | plugins/MacVST/DitherFloat/source/DitherFloat.h | 64 | ||||
-rwxr-xr-x | plugins/MacVST/DitherFloat/source/DitherFloatProc.cpp | 184 |
3 files changed, 375 insertions, 0 deletions
diff --git a/plugins/MacVST/DitherFloat/source/DitherFloat.cpp b/plugins/MacVST/DitherFloat/source/DitherFloat.cpp new file mode 100755 index 0000000..ffd9e94 --- /dev/null +++ b/plugins/MacVST/DitherFloat/source/DitherFloat.cpp @@ -0,0 +1,127 @@ +/* ======================================== + * DitherFloat - DitherFloat.h + * Copyright (c) 2016 airwindows, All rights reserved + * ======================================== */ + +#ifndef __DitherFloat_H +#include "DitherFloat.h" +#endif + +AudioEffect* createEffectInstance(audioMasterCallback audioMaster) {return new DitherFloat(audioMaster);} + +DitherFloat::DitherFloat(audioMasterCallback audioMaster) : + AudioEffectX(audioMaster, kNumPrograms, kNumParameters) +{ + A = 0.0; + B = 1.0; + fpNShapeL = 0.0; + fpNShapeR = 0.0; + //this is reset: values being initialized only once. Startup values, whatever they are. + + _canDo.insert("plugAsChannelInsert"); // plug-in can be used as a channel insert effect. + _canDo.insert("plugAsSend"); // plug-in can be used as a send effect. + _canDo.insert("x2in2out"); + setNumInputs(kNumInputs); + setNumOutputs(kNumOutputs); + setUniqueID(kUniqueId); + canProcessReplacing(); // supports output replacing + canDoubleReplacing(); // supports double precision processing + programsAreChunks(true); + vst_strncpy (_programName, "Default", kVstMaxProgNameLen); // default program name +} + +DitherFloat::~DitherFloat() {} +VstInt32 DitherFloat::getVendorVersion () {return 1000;} +void DitherFloat::setProgramName(char *name) {vst_strncpy (_programName, name, kVstMaxProgNameLen);} +void DitherFloat::getProgramName(char *name) {vst_strncpy (name, _programName, kVstMaxProgNameLen);} +//airwindows likes to ignore this stuff. Make your own programs, and make a different plugin rather than +//trying to do versioning and preventing people from using older versions. Maybe they like the old one! + +static float pinParameter(float data) +{ + if (data < 0.0f) return 0.0f; + if (data > 1.0f) return 1.0f; + return data; +} + +VstInt32 DitherFloat::getChunk (void** data, bool isPreset) +{ + float *chunkData = (float *)calloc(kNumParameters, sizeof(float)); + chunkData[0] = A; + chunkData[1] = B; + /* Note: The way this is set up, it will break if you manage to save settings on an Intel + machine and load them on a PPC Mac. However, it's fine if you stick to the machine you + started with. */ + + *data = chunkData; + return kNumParameters * sizeof(float); +} + +VstInt32 DitherFloat::setChunk (void* data, VstInt32 byteSize, bool isPreset) +{ + float *chunkData = (float *)data; + A = pinParameter(chunkData[0]); + B = pinParameter(chunkData[1]); + /* We're ignoring byteSize as we found it to be a filthy liar */ + + /* calculate any other fields you need here - you could copy in + code from setParameter() here. */ + return 0; +} + +void DitherFloat::setParameter(VstInt32 index, float value) { + switch (index) { + case kParamA: A = value; break; + case kParamB: B = value; break; + default: throw; // unknown parameter, shouldn't happen! + } +} + +float DitherFloat::getParameter(VstInt32 index) { + switch (index) { + case kParamA: return A; break; + case kParamB: return B; break; + default: break; // unknown parameter, shouldn't happen! + } return 0.0; //we only need to update the relevant name, this is simple to manage +} + +void DitherFloat::getParameterName(VstInt32 index, char *text) { + switch (index) { + case kParamA: vst_strncpy (text, "Offset", kVstMaxParamStrLen); break; + case kParamB: vst_strncpy (text, "Dither", kVstMaxParamStrLen); break; + default: break; // unknown parameter, shouldn't happen! + } //this is our labels for displaying in the VST host +} + +void DitherFloat::getParameterDisplay(VstInt32 index, char *text) { + switch (index) { + case kParamA: int2string ((VstInt32)(A * 32), text, kVstMaxParamStrLen); break; + case kParamB: float2string (B, text, kVstMaxParamStrLen); break; + default: break; // unknown parameter, shouldn't happen! + } //this displays the values and handles 'popups' where it's discrete choices +} + +void DitherFloat::getParameterLabel(VstInt32 index, char *text) { + switch (index) { + case kParamA: vst_strncpy (text, "", kVstMaxParamStrLen); break; + case kParamB: vst_strncpy (text, "", kVstMaxParamStrLen); break; + default: break; // unknown parameter, shouldn't happen! + } +} + +VstInt32 DitherFloat::canDo(char *text) +{ return (_canDo.find(text) == _canDo.end()) ? -1: 1; } // 1 = yes, -1 = no, 0 = don't know + +bool DitherFloat::getEffectName(char* name) { + vst_strncpy(name, "DitherFloat", kVstMaxProductStrLen); return true; +} + +VstPlugCategory DitherFloat::getPlugCategory() {return kPlugCategEffect;} + +bool DitherFloat::getProductString(char* text) { + vst_strncpy (text, "airwindows DitherFloat", kVstMaxProductStrLen); return true; +} + +bool DitherFloat::getVendorString(char* text) { + vst_strncpy (text, "airwindows", kVstMaxVendorStrLen); return true; +} diff --git a/plugins/MacVST/DitherFloat/source/DitherFloat.h b/plugins/MacVST/DitherFloat/source/DitherFloat.h new file mode 100755 index 0000000..b8ddf51 --- /dev/null +++ b/plugins/MacVST/DitherFloat/source/DitherFloat.h @@ -0,0 +1,64 @@ +/* ======================================== + * DitherFloat - DitherFloat.h + * Created 8/12/11 by SPIAdmin + * Copyright (c) 2011 __MyCompanyName__, All rights reserved + * ======================================== */ + +#ifndef __DitherFloat_H +#define __DitherFloat_H + +#ifndef __audioeffect__ +#include "audioeffectx.h" +#endif + +#include <set> +#include <string> +#include <math.h> + +enum { + kParamA = 0, + kParamB = 1, + kNumParameters = 2 +}; // + +const int kNumPrograms = 0; +const int kNumInputs = 2; +const int kNumOutputs = 2; +const unsigned long kUniqueId = 'dthf'; //Change this to what the AU identity is! + +class DitherFloat : + public AudioEffectX +{ +public: + DitherFloat(audioMasterCallback audioMaster); + ~DitherFloat(); + virtual bool getEffectName(char* name); // The plug-in name + virtual VstPlugCategory getPlugCategory(); // The general category for the plug-in + virtual bool getProductString(char* text); // This is a unique plug-in string provided by Steinberg + virtual bool getVendorString(char* text); // Vendor info + virtual VstInt32 getVendorVersion(); // Version number + virtual void processReplacing (float** inputs, float** outputs, VstInt32 sampleFrames); + virtual void processDoubleReplacing (double** inputs, double** outputs, VstInt32 sampleFrames); + virtual void getProgramName(char *name); // read the name from the host + virtual void setProgramName(char *name); // changes the name of the preset displayed in the host + virtual VstInt32 getChunk (void** data, bool isPreset); + virtual VstInt32 setChunk (void* data, VstInt32 byteSize, bool isPreset); + virtual float getParameter(VstInt32 index); // get the parameter value at the specified index + virtual void setParameter(VstInt32 index, float value); // set the parameter at index to value + virtual void getParameterLabel(VstInt32 index, char *text); // label for the parameter (eg dB) + virtual void getParameterName(VstInt32 index, char *text); // name of the parameter + virtual void getParameterDisplay(VstInt32 index, char *text); // text description of the current value + virtual VstInt32 canDo(char *text); +private: + char _programName[kVstMaxProgNameLen + 1]; + std::set< std::string > _canDo; + + long double fpNShapeL; + long double fpNShapeR; + //default stuff + + float A; + float B; +}; + +#endif diff --git a/plugins/MacVST/DitherFloat/source/DitherFloatProc.cpp b/plugins/MacVST/DitherFloat/source/DitherFloatProc.cpp new file mode 100755 index 0000000..3c76a1a --- /dev/null +++ b/plugins/MacVST/DitherFloat/source/DitherFloatProc.cpp @@ -0,0 +1,184 @@ +/* ======================================== + * DitherFloat - DitherFloat.h + * Copyright (c) 2016 airwindows, All rights reserved + * ======================================== */ + +#ifndef __DitherFloat_H +#include "DitherFloat.h" +#endif + +void DitherFloat::processReplacing(float **inputs, float **outputs, VstInt32 sampleFrames) +{ + float* in1 = inputs[0]; + float* in2 = inputs[1]; + float* out1 = outputs[0]; + float* out2 = outputs[1]; + + double overallscale = 1.0; + overallscale /= 44100.0; + overallscale *= getSampleRate(); + + int floatOffset = (A * 32); + long double blend = B; + + long double gain = 0; + + switch (floatOffset) + { + case 0: gain = 1.0; break; + case 1: gain = 2.0; break; + case 2: gain = 4.0; break; + case 3: gain = 8.0; break; + case 4: gain = 16.0; break; + case 5: gain = 32.0; break; + case 6: gain = 64.0; break; + case 7: gain = 128.0; break; + case 8: gain = 256.0; break; + case 9: gain = 512.0; break; + case 10: gain = 1024.0; break; + case 11: gain = 2048.0; break; + case 12: gain = 4096.0; break; + case 13: gain = 8192.0; break; + case 14: gain = 16384.0; break; + case 15: gain = 32768.0; break; + case 16: gain = 65536.0; break; + case 17: gain = 131072.0; break; + case 18: gain = 262144.0; break; + case 19: gain = 524288.0; break; + case 20: gain = 1048576.0; break; + case 21: gain = 2097152.0; break; + case 22: gain = 4194304.0; break; + case 23: gain = 8388608.0; break; + case 24: gain = 16777216.0; break; + case 25: gain = 33554432.0; break; + case 26: gain = 67108864.0; break; + case 27: gain = 134217728.0; break; + case 28: gain = 268435456.0; break; + case 29: gain = 536870912.0; break; + case 30: gain = 1073741824.0; break; + case 31: gain = 2147483648.0; break; + case 32: gain = 4294967296.0; break; + } + //we are directly punching in the gain values rather than calculating them + + while (--sampleFrames >= 0) + { + long double inputSampleL = *in1 + (gain-1); + long double inputSampleR = *in2 + (gain-1); + + + + //stereo 32 bit dither, made small and tidy. + int expon; frexpf((float)inputSampleL, &expon); + long double dither = (rand()/(RAND_MAX*7.737125245533627e+25))*pow(2,expon+62) * blend; //remove 'blend' for real use, it's for the demo; + inputSampleL += (dither-fpNShapeL); fpNShapeL = dither; + frexpf((float)inputSampleR, &expon); + dither = (rand()/(RAND_MAX*7.737125245533627e+25))*pow(2,expon+62) * blend; //remove 'blend' for real use, it's for the demo; + inputSampleR += (dither-fpNShapeR); fpNShapeR = dither; + //end 32 bit dither + + + + inputSampleL = (float)inputSampleL; //equivalent of 'floor' for 32 bit floating point + inputSampleR = (float)inputSampleR; //equivalent of 'floor' for 32 bit floating point + //We do that separately, we're truncating to floating point WHILE heavily offset. + + *out1 = inputSampleL - (gain-1); + *out2 = inputSampleR - (gain-1); + + *in1++; + *in2++; + *out1++; + *out2++; + } +} + +void DitherFloat::processDoubleReplacing(double **inputs, double **outputs, VstInt32 sampleFrames) +{ + double* in1 = inputs[0]; + double* in2 = inputs[1]; + double* out1 = outputs[0]; + double* out2 = outputs[1]; + + double overallscale = 1.0; + overallscale /= 44100.0; + overallscale *= getSampleRate(); + + int floatOffset = (A * 32); + long double blend = B; + + long double gain = 0; + + switch (floatOffset) + { + case 0: gain = 1.0; break; + case 1: gain = 2.0; break; + case 2: gain = 4.0; break; + case 3: gain = 8.0; break; + case 4: gain = 16.0; break; + case 5: gain = 32.0; break; + case 6: gain = 64.0; break; + case 7: gain = 128.0; break; + case 8: gain = 256.0; break; + case 9: gain = 512.0; break; + case 10: gain = 1024.0; break; + case 11: gain = 2048.0; break; + case 12: gain = 4096.0; break; + case 13: gain = 8192.0; break; + case 14: gain = 16384.0; break; + case 15: gain = 32768.0; break; + case 16: gain = 65536.0; break; + case 17: gain = 131072.0; break; + case 18: gain = 262144.0; break; + case 19: gain = 524288.0; break; + case 20: gain = 1048576.0; break; + case 21: gain = 2097152.0; break; + case 22: gain = 4194304.0; break; + case 23: gain = 8388608.0; break; + case 24: gain = 16777216.0; break; + case 25: gain = 33554432.0; break; + case 26: gain = 67108864.0; break; + case 27: gain = 134217728.0; break; + case 28: gain = 268435456.0; break; + case 29: gain = 536870912.0; break; + case 30: gain = 1073741824.0; break; + case 31: gain = 2147483648.0; break; + case 32: gain = 4294967296.0; break; + } + //we are directly punching in the gain values rather than calculating them + + while (--sampleFrames >= 0) + { + long double inputSampleL = *in1 + (gain-1); + long double inputSampleR = *in2 + (gain-1); + + + + //stereo 32 bit dither, made small and tidy. + int expon; frexpf((float)inputSampleL, &expon); + long double dither = (rand()/(RAND_MAX*7.737125245533627e+25))*pow(2,expon+62) * blend; //remove 'blend' for real use, it's for the demo; + inputSampleL += (dither-fpNShapeL); fpNShapeL = dither; + frexpf((float)inputSampleR, &expon); + dither = (rand()/(RAND_MAX*7.737125245533627e+25))*pow(2,expon+62) * blend; //remove 'blend' for real use, it's for the demo; + inputSampleR += (dither-fpNShapeR); fpNShapeR = dither; + //end 32 bit dither + + + + inputSampleL = (float)inputSampleL; //equivalent of 'floor' for 32 bit floating point + inputSampleR = (float)inputSampleR; //equivalent of 'floor' for 32 bit floating point + //We do that separately, we're truncating to floating point WHILE heavily offset. + + //note for 64 bit version: this is not for actually dithering 64 bit floats! + //This is specifically for demonstrating the sound of 32 bit floating point dither + //even over a 64 bit buss. Therefore it should be using float, above! + + *out1 = inputSampleL - (gain-1); + *out2 = inputSampleR - (gain-1); + + *in1++; + *in2++; + *out1++; + *out2++; + } +} |