Class AudioUtility
java.lang.Object
net.paulhertz.pixelaudio.schedule.AudioUtility
Utility conversions for audio.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static enumStrategies for adjusting signal level. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final doublePrecomputed log(2) for efficiency.static final doubleConstant: 2^(1/12), the ratio of one semitone. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic floatcomputePeak(float[] signal) Computes the peak absolute sample value of a signal.static floatcomputeRMS(float[] signal) Computes the RMS (root mean square) level of a signal.static floatdbToLinear(float dB) Converts a gain value in decibels (dB) to a linear amplitude multiplier.static intfileSamplesRequiredForDisplay(int mapSize, float fileSampleRate, float audioOutRate) Computes how many source-file samples are needed to fill a display map at output rate.static floatfrequencyPianoKey(float freq) Convert frequency to fractional MIDI-style key number.static floatlinearToDb(float linear) static longmillisToSamples(double millis, double sampleRate) Converts milliseconds to the nearest sample count.static voidnormalizePeakDb(float[] signal, float targetPeakDB) Normalizes a signal so its peak reaches a target dBFS value.static voidnormalizePeakLevel(float[] signal, float targetPeakLevel) Normalizes a signal so its peak reaches a target linear level.static floatnormalizeRMS(float[] signal, float targetDB) Normalizes a single-channel signal array to a target RMS level in dBFS.static voidnormalizeRmsWithCeiling(float[] signal, float targetRmsDB, float peakCeilingDB) Normalizes toward a target RMS level while limiting the applied gain by a peak ceiling.static floatpianoKeyFrequency(float keyNumber) Convert MIDI-style key number to frequency (A4 = 440 Hz, key 49).static floatratioToSemitones(float ratio) Convert frequency ratio to semitone offset.static ddf.minim.MultiChannelBufferresampleMCB(ddf.minim.MultiChannelBuffer src, float sourceRate, float targetRate) Resamples all channels in a MultiChannelBuffer from sourceRate to targetRate.static ddf.minim.MultiChannelBufferresampleMCBToOutput(ddf.minim.MultiChannelBuffer src, float sourceRate, ddf.minim.AudioOutput out) Convenience: resample MultiChannelBuffer from sourceRate to match AudioOutput.static float[]resampleMono(float[] source, float sourceRate, float targetRate) Resamples a mono buffer from sourceRate to targetRate using linear interpolation.static float[]resampleMonoToOutput(float[] source, float sourceRate, ddf.minim.AudioOutput out) Convenience: resample mono buffer from sourceRate to match AudioOutput sample rate.static doublesamplesToMillis(long samples, double sampleRate) Converts a sample count to milliseconds.static voidsaveAudioTo32BitPCMFile(float[] samples, float sampleRate, String fileName) Saves audio data to a 32-bit floating point format that has higher resolution than 16-bit integer PCM.static voidsaveAudioToFile(float[] samples, float sampleRate, String fileName) Saves audio data to 16-bit integer PCM format, which Processing can also open.static voidsaveStereoAudioTo32BitPCMFile(float[] leftChannel, float[] rightChannel, float sampleRate, String fileName) Saves stereo audio data to a 32-bit floating point format that has higher resolution than 16-bit integer PCM.static voidsaveStereoAudioToFile(float[] leftChannel, float[] rightChannel, float sampleRate, String fileName) Saves stereo audio data to 16-bit integer PCM format, which Processing can also open.static longsecsToSamples(double secs, double sampleRate) Converts seconds to the nearest sample count.static floatsemitonesToRatio(float semitones) Convert semitone offset to frequency ratio.static floattransposeFrequency(float freq, float semitones) Apply a semitone offset directly to a frequency.
-
Field Details
-
SEMITONE_FAC
public static final double SEMITONE_FACConstant: 2^(1/12), the ratio of one semitone. -
LOG2
private static final double LOG2Precomputed log(2) for efficiency.
-
-
Constructor Details
-
AudioUtility
private AudioUtility()
-
-
Method Details
-
millisToSamples
public static long millisToSamples(double millis, double sampleRate) Converts milliseconds to the nearest sample count.- Parameters:
millis- duration in millisecondssampleRate- sample rate in Hz- Returns:
- sample count rounded to nearest integer
-
samplesToMillis
public static double samplesToMillis(long samples, double sampleRate) Converts a sample count to milliseconds.- Parameters:
samples- sample countsampleRate- sample rate in Hz- Returns:
- duration in milliseconds
-
secsToSamples
public static long secsToSamples(double secs, double sampleRate) Converts seconds to the nearest sample count.- Parameters:
secs- duration in secondssampleRate- sample rate in Hz- Returns:
- sample count rounded to nearest integer
-
dbToLinear
public static float dbToLinear(float dB) Converts a gain value in decibels (dB) to a linear amplitude multiplier.0.0 dB -> 1.0-6.0 dB -> ~0.501+6.0 dB -> ~1.995- Parameters:
dB- gain in decibels- Returns:
- linear gain multiplier
-
linearToDb
public static float linearToDb(float linear) - Parameters:
linear- decimal gain value, for example from a UI slider- Returns:
- values in decibels for linear
-
semitonesToRatio
public static float semitonesToRatio(float semitones) Convert semitone offset to frequency ratio.- Parameters:
semitones- number of semitones (can be fractional)- Returns:
- frequency ratio (>0)
Examples:
12 -> 2.00 -> 1.0-12 -> 0.5
-
ratioToSemitones
public static float ratioToSemitones(float ratio) Convert frequency ratio to semitone offset.- Parameters:
ratio- frequency ratio (>0)- Returns:
- semitone offset (can be fractional)
-
pianoKeyFrequency
public static float pianoKeyFrequency(float keyNumber) Convert MIDI-style key number to frequency (A4 = 440 Hz, key 49).- Parameters:
keyNumber- MIDI-style key number, where A4 is 49- Returns:
- frequency in Hz
-
frequencyPianoKey
public static float frequencyPianoKey(float freq) Convert frequency to fractional MIDI-style key number.- Parameters:
freq- frequency in Hz- Returns:
- fractional MIDI-style key number
-
transposeFrequency
public static float transposeFrequency(float freq, float semitones) Apply a semitone offset directly to a frequency.- Parameters:
freq- source frequency in Hzsemitones- transposition amount in semitones- Returns:
- transposed frequency in Hz
-
computePeak
public static float computePeak(float[] signal) Computes the peak absolute sample value of a signal.- Parameters:
signal- the audio samples- Returns:
- Maximum absolute sample value (linear scale)
-
computeRMS
public static float computeRMS(float[] signal) Computes the RMS (root mean square) level of a signal.- Parameters:
signal- the audio samples- Returns:
- RMS value (linear scale)
-
normalizeRMS
public static float normalizeRMS(float[] signal, float targetDB) Normalizes a single-channel signal array to a target RMS level in dBFS.- Parameters:
signal- the audio samples to normalize (modified in place)targetDB- the peak level in dB (e.g. -3.0f for moderately loud, -12.0f for safe headroom)- Returns:
- gain applied to signal
-
normalizePeakDb
public static void normalizePeakDb(float[] signal, float targetPeakDB) Normalizes a signal so its peak reaches a target dBFS value.- Parameters:
signal- audio samples to modify in placetargetPeakDB- target peak level in dBFS
-
normalizePeakLevel
public static void normalizePeakLevel(float[] signal, float targetPeakLevel) Normalizes a signal so its peak reaches a target linear level.- Parameters:
signal- audio samples to modify in placetargetPeakLevel- target peak level on a linear scale
-
normalizeRmsWithCeiling
public static void normalizeRmsWithCeiling(float[] signal, float targetRmsDB, float peakCeilingDB) Normalizes toward a target RMS level while limiting the applied gain by a peak ceiling.- Parameters:
signal- audio samples to modify in placetargetRmsDB- target RMS level in dBFSpeakCeilingDB- maximum permitted peak level in dBFS
-
fileSamplesRequiredForDisplay
public static int fileSamplesRequiredForDisplay(int mapSize, float fileSampleRate, float audioOutRate) Computes how many source-file samples are needed to fill a display map at output rate.- Parameters:
mapSize- number of display/audio output samplesfileSampleRate- sample rate of the source audio fileaudioOutRate- sample rate of the audio output- Returns:
- required source sample count, rounded up
-
resampleMono
public static float[] resampleMono(float[] source, float sourceRate, float targetRate) Resamples a mono buffer from sourceRate to targetRate using linear interpolation.- Parameters:
source- mono samples at sourceRatesourceRate- sample rate of the source buffer (Hz)targetRate- desired sample rate (Hz)- Returns:
- new float[] at targetRate
-
resampleMonoToOutput
public static float[] resampleMonoToOutput(float[] source, float sourceRate, ddf.minim.AudioOutput out) Convenience: resample mono buffer from sourceRate to match AudioOutput sample rate.- Parameters:
source- mono samples at sourceRatesourceRate- sample rate of the source buffer (Hz)out- target AudioOutput- Returns:
- new mono buffer at the output sample rate
-
resampleMCB
public static ddf.minim.MultiChannelBuffer resampleMCB(ddf.minim.MultiChannelBuffer src, float sourceRate, float targetRate) Resamples all channels in a MultiChannelBuffer from sourceRate to targetRate. Produces a new MultiChannelBuffer at targetRate. For PixelAudio you may only need channel 0 (mono); this is available mainly for completeness.- Parameters:
src- source buffer to resamplesourceRate- sample rate of the source buffer (Hz)targetRate- desired sample rate (Hz)- Returns:
- new buffer at targetRate
-
resampleMCBToOutput
public static ddf.minim.MultiChannelBuffer resampleMCBToOutput(ddf.minim.MultiChannelBuffer src, float sourceRate, ddf.minim.AudioOutput out) Convenience: resample MultiChannelBuffer from sourceRate to match AudioOutput.- Parameters:
src- source buffer to resamplesourceRate- sample rate of the source buffer (Hz)out- target AudioOutput- Returns:
- new buffer at the output sample rate
-
saveAudioToFile
public static void saveAudioToFile(float[] samples, float sampleRate, String fileName) throws IOException, UnsupportedAudioFileException Saves audio data to 16-bit integer PCM format, which Processing can also open.- Parameters:
samples- an array of floats in the audio range (-1.0f, 1.0f)sampleRate- audio sample rate for the filefileName- name of the file to save to- Throws:
IOException- an Exception you'll need to handle to call this method (see keyPressed entry for 's')UnsupportedAudioFileException
-
saveAudioTo32BitPCMFile
public static void saveAudioTo32BitPCMFile(float[] samples, float sampleRate, String fileName) throws IOException Saves audio data to a 32-bit floating point format that has higher resolution than 16-bit integer PCM. The format can't be opened by Processing but can be opened by audio applications.- Parameters:
samples- an array of floats in the audio range (-1.0f, 1.0f)sampleRate- the sample rate for the filefileName- name of the file- Throws:
IOException- an Exception you'll need to catch to call this method (see keyPressed entry for 's')
-
saveStereoAudioToFile
public static void saveStereoAudioToFile(float[] leftChannel, float[] rightChannel, float sampleRate, String fileName) throws IOException, UnsupportedAudioFileException Saves stereo audio data to 16-bit integer PCM format, which Processing can also open.- Parameters:
leftChannel- an array of floats in the audio range (-1.0f, 1.0f)rightChannel- an array of floats in the audio range (-1.0f, 1.0f)sampleRate- audio sample rate for the filefileName- name of the file to save to- Throws:
IOException- an Exception you'll need to handle to call this method (see keyPressed entry for 's')UnsupportedAudioFileException
-
saveStereoAudioTo32BitPCMFile
public static void saveStereoAudioTo32BitPCMFile(float[] leftChannel, float[] rightChannel, float sampleRate, String fileName) throws IOException Saves stereo audio data to a 32-bit floating point format that has higher resolution than 16-bit integer PCM. The format can't be opened by Processing but can be opened by audio applications.- Parameters:
leftChannel- an array of floats in the audio range (-1.0f, 1.0f)rightChannel- an array of floats in the audio range (-1.0f, 1.0f)sampleRate- the sample rate for the filefileName- name of the file- Throws:
IOException
-