smf.h File Reference

Public interface declaration for libsmf, Standard MIDI File format library. More...

Go to the source code of this file.

Data Structures

struct  smf_struct
 Represents a "song", that is, collection of one or more tracks. More...
struct  smf_tempo_struct
 Describes a single tempo or time signature change. More...
struct  smf_track_struct
 Represents a single track. More...
struct  smf_event_struct
 Represents a single MIDI event or metaevent. More...

Typedefs

typedef struct smf_struct smf_t
typedef struct smf_tempo_struct smf_tempo_t
typedef struct smf_track_struct smf_track_t
typedef struct smf_event_struct smf_event_t

Functions

smf_tsmf_new (void) WARN_UNUSED_RESULT
 Allocates new smf_t structure.
void smf_delete (smf_t *smf)
 Frees smf and all it's descendant structures.
int smf_set_format (smf_t *smf, int format) WARN_UNUSED_RESULT
 Sets "Format" field of MThd header to the specified value.
int smf_set_ppqn (smf_t *smf, int format) WARN_UNUSED_RESULT
 Sets the PPQN ("Division") field of MThd header.
char * smf_decode (const smf_t *smf) WARN_UNUSED_RESULT
smf_track_tsmf_get_track_by_number (const smf_t *smf, int track_number) WARN_UNUSED_RESULT
smf_event_tsmf_peek_next_event (smf_t *smf) WARN_UNUSED_RESULT
smf_event_tsmf_get_next_event (smf_t *smf) WARN_UNUSED_RESULT
void smf_skip_next_event (smf_t *smf)
 Advance the "next event counter".
void smf_rewind (smf_t *smf)
 Rewinds the SMF.
int smf_seek_to_seconds (smf_t *smf, double seconds) WARN_UNUSED_RESULT
 Seeks the SMF to the given position.
int smf_seek_to_pulses (smf_t *smf, int pulses) WARN_UNUSED_RESULT
 Seeks the SMF to the given position.
int smf_seek_to_event (smf_t *smf, const smf_event_t *event) WARN_UNUSED_RESULT
 Seeks the SMF to the given event.
int smf_get_length_pulses (const smf_t *smf) WARN_UNUSED_RESULT
double smf_get_length_seconds (const smf_t *smf) WARN_UNUSED_RESULT
int smf_event_is_last (const smf_event_t *event) WARN_UNUSED_RESULT
void smf_add_track (smf_t *smf, smf_track_t *track)
 Appends smf_track_t to smf.
void smf_track_remove_from_smf (smf_track_t *track)
 Detaches track from the smf.
smf_track_tsmf_track_new (void) WARN_UNUSED_RESULT
 Allocates new smf_track_t structure.
void smf_track_delete (smf_track_t *track)
 Detaches track from its smf and frees it.
smf_event_tsmf_track_get_next_event (smf_track_t *track) WARN_UNUSED_RESULT
 Returns next event from the track given and advances next event counter.
smf_event_tsmf_track_get_event_by_number (const smf_track_t *track, int event_number) WARN_UNUSED_RESULT
smf_event_tsmf_track_get_last_event (const smf_track_t *track) WARN_UNUSED_RESULT
void smf_track_add_event_delta_pulses (smf_track_t *track, smf_event_t *event, int pulses)
 Adds event to the track at the time "pulses" clocks from the previous event in this track.
void smf_track_add_event_pulses (smf_track_t *track, smf_event_t *event, int pulses)
 Adds event to the track at the time "pulses" clocks from the start of song.
void smf_track_add_event_seconds (smf_track_t *track, smf_event_t *event, double seconds)
 Adds event to the track at the time "seconds" seconds from the start of song.
int smf_track_add_eot_delta_pulses (smf_track_t *track, int delta) WARN_UNUSED_RESULT
 Add End Of Track metaevent.
int smf_track_add_eot_pulses (smf_track_t *track, int pulses) WARN_UNUSED_RESULT
int smf_track_add_eot_seconds (smf_track_t *track, double seconds) WARN_UNUSED_RESULT
void smf_event_remove_from_track (smf_event_t *event)
 Detaches event from its track.
smf_event_tsmf_event_new (void) WARN_UNUSED_RESULT
 Allocates new smf_event_t structure.
