Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class FMODStreamCallback
: MonoBehaviour
{
[SerializeField] public AudioGenerator Generator;
private Channel _channel;
private GCHandle _handle;
private DSP _dsp;
private void OnEnable()
{
var ddesc = new DSP_DESCRIPTION
{
numinputbuffers = 0,
numoutputbuffers = 1,
read = ReadDsp,
};
RuntimeManager.CoreSystem.createDSP(ref ddesc, out _dsp);
_handle = GCHandle.Alloc(Generator);
_dsp.setUserData((IntPtr)_handle);
RuntimeManager.CoreSystem.playDSP(_dsp, default, false, out _channel);
_channel.set3DMinMaxDistance(3, 100);
_channel.setMode(MODE._3D);
}
private static RESULT ReadDsp(ref DSP_STATE dsp_state, IntPtr _, IntPtr outbuffer, uint length, int _, ref int outchannels)
{
// Demo of how to query the DSP state for relevant info
var functions = (DSP_STATE_FUNCTIONS)Marshal.PtrToStructure(dsp_state.functions, typeof(DSP_STATE_FUNCTIONS));
var rate = 0;
functions.getsamplerate(ref dsp_state, ref rate);
// Get the GC heap object stashed in UserData, pump it for audio
functions.getuserdata(ref dsp_state, out var userdata);
var tgt = (AudioGenerator)GCHandle.FromIntPtr(userdata).Target;
tgt.GetAudio(outbuffer, length, (uint)outchannels);
return RESULT.OK;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment