remove array parameters from API functions

pull/48/head
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; 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); 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!"); throw new AudioLoadException("Error loading file!");
} }
var info = FAudio.stb_vorbis_get_info(filePointer); var info = FAudio.stb_vorbis_get_info(filePointer);
var bufferSize = FAudio.stb_vorbis_stream_length_in_samples(filePointer) * info.channels; var lengthInFloats =
var buffer = new float[bufferSize]; 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( FAudio.stb_vorbis_get_samples_float_interleaved(
filePointer, filePointer,
info.channels, info.channels,
buffer, (nint) buffer,
(int) bufferSize (int) lengthInFloats
); );
FAudio.stb_vorbis_close(filePointer); FAudio.stb_vorbis_close(filePointer);
return new StaticSound( return new StaticSound(
device, device,
3,
32,
(ushort) (4 * info.channels),
(ushort) info.channels, (ushort) info.channels,
info.sample_rate, info.sample_rate,
buffer, (nint) buffer,
0, (uint) lengthInBytes,
(uint) buffer.Length true);
);
} }
// mostly borrowed from https://github.com/FNA-XNA/FNA/blob/b71b4a35ae59970ff0070dea6f8620856d8d4fec/src/Audio/SoundEffect.cs#L385 // 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; 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> /// <summary>
/// Gets a sound instance from the pool. /// Gets a sound instance from the pool.
/// NOTE: If you lose track of instances, you will create garbage collection pressure! /// 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> /// <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>( public unsafe void SetBufferData<T>(
Buffer buffer, Buffer buffer,
T[] data, Span<T> data,
uint bufferOffsetInBytes = 0 uint bufferOffsetInBytes = 0
) where T : unmanaged ) where T : unmanaged
{ {
@ -1764,14 +1764,14 @@ namespace MoonWorks.Graphics
/// Copies array data into a buffer. /// Copies array data into a buffer.
/// </summary> /// </summary>
/// <param name="buffer">The buffer to copy to.</param> /// <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="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="startElement">The index of the first element to copy from the array.</param>
/// <param name="numElements">How many elements to copy.</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> /// <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>( public unsafe void SetBufferData<T>(
Buffer buffer, Buffer buffer,
T[] data, Span<T> data,
uint bufferOffsetInBytes, uint bufferOffsetInBytes,
uint startElement, uint startElement,
uint numElements uint numElements
@ -1782,15 +1782,16 @@ namespace MoonWorks.Graphics
#endif #endif
var elementSize = Marshal.SizeOf<T>(); var elementSize = Marshal.SizeOf<T>();
var dataOffsetInBytes = (int) startElement * elementSize;
fixed (T* ptr = &data[startElement]) fixed (T* ptr = data)
{ {
Refresh.Refresh_SetBufferData( Refresh.Refresh_SetBufferData(
Device.Handle, Device.Handle,
Handle, Handle,
buffer.Handle, buffer.Handle,
bufferOffsetInBytes, bufferOffsetInBytes,
(IntPtr) ptr, (IntPtr) ptr + dataOffsetInBytes,
(uint) (numElements * elementSize) (uint) (numElements * elementSize)
); );
} }
@ -1819,8 +1820,8 @@ namespace MoonWorks.Graphics
/// <summary> /// <summary>
/// Asynchronously copies data into a texture. /// Asynchronously copies data into a texture.
/// </summary> /// </summary>
/// <param name="data">An array of data to copy into the texture.</param> /// <param name="data">A span of data to copy into the texture.</param>
public unsafe void SetTextureData<T>(Texture texture, T[] data) where T : unmanaged public unsafe void SetTextureData<T>(Texture texture, Span<T> data) where T : unmanaged
{ {
SetTextureData(new TextureSlice(texture), data); SetTextureData(new TextureSlice(texture), data);
} }
@ -1829,8 +1830,8 @@ namespace MoonWorks.Graphics
/// Asynchronously copies data into a texture slice. /// Asynchronously copies data into a texture slice.
/// </summary> /// </summary>
/// <param name="textureSlice">The texture slice to copy into.</param> /// <param name="textureSlice">The texture slice to copy into.</param>
/// <param name="data">An array of data to copy into the texture.</param> /// <param name="data">A span of data to copy into the texture.</param>
public unsafe void SetTextureData<T>(in TextureSlice textureSlice, T[] data) where T : unmanaged public unsafe void SetTextureData<T>(in TextureSlice textureSlice, Span<T> data) where T : unmanaged
{ {
#if DEBUG #if DEBUG
AssertRenderPassInactive("Cannot copy during render pass!"); AssertRenderPassInactive("Cannot copy during render pass!");
@ -1838,7 +1839,7 @@ namespace MoonWorks.Graphics
var size = sizeof(T); var size = sizeof(T);
fixed (T* ptr = &data[0]) fixed (T* ptr = data)
{ {
Refresh.Refresh_SetTextureData( Refresh.Refresh_SetTextureData(
Device.Handle, Device.Handle,