Skip to content

Commit c92ad33

Browse files
committed
synthio: allow negative amplitudes
Previously, negative amplitudes were clamped to zero. Now, they are allowed to range from -ALMOST_ONE to +ALMOST_ONE. This is useful in certain circumstances, such as using synthio to create CV-like outputs that can be positive or negative, by using the amplitude property of the note.
1 parent 383f797 commit c92ad33

File tree

5 files changed

+16137
-2057
lines changed

5 files changed

+16137
-2057
lines changed

shared-module/synthio/Note.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ STATIC uint32_t pitch_bend(uint32_t frequency_scaled, int32_t bend_value) {
214214
#define ONE MICROPY_FLOAT_CONST(1.)
215215
#define ALMOST_ONE (MICROPY_FLOAT_CONST(32767.) / 32768)
216216

217-
uint32_t synthio_note_step(synthio_note_obj_t *self, int32_t sample_rate, int16_t dur, uint16_t loudness[2]) {
217+
uint32_t synthio_note_step(synthio_note_obj_t *self, int32_t sample_rate, int16_t dur, int16_t loudness[2]) {
218218
int panning = synthio_block_slot_get_scaled(&self->panning, -ALMOST_ONE, ALMOST_ONE);
219219
int left_panning_scaled, right_panning_scaled;
220220
if (panning >= 0) {
@@ -225,7 +225,7 @@ uint32_t synthio_note_step(synthio_note_obj_t *self, int32_t sample_rate, int16_
225225
left_panning_scaled = 32767 + panning;
226226
}
227227

228-
int amplitude = synthio_block_slot_get_scaled(&self->amplitude, ZERO, ALMOST_ONE);
228+
int amplitude = synthio_block_slot_get_scaled(&self->amplitude, -ALMOST_ONE, ALMOST_ONE);
229229
left_panning_scaled = (left_panning_scaled * amplitude) >> 15;
230230
right_panning_scaled = (right_panning_scaled * amplitude) >> 15;
231231
loudness[0] = (loudness[0] * left_panning_scaled) >> 15;

shared-module/synthio/Note.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ typedef struct synthio_note_obj {
5555
} synthio_note_obj_t;
5656

5757
void synthio_note_recalculate(synthio_note_obj_t *self, int32_t sample_rate);
58-
uint32_t synthio_note_step(synthio_note_obj_t *self, int32_t sample_rate, int16_t dur, uint16_t loudness[2]);
58+
uint32_t synthio_note_step(synthio_note_obj_t *self, int32_t sample_rate, int16_t dur, int16_t loudness[2]);
5959
void synthio_note_start(synthio_note_obj_t *self, int32_t sample_rate);
6060
bool synthio_note_playing(synthio_note_obj_t *self);

shared-module/synthio/__init__.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ int16_t mix_down_sample(int32_t sample) {
172172
return sample;
173173
}
174174

175-
static bool synth_note_into_buffer(synthio_synth_t *synth, int chan, int32_t *out_buffer32, int16_t dur, uint16_t loudness[2]) {
175+
static bool synth_note_into_buffer(synthio_synth_t *synth, int chan, int32_t *out_buffer32, int16_t dur, int16_t loudness[2]) {
176176
mp_obj_t note_obj = synth->span.note_obj[chan];
177177

178178
int32_t sample_rate = synth->sample_rate;
@@ -298,7 +298,7 @@ STATIC mp_obj_t synthio_synth_get_note_filter(mp_obj_t note_obj) {
298298
return mp_const_none;
299299
}
300300

301-
STATIC void sum_with_loudness(int32_t *out_buffer32, int32_t *tmp_buffer32, uint16_t loudness[2], size_t dur, int synth_chan) {
301+
STATIC void sum_with_loudness(int32_t *out_buffer32, int32_t *tmp_buffer32, int16_t loudness[2], size_t dur, int synth_chan) {
302302
if (synth_chan == 1) {
303303
for (size_t i = 0; i < dur; i++) {
304304
*out_buffer32++ += (*tmp_buffer32++ *loudness[0]) >> 16;
@@ -344,7 +344,7 @@ void synthio_synth_synthesize(synthio_synth_t *synth, uint8_t **bufptr, uint32_t
344344
continue;
345345
}
346346

347-
uint16_t loudness[2] = {synth->envelope_state[chan].level, synth->envelope_state[chan].level};
347+
int16_t loudness[2] = {synth->envelope_state[chan].level, synth->envelope_state[chan].level};
348348

349349
if (!synth_note_into_buffer(synth, chan, tmp_buffer32, dur, loudness)) {
350350
// for some other reason, such as being above nyquist, note

tests/circuitpython/synth_note_amplitude.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
@synth_test
55
def gen(synth):
6-
l = LFO(bend_out, offset=0.2, scale=0.8, rate=4, once=True)
6+
l = LFO(sine, offset=0.2, scale=0.8, rate=2)
77
yield [l]
8-
n = Note(128, amplitude=l)
8+
n = Note(8, amplitude=l)
99
synth.press(n)
10-
yield 1 / 4
10+
yield 2

0 commit comments

Comments
 (0)