MIDI File

Overview

midi_file is part of q_io, the optional audio and MIDI I/O library. The reader inside it, midi::file_reader, is part of q_lib and needs no file system. See q_io.

midi_file reads a standard MIDI file from disk and plays it into a MIDI processor, the same processor a live input would feed. Each message arrives timed in samples at the sample rate given, so a synthesizer can render a file the way it plays a keyboard.

The reading is done by midi::file_reader, which works on bytes already in memory and holds only a view into them. midi_file loads the file’s bytes and is a file_reader from then on. A firmware with its MIDI files in flash, or an application that receives one over a network, uses file_reader directly on those bytes.

On the way, the reader:

  • applies the file’s tempo, including any change in it, so a message’s time is where it falls in the piece;

  • merges the tracks, so messages arrive in time order whatever track they came from;

  • follows running status, and the SMPTE time division as well as ticks per quarter note;

  • passes system exclusive messages as sysex_view, and skips meta events.

Formats 0 and 1 are read as one piece. Format 2 holds independent sequences, so its tracks are played one after another. A file that is missing, or malformed anywhere, leaves the reader false, and playing it does nothing.

file_reader

midi::file_reader reads a standard MIDI file from bytes in memory.

Include

#include <q/midi/file_reader.hpp>

Declaration

namespace midi_1_0
{
   struct file_reader
   {
                           file_reader(byte_span bytes);

      explicit             operator bool() const;

      int                  format() const;
      std::size_t          tracks() const;
      float                tempo() const;
      double               duration() const;

                           template <typename P>
                           requires concepts::midi::Processor<P>
      void                 operator()(float sps, P&& proc) const;
   };
}

namespace midi
{
   using midi_1_0::file_reader;
}

Expressions

Notation

r, a, b

Objects of type midi::file_reader.

bytes

A byte_span holding a standard MIDI file.

sps

Floating point value for samples per second: the sample rate the messages are timed at.

proc

A MIDI processor (see MIDI Processor).

Constructors and Assignment

Expression Semantics

midi::file_reader(bytes)

Construct a midi::file_reader given bytes (the file’s bytes).

midi::file_reader(r)

Copy construct from midi::file_reader r.

a = b

Assign b to a.

C++ brace initialization may also be used.
The bytes stay the caller’s and must outlive the reader.

Accessors

Expression Semantics Return Type

bool(r)

Check if the file was read and is well formed.

bool

r.format()

Get the file’s format: 0, 1 or 2.

int

r.tracks()

Get the number of tracks.

std::size_t

r.tempo()

Get the file’s first tempo, in beats per minute; 120 when it sets none.

float

r.duration()

Get the time of the file’s last event, in seconds.

double

Function Call

Expression Semantics Return Type

r(sps, proc)

Play the whole file into proc, each message timed in samples at sps from the start of the piece.

void

r(sps, proc) delivers every message at once, in time order, each with its time. To play the messages in real time, or one block at a time, keep them and play them out against your own clock, as the example below does.

midi_file

midi_file reads a standard MIDI file from disk.

Include

#include <q_io/midi_file.hpp>

Declaration

class midi_file : public midi::file_reader
{
public:
                     midi_file(std::string const& path);

   std::size_t       size() const;
};

Expressions

midi_file is derived from midi::file_reader. In addition to valid expressions for midi::file_reader, midi_file allows these expressions.

Notation

f

Object of type midi_file.

path

A std::string: the file’s path.

Constructors

Expression Semantics

midi_file(path)

Construct a midi_file from the file at path.

midi_file is non-copyable.

C++ brace initialization may also be used.

Accessors

Expression Semantics Return Type

f.size()

Get the file’s size, in bytes.

std::size_t

Example

Record the notes of the file riff.mid, timed in samples at sps samples per second, to play them through a synth afterward. The FM synth example renders four DX7 factory patches from the MIDI files beside its source this way.

namespace q = cycfi::q;
namespace midi = q::midi_1_0;

struct note_event
{
   std::size_t    time;                   // samples from the start
   std::uint8_t   key;
   float          velocity;               // 0 for a note off
};

struct recorder
{
   void operator()(midi::message_base const&, std::size_t) {}

   void operator()(midi::note_on msg, std::size_t time)
   {
      // A note on with velocity 0 is a note off. Many sequencers write
      // note offs this way.
      events.push_back({time, msg.key(),
         msg.velocity() == 0? 0.0f : msg.velocity() / 127.0f});
   }

   void operator()(midi::note_off msg, std::size_t time)
   {
      events.push_back({time, msg.key(), 0.0f});
   }

   std::vector<note_event> events;
};

q::midi_file file{"riff.mid"};            (1)
if (file)                                 (2)
{
   recorder rec;
   file(sps, rec);                        (3)
}
1 Read the file from disk.
2 Check that the file was read and is well formed.
3 Play every message of the file into rec, in time order, timed in samples.

The first overload of recorder takes every message it has no other overload for, and ignores it.