ardour
Functions
smf.c File Reference
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <errno.h>
#include <arpa/inet.h>
#include "smf.h"
#include "smf_private.h"

Go to the source code of this file.

Functions

smf_tsmf_new (void)
 
void smf_delete (smf_t *smf)
 
smf_track_tsmf_track_new (void)
 
void smf_track_delete (smf_track_t *track)
 
void smf_add_track (smf_t *smf, smf_track_t *track)
 
void smf_track_remove_from_smf (smf_track_t *track)
 
smf_event_tsmf_event_new (void)
 
smf_event_tsmf_event_new_from_pointer (const void *midi_data, size_t len)
 
smf_event_tsmf_event_new_from_bytes (int first_byte, int second_byte, int third_byte)
 
void smf_event_delete (smf_event_t *event)
 
static gint events_array_compare_function (gconstpointer aa, gconstpointer bb)
 
static void remove_eot_if_before_pulses (smf_track_t *track, size_t pulses)
 
void smf_track_add_event (smf_track_t *track, smf_event_t *event)
 
int smf_track_add_eot_delta_pulses (smf_track_t *track, uint32_t delta)
 
int smf_track_add_eot_pulses (smf_track_t *track, size_t pulses)
 
int smf_track_add_eot_seconds (smf_track_t *track, double seconds)
 
void smf_event_remove_from_track (smf_event_t *event)
 
int smf_event_is_tempo_change_or_time_signature (const smf_event_t *event)
 
int smf_set_format (smf_t *smf, int format)
 
int smf_set_ppqn (smf_t *smf, uint16_t ppqn)
 
smf_event_tsmf_track_get_next_event (smf_track_t *track)
 
static smf_event_tsmf_peek_next_event_from_track (smf_track_t *track)
 
smf_track_tsmf_get_track_by_number (const smf_t *smf, int track_number)
 
smf_event_tsmf_track_get_event_by_number (const smf_track_t *track, size_t event_number)
 
smf_event_tsmf_track_get_last_event (const smf_track_t *track)
 
smf_track_tsmf_find_track_with_next_event (smf_t *smf)
 
smf_event_tsmf_get_next_event (smf_t *smf)
 
void smf_skip_next_event (smf_t *smf)
 
smf_event_tsmf_peek_next_event (smf_t *smf)
 
void smf_rewind (smf_t *smf)
 
int smf_seek_to_event (smf_t *smf, const smf_event_t *target)
 
int smf_seek_to_seconds (smf_t *smf, double seconds)
 
int smf_seek_to_pulses (smf_t *smf, size_t pulses)
 
size_t smf_get_length_pulses (const smf_t *smf)
 
double smf_get_length_seconds (const smf_t *smf)
 
int smf_event_is_last (const smf_event_t *event)
 
const char * smf_get_version (void)
 

Detailed Description

Various functions.

Definition in file smf.c.

Function Documentation

static gint events_array_compare_function ( gconstpointer  aa,
gconstpointer  bb 
)
static

Used for sorting track->events_array.

Definition at line 389 of file smf.c.

static void remove_eot_if_before_pulses ( smf_track_t track,
size_t  pulses 
)
static

Definition at line 423 of file smf.c.

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.

void smf_delete ( smf_t smf)

Frees smf and all it's descendant structures.

Definition at line 88 of file smf.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.

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_tempo_change_or_time_signature ( const smf_event_t event)
Returns
Nonzero if event is Tempo Change or Time Signature metaevent.

Definition at line 639 of file smf.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.

void smf_event_remove_from_track ( smf_event_t event)

Detaches event from its track.

Definition at line 588 of file smf.c.

smf_track_t* smf_find_track_with_next_event ( smf_t smf)

Searches for track that contains next event, in time order. In other words, returns the track that contains event that should be played next.

Returns
Track with next event or NULL, if there are no events left.

Definition at line 816 of file smf.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_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_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.

static smf_event_t* smf_peek_next_event_from_track ( smf_track_t track)
static

Returns next event from the track given. Does not change next event counter, so repeatedly calling this routine will return the same event.

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

Definition at line 736 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_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 ( smf_track_t track,
smf_event_t event 
)

Adds the event to the track and computes ->delta_pulses. Note that it is faster to append events to the end of the track than to insert them in the middle. Usually you want to use smf_track_add_event_seconds or smf_track_add_event_pulses instead of this one. Event needs to have ->time_pulses and ->time_seconds already set. If you try to add event after an EOT, EOT event will be automatically deleted.

Definition at line 449 of file smf.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.