Skip to content

Commit 64227fb

Browse files
committed
only refresh instrument parameters on instrument switch
refactor
1 parent 97775a6 commit 64227fb

File tree

4 files changed

+83
-28
lines changed

4 files changed

+83
-28
lines changed

src/main.c

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,45 @@ void doSynthCommands() {
133133
} else if (target == 2) {
134134
instrument_settings->output_channel = value;
135135
} else if (target == 3) {
136-
instrument_settings->p0 = value;
136+
if (instrument_settings->next_type != instrument_settings->type) {
137+
// note : parameters may be refreshed during instrument switch, so should be only updated when the type really switch later on
138+
instrument_settings->next_p0 = value;
139+
} else {
140+
instrument_settings->p0 = value;
141+
instrument_settings->next_p0 = value;
142+
}
137143
} else if (target == 4) {
138-
instrument_settings->p1 = value;
144+
if (instrument_settings->next_type != instrument_settings->type) {
145+
// note : parameters may be refreshed during instrument switch, so should be only updated when the type really switch later on
146+
instrument_settings->next_p1 = value;
147+
} else {
148+
instrument_settings->p1 = value;
149+
instrument_settings->next_p1;
150+
}
139151
} else if (target == 5) {
140-
instrument_settings->p2 = value;
152+
if (instrument_settings->next_type != instrument_settings->type) {
153+
// note : parameters may be refreshed during instrument switch, so should be only updated when the type really switch later on
154+
instrument_settings->next_p2 = value;
155+
} else {
156+
instrument_settings->p2 = value;
157+
instrument_settings->next_p2;
158+
}
141159
} else if (target == 6) {
142-
instrument_settings->p3 = value;
160+
if (instrument_settings->next_type != instrument_settings->type) {
161+
// note : parameters may be refreshed during instrument switch, so should be only updated when the type really switch later on
162+
instrument_settings->next_p3 = value;
163+
} else {
164+
instrument_settings->p3 = value;
165+
instrument_settings->next_p3;
166+
}
143167
} else if (target == 7) {
144-
instrument_settings->p4 = value;
168+
if (instrument_settings->next_type != instrument_settings->type) {
169+
// note : parameters may be refreshed during instrument switch, so should be only updated when the type really switch later on
170+
instrument_settings->next_p4 = value;
171+
} else {
172+
instrument_settings->p4 = value;
173+
instrument_settings->next_p4;
174+
}
145175
}
146176
} else {
147177
#ifdef DEBUG
@@ -1652,39 +1682,25 @@ static int audioCallback(float **inputBuffer, float **outputBuffer, unsigned lon
16521682
struct _synth_instrument *instrument = &curr_synth.instruments[k];
16531683
struct _synth_instrument_states *instrument_states = &fas_instrument_states[k];
16541684

1655-
// instrument switch
1685+
// smooth processing of instrument type
16561686
if (instrument_states->state == 1) {
16571687
// old instrument faded off, switch to the new one
16581688
instrument->type = instrument->next_type;
1689+
instrument->p0 = instrument->next_p0;
1690+
instrument->p1 = instrument->next_p1;
1691+
instrument->p2 = instrument->next_p2;
1692+
instrument->p3 = instrument->next_p3;
1693+
instrument->p4 = instrument->next_p4;
16591694

16601695
instrument_states->state = 0;
16611696

16621697
// override current notes data to smoothly switch on and force an instrument note on trigger (to initialize the newly assigned instrument)
1663-
for (j = s; j < e; j += 1) {
1664-
struct note *n = &curr_notes[j];
1665-
1666-
// override current notes data
1667-
n->previous_volume_l = 0;
1668-
n->previous_volume_r = 0;
1669-
1670-
n->diff_volume_l = -n->previous_volume_l;
1671-
n->diff_volume_r = -n->previous_volume_r;
1672-
}
1698+
notesOn(curr_notes, s, e);
16731699
}
16741700

16751701
if (instrument->type != instrument->next_type) {
1676-
// let the actual instrument fade off (smooth transition)
1677-
for (j = s; j < e; j += 1) {
1678-
struct note *n = &curr_notes[j];
1679-
1680-
// override current notes data
1681-
n->volume_l = 0;
1682-
n->volume_r = 0;
1683-
1684-
n->diff_volume_l = -n->previous_volume_l;
1685-
n->diff_volume_r = -n->previous_volume_r;
1686-
}
1687-
1702+
// let the actual instrument fade off (smooth transition) by inserting note off values
1703+
notesOff(curr_notes, s, e);
16881704
// indicate a transition state
16891705
instrument_states->state = 1;
16901706
}

src/note.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,31 @@ void fillNotesBuffer(unsigned int samples_count, unsigned int waves_count, unsig
183183
#endif
184184
}
185185
}
186+
187+
void notesOn(struct note *cn, unsigned int start, unsigned int end) {
188+
unsigned int j = start;
189+
for (j = start; j < end; j += 1) {
190+
struct note *n = &cn[j];
191+
192+
// override notes data
193+
n->previous_volume_l = 0;
194+
n->previous_volume_r = 0;
195+
196+
n->diff_volume_l = -n->previous_volume_l;
197+
n->diff_volume_r = -n->previous_volume_r;
198+
}
199+
}
200+
201+
void notesOff(struct note *cn, unsigned int start, unsigned int end) {
202+
unsigned int j = start;
203+
for (j = start; j < end; j += 1) {
204+
struct note *n = &cn[j];
205+
206+
// override notes data
207+
n->volume_l = 0;
208+
n->volume_r = 0;
209+
210+
n->diff_volume_l = -n->previous_volume_l;
211+
n->diff_volume_r = -n->previous_volume_r;
212+
}
213+
}

src/note.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@
4343
unsigned int instruments, unsigned int data_frame_size, struct note *note_buffer,
4444
unsigned int h, size_t data_length, void *prev_data, void *data);
4545

46+
// override a note buffer with note on
47+
extern void notesOn(struct note *cn, unsigned int start, unsigned int end);
48+
// override a note buffer with note off
49+
extern void notesOff(struct note *cn, unsigned int start, unsigned int end);
50+
4651
#endif

src/types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@
8282
double p2;
8383
double p3;
8484
double p4;
85+
86+
int next_p0;
87+
double next_p1;
88+
double next_p2;
89+
double next_p3;
90+
double next_p4;
8591

8692
FAS_FLOAT last_sample_l;
8793
FAS_FLOAT last_sample_r;

0 commit comments

Comments
 (0)