DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
dspark::MidiFile Class Reference

Transactional, resource-bounded SMF parser and authoring API. More...

#include <MidiFile.h>

Public Member Functions

bool create (uint16_t format, uint16_t ppqn, size_t trackCount=1)
 Replaces the document with an empty writable format 0 or 1 file.
 
bool createSmpte (uint16_t format, int framesPerSecond, uint8_t ticksPerFrame, size_t trackCount=1)
 Replaces the document with an empty format 0 or 1 file that uses SMPTE time division.
 
std::optional< size_t > addTrack ()
 Adds an empty track to a format 1 document.
 
bool addChannelEvent (size_t track, uint32_t delta, uint8_t status, uint8_t data1, uint8_t data2=0)
 Adds a validated MIDI channel event.
 
bool addSysExEvent (size_t track, uint32_t delta, MidiEventKind f0OrF7, std::span< const uint8_t > payload)
 Adds an F0 or F7 SysEx packet event.
 
bool addMetaEvent (size_t track, uint32_t delta, uint8_t type, std::span< const uint8_t > payload={})
 Adds an opaque meta event.
 
void clear () noexcept
 Restores the empty-state sentinel.
 
bool read (const std::filesystem::path &path, const MidiReadOptions &options={})
 Reads and validates an SMF format 0, 1, or 2 file.
 
bool write (const std::filesystem::path &path) const
 Writes a validated format 0 or 1 document.
 
int format () const noexcept
 Returns 0, 1, or 2; returns -1 for an empty object.
 
uint16_t ticksPerQuarter () const noexcept
 Returns PPQN, or zero for an empty object or SMPTE division.
 
uint16_t division () const noexcept
 Returns the raw header division word, or zero for an empty object.
 
bool isSmpteDivision () const noexcept
 True when the document uses SMPTE time division.
 
int smpteFramesPerSecond () const noexcept
 Returns the SMPTE frame-rate code: 24, 25, 29 or 30.
 
uint8_t ticksPerFrame () const noexcept
 Returns SMPTE ticks per frame, or 0 for PPQN division.
 
MidiReadRecovery recoveries () const noexcept
 Returns the recoveries the last successful read applied.
 
const std::vector< MidiTrack > & tracks () const noexcept
 Returns immutable semantic tracks as an owner-thread reference view.
 
std::optional< std::vector< MidiTempoChange > > tempoMap (size_t track=0) const
 Builds the effective tempo map for a track.
 
std::optional< uint64_t > tickToMicroseconds (uint64_t tick, size_t track=0) const noexcept
 Converts an absolute tick to floor(exact elapsed microseconds).
 
std::optional< double > tickToSeconds (uint64_t tick, size_t track=0) const noexcept
 Converts an absolute tick directly from the shared rational sum.
 

Static Public Attributes

static constexpr uint64_t kMaxInputBytes = 256ull * 1024 * 1024
 Maximum accepted or produced file size.
 
static constexpr uint32_t kMaxTracks = 4096
 Maximum number of track chunks.
 
static constexpr uint64_t kMaxEvents = 2000000
 Maximum aggregate event count.
 
static constexpr uint64_t kMaxAggregatePayloadBytes = 128ull * 1024 * 1024
 Maximum aggregate SysEx and meta payload bytes.
 
static constexpr uint64_t kMaxTrackChunkBytes = 128ull * 1024 * 1024
 Maximum bytes in one track or skipped alien chunk.
 

Detailed Description

Transactional, resource-bounded SMF parser and authoring API.

A failed read closes the logical file and leaves the object empty. Failed authoring operations leave the existing document unchanged. write() validates and serializes the complete document before opening its target.

Threading: owner-managed offline use. tracks() returns an owner-thread reference view; no thread may read that view while another thread accesses the same MidiFile instance mutably.

Definition at line 198 of file MidiFile.h.

Member Function Documentation

◆ addChannelEvent()

bool dspark::MidiFile::addChannelEvent ( size_t  track,
uint32_t  delta,
uint8_t  status,
uint8_t  data1,
uint8_t  data2 = 0 
)
inline

Adds a validated MIDI channel event.

Status must be 0x80..0xef. Program-change and channel-pressure events use one data byte and require data2 == 0; all other channel events use two 7-bit data bytes.

