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.
This commit is contained in:
Jeff Epler 2023-05-06 08:35:48 -05:00
parent a854e554f4
commit a53c0ed066
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
4 changed files with 28 additions and 11 deletions

View File

@ -262,16 +262,29 @@ STATIC mp_obj_t synthio_from_file(size_t n_args, const mp_obj_t *pos_args, mp_ma
}
MP_DEFINE_CONST_FUN_OBJ_KW(synthio_from_file_obj, 1, synthio_from_file);
//| def midi_to_hz(midi_note: int) -> float:
//| def midi_to_hz(midi_note: float) -> float:
//| """Converts the given midi note (60 = middle C, 69 = concert A) to Hz"""
//|
/
STATIC mp_obj_t midi_to_hz(mp_obj_t arg) {
mp_int_t note = mp_arg_validate_int_range(mp_obj_get_int(arg), 1, 127, MP_QSTR_note);
mp_float_t note = mp_arg_validate_obj_float_range(arg, 1, 127, MP_QSTR_note);
return mp_obj_new_float(common_hal_synthio_midi_to_hz_float(note));
}
MP_DEFINE_CONST_FUN_OBJ_1(synthio_midi_to_hz_obj, midi_to_hz);
//| def onevo_to_hz(ctrl: float) -> float:
//| """Converts a 1v/octave signal to Hz.
//|
//| 60/12 (5.0) corresponds to middle C, 69/12 is concert A."""
//|
STATIC mp_obj_t onevo_to_hz(mp_obj_t arg) {
mp_float_t note = mp_arg_validate_obj_float_range(arg, 0, 11, MP_QSTR_ctrl);
return mp_obj_new_float(common_hal_synthio_onevo_to_hz_float(note));
}
MP_DEFINE_CONST_FUN_OBJ_1(synthio_onevo_to_hz_obj, onevo_to_hz);
STATIC const mp_rom_map_elem_t synthio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_synthio) },
{ 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[] = {
{ MP_ROM_QSTR(MP_QSTR_from_file), MP_ROM_PTR(&synthio_from_file_obj) },
{ MP_ROM_QSTR(MP_QSTR_Envelope), MP_ROM_PTR(&synthio_envelope_type_obj) },
{ MP_ROM_QSTR(MP_QSTR_midi_to_hz), MP_ROM_PTR(&synthio_midi_to_hz_obj) },
{ MP_ROM_QSTR(MP_QSTR_onevo_to_hz), MP_ROM_PTR(&synthio_midi_to_hz_obj) },
};
STATIC MP_DEFINE_CONST_DICT(synthio_module_globals, synthio_module_globals_table);

View File

@ -33,4 +33,5 @@ extern int16_t shared_bindings_synthio_square_wave[];
extern const mp_obj_namedtuple_type_t synthio_envelope_type_obj;
void synthio_synth_envelope_set(synthio_synth_t *synth, mp_obj_t envelope_obj);
mp_obj_t synthio_synth_envelope_get(synthio_synth_t *synth);
mp_float_t common_hal_synthio_midi_to_hz_float(mp_int_t note);
mp_float_t common_hal_synthio_midi_to_hz_float(mp_float_t note);
mp_float_t common_hal_synthio_onevo_to_hz_float(mp_float_t note);

View File

@ -45,10 +45,12 @@ STATIC int64_t round_float_to_int64(mp_float_t f) {
return (int64_t)(f + MICROPY_FLOAT_CONST(0.5));
}
mp_float_t common_hal_synthio_midi_to_hz_float(mp_int_t arg) {
uint8_t octave = arg / 12;
uint16_t base_freq = notes[arg % 12];
return MICROPY_FLOAT_C_FUN(ldexp)(base_freq, octave - 10);
mp_float_t common_hal_synthio_midi_to_hz_float(mp_float_t arg) {
return common_hal_synthio_onevo_to_hz_float(arg / 12.);
}
mp_float_t common_hal_synthio_onevo_to_hz_float(mp_float_t octave) {
return notes[0] * MICROPY_FLOAT_C_FUN(pow)(2., octave - 10);
}
STATIC int16_t convert_time_to_rate(uint32_t sample_rate, mp_obj_t time_in, int16_t difference) {

View File

@ -1,10 +1,10 @@
()
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
(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.6076004423605, amplitude=1.0, tremolo_rate=0.0, tremolo_depth=0.0, vibrato_rate=0.0, vibrato_depth=0.0, waveform=None, envelope=None),)
[-16383, -16383, -16383, -16383, 16382, 16382, 16382, 16382, 16382, -16383, -16383, -16383, -16383, -16383, 16382, 16382, 16382, 16382, 16382, -16383, -16383, -16383, -16383, -16383]
(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))
(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))
[0, 0, 0, 0, 0, 0, 0, 0, 16382, 0, 0, 0, 0, -16382, 0, 0, 0, 0, 16382, 0, 0, 0, 0, -16382]
(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.6076004423605, amplitude=1.0, tremolo_rate=0.0, tremolo_depth=0.0, vibrato_rate=0.0, vibrato_depth=0.0, waveform=None, envelope=None),)
[0, 0, 0, 16382, 0, 0, 0, 0, 0, 0, 0, 0, 16382, 0, 0, 0, 0, -16382, 0, 0, 0, 0, 16382, 0]
(-5242, 5241)
(-10484, 10484)