From 1d82a5c9523e63f709090c59039a4a99a0ac4cab Mon Sep 17 00:00:00 2001 From: Khang Date: Thu, 27 Feb 2025 20:19:20 -0500 Subject: [PATCH] Don't read audio before the first note --- .../Extensions/Audio/StreamWriter.cs | 48 ++----------------- .../Extensions/MIDI/MIDIConverter.cs | 24 +++++++--- 2 files changed, 22 insertions(+), 50 deletions(-) diff --git a/OmniConverter/Extensions/Audio/StreamWriter.cs b/OmniConverter/Extensions/Audio/StreamWriter.cs index d622daa..e909619 100644 --- a/OmniConverter/Extensions/Audio/StreamWriter.cs +++ b/OmniConverter/Extensions/Audio/StreamWriter.cs @@ -11,39 +11,10 @@ namespace OmniConverter public unsafe interface ISampleWriter { void Write(float[] buffer, int offset, int count); - void Write(float* buffer, int offset, int count); + void Skip(int count); void Flush(); } - public sealed class WaveSampleWriter : ISampleWriter, IDisposable - { - WaveWriter writer; - - public WaveSampleWriter(WaveWriter writer) - { - this.writer = writer; - } - - public int Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - public void Dispose() - { - writer.Dispose(); - } - - public void Write(float[] buffer, int offset, int count) - { - writer.WriteSamples(buffer, offset, count); - } - - public unsafe void Write(float* buffer, int offset, int count) - { - throw new NotImplementedException(); - } - - public void Flush() { } - } - public sealed class MultiStreamMerger : ISampleSource { class Writer : ISampleWriter @@ -98,21 +69,12 @@ public unsafe void Write(float[] buffer, int offset, int count) } } - public unsafe void Write(float* buffer, int offset, int count) + public unsafe void Skip(int count) { - int remaining = count; - - while (remaining != 0) + lock (Stream) { - int chunk = Math.Min(remaining, BUF_SIZE - bufPos); - Marshal.Copy((IntPtr)buffer + offset * 4, buf, bufPos, chunk); - - offset += chunk; - bufPos += chunk; - remaining -= chunk; - - if (bufPos == BUF_SIZE) - Flush(); + ResizeStream(streamPos + count); + streamPos += count; } } diff --git a/OmniConverter/Extensions/MIDI/MIDIConverter.cs b/OmniConverter/Extensions/MIDI/MIDIConverter.cs index 3330a68..cd23561 100644 --- a/OmniConverter/Extensions/MIDI/MIDIConverter.cs +++ b/OmniConverter/Extensions/MIDI/MIDIConverter.cs @@ -886,6 +886,7 @@ public void Process(ISampleWriter output, CancellationToken cancToken, Func prevWriteTime) // <<<<< EVER!!!!!!!!! prevWriteTime = writeTime; - while (requestedData > 0) + if (notePlayed) { - var isSmallChunk = requestedData < buffer.Length; - var readData = _midiRenderer.Read(buffer, 0, requestedData, isSmallChunk ? requestedData : buffer.Length); - - if (readData > 0) + while (requestedData > 0) { - output.Write(buffer, 0, isSmallChunk ? requestedData : buffer.Length); - requestedData = isSmallChunk ? 0 : requestedData - buffer.Length; + var isSmallChunk = requestedData < buffer.Length; + var readData = _midiRenderer.Read(buffer, 0, requestedData, isSmallChunk ? requestedData : buffer.Length); + + if (readData > 0) + { + output.Write(buffer, 0, isSmallChunk ? requestedData : buffer.Length); + requestedData = isSmallChunk ? 0 : requestedData - buffer.Length; + } } } + else + { + output.Skip(requestedData); + } switch (e) {