|
DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
|
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. | |
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.
|
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.
|
inline |
Adds an opaque meta event.
Definition at line 312 of file MidiFile.h.
|
inline |
Adds an F0 or F7 SysEx packet event.
| track | Destination track index. |
| delta | Delta time in ticks from the preceding event. |
| f0OrF7 | Must be MidiEventKind::SysExF0 or SysExF7. |
| payload | SysEx payload bytes, excluding the status byte. |
Definition at line 298 of file MidiFile.h.
|
inline |
Adds an empty track to a format 1 document.
Definition at line 254 of file MidiFile.h.
|
inlinenoexcept |
Restores the empty-state sentinel.
Definition at line 327 of file MidiFile.h.
|
inline |
Replaces the document with an empty writable format 0 or 1 file.
| format | SMF format, either 0 or 1. |
| ppqn | Ticks per quarter note in the range 1..32767. |
| trackCount | Initial track count. Format 0 requires exactly one. |
Definition at line 219 of file MidiFile.h.
|
inline |
Replaces the document with an empty format 0 or 1 file that uses SMPTE time division.
| format | SMF format, either 0 or 1. |
| framesPerSecond | 24, 25, 29 (30 drop frame, 29.97 frames per second) or 30. |
| ticksPerFrame | Ticks per frame in the range 1..255. |
| trackCount | Initial track count. Format 0 requires exactly one. |
Definition at line 237 of file MidiFile.h.
|
inlinenoexcept |
Returns the raw header division word, or zero for an empty object.
Definition at line 484 of file MidiFile.h.
|
inlinenoexcept |
Returns 0, 1, or 2; returns -1 for an empty object.
Definition at line 475 of file MidiFile.h.
|
inlinenoexcept |
True when the document uses SMPTE time division.
Definition at line 487 of file MidiFile.h.
|
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.
| path | File to read. |
| options | Allowed recoveries and division kinds. |
Definition at line 350 of file MidiFile.h.
|
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.
|
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.
|
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.
|
inlinenoexcept |
Returns SMPTE ticks per frame, or 0 for PPQN division.
Definition at line 505 of file MidiFile.h.
|
inlinenoexcept |
Returns PPQN, or zero for an empty object or SMPTE division.
Definition at line 478 of file MidiFile.h.
|
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.
Definition at line 580 of file MidiFile.h.
|
inlinenoexcept |
Converts an absolute tick directly from the shared rational sum.
Same time base as tickToMicroseconds(), rounded once to double.
Definition at line 598 of file MidiFile.h.
|
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.
|
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.
|
staticconstexpr |
Maximum aggregate SysEx and meta payload bytes.
Definition at line 208 of file MidiFile.h.
|
staticconstexpr |
Maximum aggregate event count.
Definition at line 206 of file MidiFile.h.
|
staticconstexpr |
Maximum accepted or produced file size.
Definition at line 202 of file MidiFile.h.
|
staticconstexpr |
Maximum bytes in one track or skipped alien chunk.
Definition at line 210 of file MidiFile.h.
|
staticconstexpr |
Maximum number of track chunks.
Definition at line 204 of file MidiFile.h.