private static double GetTableSample(EOscillatorType oscillatorType, double t) { switch (oscillatorType) { case EOscillatorType.Sine: return(Math.Sin(DSPFunctions.Pi2 * t)); case EOscillatorType.Triangle: if (t < 0.25) { return(4 * t); } if (t < 0.75) { return(2 - 4 * t); } return(4 * (t - 1)); case EOscillatorType.Square: return((t < 0.5f) ? 1 : -1); case EOscillatorType.Saw: return(2 * t - 1); case EOscillatorType.Noise: return(_random.NextDouble() * 2 - 1); default: throw new ArgumentOutOfRangeException(); } }
double naiveWaveformForMode(EOscillatorType mode) { switch (mode) { case EOscillatorType.Sine: return(Math.Sin(phase)); case EOscillatorType.Saw: return((2.0 * phase / twoPI) - 1.0); case EOscillatorType.Square: if (phase < Math.PI) { return(1.0); } else { return(-1.0); } case EOscillatorType.Noise: return(_random.NextDouble() * 2 - 1); default: throw new ArgumentOutOfRangeException(); } }
private static double GenerateNextSample(EOscillatorType oscillatorType, double frequency, double time) { var ph = time * frequency; ph -= (int)ph; return(GetTableSample(oscillatorType, ph)); }
public Oscillator(AudioProcessor audioProcessor, OscParameters paramOwner, int note, double detune, double phase) : base(audioProcessor) { audioProcessor.OnSampleRateChanged += SampleRateChanged; paramOwner.OscillatorType.OnValueChange += TypeChanged; type = paramOwner.OscillatorType.Value; calc = twoPI / Processor.SampleRate; this.note = note; this.phase = phase; this.detune = detune; this.paramOwner = paramOwner; }
void SetOsc(float baseFreq, EOscillatorType osctype) { ClearWaveTable(); var sampleRate = paramOwner.Processor.SampleRate; // calc number of harmonics where the highest harmonic baseFreq and lowest alias an octave higher would meet int maxHarms = (int)(sampleRate / (3.0 * baseFreq) + 0.5); // round up to nearest power of two uint v = (uint)maxHarms; v--; // so we don't go up if already a power of 2 v |= v >> 1; // roll the highest bit into all lower bits... v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; // and increment to power of 2 int tableLen = (int)(v * 2 * overSamp); // double for the sample rate, then oversampling double[] ar = new double[tableLen]; // for ifft double[] ai = new double[tableLen]; double topFreq = baseFreq * 2.0 / sampleRate; double scale = 0.0; for (; maxHarms >= 1; maxHarms >>= 1) { if (osctype == EOscillatorType.Saw) { DefineSawtooth(tableLen, maxHarms, ar, ai); } else if (osctype == EOscillatorType.Triangle) { DefineTriangle(tableLen, maxHarms, ar, ai); } else if (osctype == EOscillatorType.Square) { DefineSquare(tableLen, maxHarms, ar, ai); } scale = makeWaveTable(tableLen, ar, ai, scale, topFreq); topFreq *= 2; if (tableLen > 99999) // variable table size (constant oversampling but with minimum table size) { tableLen >>= 1; } } }
private void TypeChanged(Parameter.EChangeType obj) { type = paramOwner.OscillatorType.Value; }