Streaming audio in real-time with AudioClip

Hi all. I’m new to Unity, so feel free to say “that’s nuts” or “there’s a better way”. I need to be able to play an audio file and alter it based on settings the user chooses in real-time. Right now I’m using an audio asset to get it working, but eventually it has to be able to play an arbitrary file. I’m streaming the audio from the asset with AudioClip. To that end I have this code:

using UnityEngine;
using System.Collections;

public class AudioPlayer : MonoBehaviour {
    int position = 0;
	public AudioClip source;

    void Start() {
        AudioClip clip = AudioClip.Create("song", source.samples, source.channels, source.frequency, false, true, OnAudioRead, OnAudioSetPosition);
        audio.clip = clip;

    void OnAudioRead(float[] data) {
		source.GetData(data, position);
		position += data.Length / source.channels;

    void OnAudioSetPosition(int newPosition) {
        position = newPosition;

When I run the program it just keeps playing the same half-second of audio and the console shows StackOverflowException errors after it’s made a few dozen calls to OnAudioRead().

This works perfectly if I set the stream parameter of AudioClip.Create() to false, but then I lose the real-time processing ability. Any ideas?

from my experience Unity does not allow you to manipulate a streamed audio in realtime, you need to load it in memory first. this is the reason why when you set stream to false, it works.

GetData doesn’t work with streamed audio clips
You should fullfill the float[] in “OnAudioRead(float data)” instead.