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,
UnityWebRequestand the Unity microphone API. - The source uses
Task.Runfor WAV decoding and asynchronous file/network operations. Unity object creation andAudioClip.SetDatastill 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
- Copy the plugin folder into
Assets/Denisitree/SonicLoad/. - 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.
- Add
SonicLoadPlayerto a GameObject and assign itstarget,path,loadOnStartandvolumefields in the Inspector. - For direct APIs, instantiate
AudioLoader,WavEncoderorMicrophoneCapturefrom your own script.
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.
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);- Set
inputPathto a WAV file. - Await
LoadFromFileAsync. - Read interleaved clip data with
AudioClip.GetData. - 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.
| Name | Type | Description | Default |
|---|---|---|---|
| LoadFromFileAsync | Task<AudioClip> | Reads a file and delegates to byte loading. | - |
| LoadFromBytesAsync | Task<AudioClip> | Decodes WAV bytes or requests a supported clip extension. | - |
| LoadFromUrlAsync | Task<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
| Name | Type | Description | Default |
|---|---|---|---|
| IsWav | static bool | Checks the RIFF/WAVE signature. | - |
| Decode | DecodeResult | Parses supported PCM WAV data. | - |
| Samples | float[] | Decoded sample values. | - |
| Channels | int | Channel count from the format chunk. | - |
| SampleRate | int | Sample rate from the format chunk. | - |
| BitsPerSample | int | Bit 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
| Name | Type | Description | Default |
|---|---|---|---|
| WriteToFileAsync | Task | Encodes float samples and writes a WAV file. | - |
| Encode | byte[] | 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
| Name | Type | Description | Default |
|---|---|---|---|
| Start | void | Starts microphone capture; sampleRate defaults to 16000. | sampleRate: 16000 |
| Stop | void | Ends recording and clears the queue. | - |
| ReadSamples | float[] | Reads up to sampleCount values from the capture clip. | - |
| ResampleToMono16KHz | static 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
| Name | Type | Description | Default |
|---|---|---|---|
| target | AudioSource | Playback target assigned the loaded clip. | null |
| path | string | File path passed to AudioLoader. | null |
| loadOnStart | bool | Controls automatic loading in Start. | true |
| volume | float | Volume 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:
| Name | Type | Description | Default |
|---|---|---|---|
| inputPath | string | Input path passed to AudioLoader. | Assets/Audio/intro.wav |
| outputPath | string | Output 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
Check that the returned clip has the expected sample data, that the target AudioSource is assigned, and that the source path was loaded successfully.
The decoder requires a RIFF/WAVE signature and supports only the sample formats handled in its branches. Unsupported data throws an exception.
Confirm that a microphone device exists and that the platform has granted microphone permission. The prototype reads from its capture clip after Start.
No. The sample explicitly labels those encodings as planned. The implemented encoder writes 16-bit PCM WAV data.
The URL method is present, but request support and audio decoding are runtime concerns. Test the target platform instead of assuming parity.
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?