diff --git a/src/Audio/StaticSound.cs b/src/Audio/StaticSound.cs index ca7188ad..da7a44d0 100644 --- a/src/Audio/StaticSound.cs +++ b/src/Audio/StaticSound.cs @@ -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(); + 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; - } - /// /// Gets a sound instance from the pool. /// NOTE: If you lose track of instances, you will create garbage collection pressure! diff --git a/src/Graphics/CommandBuffer.cs b/src/Graphics/CommandBuffer.cs index e5cf272e..508c5d09 100644 --- a/src/Graphics/CommandBuffer.cs +++ b/src/Graphics/CommandBuffer.cs @@ -1714,7 +1714,7 @@ namespace MoonWorks.Graphics /// Specifies whether the buffer should be copied in immediate or deferred mode. When in doubt, use deferred. public unsafe void SetBufferData( Buffer buffer, - T[] data, + Span data, uint bufferOffsetInBytes = 0 ) where T : unmanaged { @@ -1764,14 +1764,14 @@ namespace MoonWorks.Graphics /// Copies array data into a buffer. /// /// The buffer to copy to. - /// The array to copy from. + /// The span to copy from. /// Specifies where in the buffer to start copying. /// The index of the first element to copy from the array. /// How many elements to copy. /// Specifies whether the buffer should be copied in immediate or deferred mode. When in doubt, use deferred. public unsafe void SetBufferData( Buffer buffer, - T[] data, + Span data, uint bufferOffsetInBytes, uint startElement, uint numElements @@ -1782,15 +1782,16 @@ namespace MoonWorks.Graphics #endif var elementSize = Marshal.SizeOf(); + 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 /// /// Asynchronously copies data into a texture. /// - /// An array of data to copy into the texture. - public unsafe void SetTextureData(Texture texture, T[] data) where T : unmanaged + /// A span of data to copy into the texture. + public unsafe void SetTextureData(Texture texture, Span data) where T : unmanaged { SetTextureData(new TextureSlice(texture), data); } @@ -1829,8 +1830,8 @@ namespace MoonWorks.Graphics /// Asynchronously copies data into a texture slice. /// /// The texture slice to copy into. - /// An array of data to copy into the texture. - public unsafe void SetTextureData(in TextureSlice textureSlice, T[] data) where T : unmanaged + /// A span of data to copy into the texture. + public unsafe void SetTextureData(in TextureSlice textureSlice, Span 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,