Skip to content

Commit d4294de

Browse files
committed
synthio: Allow bends of more than one octave
up to +-12 sounds good, right?
1 parent 0b926f8 commit d4294de

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

shared-bindings/synthio/Note.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ MP_PROPERTY_GETSET(synthio_note_amplitude_obj,
161161

162162
//|
163163
//| bend: BlockInput
164-
//| """The pitch bend depth of the note, from -1 to +1
164+
//| """The pitch bend depth of the note, from -12 to +12
165165
//|
166166
//| A depth of 0 plays the programmed frequency. A depth of 1 corresponds to a bend of 1
167167
//| octave. A depth of (1/12) = 0.833 corresponds to a bend of 1 semitone,
@@ -244,7 +244,7 @@ MP_PROPERTY_GETSET(synthio_note_ring_frequency_obj,
244244
(mp_obj_t)&synthio_note_set_ring_frequency_obj);
245245

246246
//| ring_bend: float
247-
//| """The pitch bend depth of the note's ring waveform, from -1 to +1
247+
//| """The pitch bend depth of the note's ring waveform, from -12 to +12
248248
//|
249249
//| A depth of 0 plays the programmed frequency. A depth of 1 corresponds to a bend of 1
250250
//| octave. A depth of (1/12) = 0.833 corresponds to a bend of 1 semitone,

shared-module/synthio/Note.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,16 @@ void synthio_note_start(synthio_note_obj_t *self, int32_t sample_rate) {
160160

161161
STATIC uint16_t pitch_bend_table[] = { 0, 1948, 4013, 6200, 8517, 10972, 13573, 16329, 19248, 22341, 25618, 29090, 32768 };
162162

163-
STATIC uint32_t pitch_bend(uint32_t frequency_scaled, int16_t bend_value) {
164-
bool down = (bend_value < 0);
165-
if (down) {
166-
bend_value += 32768;
167-
}
163+
STATIC uint32_t pitch_bend(uint32_t frequency_scaled, int32_t bend_value) {
164+
int octave = bend_value >> 15;
165+
bend_value &= 0x7fff;
168166
uint32_t bend_value_semitone = (uint32_t)bend_value * 24; // 65536/semitone
169167
uint32_t semitone = bend_value_semitone >> 16;
170168
uint32_t fractone = bend_value_semitone & 0xffff;
171169
uint32_t f_lo = pitch_bend_table[semitone];
172170
uint32_t f_hi = pitch_bend_table[semitone + 1]; // table has 13 entries, indexing with semitone=12 is OK
173171
uint32_t f = ((f_lo * (65535 - fractone) + f_hi * fractone) >> 16) + BEND_OFFSET;
174-
return (frequency_scaled * (uint64_t)f) >> (15 + down);
172+
return (frequency_scaled * (uint64_t)f) >> (15 - octave);
175173
}
176174

177175
#define ZERO MICROPY_FLOAT_CONST(0.)
@@ -196,11 +194,11 @@ uint32_t synthio_note_step(synthio_note_obj_t *self, int32_t sample_rate, int16_
196194
loudness[1] = (loudness[1] * right_panning_scaled) >> 15;
197195

198196
if (self->ring_frequency_scaled != 0) {
199-
int ring_bend_value = synthio_lfo_obj_tick_scaled(&self->ring_bend, -ONE, ALMOST_ONE, 15);
197+
int ring_bend_value = synthio_lfo_obj_tick_scaled(&self->ring_bend, -12, 12, 15);
200198
self->ring_frequency_bent = pitch_bend(self->ring_frequency_scaled, ring_bend_value);
201199
}
202200

203-
int bend_value = synthio_lfo_obj_tick_scaled(&self->bend, -ONE, ALMOST_ONE, 15);
201+
int bend_value = synthio_lfo_obj_tick_scaled(&self->bend, -12, 12, 15);
204202
uint32_t frequency_scaled = pitch_bend(self->frequency_scaled, bend_value);
205203
return frequency_scaled;
206204

0 commit comments

Comments
 (0)