Skip to content

Commit 1701552

Browse files
committed
synthio: make sustain level relative to attack level
and re-vamp overall envelope calculation again. Now, if you set a low overall attack level like 0.2 this avoids the "diminishing volume" effect when many notes sound at once. You need simply choose a maximum attack level that is appropriate for the max number of voices that will actually be played.
1 parent c06597c commit 1701552

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

shared-bindings/synthio/__init__.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ static const mp_arg_t envelope_properties[] = {
7979
//| :param float attack_time: The time in seconds it takes to ramp from 0 volume to attack_volume
8080
//| :param float decay_time: The time in seconds it takes to ramp from attack_volume to sustain_volume
8181
//| :param float release_time: The time in seconds it takes to ramp from sustain_volume to release_volume. When a note is released before it has reached the sustain phase, the release is done with the same slope indicated by ``release_time`` and ``sustain_level``
82-
//| :param float attack_level: The relative level, in the range ``0.0`` to ``1.0`` of the peak volume of the attack phase
83-
//| :param float sustain_level: The relative level, in the range ``0.0`` to ``1.0`` of the volume of the sustain phase
82+
//| :param float attack_level: The level, in the range ``0.0`` to ``1.0`` of the peak volume of the attack phase
83+
//| :param float sustain_level: The level, in the range ``0.0`` to ``1.0`` of the volume of the sustain phase relative to the attack level
8484
//| """
8585
//| attack_time: float
8686
//| """The time in seconds it takes to ramp from 0 volume to attack_volume"""
@@ -92,10 +92,10 @@ static const mp_arg_t envelope_properties[] = {
9292
//| """The time in seconds it takes to ramp from sustain_volume to release_volume. When a note is released before it has reached the sustain phase, the release is done with the same slope indicated by ``release_time`` and ``sustain_level``"""
9393
//|
9494
//| attack_level: float
95-
//| """The relative level, in the range ``0.0`` to ``1.0`` of the peak volume of the attack phase"""
95+
//| """The level, in the range ``0.0`` to ``1.0`` of the peak volume of the attack phase"""
9696
//|
9797
//| sustain_level: float
98-
//| """The relative level, in the range ``0.0`` to ``1.0`` of the volume of the sustain phase"""
98+
//| """The level, in the range ``0.0`` to ``1.0`` of the volume of the sustain phase relative to the attack level"""
9999
//|
100100

101101
STATIC mp_obj_t synthio_envelope_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {

shared-module/synthio/__init__.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void synthio_envelope_definition_set(synthio_envelope_definition_t *envelope, mp
7777
mp_obj_tuple_get(obj, &len, &fields);
7878

7979
envelope->attack_level = (int)(32767 * mp_obj_get_float(fields[3]));
80-
envelope->sustain_level = (int)(32767 * mp_obj_get_float(fields[4]));
80+
envelope->sustain_level = (int)(32767 * mp_obj_get_float(fields[4]) * mp_obj_get_float(fields[3]));
8181

8282
envelope->attack_step = convert_time_to_rate(
8383
sample_rate, fields[0], envelope->attack_level);
@@ -152,10 +152,10 @@ STATIC uint32_t synthio_synth_sum_envelope(synthio_synth_t *synth) {
152152
for (int chan = 0; chan < CIRCUITPY_SYNTHIO_MAX_CHANNELS; chan++) {
153153
if (synth->span.note_obj[chan] != SYNTHIO_SILENCE) {
154154
synthio_envelope_state_t *state = &synth->envelope_state[chan];
155-
if (state->state == SYNTHIO_ENVELOPE_STATE_RELEASE) {
156-
result += synth->envelope_definition.sustain_level;
157-
} else {
155+
if (state->state == SYNTHIO_ENVELOPE_STATE_ATTACK) {
158156
result += state->level;
157+
} else {
158+
result += synth->envelope_definition.attack_level;
159159
}
160160
}
161161
}

0 commit comments

Comments
 (0)