Skip to content

Commit

Permalink
Add timing functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Artefact2 committed Oct 24, 2014
1 parent b95ae54 commit c9b5bba
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ libxm

A small XM (FastTracker II Extended Module) player library. Designed
for easy integration in demos and such, and provides timing functions
for easy sync (well, not yet, but it's planned).
for easy sync against specific instruments, samples or channels.

Written in C11 and released under the WTFPL license, version 2.

Expand Down
57 changes: 49 additions & 8 deletions include/xm.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,30 @@ const char* xm_get_tracker_name(xm_context_t*);



/** Get the number of channels. */
uint16_t xm_get_number_of_channels(xm_context_t*);

/** Get the module length (in patterns). */
uint16_t xm_get_module_length(xm_context_t*);

/** Get the number of patterns. */
uint16_t xm_get_number_of_patterns(xm_context_t*);

/** Get the number of rows of a pattern. */
/** Get the number of rows of a pattern.
*
* @note Pattern numbers go from 0 to
* xm_get_number_of_patterns(...)-1.
*/
uint16_t xm_get_number_of_rows(xm_context_t*, uint16_t);

/** Get the number of instruments. */
uint16_t xm_get_number_of_instruments(xm_context_t*);

/** Get the number of samples of an instrument. */
/** Get the number of samples of an instrument.
*
* @note Instrument numbers go from 1 to
* xm_get_number_of_instruments(...).
*/
uint16_t xm_get_number_of_samples(xm_context_t*, uint16_t);


Expand All @@ -89,13 +100,43 @@ void xm_get_playing_speed(xm_context_t*, uint16_t* bpm, uint16_t* tempo);

/** Get the current position in the module being played.
*
* @param pattern_index will receive the current pattern index in the
* POT (pattern order table)
* @param pattern will receive the current pattern number
* @param row will receive the current row
* @param samples will receive the total number of generated samples
* (divide by sample rate to get seconds of generated audio)
* @param pattern_index if not NULL, will receive the current pattern
* index in the POT (pattern order table)
*
* @param pattern if not NULL, will receive the current pattern number
*
* @param row if not NULL, will receive the current row
*
* @param samples if not NULL, will receive the total number of
* generated samples (divide by sample rate to get seconds of
* generated audio)
*/
void xm_get_position(xm_context_t*, uint8_t* pattern_index, uint8_t* pattern, uint8_t* row, uint64_t* samples);

/** Get the latest time (in number of generated samples) when a
* particular instrument was triggered in any channel.
*
* @note Instrument numbers go from 1 to
* xm_get_number_of_instruments(...).
*/
uint64_t xm_get_latest_trigger_of_instrument(xm_context_t*, uint16_t);

/** Get the latest time (in number of generated samples) when a
* particular sample was triggered in any channel.
*
* @note Instrument numbers go from 1 to
* xm_get_number_of_instruments(...).
*
* @note Sample numbers go from 0 to
* xm_get_nubmer_of_samples(...,instr)-1.
*/
uint64_t xm_get_latest_trigger_of_sample(xm_context_t*, uint16_t instr, uint16_t sample);

/** Get the latest time (in number of generated samples) when any
* instrument was triggered in a given channel.
*
* @note Channel numbers go from 1 to xm_get_number_of_channels(...).
*/
uint64_t xm_get_latest_trigger_of_channel(xm_context_t*, uint16_t);

#endif
22 changes: 21 additions & 1 deletion src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ const char* xm_get_tracker_name(xm_context_t* ctx) {
return ctx->module.trackername;
}



uint16_t xm_get_number_of_channels(xm_context_t* ctx) {
return ctx->module.num_channels;
}

uint16_t xm_get_module_length(xm_context_t* ctx) {
return ctx->module.length;
}
Expand All @@ -103,9 +109,11 @@ uint16_t xm_get_number_of_instruments(xm_context_t* ctx) {
}

uint16_t xm_get_number_of_samples(xm_context_t* ctx, uint16_t instrument) {
return ctx->module.instruments[instrument].num_samples;
return ctx->module.instruments[instrument - 1].num_samples;
}



void xm_get_playing_speed(xm_context_t* ctx, uint16_t* bpm, uint16_t* tempo) {
if(bpm) *bpm = ctx->bpm;
if(tempo) *tempo = ctx->tempo;
Expand All @@ -117,3 +125,15 @@ void xm_get_position(xm_context_t* ctx, uint8_t* pattern_index, uint8_t* pattern
if(row) *row = ctx->current_row;
if(samples) *samples = ctx->generated_samples;
}

uint64_t xm_get_latest_trigger_of_instrument(xm_context_t* ctx, uint16_t instr) {
return ctx->module.instruments[instr - 1].latest_trigger;
}

uint64_t xm_get_latest_trigger_of_sample(xm_context_t* ctx, uint16_t instr, uint16_t sample) {
return ctx->module.instruments[instr - 1].samples[sample].latest_trigger;
}

uint64_t xm_get_latest_trigger_of_channel(xm_context_t* ctx, uint16_t chn) {
return ctx->channels[chn - 1].latest_trigger;
}
8 changes: 8 additions & 0 deletions src/play.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,14 @@ static void xm_trigger_note(xm_context_t* ctx, xm_channel_context_t* ch, unsigne
ch->period = xm_period(ctx, ch->note);
xm_update_frequency(ctx, ch);
}

ch->latest_trigger = ctx->generated_samples;
if(ch->instrument != NULL) {
ch->instrument->latest_trigger = ctx->generated_samples;
}
if(ch->sample != NULL) {
ch->sample->latest_trigger = ctx->generated_samples;
}
}

static void xm_cut_note(xm_channel_context_t* ch) {
Expand Down
3 changes: 3 additions & 0 deletions src/xm_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ struct xm_sample_s {
xm_loop_type_t loop_type;
float panning;
int8_t relative_note;
uint64_t latest_trigger;

float* data;
};
Expand All @@ -111,6 +112,7 @@ struct xm_sample_s {
uint8_t vibrato_depth;
uint8_t vibrato_rate;
uint16_t volume_fadeout;
uint64_t latest_trigger;

xm_sample_t* samples;
};
Expand Down Expand Up @@ -208,6 +210,7 @@ struct xm_sample_s {

unsigned long frame_count;
float end_of_previous_sample[XM_SAMPLE_RAMPING_POINTS];
uint64_t latest_trigger;

float actual_panning;
float target_panning;
Expand Down

0 comments on commit c9b5bba

Please sign in to comment.