Skip to content

Commit 79155af

Browse files
authored
Merge pull request #8642 from jepler/synthio-note-amplitude-signed
Synthio: Allow amplitude to be signed (+ other stuff)
2 parents 383f797 + 19df6ad commit 79155af

File tree

12 files changed

+16205
-2089
lines changed

12 files changed

+16205
-2089
lines changed

ports/unix/main.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,34 @@
5454
#include "genhdr/mpversion.h"
5555
#include "input.h"
5656

57+
#if defined(MICROPY_UNIX_COVERAGE) // CIRCUITPY-CHANGE
58+
#include "py/objstr.h"
59+
typedef int os_getenv_err_t;
60+
mp_obj_t common_hal_os_getenv(const char *key, mp_obj_t default_);
61+
os_getenv_err_t common_hal_os_getenv_str(const char *key, char *value, size_t value_len);
62+
os_getenv_err_t common_hal_os_getenv_int(const char *key, mp_int_t *value);
63+
64+
STATIC mp_obj_t mod_os_getenv_int(mp_obj_t var_in) {
65+
mp_int_t value;
66+
os_getenv_err_t result = common_hal_os_getenv_int(mp_obj_str_get_str(var_in), &value);
67+
if (result == 0) {
68+
return mp_obj_new_int(value);
69+
}
70+
return mp_const_none;
71+
}
72+
MP_DEFINE_CONST_FUN_OBJ_1(mod_os_getenv_int_obj, mod_os_getenv_int);
73+
74+
STATIC mp_obj_t mod_os_getenv_str(mp_obj_t var_in) {
75+
char buf[4096];
76+
os_getenv_err_t result = common_hal_os_getenv_str(mp_obj_str_get_str(var_in), buf, sizeof(buf));
77+
if (result == 0) {
78+
return mp_obj_new_str_copy(&mp_type_str, (byte *)buf, strlen(buf));
79+
}
80+
return mp_const_none;
81+
}
82+
MP_DEFINE_CONST_FUN_OBJ_1(mod_os_getenv_str_obj, mod_os_getenv_str);
83+
#endif
84+
5785
// Command line options, with their defaults
5886
STATIC bool compile_only = false;
5987
STATIC uint emit_opt = MP_EMIT_OPT_NONE;
@@ -596,6 +624,8 @@ MP_NOINLINE int main_(int argc, char **argv) {
596624
// CIRCUITPY-CHANGE: test native base classes work as needed by CircuitPython libraries.
597625
extern const mp_obj_type_t native_base_class_type;
598626
mp_store_global(MP_QSTR_NativeBaseClass, MP_OBJ_FROM_PTR(&native_base_class_type));
627+
mp_store_global(MP_QSTR_getenv_int, MP_OBJ_FROM_PTR(&mod_os_getenv_int_obj));
628+
mp_store_global(MP_QSTR_getenv_str, MP_OBJ_FROM_PTR(&mod_os_getenv_str_obj));
599629
}
600630
#endif
601631

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/getenv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def run_test(key, content):
8282
run_test(f"key{i}", content_good)
8383

8484
run_test(f"K", b"K = 7\r\n")
85-
print(os.getenv_int("K"))
85+
print(getenv_int("K"))
8686

8787
# Test value without trailing newline
8888
run_test(f"noeol", b"noeol=3")

tests/circuitpython/synth_block_scale_offset.py.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
0.6026482747395835 -1.794703450520833 0.1920551757812494 -5.397351725260417
123123
0.6239809570312501 -1.7520380859375 0.1280571289062498 -5.37601904296875
124124
0.6453136393229166 -1.709372721354167 0.0640590820312501 -5.354686360677084
125-
0.6666463216145831 -1.666707356770834 6.103515625044409e-05 -5.333353678385417
125+
0.6666463216145831 -1.666707356770834 6.103515625044408e-05 -5.333353678385417
126126
0.6879790039062498 -1.6240419921875 -0.06393701171874921 -5.31202099609375
127127
0.7093116861979163 -1.581376627604167 -0.1279350585937489 -5.290688313802084
128128
0.7306443684895827 -1.538711263020835 -0.1919331054687481 -5.269355631510417

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)