|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
-
-
- #pragma once
-
- #include "Arduino.h"
- #include "AudioStream.h"
- #include <math.h>
- #include <stdint.h>
-
- #define WAVETABLE_CENTS_SHIFT(C) (pow(2.0, (C)/1200.0))
- #define WAVETABLE_NOTE_TO_FREQUENCY(N) (440.0 * pow(2.0, ((N) - 69) / 12.0))
- #define WAVETABLE_DECIBEL_SHIFT(dB) (pow(10.0, (dB)/20.0))
-
- class AudioSynthWavetable : public AudioStream
- {
- public:
- struct sample_data {
-
- const int16_t* sample;
- const bool LOOP;
- const int INDEX_BITS;
- const float PER_HERTZ_PHASE_INCREMENT;
- const uint32_t MAX_PHASE;
- const uint32_t LOOP_PHASE_END;
- const uint32_t LOOP_PHASE_LENGTH;
- const uint16_t INITIAL_ATTENUATION_SCALAR;
-
-
- const uint32_t DELAY_COUNT;
- const uint32_t ATTACK_COUNT;
- const uint32_t HOLD_COUNT;
- const uint32_t DECAY_COUNT;
- const uint32_t RELEASE_COUNT;
- const int32_t SUSTAIN_MULT;
-
-
- const uint32_t VIBRATO_DELAY;
- const uint32_t VIBRATO_INCREMENT;
- const float VIBRATO_PITCH_COEFFICIENT_INITIAL;
- const float VIBRATO_PITCH_COEFFICIENT_SECOND;
-
-
- const uint32_t MODULATION_DELAY;
- const uint32_t MODULATION_INCREMENT;
- const float MODULATION_PITCH_COEFFICIENT_INITIAL;
- const float MODULATION_PITCH_COEFFICIENT_SECOND;
- const int32_t MODULATION_AMPLITUDE_INITIAL_GAIN;
- const int32_t MODULATION_AMPLITUDE_SECOND_GAIN;
- };
- static const int32_t UNITY_GAIN = INT32_MAX;
- static constexpr float SAMPLES_PER_MSEC = (AUDIO_SAMPLE_RATE_EXACT/1000.0);
- static const int32_t LFO_SMOOTHNESS = 3;
- static constexpr float LFO_PERIOD = (AUDIO_BLOCK_SAMPLES/(1 << (LFO_SMOOTHNESS-1)));
- static const int32_t ENVELOPE_PERIOD = 8;
-
- struct instrument_data {
- const uint8_t sample_count;
- const uint8_t* sample_note_ranges;
- const sample_data* samples;
- };
- enum { DEFAULT_AMPLITUDE = 90 };
- enum { TRIANGLE_INITIAL_PHASE = -0x40000000 };
- enum envelopeStateEnum { STATE_IDLE, STATE_DELAY, STATE_ATTACK, STATE_HOLD, STATE_DECAY, STATE_SUSTAIN, STATE_RELEASE };
-
- public:
-
-
- AudioSynthWavetable(void) : AudioStream(0, NULL) {}
-
-
-
- void setInstrument(const instrument_data& instrument) {
- cli();
- this->instrument = &instrument;
- current_sample = NULL;
- env_state = STATE_IDLE;
- state_change = true;
- sei();
- }
-
-
-
- void amplitude(float v) {
- v = (v < 0.0) ? 0.0 : (v > 1.0) ? 1.0 : v;
- tone_amp = (uint16_t)(UINT16_MAX*v);
- }
-
-
-
- static float midi_volume_transform(int midi_amp) {
-
-
- return powf(midi_amp / 127.0, 4);
- }
-
-
-
- static float noteToFreq(int note) {
- float exp = note * (1.0 / 12.0) + 3.0313597;
- return powf(2.0, exp);
- }
-
-
-
- static int freqToNote(float freq) {
- return 12*log2f(freq) - 35.8763164;
- }
-
-
- void stop(void);
-
- void playFrequency(float freq, int amp = DEFAULT_AMPLITUDE);
- void playNote(int note, int amp = DEFAULT_AMPLITUDE);
- bool isPlaying(void) { return env_state != STATE_IDLE; }
- void setFrequency(float freq);
- virtual void update(void);
-
- envelopeStateEnum getEnvState(void) { return env_state; }
-
- private:
- void setState(int note, int amp, float freq);
- volatile bool state_change = false;
-
- volatile const instrument_data* instrument = NULL;
- volatile const sample_data* current_sample = NULL;
-
-
- volatile uint32_t tone_phase = 0;
- volatile uint32_t tone_incr = 0;
- volatile uint16_t tone_amp = 0;
-
-
- volatile envelopeStateEnum env_state = STATE_IDLE;
- volatile int32_t env_count = 0;
- volatile int32_t env_mult = 0;
- volatile int32_t env_incr = 0;
-
-
- volatile uint32_t vib_count = 0;
- volatile uint32_t vib_phase = 0;
- volatile int32_t vib_pitch_offset_init = 0;
- volatile int32_t vib_pitch_offset_scnd = 0;
-
-
- volatile uint32_t mod_count = 0;
- volatile uint32_t mod_phase = TRIANGLE_INITIAL_PHASE;
- volatile int32_t mod_pitch_offset_init = 0;
- volatile int32_t mod_pitch_offset_scnd = 0;
- };
-
|