Documentation Unity plugins
Docs/Plugins/SonicLoad
Draft documentation · API may change

SonicLoad

Runtime audio import, WAV processing and playback.

SonicLoad is the current audio path for loading bytes or files into Unity AudioClip values, decoding RIFF/WAV data and writing float samples back to a WAV file.

Overview

SonicLoad is for Unity projects that need an inspector-friendly player, direct file or URL loading, microphone sample access, and explicit float-buffer processing. Its source is organized around AudioLoader, WavDecoder, WavEncoder, MicrophoneCapture and SonicLoadPlayer.

The current implementation creates clips from decoded PCM samples and writes 16-bit PCM WAV data. The sample explicitly says MP3, FLAC and OGG encoding remain planned; those formats must not be treated as available export paths. One additional source caveat is that LoadFromFileAsync passes a filename without its extension into LoadFromBytesAsync, so its extension-based branch does not receive the original suffix.

Requirements

  • Target Unity baseline: 2021.3 LTS and newer, as stated by the site prototype.
  • The runtime code uses UnityEngine audio types, UnityWebRequest and the Unity microphone API.
  • The source uses Task.Run for WAV decoding and asynchronous file/network operations. Unity object creation and AudioClip.SetData still belong to the Unity-facing path.
  • Planned platforms are Windows, macOS, Linux, Android and iOS. The source does not declare a platform-specific native library for SonicLoad.
  • Mono/IL2CPP behavior has not been validated by this prototype. Test file access, microphone permissions and audio APIs on each target build.

Setup

  1. Copy the plugin folder into Assets/Denisitree/SonicLoad/.
  2. Keep Runtime and Samples in their corresponding source folders; add an assembly definition only if your project uses one. No assembly definition is provided in this source preview.
  3. Add SonicLoadPlayer to a GameObject and assign its target, path, loadOnStart and volume fields in the Inspector.
  4. For direct APIs, instantiate AudioLoader, WavEncoder or MicrophoneCapture from your own script.
Honesty note: the file loader's non-WAV branch uses UnityWebRequestMultimedia with a file URL, while the sample's export path is WAV. Do not describe MP3, FLAC or OGG export as implemented.

Quick start

The sample source loads a WAV file, reads its samples and writes another WAV file.

SonicLoadDemo.cs
var loader = new AudioLoader();
AudioClip clip = await loader.LoadFromFileAsync(inputPath);
var encoder = new WavEncoder();
float[] samples = new float[clip.samples * clip.channels];
clip.GetData(samples, 0);
await encoder.WriteToFileAsync(outputPath, samples, clip.frequency, clip.channels);
  1. Set inputPath to a WAV file.
  2. Await LoadFromFileAsync.
  3. Read interleaved clip data with AudioClip.GetData.
  4. Await WriteToFileAsync.

Core concepts

Async file and URL paths

AudioLoader reads file bytes asynchronously, polls UnityWebRequestAsyncOperation.isDone with Task.Yield, and returns the downloaded clip. Invalid paths and request failures throw exceptions.

PCM to AudioClip

WavDecoder.Decode parses RIFF chunks and supports 16-bit, 24-bit and 32-bit sample representations. The loader creates a non-streaming clip with the decoded channels and sample rate, then calls SetData.

Microphone buffer

MicrophoneCapture stores a capture clip and reads a requested number of samples from it. Its current resampler helper takes every second source sample and returns a smaller float array; it is a simple prototype helper, not a general sample-rate converter.

WAV export

WavEncoder.Encode writes a RIFF header and clamps each float sample before converting it to 16-bit PCM. WriteToFileAsync creates the destination directory and writes the encoded bytes.

API reference

AudioLoader

Public class for loading clips from files, byte arrays or URLs.

