libsmf

- Main Page
- Data Structures
- Files

- File List
- Globals

Functions
smf.c File Reference

Various functions. More...

Go to the source code of this file.

## Functions
smf_t * | smf_new (void)
| Allocates new smf_t structure. More...
void | smf_delete (smf_t *smf)
| Frees smf and all it's descendant structures. More...
smf_track_t * | smf_track_new (void)
| Allocates new smf_track_t structure. More...
void | smf_track_delete (smf_track_t *track)
| Detaches track from its smf and frees it. More...
void | smf_add_track (smf_t *smf, smf_track_t *track)
| Appends smf_track_t to smf. More...
void | smf_track_remove_from_smf (smf_track_t *track)
| Detaches track from the smf. More...
smf_event_t * | smf_event_new (void)
| Allocates new smf_event_t structure. More...
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". More...
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. More...
void | smf_event_delete (smf_event_t *event)
| Detaches event from its track and frees it. More...
void | smf_track_add_event (smf_track_t *track, smf_event_t *event)
| Adds the event to the track and computes ->delta_pulses. More...
int | smf_track_add_eot_delta_pulses (smf_track_t *track, int delta)
| Add End Of Track metaevent. More...
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_event_remove_from_track (smf_event_t *event)
| Detaches event from its track. More...
int | smf_event_is_tempo_change_or_time_signature (const smf_event_t *event)
int | smf_set_format (smf_t *smf, int format)
| Sets "Format" field of MThd header to the specified value. More...
int | smf_set_ppqn (smf_t *smf, int ppqn)
| Sets the PPQN ("Division") field of MThd header. More...
smf_event_t * | smf_track_get_next_event (smf_track_t *track)
| Returns next event from the track given and advances next event counter. More...
smf_track_t * | smf_get_track_by_number (const smf_t *smf, int track_number)
smf_event_t * | smf_track_get_event_by_number (const smf_track_t *track, int event_number)
smf_event_t * | smf_track_get_last_event (const smf_track_t *track)
smf_track_t * | smf_find_track_with_next_event (smf_t *smf)
| Searches for track that contains next event, in time order. More...
smf_event_t * | smf_get_next_event (smf_t *smf)
void | smf_skip_next_event (smf_t *smf)
| Advance the "next event counter". More...
smf_event_t * | smf_peek_next_event (smf_t *smf)
void | smf_rewind (smf_t *smf)
| Rewinds the SMF. More...
int | smf_seek_to_event (smf_t *smf, const smf_event_t *target)
| Seeks the SMF to the given event. More...
int | smf_seek_to_seconds (smf_t *smf, double seconds)
| Seeks the SMF to the given position. More...
int | smf_seek_to_pulses (smf_t *smf, int pulses)
| Seeks the SMF to the given position. More...
int | 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

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, smf_track_struct::smf, smf_set_format(), smf_track_struct::track_number, and smf_struct::tracks_array.

Referenced by smf_load_from_memory().

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.

void smf_event_delete | ( | smf_event_t * | event | ) |

Detaches event from its track and frees it.

Definition at line 363 of file smf.c.

References smf_event_struct::midi_buffer, smf_event_struct::midi_buffer_length, smf_event_remove_from_track(), and smf_event_struct::track.

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

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_tempo_change_or_time_signature | ( | const smf_event_t * | event | ) |

Returns
  Nonzero if event is Tempo Change or Time Signature metaevent.

Definition at line 631 of file smf.c.

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

Referenced by smf_event_remove_from_track(), and smf_track_add_event().

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().

void smf_event_remove_from_track | ( | smf_event_t * | event | ) |

Detaches event from its track.

Definition at line 581 of file smf.c.

References smf_event_struct::delta_time_pulses, smf_event_struct::event_number, smf_track_struct::events_array, smf_track_struct::next_event_number, smf_track_struct::number_of_events, remove_last_tempo_with_pulses(), smf_track_struct::smf, smf_create_tempo_map_and_compute_seconds(), smf_event_is_last(), smf_event_is_tempo_change_or_time_signature(), smf_track_get_event_by_number(), smf_event_struct::time_pulses, and smf_event_struct::track.

Referenced by smf_event_delete().

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

References smf_track_struct::next_event_number, smf_struct::number_of_tracks, smf_get_track_by_number(), and smf_track_struct::time_of_next_event.

Referenced by smf_get_next_event(), and smf_peek_next_event().

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_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_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 | ) |

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

References smf_struct::last_seek_position, smf_track_struct::next_event_number, smf_track_struct::number_of_events, smf_struct::number_of_tracks, smf_get_track_by_number(), and smf_track_struct::time_of_next_event.

Referenced by smf_create_tempo_map_and_compute_seconds(), smf_load(), smf_save(), smf_seek_to_event(), smf_seek_to_pulses(), and smf_seek_to_seconds().

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
| ) |  |

Definition at line 538 of file smf.c.

References smf_event_new_from_bytes(), smf_track_add_event_pulses(), smf_track_get_last_event(), and smf_event_struct::time_pulses.

int smf_track_add_eot_seconds | ( | smf_track_t * | track,
|  | double | seconds
| ) |  |

Definition at line 558 of file smf.c.

References smf_event_new_from_bytes(), smf_track_add_event_seconds(), smf_track_get_last_event(), and smf_event_struct::time_seconds.

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

References smf_event_struct::delta_time_pulses, smf_event_struct::event_number, smf_track_struct::events_array, maybe_add_to_tempo_map(), smf_track_struct::next_event_number, smf_track_struct::number_of_events, smf_track_struct::smf, smf_create_tempo_map_and_compute_seconds(), smf_event_is_last(), smf_event_is_tempo_change_or_time_signature(), smf_track_get_event_by_number(), smf_track_get_last_event(), smf_event_struct::time_pulses, smf_event_struct::time_seconds, smf_event_struct::track, and smf_track_struct::track_number.

Referenced by smf_track_add_event_pulses(), and smf_track_add_event_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 | ) |

Detaches track from the smf.

Definition at line 178 of file smf.c.

References smf_track_struct::number_of_events, smf_struct::number_of_tracks, smf, smf_track_struct::smf, smf_get_track_by_number(), smf_track_get_event_by_number(), smf_track_struct::track_number, smf_event_struct::track_number, and smf_struct::tracks_array.

Referenced by smf_track_delete().