2
0
mirror of https://github.com/raylib-cs/raylib-cs synced 2025-04-03 11:09:40 -04:00

AttachAudioMixedProcessor Utils for easy callback (#215)

This commit is contained in:
Nicky McDonald 2024-02-24 03:44:13 -05:00 committed by GitHub
parent 818b52cbeb
commit b049b4f88d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,10 @@
using System;
namespace Raylib_cs;
/// <summary>
/// Audio stream processor.
/// Used with Raylib.AttachAudioMixedProcessor()
/// and Raylib.DetachAudioMixedProcessor()
/// </summary>
public delegate void AudioCallback<T>(Span<T> buffer);

View File

@ -0,0 +1,23 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Raylib_cs;
internal static unsafe class AudioMixed
{
public static AudioCallback<float> Callback = null;
[UnmanagedCallersOnly(CallConvs = new[]
{
typeof(CallConvCdecl),
})]
public static void Processor(void* buffer, uint frames)
{
// The buffer is stereo audio, so we need to double our frame count.
frames = Math.Min(frames * 2, int.MaxValue);
Span<float> floats = new(buffer, (int)frames);
Callback?.Invoke(floats);
}
}

View File

@ -1261,6 +1261,30 @@ public static unsafe partial class Raylib
}
}
/// <summary>
/// Attach audio stream processor to the entire audio pipeline
/// </summary>
public static void AttachAudioMixedProcessor(AudioCallback<float> processor)
{
if (AudioMixed.Callback == null)
{
AudioMixed.Callback = processor;
AttachAudioMixedProcessor(&AudioMixed.Processor);
}
}
/// <summary>
/// Detach audio stream processor from the entire audio pipeline
/// </summary>
public static void DetachAudioMixedProcessor(AudioCallback<float> processor)
{
if (AudioMixed.Callback == processor)
{
DetachAudioMixedProcessor(&AudioMixed.Processor);
AudioMixed.Callback = null;
}
}
public static string SubText(this string input, int position, int length)
{
return input.Substring(position, Math.Min(length, input.Length));