ardour
Classes | Macros | Typedefs | Functions
smf.h File Reference
#include <stdio.h>
#include <stdint.h>
#include <glib.h>

Go to the source code of this file.

Classes

struct  smf_struct
 
struct  smf_tempo_struct
 
struct  smf_track_struct
 
struct  smf_event_struct
 

Macros

#define WARN_UNUSED_RESULT
 

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
 
void smf_delete (smf_t *smf)
 
int smf_set_format (smf_t *smf, int format) WARN_UNUSED_RESULT
 
int smf_set_ppqn (smf_t *smf, uint16_t ppqn) WARN_UNUSED_RESULT
 
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)
 
void smf_rewind (smf_t *smf)
 
int smf_seek_to_seconds (smf_t *smf, double seconds) WARN_UNUSED_RESULT
 
int smf_seek_to_pulses (smf_t *smf, size_t pulses) WARN_UNUSED_RESULT
 
int smf_seek_to_event (smf_t *smf, const smf_event_t *event) WARN_UNUSED_RESULT
 
size_t 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)
 
void smf_track_remove_from_smf (smf_track_t *track)
 
smf_track_tsmf_track_new (void) WARN_UNUSED_RESULT
 
void smf_track_delete (smf_track_t *track)
 
smf_event_tsmf_track_get_next_event (smf_track_t *track) WARN_UNUSED_RESULT
 
smf_event_tsmf_track_get_event_by_number (const smf_track_t *track, size_t num) 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, uint32_t delta)
 
void smf_track_add_event_pulses (smf_track_t *track, smf_event_t *event, size_t pulses)
 
void smf_track_add_event_seconds (smf_track_t *track, smf_event_t *event, double seconds)
 
int smf_track_add_eot_delta_pulses (smf_track_t *track, uint32_t delta) WARN_UNUSED_RESULT
 
int smf_track_add_eot_pulses (smf_track_t *track, size_t 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)
 
smf_event_tsmf_event_new (void) WARN_UNUSED_RESULT
 
smf_event_tsmf_event_new_from_pointer (const void *midi_data, size_t len) WARN_UNUSED_RESULT
 
smf_event_tsmf_event_new_from_bytes (int byte1, int byte2, int byte3) WARN_UNUSED_RESULT
 
smf_event_tsmf_event_new_textual (int type, const char *text)
 
void smf_event_delete (smf_event_t *event)
 
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
 
int smf_format_vlq (unsigned char *buf, int length, unsigned long value)
 
int smf_extract_vlq (const unsigned char *buf, const size_t buffer_length, uint32_t *value, uint32_t *len)
 
smf_tsmf_load (FILE *) WARN_UNUSED_RESULT
 
smf_tsmf_load_from_memory (const void *buffer, const size_t buffer_length) WARN_UNUSED_RESULT
 
int smf_save (smf_t *smf, FILE *file) WARN_UNUSED_RESULT
 
smf_tempo_tsmf_get_tempo_by_pulses (const smf_t *smf, size_t pulses) WARN_UNUSED_RESULT
 
smf_tempo_tsmf_get_tempo_by_seconds (const smf_t *smf, double seconds) WARN_UNUSED_RESULT
 
smf_tempo_tsmf_get_tempo_by_number (const smf_t *smf, size_t number) WARN_UNUSED_RESULT
 
smf_tempo_tsmf_get_last_tempo (const smf_t *smf) WARN_UNUSED_RESULT
 
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.

Macro Definition Documentation

#define WARN_UNUSED_RESULT

Definition at line 230 of file smf.h.

Typedef Documentation

typedef struct smf_event_struct smf_event_t

Definition at line 327 of file smf.h.

typedef struct smf_struct smf_t

Definition at line 260 of file smf.h.

typedef struct smf_tempo_struct smf_tempo_t

Definition at line 273 of file smf.h.

typedef struct smf_track_struct smf_track_t

Definition at line 297 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.

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 605 of file smf_decode.c.

void smf_delete ( smf_t smf)

Frees smf and all it's descendant structures.

Definition at line 88 of file smf.c.

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 519 of file smf_decode.c.

void smf_event_delete ( smf_event_t event)

Detaches event from its track and frees it.

Definition at line 371 of file smf.c.

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 668 of file smf_load.c.

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

Definition at line 467 of file smf_save.c.

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 1108 of file smf.c.

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.

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.

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.

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.

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 648 of file smf_load.c.

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 770 of file smf_load.c.

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 225 of file smf.c.

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_byteFirst byte of MIDI message. Must be valid status byte.
second_byteSecond byte of MIDI message or -1, if message is one byte long.
third_byteThird byte of MIDI message or -1, if message is two bytes long.
Returns
Event containing MIDI data or NULL.

