Skip to content

Commit

Permalink
add MidiFileIn bpq() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Oct 12, 2024
1 parent c18b3cd commit c617a1a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
7 changes: 7 additions & 0 deletions VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ ChucK VERSIONS log

1.5.3.2
=======
- (added) MidiFileIn.bpm()
"Same as beatsPerMinute()."
- (added) MidiFileIn.ticksPerQuarter();
- (added) MidiFileIn.tpq();
"Get the ticks per quarter (TPQ) value from the MIDI file header."
- (fixed) documentation for MidiMsg.when:
"Duration since the last MidiMsg (only valid for MidiFileIn)."
- (fixed, windows) SndBuf issue when compiled with UNICODE (FYI SndBuf is still ANSI,
but this averts an error due to char width mismatch)

Expand Down
3 changes: 2 additions & 1 deletion src/core/chuck_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,8 @@ t_CKBOOL init_class_Midi( Chuck_Env * env )
if( MidiMsg_offset_data3 == CK_INVALID_OFFSET ) goto error;

// add member variable
doc = "Time when the MidiMsg occurred, relative to the start of the file (only valid for MidiFileIn).";
doc = "Duration since the last MidiMsg (only valid for MidiFileIn).";
// Time when the MidiMsg occurred, relative to the start of the file (only valid for MidiFileIn).";
MidiMsg_offset_when = type_engine_import_mvar( env, "dur", "when", FALSE, doc.c_str() );
if( MidiMsg_offset_when == CK_INVALID_OFFSET ) goto error;

Expand Down
48 changes: 48 additions & 0 deletions src/core/ugen_stk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,8 @@ CK_DLL_DTOR( MidiFileIn_dtor );
CK_DLL_MFUN( MidiFileIn_open );
CK_DLL_MFUN( MidiFileIn_close );
CK_DLL_MFUN( MidiFileIn_numTracks );
CK_DLL_MFUN( MidiFileIn_ticksPerQuarter );
CK_DLL_MFUN( MidiFileIn_beatsPerMinute );
CK_DLL_MFUN( MidiFileIn_read );
CK_DLL_MFUN( MidiFileIn_readTrack );
CK_DLL_MFUN( MidiFileIn_rewind );
Expand Down Expand Up @@ -5231,6 +5233,22 @@ Modified algorithm code by Gary Scavone, 2005.";
func->doc = "Get the number of tracks in the open MIDI file.";
if( !type_engine_import_mfun( env, func ) ) goto error;

func = make_new_mfun( "int", "ticksPerQuarter", MidiFileIn_ticksPerQuarter );
func->doc = "Get the ticks per quarter (TPQ) value from the MIDI file header.";
if( !type_engine_import_mfun( env, func ) ) goto error;

func = make_new_mfun( "int", "tpq", MidiFileIn_ticksPerQuarter );
func->doc = "Same as ticksPerQuarter().";
if( !type_engine_import_mfun( env, func ) ) goto error;

func = make_new_mfun( "float", "beatsPerMinute", MidiFileIn_beatsPerMinute );
func->doc = "Get the beats per minute (BPM) value from the MIDI file header.";
if( !type_engine_import_mfun( env, func ) ) goto error;

func = make_new_mfun( "float", "bpm", MidiFileIn_beatsPerMinute );
func->doc = "Same as beatsPerMinute().";
if( !type_engine_import_mfun( env, func ) ) goto error;

func = make_new_mfun( "void", "rewind", MidiFileIn_rewind );
func->doc = "Rewind MIDI reader to beginning of default track 0.";
if( !type_engine_import_mfun( env, func ) ) goto error;
Expand Down Expand Up @@ -20156,6 +20174,8 @@ MidiFileIn :: MidiFileIn( std::string fileName )
{
// ge: initialize
bpm_ = 0;
// ge & alex & kiran
tpq_ = 0;
// 1.4.1.1 (ge) string buffer for error message
std::stringstream msg;
// 1.5.0.4 (ge) add initialization
Expand Down Expand Up @@ -20228,6 +20248,8 @@ MidiFileIn :: MidiFileIn( std::string fileName )
}
else {
tickrate = (double) (*data & 0x7FFF); // ticks per quarter note
// save for lookup later | (ge & alex & kiran) 1.5.3.2
tpq_ = tickrate;
}

// Now locate the track offsets and lengths. If not using time
Expand Down Expand Up @@ -20354,6 +20376,12 @@ double MidiFileIn :: getBPM()
return bpm_;
}

// ge & alex & kiran: implemented 1.5.3.2
double MidiFileIn :: getTPQ()
{
return tpq_;
}

unsigned long MidiFileIn :: getNextEvent( std::vector<unsigned char> *event, unsigned int track )
{
// Fill the user-provided vector with the next event in the
Expand Down Expand Up @@ -29231,6 +29259,26 @@ CK_DLL_MFUN( MidiFileIn_numTracks )
RETURN->v_int = 0;
}

CK_DLL_MFUN( MidiFileIn_beatsPerMinute )
{
stk::MidiFileIn *f = (stk::MidiFileIn *) OBJ_MEMBER_UINT(SELF, MidiFileIn_offset_data);

if(f)
RETURN->v_float = f->getBPM();
else
RETURN->v_float = 0;
}

CK_DLL_MFUN( MidiFileIn_ticksPerQuarter )
{
stk::MidiFileIn *f = (stk::MidiFileIn *) OBJ_MEMBER_UINT(SELF, MidiFileIn_offset_data);

if(f)
RETURN->v_int = f->getTPQ();
else
RETURN->v_int = 0;
}

CK_DLL_MFUN( MidiFileIn_read )
{
stk::MidiFileIn *f = (stk::MidiFileIn *) OBJ_MEMBER_UINT(SELF, MidiFileIn_offset_data);
Expand Down
8 changes: 8 additions & 0 deletions src/core/ugen_stk.h
Original file line number Diff line number Diff line change
Expand Up @@ -6752,6 +6752,12 @@ class MidiFileIn : public Stk
*/
double getBPM();

//! ge & alex & kiran: get the ticks per beat/quarter (TPQ)
/*!
this is only valid if NOT using SMPTE time codes
*/
double getTPQ();

protected:

// This protected class function is used for reading variable-length
Expand All @@ -6773,6 +6779,8 @@ class MidiFileIn : public Stk
std::vector<char> trackStatus_;
// ge:
double bpm_;
// ge & alex & kiran
double tpq_; // ticks per quarter | 1.5.3.2

// This structure and the following variables are used to save and
// keep track of a format 1 tempo map (and the initial tickSeconds
Expand Down

0 comments on commit c617a1a

Please sign in to comment.