Skip to content

Instantly share code, notes, and snippets.

@martindevans
Created January 17, 2022 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martindevans/183555c065d61207c9db4d9a3bd48691 to your computer and use it in GitHub Desktop.
Save martindevans/183555c065d61207c9db4d9a3bd48691 to your computer and use it in GitHub Desktop.
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