Skip to content

Commit

Permalink
Don't read audio before the first note
Browse files Browse the repository at this point in the history
  • Loading branch information
khang06 committed Feb 28, 2025
1 parent 35bf25b commit 1d82a5c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 50 deletions.
48 changes: 5 additions & 43 deletions OmniConverter/Extensions/Audio/StreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand Down
24 changes: 17 additions & 7 deletions OmniConverter/Extensions/MIDI/MIDIConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ public void Process(ISampleWriter output, CancellationToken cancToken, Func<ulon
long prevWriteTime = 0;
double deltaTime = 0;
byte[] scratch = new byte[16];
bool notePlayed = false;

Debug.PrintToConsole(Debug.LogType.Message, $"Initialized {_midiRenderer.UniqueID}.");

Expand All @@ -904,6 +905,8 @@ public void Process(ISampleWriter output, CancellationToken cancToken, Func<ulon

if (e is UndefinedEvent)
continue;
else if (!notePlayed && e is NoteOnEvent)
notePlayed = true;

var eb = e.GetData(scratch);

Expand All @@ -922,17 +925,24 @@ public void Process(ISampleWriter output, CancellationToken cancToken, Func<ulon
if (writeTime > 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)
{
Expand Down

0 comments on commit 1d82a5c

Please sign in to comment.