smf_event_tsmf_event_new_from_pointer (void *midi_data, int len) WARN_UNUSED_RESULT
 Allocates an smf_event_t structure and fills it with "len" bytes copied from "midi_data".
smf_event_tsmf_event_new_from_bytes (int first_byte, int second_byte, int third_byte) WARN_UNUSED_RESULT
 Allocates an smf_event_t structure and fills it with at most three bytes of data.
smf_event_tsmf_event_new_textual (int type, const char *text)
void smf_event_delete (smf_event_t *event)
 Detaches event from its track and frees it.
int smf_event_is_valid (const smf_event_t *event) WARN_UNUSED_RESULT
int smf_event_is_metadata (const smf_event_t *event) WARN_UNUSED_RESULT
int smf_event_is_system_realtime (const smf_event_t *event) WARN_UNUSED_RESULT
int smf_event_is_system_common (const smf_event_t *event) WARN_UNUSED_RESULT
int smf_event_is_sysex (const smf_event_t *event) WARN_UNUSED_RESULT
int smf_event_is_eot (const smf_event_t *event) WARN_UNUSED_RESULT
int smf_event_is_textual (const smf_event_t *event) WARN_UNUSED_RESULT
char * smf_event_decode (const smf_event_t *event) WARN_UNUSED_RESULT
char * smf_event_extract_text (const smf_event_t *event) WARN_UNUSED_RESULT
 Extracts text from "textual metaevents", such as Text or Lyric.
smf_tsmf_load (const char *file_name) WARN_UNUSED_RESULT
 Loads SMF file.
smf_tsmf_load_from_memory (const void *buffer, const int buffer_length) WARN_UNUSED_RESULT
 Creates new SMF and fills it with data loaded from the given buffer.
int smf_save (smf_t *smf, const char *file_name) WARN_UNUSED_RESULT
 Writes the contents of SMF to the file given.
