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

Name Type Description
samples System.Single[] The original samples (interleaved for multi-channel)
channels System.Int32 Number of audio channels
startSample System.Int32 Start sample index (per channel)
endSample System.Int32 End 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

Name Type Description
samples System.Single[] The audio samples (interleaved for multi-channel)
channels System.Int32 Number of audio channels
threshold System.Single Amplitude 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

Name Type Description
samples System.Single[] The audio samples to normalize (modified in place)
targetPeak System.Single Target 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

Name Type Description
samples System.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

Name Type Description
samples System.Single[] The audio samples (modified in place)
channels System.Int32 Number of audio channels
fadeSamples System.Int32 Number of samples over which to fade
fadeIn System.Boolean True for fade in, false for fade out
curve UnityEngine.AnimationCurve AnimationCurve defining the fade shape (null for linear)
public static void AdjustVolume(float[] samples, float volume)

Adjusts the volume of audio samples.

Parameters

Name Type Description
samples System.Single[] The audio samples (modified in place)
volume System.Single Volume 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

Name Type Description
folder System.String The target folder
originalFilename System.String The 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

Name Type Description
outputPath System.String Output file path
samples System.Single[] Audio samples (interleaved for multi-channel)
channels System.Int32 Number of audio channels
frequency System.Int32 Sample rate in Hz
use32BitFloat System.Boolean Use 32-bit float format instead of 16-bit PCM

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

Name Type Description
samples System.Single[] Audio samples
channels System.Int32 Number of channels
frequency System.Int32 Sample rate
name System.String Name 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

Name Type Description
time System.Single Playback time in seconds.
frequency System.Int32 Clip 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

Name Type Description
sample System.Int32 Per-channel sample-frame index.
frequency System.Int32 Clip 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

Name Type Description
clip UnityEngine.AudioClip Clip 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.