Definition at line 278 of file MidiFile.h.

◆ addMetaEvent()

bool dspark::MidiFile::addMetaEvent ( size_t  track,
uint32_t  delta,
uint8_t  type,
std::span< const uint8_t >  payload = {} 
)
inline

Adds an opaque meta event.

Definition at line 312 of file MidiFile.h.

◆ addSysExEvent()

bool dspark::MidiFile::addSysExEvent ( size_t  track,
uint32_t  delta,
MidiEventKind  f0OrF7,
std::span< const uint8_t >  payload 
)
inline

Adds an F0 or F7 SysEx packet event.

Parameters
trackDestination track index.
deltaDelta time in ticks from the preceding event.
f0OrF7Must be MidiEventKind::SysExF0 or SysExF7.
payloadSysEx payload bytes, excluding the status byte.

Definition at line 298 of file MidiFile.h.

◆ addTrack()

std::optional< size_t > dspark::MidiFile::addTrack ( )
inline

Adds an empty track to a format 1 document.

Returns
The new track index, or nullopt when the operation is invalid.

Definition at line 254 of file MidiFile.h.

◆ clear()

void dspark::MidiFile::clear ( )
inlinenoexcept

Restores the empty-state sentinel.

Definition at line 327 of file MidiFile.h.

◆ create()

bool dspark::MidiFile::create ( uint16_t  format,
uint16_t  ppqn,
size_t  trackCount = 1 
)
inline

Replaces the document with an empty writable format 0 or 1 file.

Parameters
formatSMF format, either 0 or 1.
ppqnTicks per quarter note in the range 1..32767.
trackCountInitial track count. Format 0 requires exactly one.
Returns
True on success; false leaves the current document unchanged.

Definition at line 219 of file MidiFile.h.

◆ createSmpte()

bool dspark::MidiFile::createSmpte ( uint16_t  format,
int  framesPerSecond,
uint8_t  ticksPerFrame,
size_t  trackCount = 1 
)
inline

Replaces the document with an empty format 0 or 1 file that uses SMPTE time division.

Parameters
formatSMF format, either 0 or 1.
framesPerSecond24, 25, 29 (30 drop frame, 29.97 frames per second) or 30.
ticksPerFrameTicks per frame in the range 1..255.
trackCountInitial track count. Format 0 requires exactly one.
Returns
True on success; false leaves the current document unchanged.

Definition at line 237 of file MidiFile.h.

◆ division()

uint16_t dspark::MidiFile::division ( ) const
inlinenoexcept

Returns the raw header division word, or zero for an empty object.

Definition at line 484 of file MidiFile.h.

◆ format()

int dspark::MidiFile::format ( ) const
inlinenoexcept

Returns 0, 1, or 2; returns -1 for an empty object.

Definition at line 475 of file MidiFile.h.

◆ isSmpteDivision()

bool dspark::MidiFile::isSmpteDivision ( ) const
inlinenoexcept

True when the document uses SMPTE time division.

Definition at line 487 of file MidiFile.h.

◆ read()

bool dspark::MidiFile::read ( const std::filesystem::path &  path,
const MidiReadOptions &  options = {} 
)
inline

Reads and validates an SMF format 0, 1, or 2 file.

With the default options every structural defect is fatal and only PPQN division is accepted. Recoveries allowed in options are applied only when needed and reported by recoveries(); a recovered document always satisfies the strict model (each track ends with one end-of-track event and no SysEx chain is left open), so it can be written back unchanged.

Parameters
pathFile to read.
optionsAllowed recoveries and division kinds.
Returns
True on success. Any failure leaves this object empty.

Definition at line 350 of file MidiFile.h.

◆ recoveries()

MidiReadRecovery dspark::MidiFile::recoveries ( ) const
inlinenoexcept

Returns the recoveries the last successful read applied.

None after a strict read, a failed read, create(), createSmpte() or clear().

Definition at line 517 of file MidiFile.h.

◆ smpteFramesPerSecond()

int dspark::MidiFile::smpteFramesPerSecond ( ) const
inlinenoexcept

Returns the SMPTE frame-rate code: 24, 25, 29 or 30.