Definition at line 290 of file smf.c.

smf_event_t* smf_event_new_from_pointer ( const void *  midi_data,
size_t  len 
)

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

Parameters
midi_dataPointer to MIDI data. It sill be copied to the newly allocated event->midi_buffer.
lenLength 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 251 of file smf.c.

smf_event_t* smf_event_new_textual ( int  type,
const char *  text 
)

Definition at line 193 of file smf_save.c.

void smf_event_remove_from_track ( smf_event_t event)

Detaches event from its track.

Definition at line 588 of file smf.c.

int smf_extract_vlq ( const unsigned char *  buf,
const size_t  buffer_length,
uint32_t *  value,
uint32_t *  len 
)

Interprets Variable Length Quantity pointed at by "buf" and puts its value into "value" and number of bytes consumed into "len", making sure it does not read past "buf" + "buffer_length". Explanation of Variable Length Quantities is here: http://www.borg.com/~jglatt/tech/midifile/vari.htm Returns 0 iff everything went OK, different value in case of error.

Definition at line 213 of file smf_load.c.

int smf_format_vlq ( unsigned char *  buf,
int  length,
unsigned long  value 
)

Definition at line 164 of file smf_save.c.

smf_tempo_t* smf_get_last_tempo ( const smf_t smf)

Return last tempo.

Definition at line 322 of file smf_tempo.c.

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

Definition at line 1051 of file smf.c.

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

Definition at line 1079 of file smf.c.

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 845 of file smf.c.

smf_tempo_t* smf_get_tempo_by_number ( const smf_t smf,
size_t  number 
)

Definition at line 257 of file smf_tempo.c.

smf_tempo_t* smf_get_tempo_by_pulses ( const smf_t smf,
size_t  pulses 
)

Return last tempo (i.e. tempo with greatest time_pulses) that happens before "pulses".

Definition at line 269 of file smf_tempo.c.

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 294 of file smf_tempo.c.

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 757 of file smf.c.

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

Definition at line 1120 of file smf.c.

smf_t* smf_load ( FILE *  file)

Loads SMF file.

Parameters
fileOpen file.
Returns
SMF or NULL, if loading failed.

Definition at line 930 of file smf_load.c.

smf_t* smf_load_from_memory ( const void *  buffer,
const size_t  buffer_length 
)

Creates new SMF and fills it with data loaded from the given buffer.

Returns
SMF or NULL, if loading failed.

Definition at line 876 of file smf_load.c.

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.

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 883 of file smf.c.

void smf_rewind ( smf_t smf)

Rewinds the SMF. What that means is, after calling this routine, smf_get_next_event will return first event in the song.

Definition at line 908 of file smf.c.

int smf_save ( smf_t smf,
FILE *  file 
)

Writes the contents of SMF to the file given.

Parameters
smfSMF.
fileFile descriptor.
Returns
0, if saving was successfull.

Definition at line 623 of file smf_save.c.

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 943 of file smf.c.

int smf_seek_to_pulses ( smf_t smf,
size_t  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 1018 of file smf.c.

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 975 of file smf.c.

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
smfSMF.
format0 for one track per file, 1 for several tracks per file.

Definition at line 660 of file smf.c.

int smf_set_ppqn ( smf_t smf,
uint16_t  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
smfSMF.
ppqnNew PPQN.

Definition at line 682 of file smf.c.

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 872 of file smf.c.

int smf_track_add_eot_delta_pulses ( smf_track_t track,
uint32_t  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 531 of file smf.c.

int smf_track_add_eot_pulses ( smf_track_t track,
size_t  pulses 
)

Definition at line 545 of file smf.c.

int smf_track_add_eot_seconds ( smf_track_t track,
double  seconds 
)

Definition at line 565 of file smf.c.

void smf_track_add_event_delta_pulses ( smf_track_t track,
smf_event_t event,
uint32_t  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 400 of file smf_tempo.c.

void smf_track_add_event_pulses ( smf_track_t track,
smf_event_t event,
size_t  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 418 of file smf_tempo.c.

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 434 of file smf_tempo.c.

void smf_track_delete ( smf_track_t track)

Detaches track from its smf and frees it.

Definition at line 131 of file smf.c.

smf_event_t* smf_track_get_event_by_number ( const smf_track_t track,
size_t  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 778 of file smf.c.

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 798 of file smf.c.

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 698 of file smf.c.

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.

void smf_track_remove_from_smf ( smf_track_t track)

Detaches track from the smf.

Definition at line 185 of file smf.c.