Skip to content

Commit a53c0ed

Browse files
committed
synthio: add onevo_to_hz, implement midi_to_hz in terms of it
this has the side effect of making some notes more accurate, the new frequency= value in the test is closer to the true midi frequency of 830.609...Hz.
1 parent a854e55 commit a53c0ed

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

shared-bindings/synthio/__init__.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,29 @@ STATIC mp_obj_t synthio_from_file(size_t n_args, const mp_obj_t *pos_args, mp_ma
262262
}
263263
MP_DEFINE_CONST_FUN_OBJ_KW(synthio_from_file_obj, 1, synthio_from_file);
264264

265-
//| def midi_to_hz(midi_note: int) -> float:
265+
//| def midi_to_hz(midi_note: float) -> float:
266266
//| """Converts the given midi note (60 = middle C, 69 = concert A) to Hz"""
267267
//|
268-
/
268+
269269
STATIC mp_obj_t midi_to_hz(mp_obj_t arg) {
270-
mp_int_t note = mp_arg_validate_int_range(mp_obj_get_int(arg), 1, 127, MP_QSTR_note);
270+
mp_float_t note = mp_arg_validate_obj_float_range(arg, 1, 127, MP_QSTR_note);
271271
return mp_obj_new_float(common_hal_synthio_midi_to_hz_float(note));
272272
}
273273
MP_DEFINE_CONST_FUN_OBJ_1(synthio_midi_to_hz_obj, midi_to_hz);
274274

275+
//| def onevo_to_hz(ctrl: float) -> float:
276+
//| """Converts a 1v/octave signal to Hz.
277+
//|
278+
//| 60/12 (5.0) corresponds to middle C, 69/12 is concert A."""
279+
//|
280+
281+
STATIC mp_obj_t onevo_to_hz(mp_obj_t arg) {
282+
mp_float_t note = mp_arg_validate_obj_float_range(arg, 0, 11, MP_QSTR_ctrl);
283+
return mp_obj_new_float(common_hal_synthio_onevo_to_hz_float(note));
284+
}
285+
MP_DEFINE_CONST_FUN_OBJ_1(synthio_onevo_to_hz_obj, onevo_to_hz);
286+
287+
275288
STATIC const mp_rom_map_elem_t synthio_module_globals_table[] = {
276289
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_synthio) },
277290
{ MP_ROM_QSTR(MP_QSTR_MidiTrack), MP_ROM_PTR(&synthio_miditrack_type) },
@@ -280,6 +293,7 @@ STATIC const mp_rom_map_elem_t synthio_module_globals_table[] = {
280293
{ MP_ROM_QSTR(MP_QSTR_from_file), MP_ROM_PTR(&synthio_from_file_obj) },
281294
{ MP_ROM_QSTR(MP_QSTR_Envelope), MP_ROM_PTR(&synthio_envelope_type_obj) },
282295
{ MP_ROM_QSTR(MP_QSTR_midi_to_hz), MP_ROM_PTR(&synthio_midi_to_hz_obj) },
296+
{ MP_ROM_QSTR(MP_QSTR_onevo_to_hz), MP_ROM_PTR(&synthio_midi_to_hz_obj) },
283297
};
284298

285299
STATIC MP_DEFINE_CONST_DICT(synthio_module_globals, synthio_module_globals_table);

shared-bindings/synthio/__init__.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ extern int16_t shared_bindings_synthio_square_wave[];
3333
extern const mp_obj_namedtuple_type_t synthio_envelope_type_obj;
3434
void synthio_synth_envelope_set(synthio_synth_t *synth, mp_obj_t envelope_obj);
3535
mp_obj_t synthio_synth_envelope_get(synthio_synth_t *synth);
36-
mp_float_t common_hal_synthio_midi_to_hz_float(mp_int_t note);
36+
mp_float_t common_hal_synthio_midi_to_hz_float(mp_float_t note);
37+
mp_float_t common_hal_synthio_onevo_to_hz_float(mp_float_t note);

shared-module/synthio/__init__.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ STATIC int64_t round_float_to_int64(mp_float_t f) {
4545
return (int64_t)(f + MICROPY_FLOAT_CONST(0.5));
4646
}
4747

48-
mp_float_t common_hal_synthio_midi_to_hz_float(mp_int_t arg) {
49-
uint8_t octave = arg / 12;
50-
uint16_t base_freq = notes[arg % 12];
51-
return MICROPY_FLOAT_C_FUN(ldexp)(base_freq, octave - 10);
48+
mp_float_t common_hal_synthio_midi_to_hz_float(mp_float_t arg) {
49+
return common_hal_synthio_onevo_to_hz_float(arg / 12.);
50+
}
51+
52+
mp_float_t common_hal_synthio_onevo_to_hz_float(mp_float_t octave) {
53+
return notes[0] * MICROPY_FLOAT_C_FUN(pow)(2., octave - 10);
5254
}
5355

5456
STATIC int16_t convert_time_to_rate(uint32_t sample_rate, mp_obj_t time_in, int16_t difference) {

tests/circuitpython/synthesizer_note.py.exp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
()
22
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
3-
(Note(frequency=830.625, amplitude=1.0, tremolo_rate=0.0, tremolo_depth=0.0, vibrato_rate=0.0, vibrato_depth=0.0, waveform=None, envelope=None),)
3+
(Note(frequency=830.6076004423605, amplitude=1.0, tremolo_rate=0.0, tremolo_depth=0.0, vibrato_rate=0.0, vibrato_depth=0.0, waveform=None, envelope=None),)
44
[-16383, -16383, -16383, -16383, 16382, 16382, 16382, 16382, 16382, -16383, -16383, -16383, -16383, -16383, 16382, 16382, 16382, 16382, 16382, -16383, -16383, -16383, -16383, -16383]
5-
(Note(frequency=830.625, amplitude=1.0, tremolo_rate=0.0, tremolo_depth=0.0, vibrato_rate=0.0, vibrato_depth=0.0, waveform=None, envelope=None), Note(frequency=830.625, amplitude=1.0, tremolo_rate=0.0, tremolo_depth=0.0, vibrato_rate=0.0, vibrato_depth=0.0, waveform=None, envelope=None))
5+
(Note(frequency=830.6076004423605, amplitude=1.0, tremolo_rate=0.0, tremolo_depth=0.0, vibrato_rate=0.0, vibrato_depth=0.0, waveform=None, envelope=None), Note(frequency=830.6076004423605, amplitude=1.0, tremolo_rate=0.0, tremolo_depth=0.0, vibrato_rate=0.0, vibrato_depth=0.0, waveform=None, envelope=None))
66
[0, 0, 0, 0, 0, 0, 0, 0, 16382, 0, 0, 0, 0, -16382, 0, 0, 0, 0, 16382, 0, 0, 0, 0, -16382]
7-
(Note(frequency=830.625, amplitude=1.0, tremolo_rate=0.0, tremolo_depth=0.0, vibrato_rate=0.0, vibrato_depth=0.0, waveform=None, envelope=None),)
7+
(Note(frequency=830.6076004423605, amplitude=1.0, tremolo_rate=0.0, tremolo_depth=0.0, vibrato_rate=0.0, vibrato_depth=0.0, waveform=None, envelope=None),)
88
[0, 0, 0, 16382, 0, 0, 0, 0, 0, 0, 0, 0, 16382, 0, 0, 0, 0, -16382, 0, 0, 0, 0, 16382, 0]
99
(-5242, 5241)
1010
(-10484, 10484)

0 commit comments

Comments
 (0)