29 is the SMF code for 30 drop frame, whose real rate is 30000/1001 (29.97) frames per second; timing uses that exact rate. Returns 0 for PPQN division or an empty object.

Definition at line 499 of file MidiFile.h.

◆ tempoMap()

std::optional< std::vector< MidiTempoChange > > dspark::MidiFile::tempoMap ( size_t  track = 0) const
inline

Builds the effective tempo map for a track.

Formats 0 and 1 always use track zero. Format 2 uses the requested independent track. The returned map always starts at tick zero with the default 500000 us/qn unless a tick-zero tempo replaces it. Returns nullopt for an SMPTE-division document: tempo events do not affect its timing.

Definition at line 545 of file MidiFile.h.

◆ ticksPerFrame()

uint8_t dspark::MidiFile::ticksPerFrame ( ) const
inlinenoexcept

Returns SMPTE ticks per frame, or 0 for PPQN division.

Definition at line 505 of file MidiFile.h.

◆ ticksPerQuarter()

uint16_t dspark::MidiFile::ticksPerQuarter ( ) const
inlinenoexcept

Returns PPQN, or zero for an empty object or SMPTE division.

Definition at line 478 of file MidiFile.h.

◆ tickToMicroseconds()

std::optional< uint64_t > dspark::MidiFile::tickToMicroseconds ( uint64_t  tick,
size_t  track = 0 
) const
inlinenoexcept

Converts an absolute tick to floor(exact elapsed microseconds).

PPQN documents integrate the tempo map. SMPTE documents ignore tempo events: a tick is 1 / (framesPerSecond * ticksPerFrame) seconds, with 30000/1001 frames per second for the 29 (30 drop frame) code.

Returns
nullopt for an empty/invalid track or uint64 overflow.

Definition at line 580 of file MidiFile.h.

◆ tickToSeconds()

std::optional< double > dspark::MidiFile::tickToSeconds ( uint64_t  tick,
size_t  track = 0 
) const
inlinenoexcept

Converts an absolute tick directly from the shared rational sum.

Same time base as tickToMicroseconds(), rounded once to double.

Returns
nullopt for an empty/invalid track or elapsed-time overflow.

Definition at line 598 of file MidiFile.h.

◆ tracks()

const std::vector< MidiTrack > & dspark::MidiFile::tracks ( ) const
inlinenoexcept

Returns immutable semantic tracks as an owner-thread reference view.

The reference is valid only while this MidiFile remains alive and until the next non-const operation that can replace or mutate its document. No thread may read the view while another thread accesses this instance mutably. This accessor does not provide atomic publication.

Definition at line 530 of file MidiFile.h.

◆ write()

bool dspark::MidiFile::write ( const std::filesystem::path &  path) const
inline

Writes a validated format 0 or 1 document.

A missing EOT is emitted with delta zero without changing tracks(). The complete output is built before the destination is opened, and false is returned when writing, flushing or closing the destination fails.

Definition at line 387 of file MidiFile.h.

Member Data Documentation

◆ kMaxAggregatePayloadBytes

constexpr uint64_t dspark::MidiFile::kMaxAggregatePayloadBytes = 128ull * 1024 * 1024
staticconstexpr

Maximum aggregate SysEx and meta payload bytes.

Definition at line 208 of file MidiFile.h.

◆ kMaxEvents

constexpr uint64_t dspark::MidiFile::kMaxEvents = 2000000
staticconstexpr

Maximum aggregate event count.

Definition at line 206 of file MidiFile.h.

◆ kMaxInputBytes

constexpr uint64_t dspark::MidiFile::kMaxInputBytes = 256ull * 1024 * 1024
staticconstexpr

Maximum accepted or produced file size.

Definition at line 202 of file MidiFile.h.

◆ kMaxTrackChunkBytes

constexpr uint64_t dspark::MidiFile::kMaxTrackChunkBytes = 128ull * 1024 * 1024
staticconstexpr

Maximum bytes in one track or skipped alien chunk.

Definition at line 210 of file MidiFile.h.

◆ kMaxTracks

constexpr uint32_t dspark::MidiFile::kMaxTracks = 4096
staticconstexpr

Maximum number of track chunks.

Definition at line 204 of file MidiFile.h.


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