remove array parameters from API functions

remotes/1695061714407202320/main
cosmonaut 2023-04-05 11:07:16 -07:00
parent dd06205399
commit 1e3f04235e
2 changed files with 24 additions and 50 deletions

View File

@ -21,7 +21,7 @@ namespace MoonWorks.Audio
private bool OwnsBuffer;
public static StaticSound LoadOgg(AudioDevice device, string filePath)
public static unsafe StaticSound LoadOgg(AudioDevice device, string filePath)
{
var filePointer = FAudio.stb_vorbis_open_filename(filePath, out var error, IntPtr.Zero);
@ -30,26 +30,30 @@ namespace MoonWorks.Audio
throw new AudioLoadException("Error loading file!");
}
var info = FAudio.stb_vorbis_get_info(filePointer);
var bufferSize = FAudio.stb_vorbis_stream_length_in_samples(filePointer) * info.channels;
var buffer = new float[bufferSize];
var lengthInFloats =
FAudio.stb_vorbis_stream_length_in_samples(filePointer) * info.channels;
var lengthInBytes = lengthInFloats * Marshal.SizeOf<float>();
var buffer = NativeMemory.Alloc((nuint) lengthInBytes);
FAudio.stb_vorbis_get_samples_float_interleaved(
filePointer,
info.channels,
buffer,
(int) bufferSize
(nint) buffer,
(int) lengthInFloats
);
FAudio.stb_vorbis_close(filePointer);
return new StaticSound(
device,
3,
32,
(ushort) (4 * info.channels),
(ushort) info.channels,
info.sample_rate,
buffer,
0,
(uint) buffer.Length
);
(nint) buffer,
(uint) lengthInBytes,
true);
}
// mostly borrowed from https://github.com/FNA-XNA/FNA/blob/b71b4a35ae59970ff0070dea6f8620856d8d4fec/src/Audio/SoundEffect.cs#L385
@ -223,37 +227,6 @@ namespace MoonWorks.Audio
OwnsBuffer = ownsBuffer;
}
public unsafe StaticSound(
AudioDevice device,
ushort channels,
uint samplesPerSecond,
float[] buffer,
uint bufferOffset, /* in floats */
uint bufferLength /* in floats */
) : base(device)
{
FormatTag = 3;
BitsPerSample = 32;
BlockAlign = (ushort) (4 * channels);
Channels = channels;
SamplesPerSecond = samplesPerSecond;
var bufferLengthInBytes = (int) (bufferLength * sizeof(float));
Handle = new FAudio.FAudioBuffer();
Handle.Flags = FAudio.FAUDIO_END_OF_STREAM;
Handle.pContext = IntPtr.Zero;
Handle.AudioBytes = (uint) bufferLengthInBytes;
Handle.pAudioData = (nint) NativeMemory.Alloc((nuint) bufferLengthInBytes);
Marshal.Copy(buffer, (int) bufferOffset, Handle.pAudioData, (int) bufferLength);
Handle.PlayBegin = 0;
Handle.PlayLength = 0;
LoopStart = 0;
LoopLength = 0;
OwnsBuffer = true;
}
/// <summary>
/// Gets a sound instance from the pool.
/// NOTE: If you lose track of instances, you will create garbage collection pressure!

View File

@ -1714,7 +1714,7 @@ namespace MoonWorks.Graphics
/// <param name="setDataOption">Specifies whether the buffer should be copied in immediate or deferred mode. When in doubt, use deferred.</param>
public unsafe void SetBufferData<T>(
Buffer buffer,
T[] data,
Span<T> data,
uint bufferOffsetInBytes = 0
) where T : unmanaged
{
@ -1764,14 +1764,14 @@ namespace MoonWorks.Graphics
/// Copies array data into a buffer.
/// </summary>
/// <param name="buffer">The buffer to copy to.</param>
/// <param name="data">The array to copy from.</param>
/// <param name="data">The span to copy from.</param>
/// <param name="bufferOffsetInBytes">Specifies where in the buffer to start copying.</param>
/// <param name="startElement">The index of the first element to copy from the array.</param>
/// <param name="numElements">How many elements to copy.</param>
/// <param name="setDataOption">Specifies whether the buffer should be copied in immediate or deferred mode. When in doubt, use deferred.</param>
public unsafe void SetBufferData<T>(
Buffer buffer,
T[] data,
Span<T> data,
uint bufferOffsetInBytes,
uint startElement,
uint numElements
@ -1782,15 +1782,16 @@ namespace MoonWorks.Graphics
#endif
var elementSize = Marshal.SizeOf<T>();
var dataOffsetInBytes = (int) startElement * elementSize;
fixed (T* ptr = &data[startElement])
fixed (T* ptr = data)
{
Refresh.Refresh_SetBufferData(
Device.Handle,
Handle,
buffer.Handle,
bufferOffsetInBytes,
(IntPtr) ptr,
(IntPtr) ptr + dataOffsetInBytes,
(uint) (numElements * elementSize)
);
}
@ -1819,8 +1820,8 @@ namespace MoonWorks.Graphics
/// <summary>
/// Asynchronously copies data into a texture.
/// </summary>
/// <param name="data">An array of data to copy into the texture.</param>
public unsafe void SetTextureData<T>(Texture texture, T[] data) where T : unmanaged
/// <param name="data">A span of data to copy into the texture.</param>
public unsafe void SetTextureData<T>(Texture texture, Span<T> data) where T : unmanaged
{
SetTextureData(new TextureSlice(texture), data);
}
@ -1829,8 +1830,8 @@ namespace MoonWorks.Graphics
/// Asynchronously copies data into a texture slice.
/// </summary>
/// <param name="textureSlice">The texture slice to copy into.</param>
/// <param name="data">An array of data to copy into the texture.</param>
public unsafe void SetTextureData<T>(in TextureSlice textureSlice, T[] data) where T : unmanaged
/// <param name="data">A span of data to copy into the texture.</param>
public unsafe void SetTextureData<T>(in TextureSlice textureSlice, Span<T> data) where T : unmanaged
{
#if DEBUG
AssertRenderPassInactive("Cannot copy during render pass!");
@ -1838,7 +1839,7 @@ namespace MoonWorks.Graphics
var size = sizeof(T);
fixed (T* ptr = &data[0])
fixed (T* ptr = data)
{
Refresh.Refresh_SetTextureData(
Device.Handle,