Skip to content

Commit

Permalink
only refresh instrument parameters on instrument switch
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
grz0zrg committed Mar 26, 2021
1 parent 97775a6 commit 64227fb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 28 deletions.
72 changes: 44 additions & 28 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,45 @@ void doSynthCommands() {
} else if (target == 2) {
instrument_settings->output_channel = value;
} else if (target == 3) {
instrument_settings->p0 = value;
if (instrument_settings->next_type != instrument_settings->type) {
// note : parameters may be refreshed during instrument switch, so should be only updated when the type really switch later on
instrument_settings->next_p0 = value;
} else {
instrument_settings->p0 = value;
instrument_settings->next_p0 = value;
}
} else if (target == 4) {
instrument_settings->p1 = value;
if (instrument_settings->next_type != instrument_settings->type) {
// note : parameters may be refreshed during instrument switch, so should be only updated when the type really switch later on
instrument_settings->next_p1 = value;
} else {
instrument_settings->p1 = value;
instrument_settings->next_p1;
}
} else if (target == 5) {
instrument_settings->p2 = value;
if (instrument_settings->next_type != instrument_settings->type) {
// note : parameters may be refreshed during instrument switch, so should be only updated when the type really switch later on
instrument_settings->next_p2 = value;
} else {
instrument_settings->p2 = value;
instrument_settings->next_p2;
}
} else if (target == 6) {
instrument_settings->p3 = value;
if (instrument_settings->next_type != instrument_settings->type) {
// note : parameters may be refreshed during instrument switch, so should be only updated when the type really switch later on
instrument_settings->next_p3 = value;
} else {
instrument_settings->p3 = value;
instrument_settings->next_p3;
}
} else if (target == 7) {
instrument_settings->p4 = value;
if (instrument_settings->next_type != instrument_settings->type) {
// note : parameters may be refreshed during instrument switch, so should be only updated when the type really switch later on
instrument_settings->next_p4 = value;
} else {
instrument_settings->p4 = value;
instrument_settings->next_p4;
}
}
} else {
#ifdef DEBUG
Expand Down Expand Up @@ -1652,39 +1682,25 @@ static int audioCallback(float **inputBuffer, float **outputBuffer, unsigned lon
struct _synth_instrument *instrument = &curr_synth.instruments[k];
struct _synth_instrument_states *instrument_states = &fas_instrument_states[k];

// instrument switch
// smooth processing of instrument type
if (instrument_states->state == 1) {
// old instrument faded off, switch to the new one
instrument->type = instrument->next_type;
instrument->p0 = instrument->next_p0;
instrument->p1 = instrument->next_p1;
instrument->p2 = instrument->next_p2;
instrument->p3 = instrument->next_p3;
instrument->p4 = instrument->next_p4;

instrument_states->state = 0;

// override current notes data to smoothly switch on and force an instrument note on trigger (to initialize the newly assigned instrument)
for (j = s; j < e; j += 1) {
struct note *n = &curr_notes[j];

// override current notes data
n->previous_volume_l = 0;
n->previous_volume_r = 0;

n->diff_volume_l = -n->previous_volume_l;
n->diff_volume_r = -n->previous_volume_r;
}
notesOn(curr_notes, s, e);
}

if (instrument->type != instrument->next_type) {
// let the actual instrument fade off (smooth transition)
for (j = s; j < e; j += 1) {
struct note *n = &curr_notes[j];

// override current notes data
n->volume_l = 0;
n->volume_r = 0;

n->diff_volume_l = -n->previous_volume_l;
n->diff_volume_r = -n->previous_volume_r;
}

// let the actual instrument fade off (smooth transition) by inserting note off values
notesOff(curr_notes, s, e);
// indicate a transition state
instrument_states->state = 1;
}
Expand Down
28 changes: 28 additions & 0 deletions src/note.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,31 @@ void fillNotesBuffer(unsigned int samples_count, unsigned int waves_count, unsig
#endif
}
}

void notesOn(struct note *cn, unsigned int start, unsigned int end) {
unsigned int j = start;
for (j = start; j < end; j += 1) {
struct note *n = &cn[j];

// override notes data
n->previous_volume_l = 0;
n->previous_volume_r = 0;

n->diff_volume_l = -n->previous_volume_l;
n->diff_volume_r = -n->previous_volume_r;
}
}

void notesOff(struct note *cn, unsigned int start, unsigned int end) {
unsigned int j = start;
for (j = start; j < end; j += 1) {
struct note *n = &cn[j];

// override notes data
n->volume_l = 0;
n->volume_r = 0;

n->diff_volume_l = -n->previous_volume_l;
n->diff_volume_r = -n->previous_volume_r;
}
}
5 changes: 5 additions & 0 deletions src/note.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@
unsigned int instruments, unsigned int data_frame_size, struct note *note_buffer,
unsigned int h, size_t data_length, void *prev_data, void *data);

// override a note buffer with note on
extern void notesOn(struct note *cn, unsigned int start, unsigned int end);
// override a note buffer with note off
extern void notesOff(struct note *cn, unsigned int start, unsigned int end);

#endif
6 changes: 6 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@
double p2;
double p3;
double p4;

int next_p0;
double next_p1;
double next_p2;
double next_p3;
double next_p4;

FAS_FLOAT last_sample_l;
FAS_FLOAT last_sample_r;
Expand Down

0 comments on commit 64227fb

Please sign in to comment.