smf_tempo_tsmf_get_tempo_by_pulses (const smf_t *smf, int pulses) WARN_UNUSED_RESULT
 Return last tempo (i.e.
smf_tempo_tsmf_get_tempo_by_seconds (const smf_t *smf, double seconds) WARN_UNUSED_RESULT
 Return last tempo (i.e.
smf_tempo_tsmf_get_tempo_by_number (const smf_t *smf, int number) WARN_UNUSED_RESULT
smf_tempo_tsmf_get_last_tempo (const smf_t *smf) WARN_UNUSED_RESULT
 Return last tempo.
const char * smf_get_version (void) WARN_UNUSED_RESULT

Detailed Description

Public interface declaration for libsmf, Standard MIDI File format library.

Definition in file smf.h.


Typedef Documentation

typedef struct smf_event_struct smf_event_t

Definition at line 334 of file smf.h.

typedef struct smf_struct smf_t

Definition at line 255 of file smf.h.

typedef struct smf_tempo_struct smf_tempo_t

Definition at line 268 of file smf.h.

typedef struct smf_track_struct smf_track_t

Definition at line 298 of file smf.h.


Function Documentation

void smf_add_track ( smf_t smf,
smf_track_t track 
)

Appends smf_track_t to smf.

Definition at line 156 of file smf.c.

References smf_struct::number_of_tracks, smf_track_struct::smf, smf_set_format(), smf_track_struct::track_number, and smf_struct::tracks_array.

Referenced by smf_load_from_memory().

char* smf_decode ( const smf_t smf  ) 
Returns:
Textual representation of the data extracted from MThd header, or NULL, if something goes wrong. Returned string looks like this:

format: 1 (several simultaneous tracks); number of tracks: 4; division: 192 PPQN.

You should free the returned string afterwards, using free(3).

Definition at line 598 of file smf_decode.c.

References smf_struct::format, smf_struct::frames_per_second, smf_struct::number_of_tracks, smf_struct::ppqn, and smf_struct::resolution.

void smf_delete ( smf_t smf  ) 

Frees smf and all it's descendant structures.

Definition at line 88 of file smf.c.

References smf_struct::number_of_tracks, smf_fini_tempo(), smf_track_delete(), smf_struct::tempo_array, and smf_struct::tracks_array.

char* smf_event_decode ( const smf_event_t event  ) 
Returns:
Textual representation of the event given, or NULL, if event is unknown. Returned string looks like this:

Note On, channel 1, note F#3, velocity 0

You should free the returned string afterwards, using free(3).

Definition at line 514 of file smf_decode.c.

References smf_event_struct::midi_buffer, smf_event_is_metadata(), smf_event_is_system_common(), smf_event_is_system_realtime(), and smf_event_length_is_valid().

void smf_event_delete ( smf_event_t event  ) 
char* smf_event_extract_text ( const smf_event_t event  ) 

Extracts text from "textual metaevents", such as Text or Lyric.

Returns:
Zero-terminated string extracted from "text events" or NULL, if there was any problem.

Definition at line 647 of file smf_load.c.

References smf_event_struct::midi_buffer, smf_event_struct::midi_buffer_length, and smf_event_is_textual().

int smf_event_is_eot ( const smf_event_t event  ) 
Returns:
Nonzero, if event is End Of Track metaevent.

Definition at line 475 of file smf_save.c.

References smf_event_struct::midi_buffer, and smf_event_struct::midi_buffer_length.

int smf_event_is_last ( const smf_event_t event  ) 
Returns:
Nonzero, if there are no events in the SMF after this one. Note that may be more than one "last event", if they occur at the same time.

Definition at line 1100 of file smf.c.

References smf_track_struct::smf, smf_get_length_pulses(), smf_event_struct::time_pulses, and smf_event_struct::track.

Referenced by smf_event_remove_from_track(), and smf_track_add_event().

int smf_event_is_metadata ( const smf_event_t event  ) 
Returns:
Nonzero if event is metaevent. You should never send metaevents; they are not really MIDI messages. They carry information like track title, time signature etc.

Definition at line 57 of file smf_decode.c.

References smf_event_struct::midi_buffer, and smf_event_struct::midi_buffer_length.

Referenced by maybe_add_to_tempo_map(), smf_event_decode(), smf_event_is_system_realtime(), smf_event_is_tempo_change_or_time_signature(), and smf_event_is_textual().

int smf_event_is_sysex ( const smf_event_t event  ) 
Returns:
Nonzero if event is SysEx message.

Definition at line 104 of file smf_decode.c.

References smf_event_struct::midi_buffer, and smf_event_struct::midi_buffer_length.

Referenced by smf_event_length_is_valid().

int smf_event_is_system_common ( const smf_event_t event  ) 
Returns:
Nonzero if event is System Common.

Definition at line 90 of file smf_decode.c.

References smf_event_struct::midi_buffer, and smf_event_struct::midi_buffer_length.

Referenced by smf_event_decode().

int smf_event_is_system_realtime ( const smf_event_t event  ) 
Returns:
Nonzero if event is System Realtime.

Definition at line 72 of file smf_decode.c.

References smf_event_struct::midi_buffer, smf_event_struct::midi_buffer_length, and smf_event_is_metadata().

Referenced by smf_event_decode().

int smf_event_is_textual ( const smf_event_t event  ) 
Returns:
1, if passed a metaevent containing text, that is, Text, Copyright, Sequence/Track Name, Instrument, Lyric, Marker, Cue Point, Program Name, or Device Name; 0 otherwise.

Definition at line 627 of file smf_load.c.

References smf_event_struct::midi_buffer, smf_event_struct::midi_buffer_length, and smf_event_is_metadata().

Referenced by smf_event_extract_text().

int smf_event_is_valid ( const smf_event_t event  ) 
Returns:
Nonzero, if MIDI data in the event is valid, 0 otherwise. For example, it checks if event length is correct.

Definition at line 745 of file smf_load.c.

References is_status_byte(), smf_event_struct::midi_buffer, smf_event_struct::midi_buffer_length, and smf_event_length_is_valid().

smf_event_t* smf_event_new ( void   ) 

Allocates new smf_event_t structure.

The caller is responsible for allocating event->midi_buffer, filling it with MIDI data and setting event->midi_buffer_length properly. Note that event->midi_buffer will be freed by smf_event_delete.

Returns:
pointer to smf_event_t or NULL.

Definition at line 217 of file smf.c.

Referenced by smf_event_new_from_bytes(), smf_event_new_from_pointer(), and smf_event_new_textual().

smf_event_t* smf_event_new_from_bytes ( int  first_byte,
int  second_byte,
int  third_byte 
)

Allocates an smf_event_t structure and fills it with at most three bytes of data.

For example, if you need to create Note On event, do something like this:

smf_event_new_from_bytes(0x90, 0x3C, 0x7f);

To create event for MIDI message that is shorter than three bytes, do something like this:

smf_event_new_from_bytes(0xC0, 0x42, -1);

Parameters:
first_byte First byte of MIDI message. Must be valid status byte.
second_byte Second byte of MIDI message or -1, if message is one byte long.
third_byte Third byte of MIDI message or -1, if message is two bytes long.
Returns:
Event containing MIDI data or NULL.

Definition at line 282 of file smf.c.

References is_status_byte(), smf_event_struct::midi_buffer, smf_event_struct::midi_buffer_length, smf_event_delete(), and smf_event_new().

Referenced by smf_track_add_eot_delta_pulses(), smf_track_add_eot_pulses(), and smf_track_add_eot_seconds().

smf_event_t* smf_event_new_from_pointer ( void *  midi_data,
int  len 
)

Allocates an smf_event_t structure and fills it with "len" bytes copied from "midi_data".

Parameters:
midi_data Pointer to MIDI data. It sill be copied to the newly allocated event->midi_buffer.
len Length of the buffer. It must be proper MIDI event length, e.g. 3 for Note On event.
Returns:
Event containing MIDI data or NULL.

Definition at line 243 of file smf.c.

References smf_event_struct::midi_buffer, smf_event_struct::midi_buffer_length, smf_event_delete(), and smf_event_new().

smf_event_t* smf_event_new_textual ( int  type,
const char *  text 
)
void smf_event_remove_from_track ( smf_event_t event  ) 
smf_tempo_t* smf_get_last_tempo ( const smf_t smf  ) 

Return last tempo.

Definition at line 326 of file smf_tempo.c.

References smf_get_tempo_by_number(), and smf_struct::tempo_array.

Referenced by remove_last_tempo_with_pulses().

int smf_get_length_pulses ( const smf_t smf  ) 
Returns:
Length of SMF, in pulses.

Definition at line 1044 of file smf.c.

References smf_struct::number_of_tracks, smf_get_track_by_number(), smf_track_get_last_event(), and smf_event_struct::time_pulses.

Referenced by smf_event_is_last().

double smf_get_length_seconds ( const smf_t smf  ) 
Returns:
Length of SMF, in seconds.

Definition at line 1071 of file smf.c.

References smf_struct::number_of_tracks, smf_get_track_by_number(), smf_track_get_last_event(), and smf_event_struct::time_seconds.

smf_event_t* smf_get_next_event ( smf_t smf  ) 
Returns:
Next event, in time order, or NULL, if there are none left.

Definition at line 835 of file smf.c.

References smf_find_track_with_next_event(), and smf_track_get_next_event().

Referenced by smf_create_tempo_map_and_compute_seconds(), and smf_skip_next_event().

smf_tempo_t* smf_get_tempo_by_number ( const smf_t smf,
int  number 
)
smf_tempo_t* smf_get_tempo_by_pulses ( const smf_t smf,
int  pulses 
)

Return last tempo (i.e.

tempo with greatest time_pulses) that happens before "pulses".

Definition at line 271 of file smf_tempo.c.

References smf_get_tempo_by_number(), smf_struct::tempo_array, and smf_tempo_struct::time_pulses.

smf_tempo_t* smf_get_tempo_by_seconds ( const smf_t smf,
double  seconds 
)

Return last tempo (i.e.

tempo with greatest time_seconds) that happens before "seconds".

Definition at line 298 of file smf_tempo.c.

References smf_get_tempo_by_number(), smf_struct::tempo_array, and smf_tempo_struct::time_seconds.

smf_track_t* smf_get_track_by_number ( const smf_t smf,
int  track_number 
)
Returns:
Track with a given number or NULL, if there is no such track. Tracks are numbered consecutively starting from one.

Definition at line 748 of file smf.c.

References smf_struct::number_of_tracks, and smf_struct::tracks_array.

Referenced by smf_find_track_with_next_event(), smf_get_length_pulses(), smf_get_length_seconds(), smf_rewind(), smf_save(), and smf_track_remove_from_smf().

const char* smf_get_version ( void   ) 
Returns:
Version of libsmf.

Definition at line 1112 of file smf.c.

smf_t* smf_load ( const char *  file_name  ) 

Loads SMF file.

Parameters:
file_name Path to the file.
Returns:
SMF or NULL, if loading failed.

Definition at line 912 of file smf_load.c.

References smf_load_from_memory(), and smf_rewind().

smf_t* smf_load_from_memory ( const void *  buffer,
const int  buffer_length 
)
smf_t* smf_new ( void   ) 

Allocates new smf_t structure.

Returns:
pointer to smf_t or NULL.

Definition at line 55 of file smf.c.

References smf, smf_init_tempo(), smf_set_format(), smf_set_ppqn(), smf_struct::tempo_array, and smf_struct::tracks_array.

Referenced by main(), and smf_load_from_memory().

smf_event_t* smf_peek_next_event ( smf_t smf  ) 
Returns:
Next event, in time order, or NULL, if there are none left. Does not advance position in song.

Definition at line 874 of file smf.c.

References smf_find_track_with_next_event().

Referenced by smf_seek_to_event(), smf_seek_to_pulses(), and smf_seek_to_seconds().

void smf_rewind ( smf_t smf  ) 
int smf_save ( smf_t smf,
const char *  file_name 
)

Writes the contents of SMF to the file given.

Parameters:
smf SMF.
file_name Path to the file.
Returns:
0, if saving was successfull.

Definition at line 620 of file smf_save.c.

References smf_struct::number_of_tracks, smf_get_track_by_number(), and smf_rewind().

int smf_seek_to_event ( smf_t smf,
const smf_event_t target 
)

Seeks the SMF to the given event.

After calling this routine, smf_get_next_event will return the event that was the second argument of this call.

Definition at line 934 of file smf.c.

References smf_event_struct::event_number, smf_struct::last_seek_position, smf_peek_next_event(), smf_rewind(), smf_skip_next_event(), smf_event_struct::track, and smf_track_struct::track_number.

int smf_seek_to_pulses ( smf_t smf,
int  pulses 
)

Seeks the SMF to the given position.

For example, after seeking to 10 pulses, smf_get_next_event will return first event that happens after the first ten pulses.

Definition at line 1009 of file smf.c.

References smf_struct::last_seek_position, smf_peek_next_event(), smf_rewind(), smf_skip_next_event(), and smf_event_struct::time_pulses.

int smf_seek_to_seconds ( smf_t smf,
double  seconds 
)

Seeks the SMF to the given position.

For example, after seeking to 1.0 seconds, smf_get_next_event will return first event that happens after the first second of song.

Definition at line 966 of file smf.c.

References smf_struct::last_seek_position, smf_peek_next_event(), smf_rewind(), smf_skip_next_event(), and smf_event_struct::time_seconds.

int smf_set_format ( smf_t smf,
int  format 
)

Sets "Format" field of MThd header to the specified value.

Note that you don't really need to use this, as libsmf will automatically change format from 0 to 1 when you add the second track.

Parameters:
smf SMF.
format 0 for one track per file, 1 for several tracks per file.

Definition at line 652 of file smf.c.

References smf_struct::format, and smf_struct::number_of_tracks.

Referenced by smf_add_track(), and smf_new().

int smf_set_ppqn ( smf_t smf,
int  ppqn 
)

Sets the PPQN ("Division") field of MThd header.

This is mandatory, you should call it right after smf_new. Note that changing PPQN will change time_seconds of all the events.

Parameters:
smf SMF.
ppqn New PPQN.

Definition at line 674 of file smf.c.

References smf_struct::ppqn.

Referenced by smf_new().

void smf_skip_next_event ( smf_t smf  ) 

Advance the "next event counter".

This is functionally the same as calling smf_get_next_event and ignoring the return value.

Definition at line 862 of file smf.c.

References smf_get_next_event().

Referenced by smf_seek_to_event(), smf_seek_to_pulses(), and smf_seek_to_seconds().

int smf_track_add_eot_delta_pulses ( smf_track_t track,
int  delta 
)

Add End Of Track metaevent.

Using it is optional, libsmf will automatically add EOT to the tracks during smf_save, with delta_pulses 0. If you try to add EOT in the middle of the track, it will fail and nonzero value will be returned. If you try to add EOT after another EOT event, it will be added, but the existing EOT event will be removed.

Returns:
0 if everything went ok, nonzero otherwise.

Definition at line 524 of file smf.c.

References smf_event_new_from_bytes(), and smf_track_add_event_delta_pulses().

int smf_track_add_eot_pulses ( smf_track_t track,
int  pulses 
)
int smf_track_add_eot_seconds ( smf_track_t track,
double  seconds 
)
void smf_track_add_event_delta_pulses ( smf_track_t track,
smf_event_t event,
int  delta 
)

Adds event to the track at the time "pulses" clocks from the previous event in this track.

The remaining two time fields will be computed automatically based on the third argument and current tempo map. Note that ->delta_pulses is computed by smf.c:smf_track_add_event, not here.

Definition at line 403 of file smf_tempo.c.

References smf_track_struct::smf, smf_track_add_event_pulses(), smf_event_struct::time_pulses, and smf_event_struct::time_seconds.

Referenced by smf_track_add_eot_delta_pulses().

void smf_track_add_event_pulses ( smf_track_t track,
smf_event_t event,
int  pulses 
)

Adds event to the track at the time "pulses" clocks from the start of song.

The remaining two time fields will be computed automatically based on the third argument and current tempo map.

Definition at line 419 of file smf_tempo.c.

References smf_track_struct::smf, smf_track_add_event(), smf_event_struct::time_pulses, and smf_event_struct::time_seconds.

Referenced by smf_track_add_eot_pulses(), and smf_track_add_event_delta_pulses().

void smf_track_add_event_seconds ( smf_track_t track,
smf_event_t event,
double  seconds 
)

Adds event to the track at the time "seconds" seconds from the start of song.

The remaining two time fields will be computed automatically based on the third argument and current tempo map.

Definition at line 437 of file smf_tempo.c.

References smf_track_struct::smf, smf_track_add_event(), smf_event_struct::time_pulses, and smf_event_struct::time_seconds.

Referenced by smf_track_add_eot_seconds().

void smf_track_delete ( smf_track_t track  ) 

Detaches track from its smf and frees it.

Definition at line 131 of file smf.c.

References smf_track_struct::events_array, smf_track_struct::number_of_events, smf_track_struct::smf, smf_event_delete(), and smf_track_remove_from_smf().

Referenced by smf_delete(), and smf_load_from_memory().

smf_event_t* smf_track_get_event_by_number ( const smf_track_t track,
int  event_number 
)
Returns:
Event with a given number or NULL, if there is no such event. Events are numbered consecutively starting from one.

Definition at line 769 of file smf.c.

References smf_track_struct::events_array, and smf_track_struct::number_of_events.

Referenced by smf_event_remove_from_track(), smf_track_add_event(), smf_track_get_last_event(), smf_track_get_next_event(), and smf_track_remove_from_smf().

smf_event_t* smf_track_get_last_event ( const smf_track_t track  ) 
Returns:
Last event on the track or NULL, if track is empty.

Definition at line 789 of file smf.c.

References smf_track_struct::number_of_events, and smf_track_get_event_by_number().

Referenced by smf_get_length_pulses(), smf_get_length_seconds(), smf_track_add_eot_pulses(), smf_track_add_eot_seconds(), and smf_track_add_event().

smf_event_t* smf_track_get_next_event ( smf_track_t track  ) 

Returns next event from the track given and advances next event counter.

Do not depend on End Of Track event being the last event on the track - it is possible that the track will not end with EOT if you haven't added it yet. EOTs are added automatically during smf_save().

Returns:
Event or NULL, if there are no more events left in this track.

Definition at line 692 of file smf.c.

References smf_track_struct::next_event_number, smf_track_struct::number_of_events, smf_track_get_event_by_number(), smf_track_struct::time_of_next_event, and smf_event_struct::time_pulses.

Referenced by smf_get_next_event().

smf_track_t* smf_track_new ( void   ) 

Allocates new smf_track_t structure.

Returns:
pointer to smf_track_t or NULL.

Definition at line 110 of file smf.c.

References smf_track_struct::events_array, and smf_track_struct::next_event_number.

Referenced by smf_load_from_memory().

void smf_track_remove_from_smf ( smf_track_t track  ) 
Generated on Sun Mar 28 19:42:17 2010 for libsmf by  doxygen 1.6.3