Gin
Loading...
Searching...
No Matches
MidiFilePlayer Class Reference

Plays back MIDI files with configurable tempo. More...

#include <gin_midifileplayer.h>

Public Member Functions

 MidiFilePlayer ()=default
bool load (const juce::File &file)
 Loads a MIDI file for playback.
void setSampleRate (double sr)
 Sets the sample rate for playback timing.
void setFallbackBpm (double bpm)
 Sets the fallback tempo to use when the MIDI file contains no tempo events.
void play ()
 Starts playback from the current position.
void stop ()
 Stops playback and sends note-off messages for any playing notes.
bool isPlaying () const
 Returns true if currently playing.
double getPlayheadPosition () const
 Returns the current playhead position in seconds.
void setPlayheadPosition (double positionInSeconds)
 Sets the playhead position in seconds.
void processBlock (int numSamples, juce::MidiBuffer &midiBuffer)
 Processes a block, filling the buffer with MIDI events.
double getLengthInSeconds () const
 Returns the total length of the MIDI file in seconds.
void setLooping (bool shouldLoop)
 Sets whether playback should loop.
bool isLooping () const
 Returns true if looping is enabled.
const juce::MidiMessageSequence & getSequence () const
 Returns the merged MIDI sequence for display purposes.
double getLengthInTicks () const
 Returns the length in ticks (for display purposes).
bool hasFileLoaded () const
 Returns true if a file has been loaded.
juce::File getLoadedFile () const
 Returns the path of the currently loaded file, or empty if none.
void clear ()
 Clears the currently loaded file.
juce::AudioPlayHead::PositionInfo populatePositionInfo ()
 Returns position info for use with an AudioPlayHead.

Detailed Description

Plays back MIDI files with configurable tempo.

MidiFilePlayer loads a standard MIDI file and plays it back through processBlock calls, filling a MidiBuffer with the appropriate events for each audio block based on the current playhead position.

Thread Safety:

Key Features:

  • Load MIDI files from juce::File
  • Respects MIDI file tempo events, with configurable fallback BPM
  • Sample-accurate playback
  • Play/stop controls with proper note-off handling
  • Playhead position tracking

Usage:

player.load(midiFile);
player.setSampleRate(44100.0);
player.setFallbackBpm(120.0); // Optional: only used if file has no tempo events
player.play();
// In audio callback
juce::MidiBuffer midiBuffer;
player.processBlock(numSamples, midiBuffer);
void processBlock(int numSamples, juce::MidiBuffer &midiBuffer)
Processes a block, filling the buffer with MIDI events.
void setSampleRate(double sr)
Sets the sample rate for playback timing.
void setFallbackBpm(double bpm)
Sets the fallback tempo to use when the MIDI file contains no tempo events.
void play()
Starts playback from the current position.
bool load(const juce::File &file)
Loads a MIDI file for playback.
MidiFilePlayer()=default
See also
juce::MidiFile, juce::MidiBuffer

Constructor & Destructor Documentation

◆ MidiFilePlayer()

MidiFilePlayer::MidiFilePlayer ( )
default

Member Function Documentation

◆ load()

bool MidiFilePlayer::load ( const juce::File & file)

Loads a MIDI file for playback.

Call from the message thread only.

Parameters
fileThe MIDI file to load
Returns
true if the file was loaded successfully

◆ setSampleRate()

void MidiFilePlayer::setSampleRate ( double sr)

Sets the sample rate for playback timing.

Parameters
srThe sample rate in Hz

◆ setFallbackBpm()

void MidiFilePlayer::setFallbackBpm ( double bpm)

Sets the fallback tempo to use when the MIDI file contains no tempo events.

Call from the message thread only.

Parameters
bpmThe fallback tempo in beats per minute (default: 120)

◆ play()

void MidiFilePlayer::play ( )

Starts playback from the current position.

◆ stop()

void MidiFilePlayer::stop ( )

Stops playback and sends note-off messages for any playing notes.

◆ isPlaying()

bool MidiFilePlayer::isPlaying ( ) const

Returns true if currently playing.

◆ getPlayheadPosition()

double MidiFilePlayer::getPlayheadPosition ( ) const

Returns the current playhead position in seconds.

◆ setPlayheadPosition()

void MidiFilePlayer::setPlayheadPosition ( double positionInSeconds)

Sets the playhead position in seconds.

Parameters
positionInSecondsThe new playhead position

◆ processBlock()

void MidiFilePlayer::processBlock ( int numSamples,
juce::MidiBuffer & midiBuffer )

Processes a block, filling the buffer with MIDI events.

Call from the audio thread only.

Parameters
numSamplesThe number of samples in this block
midiBufferThe buffer to fill with MIDI events

◆ getLengthInSeconds()

double MidiFilePlayer::getLengthInSeconds ( ) const

Returns the total length of the MIDI file in seconds.

◆ setLooping()

void MidiFilePlayer::setLooping ( bool shouldLoop)

Sets whether playback should loop.

◆ isLooping()

bool MidiFilePlayer::isLooping ( ) const

Returns true if looping is enabled.

◆ getSequence()

const juce::MidiMessageSequence & MidiFilePlayer::getSequence ( ) const

Returns the merged MIDI sequence for display purposes.

Note: Only safe to call when not loading a new file.

◆ getLengthInTicks()

double MidiFilePlayer::getLengthInTicks ( ) const

Returns the length in ticks (for display purposes).

◆ hasFileLoaded()

bool MidiFilePlayer::hasFileLoaded ( ) const

Returns true if a file has been loaded.

◆ getLoadedFile()

juce::File MidiFilePlayer::getLoadedFile ( ) const

Returns the path of the currently loaded file, or empty if none.

◆ clear()

void MidiFilePlayer::clear ( )

Clears the currently loaded file.

◆ populatePositionInfo()

juce::AudioPlayHead::PositionInfo MidiFilePlayer::populatePositionInfo ( )

Returns position info for use with an AudioPlayHead.

Call from the audio thread before processing.

Returns
PositionInfo populated with current playback state

The documentation for this class was generated from the following file: