MidiReader
Declaration
public sealed class Strawberry.Sound.Midi.MidiReader
An ISoundReader that synthesises PCM audio from a MIDI file, compatible with the Strawberry engine's streaming system.
Remarks: Usage patterns: 1. 2. 3. The reader renders audio on-demand as Read is called by the streaming system. Seek rebuilds synth state by replaying MIDI events from the beginning up to the target position (without rendering audio), then resumes normal rendering from that point.
Implements: Strawberry.Sound.ISoundReader
Constructors
MidiReader
Create a MidiReader with an explicitly provided synth backend. The caller is responsible for the backend's lifetime (not disposed by this reader).
void MidiReader(Stream midiStream, ISynthBackend synthBackend)
Parameters:
| Name | Type | Description |
|---|---|---|
midiStream |
System.IO.Stream |
Stream containing MIDI file data. |
synthBackend |
Strawberry.Sound.Midi.ISynthBackend |
Pre-configured synthesizer backend. |
MidiReader
Create a MidiReader that auto-selects a synth backend. Attempts to load TinySoundFont native; falls back to ManagedSynthBackend. The synth backend is owned and disposed by this reader.
void MidiReader(Stream midiStream, Stream soundFont = null, int sampleRate = 44100, int channels = 2)
Parameters:
| Name | Type | Description |
|---|---|---|
midiStream |
System.IO.Stream |
Stream containing MIDI file data. |
soundFont |
System.IO.Stream = null |
|
sampleRate |
System.Int32 = 44100 |
Output sample rate (default 44100). |
channels |
System.Int32 = 2 |
Output channels: 1 or 2 (default 2). |
Properties
Stream
The original MIDI file stream (read-only reference).
Stream Stream { get }
BitsPerSample
Always 16 (16-bit PCM output for OpenAL compatibility).
int BitsPerSample { get }
SampleRate
Output sample rate (matches the synth backend).
int SampleRate { get }
Channels
Output channel count (1 = mono, 2 = stereo).
int Channels { get }
DataSize
Total PCM data size in bytes.
int DataSize { get }
Methods
Read
Read synthesised PCM audio into the buffer. Audio is rendered on-demand by advancing through the MIDI event list and feeding the synth backend.
int Read(byte[] buffer, int offset, int count)
Parameters:
| Name | Type | Description |
|---|---|---|
buffer |
System.Byte[] |
|
offset |
System.Int32 |
|
count |
System.Int32 |
Returns: Number of bytes written into the buffer, or 0 if at end of data.
Seek
Seek to a byte offset in the decoded PCM data. Rebuilds synth state by replaying all MIDI events from the start up to the target position (without rendering audio), then positions the render cursor at the target.
void Seek(long offset)
Parameters:
| Name | Type | Description |
|---|---|---|
offset |
System.Int64 |
ReadAll
Read the entire MIDI file as decoded PCM data. Resets the reader to the beginning first, then renders all audio.
byte[] ReadAll()
Dispose
void Dispose()