aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/softsynth/math/AudioMath.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/softsynth/math/AudioMath.java')
-rw-r--r--src/com/softsynth/math/AudioMath.java34
1 files changed, 29 insertions, 5 deletions
diff --git a/src/com/softsynth/math/AudioMath.java b/src/com/softsynth/math/AudioMath.java
index 64f064f..6d5ab07 100644
--- a/src/com/softsynth/math/AudioMath.java
+++ b/src/com/softsynth/math/AudioMath.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,7 +18,7 @@ package com.softsynth.math;
/**
* Miscellaneous math functions useful in Audio
- *
+ *
* @author (C) 1998 Phil Burk
*/
public class AudioMath {
@@ -26,6 +26,7 @@ public class AudioMath {
private final static double a2dScalar = 20.0 / Math.log(10.0);
public static final int CONCERT_A_PITCH = 69;
public static final double CONCERT_A_FREQUENCY = 440.0;
+ private static double mConcertAFrequency = CONCERT_A_FREQUENCY;
/**
* Convert amplitude to decibels. 1.0 is zero dB. 0.5 is -6.02 dB.
@@ -47,7 +48,7 @@ public class AudioMath {
* Calculate MIDI pitch based on frequency in Hertz. Middle C is 60.0.
*/
public static double frequencyToPitch(double frequency) {
- return CONCERT_A_PITCH + 12 * Math.log(frequency / CONCERT_A_FREQUENCY) / Math.log(2.0);
+ return CONCERT_A_PITCH + 12 * Math.log(frequency / mConcertAFrequency) / Math.log(2.0);
}
/**
@@ -55,6 +56,29 @@ public class AudioMath {
* pitches so 60.5 would give you a pitch half way between C and C#.
*/
public static double pitchToFrequency(double pitch) {
- return CONCERT_A_FREQUENCY * Math.pow(2.0, ((pitch - CONCERT_A_PITCH) * (1.0 / 12.0)));
+ return mConcertAFrequency * Math.pow(2.0, ((pitch - CONCERT_A_PITCH) * (1.0 / 12.0)));
+ }
+
+ /**
+ * This can be used to globally adjust the tuning in JSyn from Concert A at 440.0 Hz to
+ * a slightly different frequency. Some orchestras use a higher frequency, eg. 441.0.
+ * This value will be used by pitchToFrequency() and frequencyToPitch().
+ *
+ * @param concertAFrequency
+ */
+ public static void setConcertAFrequency(double concertAFrequency) {
+ mConcertAFrequency = concertAFrequency;
+ }
+
+ public static double getConcertAFrequency() {
+ return mConcertAFrequency;
+ }
+
+ /** Convert a delta value in semitones to a frequency multiplier.
+ * @param semitones
+ * @return scaler For example 2.0 for an input of 12.0 semitones.
+ */
+ public static double semitonesToFrequencyScaler(double semitones) {
+ return Math.pow(2.0, semitones / 12.0);
}
}