/// <summary> /// Collapses the tone to the nearest chromatic note /// </summary> public FeedbackChords( bool major, double lateralization, double tone, NoteProgression progression = NoteProgression.Scale, int octaveOffset = 0, int octaveSpan = 3) { this.major = major; IReadOnlyList <FrequencySet> scale = GetSequence(progression); int notes = scale.Count * octaveSpan + 1; int note = (int)Math.Floor(notes * tone); note = GeneralMath.Clamp(note, 0, notes - 1); int octaves = octaveOffset + note / scale.Count; int noteIndex = note % scale.Count; fundamentalFreq = BaseFreqLB * Math.Pow(2, octaves) * scale[noteIndex].freqRatio; Duration = major ? 0.2 : 0.4; //Angle is between -Range/2 and +Range/2 angle = Range * (lateralization - 0.5); BuildStream(); }
public override void Seek(int position) { position = GeneralMath.Clamp(position, 0, ChannelSamples); stream.Seek(position); this.position = position; if (position < attackUpEndSample) { envelopeState = EnvelopeState.AttackUp; currentEnvelope = ENVELOPE_CUTOFF * Math.Pow(1.0 / ENVELOPE_CUTOFF, position / (double)attackUpSamples); } else if (position < attackDownEndSample) { envelopeState = EnvelopeState.AttackDown; currentEnvelope = Math.Pow(sustainAmplitude, (position - attackUpEndSample) / (double)attackDownSamples); } else if (position < sustainEndSample) { envelopeState = EnvelopeState.Sustain; currentEnvelope = sustainAmplitude * Math.Pow(sustainDecayRate, (position - attackDownEndSample)); } else { envelopeState = EnvelopeState.Released; currentEnvelope = sustainAmplitude * Math.Pow(sustainDecayRate, sustainEndSample - attackDownEndSample) * Math.Pow(releaseDecayRate, position - sustainEndSample); } }
public static void SPLToAdjustmentDB( double dbSPLL, double dbSPLR, out double dbAdjustL, out double dbAdjustR, Calibration.Source source = Calibration.Source.Custom) { dbSPLL = GeneralMath.Clamp(dbSPLL, -60, dbMax); dbSPLR = GeneralMath.Clamp(dbSPLR, -60, dbMax); //Start with Left calculation Calibration.GetLevelOffset( level: dbSPLL, levelOffsetL: out double dbOffsetL, levelOffsetR: out double dbOffsetR, source: source); dbAdjustL = dbOffsetL + dbSPLL - dbOffset; //If they're not the same, then generate a new set of offsets // (It doesn't matter if we mess up OffsetL, we already finished using it) if (dbSPLL != dbSPLR) { //To right calculation if it's different Calibration.GetLevelOffset( level: dbSPLR, levelOffsetL: out dbOffsetL, levelOffsetR: out dbOffsetR, source: source); } dbAdjustR = dbOffsetR + dbSPLR - dbOffset; }
public override bool TryValue(ref string newValue) { //Test parsability if (int.TryParse(newValue, out int result)) { //Test bounds if (result >= minVal && result <= maxVal) { tmpNewValue = newValue; if (result == GetInnerValue()) { tmpNewValue = ""; } nameDirty = true; return(true); } else { //Failed bounds check result = GeneralMath.Clamp(result, minVal, maxVal); //Update value string newValue = result.ToString(); return(false); } } //Failed to parse return(false); }
/// <summary> /// Generates the "<paramref name="tone"/>"th note of a 3-octave C Scale /// </summary> public FeedbackChords( bool major, int tone, NoteProgression progression = NoteProgression.Scale, int octaveOffset = 0, int octaveSpan = 3) { this.major = major; IReadOnlyList <FrequencySet> scale = GetSequence(progression); //Note spans 3 octaves of CMajor scale. int notes = scale.Count * octaveSpan + 1; int note = GeneralMath.Clamp(tone, 0, notes - 1); int octaves = octaveOffset + note / scale.Count; int noteIndex = note % scale.Count; fundamentalFreq = BaseFreqLB * Math.Pow(2, octaves) * scale[noteIndex].freqRatio; Duration = major ? 0.2 : 0.4; //Place sound Forward angle = 0.0; BuildStream(); }
public override void Seek(int position) { position = GeneralMath.Clamp(position, 0, ChannelSamples); this.position = position; stream.Seek(position); envelopeStream.Seek(position); }
/// <summary> /// Update color value to be between 0 and 1 /// </summary> /// <param name="color"></param> /// <param name="colorType"></param> private static void updateColorValue(ref float color, string colorType) { if (color > 1f || color < 0f) { Debug.LogWarning($"{colorType} should be set between 0 and 1"); color = GeneralMath.Clamp(color, 0f, 1f); } }
public static Color JetMap(double value, double min, double max) { double z = 4.0 * (value - min) / (max - min); return(new Color( r: (float)GeneralMath.Clamp(1.5 - Math.Abs(z - 3.0), 0.0, 1.0), g: (float)GeneralMath.Clamp(1.5 - Math.Abs(z - 2.0), 0.0, 1.0), b: (float)GeneralMath.Clamp(1.5 - Math.Abs(z - 1.0), 0.0, 1.0))); }
double ISimpleDoubleStepTemplate.GetPartialValue(double stepNumber) { if (flipSign) { stepNumber *= -1; } return(GeneralMath.Clamp(ConvergenceValue + delta * Math.Pow(BaseMajorFactor, stepNumber / StepsPerMajorFactor), Minimum, Maximum)); }
public void SetPitch(double pitchFactor) { pitchFactor = GeneralMath.Clamp(pitchFactor, 0.1, 10.0); if (lastPitchShiftEffect is not null) { lastPitchShiftEffect.PitchFactor = pitchFactor; } }
double ISimpleDoubleStepTemplate.GetPartialValue(double stepNumber) { if (DecreaseParameter) { stepNumber *= -1; } return(GeneralMath.Clamp(BaseValue + BaseStepSize * stepNumber, Minimum, Maximum)); }
public static Color JetMap(float value, float min, float max) { float z = 4f * (value - min) / (max - min); return(new Color( r: GeneralMath.Clamp(1.5f - Math.Abs(z - 3f), 0f, 1f), g: GeneralMath.Clamp(1.5f - Math.Abs(z - 2f), 0f, 1f), b: GeneralMath.Clamp(1.5f - Math.Abs(z - 1f), 0f, 1f))); }
int ISimpleIntStepTemplate.GetValue(int stepNumber) { if (DecreaseParameter) { stepNumber *= -1; } return(GeneralMath.Clamp(BaseValue + BaseStepSize * stepNumber, Minimum, Maximum)); }
public static int NearestValidOffset(double offset) { if (offset < -90.0 || offset > 90.0) { Debug.LogError($"Spatialization offset ({offset})is outside of bounds [-90.0,90.0]. Clamping."); offset = GeneralMath.Clamp(offset, -90.0, 90.0); } return((int)Math.Round(10.0 * offset)); }
public override void Seek(int position) { position = GeneralMath.Clamp(position, 0, Samples); stream.Seek(position); cycles = position / shifterSamples.Length; partial = Complex64.FromPolarCoordinates( magnitude: 1.0, phase: cycles * cyclePartial); this.position = position % shifterSamples.Length; }
public override void Seek(int position) { position = GeneralMath.Clamp(position, 0, ChannelSamples); if (position + sampleOffset > 0) { stream.Seek(position + sampleOffset); } else { stream.Reset(); } }
public override void Seek(int position) { currentSample = GeneralMath.Clamp(position, 0, ChannelSamples); if (position + sampleOffset >= ChannelSamples) { stream.Seek(ChannelSamples - (position + sampleOffset)); } else { stream.Seek(position + sampleOffset); } }
public override int Read(float[] data, int offset, int count) { int samplesRead = stream.Read(data, offset, count); for (int i = 0; i < samplesRead; i++) { if (data[offset + i] > 1f || data[offset + i] < -1f) { data[offset + i] = GeneralMath.Clamp(data[offset + i], -1f, 1f); } } return(samplesRead); }
public override void Seek(int position) { position = GeneralMath.Clamp(position, 0, ChannelSamples); adjIndexA = 0; adjIndexB = adjustments.Length / 2; bufferIndex = 0; bufferCount = 0; samplesRemaining = -1; stream.Seek(position); Array.Clear(oldBuffer, 0, bufferSize); Array.Clear(newBuffer, 0, bufferSize); }
private int ReadBody(float[] buffer, int offset, int count) { int samplesWritten = GeneralMath.Clamp(count, 0, bufferCount - bufferIndex); Array.Copy( sourceArray: outputAccumulation, sourceIndex: bufferIndex, destinationArray: buffer, destinationIndex: offset, length: samplesWritten); bufferIndex += samplesWritten; return(samplesWritten); }
private void UpdateCoefficients(float filterSample) { filterSample = GeneralMath.Clamp(filterSample, -1f, 1f); if (lastFilterSample == filterSample) { return; } lastFilterSample = filterSample; double k = Math.Tan(freqMid + freqFactor * filterSample); double norm = 1.0 / (1.0 + k / Q + k * k); switch (filterType) { case FilterType.HighPass: a0 = 1.0 * norm; a1 = -2.0 * a0; a2 = a0; b1 = 2.0 * (k * k - 1.0) * norm; b2 = (1.0 - k / Q + k * k) * norm; break; case FilterType.LowPass: a0 = k * k * norm; a1 = 2.0 * a0; a2 = a0; b1 = 2.0 * (k * k - 1.0) * norm; b2 = (1.0 - k / Q + k * k) * norm; break; case FilterType.BandPass: a0 = k / Q * norm; a1 = 0.0; a2 = -a0; b1 = 2.0 * (k * k - 1.0) * norm; b2 = (1.0 - k / Q + k * k) * norm; break; default: throw new Exception($"Unrecognized FilterType: {filterType}"); } }
public static Color JetMapLimit(double value, double min, double max) { if (value < min) { return(Color.black); } if (value > max) { return(Color.white); } double z = 4.0 * (value - min) / (max - min); return(new Color( r: (float)GeneralMath.Clamp(1.5 - Math.Abs(z - 3.0), 0.0, 1.0), g: (float)GeneralMath.Clamp(1.5 - Math.Abs(z - 2.0), 0.0, 1.0), b: (float)GeneralMath.Clamp(1.5 - Math.Abs(z - 1.0), 0.0, 1.0))); }
public int GetInnerValue() { int returnVal = defaultVal; switch (scope) { case SettingScope.Global: returnVal = PlayerData.GlobalData.GetInt(name, defaultVal); break; case SettingScope.User: returnVal = PlayerData.GetInt(name, defaultVal); break; default: Debug.LogError($"Unexpected SettingScope: {scope}"); break; } return(GeneralMath.Clamp(returnVal, minVal, maxVal)); }
public T GetAs <T>(RuntimeContext context) { Type returnType = typeof(T); if (!returnType.AssignableFromType(valueType)) { throw new ScriptRuntimeException($"Tried to retrieve result of applying Clamp to {value} as type {returnType.Name}"); } if (returnType == typeof(int)) { return((T)(object)GeneralMath.Clamp( value: value.GetAs <int>(context), min: lowerbound.GetAs <int>(context), max: upperbound.GetAs <int>(context))); } return((T)(object)GeneralMath.Clamp( value: value.GetAs <double>(context), min: lowerbound.GetAs <double>(context), max: upperbound.GetAs <double>(context))); }
public HarmonicComplex( int toneNum, NoteProgression progression = NoteProgression.Scale, int octaveOffset = 0, int octaveSpan = 3, double duration = 0.2) { IReadOnlyList <FrequencySet> scale = GetSequence(progression); int notes = scale.Count * octaveSpan + 1; int note = GeneralMath.Clamp(toneNum, 0, notes - 1); int octaves = octaveOffset + note / scale.Count; int noteIndex = note % scale.Count; fundamentalFreq = BaseFreqLB * Math.Pow(2, octaves) * scale[noteIndex].freqRatio; Duration = duration; angle = 0.0; BuildStream(); }
public override void Seek(int position) { Position = GeneralMath.Clamp(position, 0, ChannelSamples); stream.Seek(Math.Max(0, Position - prependedSamples)); }
double ISimpleIntStepTemplate.GetPartialValue(double stepNumber) => values[(int)Math.Round(GeneralMath.Clamp(stepNumber, 0, values.Length - 1))];
public override void Seek(int position) { Position = GeneralMath.Clamp(position, 0, ChannelSamples); stream.Seek(Position + sampleShift); }
public override void Seek(int position) { stream.Seek(position - preDelaySamples); this.position = GeneralMath.Clamp(position, 0, totalSamples); }
public override void Seek(int position) => this.position = GeneralMath.Clamp(position, 0, _channelSamples);