Skip to content

AudioProcessor

ClassAudioToolAudioTool.Editor
public static class AudioProcessor

Audio processing utilities for trimming, normalizing, fading, and exporting audio files.

public static float[] TrimAudio(float[] samples, int channels, int startSample, int endSample)

Trims audio samples to the specified range.

Parameters

NameTypeDescription
samplesSystem.Single[]The original samples (interleaved for multi-channel)
channelsSystem.Int32Number of audio channels
startSampleSystem.Int32Start sample index (per channel)
endSampleSystem.Int32End sample index (per channel)

Returns

System.Single[]: Trimmed samples array

public static (int startSample, int endSample) DetectSilence(float[] samples, int channels, float threshold = 0.01)

Detects silence at the beginning and end of the audio.

Parameters

NameTypeDescription
samplesSystem.Single[]The audio samples (interleaved for multi-channel)
channelsSystem.Int32Number of audio channels
thresholdSystem.SingleAmplitude threshold below which is considered silence (0.0-1.0)

Returns

System.ValueTuple{System.Int32,System.Int32}: Tuple of (startSample, endSample) representing the non-silent portion

public static float Normalize(float[] samples, float targetPeak = 0.95)

Normalizes audio samples to a target peak level.

Parameters

NameTypeDescription
samplesSystem.Single[]The audio samples to normalize (modified in place)
targetPeakSystem.SingleTarget peak amplitude (0.0-1.0)

Returns

System.Single: The normalization factor applied

public static float GetPeakAmplitude(float[] samples)

Gets the peak amplitude of the audio samples.

Parameters

NameTypeDescription
samplesSystem.Single[]The audio samples

Returns

System.Single: Peak amplitude (0.0-1.0)

ApplyFade(float[], int, int, bool, AnimationCurve)

Section titled “ApplyFade(float[], int, int, bool, AnimationCurve)”
public static void ApplyFade(float[] samples, int channels, int fadeSamples, bool fadeIn, AnimationCurve curve = null)

Applies a fade effect to the audio samples.

Parameters

NameTypeDescription
samplesSystem.Single[]The audio samples (modified in place)
channelsSystem.Int32Number of audio channels
fadeSamplesSystem.Int32Number of samples over which to fade
fadeInSystem.BooleanTrue for fade in, false for fade out
curveUnityEngine.AnimationCurveAnimationCurve defining the fade shape (null for linear)
public static void AdjustVolume(float[] samples, float volume)

Adjusts the volume of audio samples.

Parameters

NameTypeDescription
samplesSystem.Single[]The audio samples (modified in place)
volumeSystem.SingleVolume multiplier (0.0-2.0, where 1.0 is original volume)
public static string GenerateUniqueFilename(string folder, string originalFilename)

Generates a unique filename for the exported audio file using auto-incrementing numbers.

Parameters

NameTypeDescription
folderSystem.StringThe target folder
originalFilenameSystem.StringThe original audio filename

Returns

System.String: Full path to the unique filename

ExportToWav(string, float[], int, int, bool)

Section titled “ExportToWav(string, float[], int, int, bool)”
public static void ExportToWav(string outputPath, float[] samples, int channels, int frequency, bool use32BitFloat = false)

Exports audio samples to a WAV file.

Parameters

NameTypeDescription
outputPathSystem.StringOutput file path
samplesSystem.Single[]Audio samples (interleaved for multi-channel)
channelsSystem.Int32Number of audio channels
frequencySystem.Int32Sample rate in Hz
use32BitFloatSystem.BooleanUse 32-bit float format instead of 16-bit PCM

ExportToWav(string, float[], int, int, WavSampleFormat)

Section titled “ExportToWav(string, float[], int, int, WavSampleFormat)”
public static void ExportToWav(string outputPath, float[] samples, int channels, int frequency, WavSampleFormat format)

Exports audio samples to a WAV file with the given sample encoding.

Parameters

NameTypeDescription
outputPathSystem.StringOutput file path; missing folders are created.
samplesSystem.Single[]Audio samples (interleaved for multi-channel)
channelsSystem.Int32Number of audio channels
frequencySystem.Int32Sample rate in Hz
formatAudioTool.WavSampleFormat16-bit or 24-bit PCM, rounded to the nearest step, or unclamped 32-bit float.

CreateClipFromSamples(float[], int, int, string)

Section titled “CreateClipFromSamples(float[], int, int, string)”
public static Task<AudioClip> CreateClipFromSamples(float[] samples, int channels, int frequency, string name = "ProcessedAudio")

Creates a new AudioClip from processed samples (for preview before export). Uses a temporary WAV file because Unity’s AudioUtil.PlayPreviewClip doesn’t work with dynamically created clips (AudioClip.Create + SetData).

Parameters

NameTypeDescription
samplesSystem.Single[]Audio samples
channelsSystem.Int32Number of channels
frequencySystem.Int32Sample rate
nameSystem.StringName for the clip

Returns

System.Threading.Tasks.Task{UnityEngine.AudioClip}: New AudioClip with the processed audio

public static int TimeToSample(float time, int frequency)

Converts time in seconds to sample index.

Parameters

NameTypeDescription
timeSystem.SinglePlayback time in seconds.
frequencySystem.Int32Clip sample rate in hertz.

Returns

System.Int32: The nearest per-channel sample-frame index.

public static float SampleToTime(int sample, int frequency)

Converts sample index to time in seconds.

Parameters

NameTypeDescription
sampleSystem.Int32Per-channel sample-frame index.
frequencySystem.Int32Clip sample rate in hertz.

Returns

System.Single: The corresponding playback time in seconds.

public static float[] GetSamples(AudioClip clip)

Gets all samples from an AudioClip.

Parameters

NameTypeDescription
clipUnityEngine.AudioClipClip whose readable sample data should be copied.

Returns

System.Single[]: A new array containing interleaved channel samples, or null when the clip is missing or its data cannot be read.