NameTypeDescriptionDefault
LoadFromFileAsyncTask<AudioClip>Reads a file and delegates to byte loading.-
LoadFromBytesAsyncTask<AudioClip>Decodes WAV bytes or requests a supported clip extension.-
LoadFromUrlAsyncTask<AudioClip>Downloads an audio clip from a URL.-
Task<AudioClip> LoadFromFileAsync(string path)
Task<AudioClip> LoadFromBytesAsync(byte[] bytes, string clipName)
Task<AudioClip> LoadFromUrlAsync(string url, string clipName)

WavDecoder and DecodeResult

NameTypeDescriptionDefault
IsWavstatic boolChecks the RIFF/WAVE signature.-
DecodeDecodeResultParses supported PCM WAV data.-
Samplesfloat[]Decoded sample values.-
ChannelsintChannel count from the format chunk.-
SampleRateintSample rate from the format chunk.-
BitsPerSampleintBit depth from the format chunk.-
static bool IsWav(byte[] data)
DecodeResult Decode(byte[] data)
struct DecodeResult { float[] Samples; int Channels; int SampleRate; int BitsPerSample; }

WavEncoder

NameTypeDescriptionDefault
WriteToFileAsyncTaskEncodes float samples and writes a WAV file.-
Encodebyte[]Returns a 16-bit PCM RIFF/WAV byte array.-
Task WriteToFileAsync(string path, float[] samples, int sampleRate, int channels)
byte[] Encode(float[] samples, int sampleRate, int channels)

MicrophoneCapture

NameTypeDescriptionDefault
StartvoidStarts microphone capture; sampleRate defaults to 16000.sampleRate: 16000
StopvoidEnds recording and clears the queue.-
ReadSamplesfloat[]Reads up to sampleCount values from the capture clip.-
ResampleToMono16KHzstatic float[]Returns a simple half-length sample selection.-
void Start(string device, int sampleRate = 16000)
void Stop()
float[] ReadSamples(int sampleCount)
static float[] ResampleToMono16KHz(float[] samples)

SonicLoadPlayer

NameTypeDescriptionDefault
targetAudioSourcePlayback target assigned the loaded clip.null
pathstringFile path passed to AudioLoader.null
loadOnStartboolControls automatic loading in Start.true
volumefloatVolume assigned before playback.1f
public AudioSource target;
public string path;
public bool loadOnStart = true;
public float volume = 1f;

SonicLoadDemo

The public sample class exposes two public fields:

NameTypeDescriptionDefault
inputPathstringInput path passed to AudioLoader.Assets/Audio/intro.wav
outputPathstringOutput path passed to WavEncoder.Assets/Generated/intro-export.wav

Recipes

Load a URL clip

Instantiate AudioLoader, call LoadFromUrlAsync(url, clipName), then assign the returned clip to an AudioSource. The source contains the URL method; networking and supported runtime formats remain platform-dependent.

Play a file on start

Add SonicLoadPlayer, assign target and path, leave loadOnStart enabled, and choose volume. Its actual Start method loads from the file and plays when the target is non-null.

Capture and process samples

Call Start, read with ReadSamples, optionally call ResampleToMono16KHz, then call Stop. The current capture code does not expose a populated streaming ring-buffer read path.

Export a WAV

Read interleaved samples from an AudioClip and pass them to WriteToFileAsync with the clip frequency and channel count, as shown in SonicLoadDemo.

Performance and platform notes

WAV decoding runs inside Task.Run, while clip creation and clip data assignment remain Unity operations. File reads, request polling and encoding are asynchronous in the API shape, but this prototype does not publish benchmarks or memory limits.

Microphone availability and file permissions differ by target. Test permissions and paths on desktop and mobile. The planned platform list is broader than the code's validation, so treat compatibility as a development target.

Troubleshooting / FAQ

Planned

  • MP3, FLAC and OGG encoding, explicitly mentioned by the sample log.
  • A more complete streaming microphone buffer path; the current queue is not populated by ReadSamples.
  • Broader platform validation and release packaging.

Something unclear?

Ask Denis