|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
-
-
- #include <Arduino.h>
- #include "synth_wavetable.h"
- #include <dspinst.h>
- #include <SerialFlash.h>
-
-
-
-
-
-
-
-
- #ifdef TIME_TEST_ON
- #define TIME_TEST(INTERVAL, CODE_BLOCK_TO_TEST) \
- static float MICROS_AVG = 0.0; \
- static int TEST_CUR_CNT = 0; \
- static int TEST_LST_CNT = 0; \
- static int NEXT_DISPLAY = 0; \
- static int TEST_TIME_ACC = 0; \
- int micros_start = micros(); \
- CODE_BLOCK_TO_TEST \
- int micros_end = micros(); \
- TEST_TIME_ACC += micros_end - micros_start; \
- ++TEST_CUR_CNT; \
- if (NEXT_DISPLAY < micros_end) { \
- MICROS_AVG += (TEST_TIME_ACC - TEST_CUR_CNT * MICROS_AVG) / (TEST_LST_CNT + TEST_CUR_CNT); \
- NEXT_DISPLAY = micros_end + INTERVAL*1000; \
- TEST_LST_CNT += TEST_CUR_CNT; \
- TEST_TIME_ACC = TEST_CUR_CNT = 0; \
- Serial.printf("avg: %f, n: %i\n", MICROS_AVG, TEST_LST_CNT); \
- }
- #else
- #define TIME_TEST(INTERVAL, CODE_BLOCK_TO_TEST) do { } while(0); \
- CODE_BLOCK_TO_TEST
- #endif
-
-
- #ifdef ENVELOPE_DEBUG
- #define PRINT_ENV(NAME) Serial.printf("%14s-- env_mult:%06.4f%% of UNITY_GAIN env_incr:%06.4f%% of UNITY_GAIN env_count:%i\n", #NAME, float(env_mult)/float(UNITY_GAIN), float(env_incr)/float(UNITY_GAIN), env_count);
- #else
- #define PRINT_ENV(NAME) do { } while(0);
- #endif
-
-
-
- void AudioSynthWavetable::stop(void) {
- cli();
- if (env_state != STATE_IDLE) {
- env_state = STATE_RELEASE;
- env_count = current_sample->RELEASE_COUNT;
- if (env_count == 0) env_count = 1;
- env_incr = -(env_mult) / (env_count * ENVELOPE_PERIOD);
- }
- PRINT_ENV(STATE_RELEASE);
- sei();
- }
-
-
- void AudioSynthWavetable::playFrequency(float freq, int amp) {
- setState(freqToNote(freq), amp, freq);
- }
-
-
- void AudioSynthWavetable::playNote(int note, int amp) {
- setState(note, amp, noteToFreq(note));
- }
-
-
- void AudioSynthWavetable::setState(int note, int amp, float freq) {
- cli();
- int i;
- env_state = STATE_IDLE;
-
- for (i = 0; note > instrument->sample_note_ranges[i]; i++);
- current_sample = &instrument->samples[i];
- if (current_sample == NULL) {
- sei();
- return;
- }
- setFrequency(freq);
- vib_count = mod_count = tone_phase = env_incr = env_mult = 0;
- vib_phase = mod_phase = TRIANGLE_INITIAL_PHASE;
- env_count = current_sample->DELAY_COUNT;
-
- tone_amp = amp * (UINT16_MAX / 127);
-
- tone_amp = current_sample->INITIAL_ATTENUATION_SCALAR * tone_amp >> 16;
- env_state = STATE_DELAY;
- PRINT_ENV(STATE_DELAY);
- state_change = true;
- sei();
- }
-
-
- void AudioSynthWavetable::setFrequency(float freq) {
- float tone_incr_temp = freq * current_sample->PER_HERTZ_PHASE_INCREMENT;
- tone_incr = tone_incr_temp;
- vib_pitch_offset_init = tone_incr_temp * current_sample->VIBRATO_PITCH_COEFFICIENT_INITIAL;
- vib_pitch_offset_scnd = tone_incr_temp * current_sample->VIBRATO_PITCH_COEFFICIENT_SECOND;
- mod_pitch_offset_init = tone_incr_temp * current_sample->MODULATION_PITCH_COEFFICIENT_INITIAL;
- mod_pitch_offset_scnd = tone_incr_temp * current_sample->MODULATION_PITCH_COEFFICIENT_SECOND;
- }
-
-
- void AudioSynthWavetable::update(void) {
- #if defined(KINETISK) || defined(__IMXRT1062__)
-
- if (env_state == STATE_IDLE || (current_sample->LOOP == false && tone_phase >= current_sample->MAX_PHASE)) {
- env_state = STATE_IDLE;
- return;
- }
-
- this->state_change = false;
-
- const sample_data* s = (const sample_data*)current_sample;
- uint32_t tone_phase = this->tone_phase;
- uint32_t tone_incr = this->tone_incr;
- uint16_t tone_amp = this->tone_amp;
-
- envelopeStateEnum env_state = this->env_state;
- int32_t env_count = this->env_count;
- int32_t env_mult = this->env_mult;
- int32_t env_incr = this->env_incr;
-
- uint32_t vib_count = this->vib_count;
- uint32_t vib_phase = this->vib_phase;
- int32_t vib_pitch_offset_init = this->vib_pitch_offset_init;
- int32_t vib_pitch_offset_scnd = this->vib_pitch_offset_scnd;
-
- uint32_t mod_count = this->mod_count;
- int32_t mod_phase = this->mod_phase;
- int32_t mod_pitch_offset_init = this->mod_pitch_offset_init;
- int32_t mod_pitch_offset_scnd = this->mod_pitch_offset_scnd;
-
- audio_block_t* block;
- block = allocate();
- if (block == NULL) return;
-
- uint32_t* p, *end;
- uint32_t index, phase_scale;
- int32_t s1, s2;
- uint32_t tmp1, tmp2;
-
-
- p = (uint32_t*)block->data;
- end = p + AUDIO_BLOCK_SAMPLES / 2;
-
-
-
-
-
-
-
-
-
-
- while (p < end) {
-
- if (s->LOOP == false && tone_phase >= s->MAX_PHASE) break;
-
-
- int32_t tone_incr_offset = 0;
- if (vib_count++ > s->VIBRATO_DELAY) {
- vib_phase += s->VIBRATO_INCREMENT;
-
-
- int32_t vib_scale = vib_phase & 0x80000000 ? 0x40000000 + vib_phase : 0x3FFFFFFF - vib_phase;
-
-
-
- int32_t vib_pitch_offset = vib_scale >= 0 ? vib_pitch_offset_init : vib_pitch_offset_scnd;
-
-
- tone_incr_offset = multiply_accumulate_32x32_rshift32_rounded(tone_incr_offset, vib_scale, vib_pitch_offset);
- }
-
-
- int32_t mod_amp = tone_amp;
- if (mod_count++ > s->MODULATION_DELAY) {
-
- mod_phase += s->MODULATION_INCREMENT;
- int32_t mod_scale = mod_phase & 0x80000000 ? 0x40000000 + mod_phase : 0x3FFFFFFF - mod_phase;
-
- int32_t mod_pitch_offset = mod_scale >= 0 ? mod_pitch_offset_init : mod_pitch_offset_scnd;
- tone_incr_offset = multiply_accumulate_32x32_rshift32_rounded(tone_incr_offset, mod_scale, mod_pitch_offset);
-
-
- int32_t mod_amp_offset = (mod_scale >= 0 ? s->MODULATION_AMPLITUDE_INITIAL_GAIN : s->MODULATION_AMPLITUDE_SECOND_GAIN);
-
- mod_scale = multiply_32x32_rshift32(mod_scale, mod_amp_offset);
-
- mod_amp = signed_multiply_accumulate_32x16b(mod_amp, mod_scale, mod_amp);
- }
-
-
-
- for (int i = LFO_PERIOD / 2; i; --i, ++p) {
-
- index = tone_phase >> (32 - s->INDEX_BITS);
-
-
- tmp1 = *((uint32_t*)(s->sample + index));
-
-
- phase_scale = (tone_phase << s->INDEX_BITS) >> 16;
-
- s1 = signed_multiply_32x16t(phase_scale, tmp1);
-
- s1 = signed_multiply_accumulate_32x16b(s1, 0xFFFF - phase_scale, tmp1);
-
- s1 = signed_multiply_32x16b(mod_amp, s1);
-
-
- tone_phase += tone_incr + tone_incr_offset;
-
- if (s->LOOP == false && tone_phase >= s->MAX_PHASE) break;
-
- tone_phase = s->LOOP && tone_phase >= s->LOOP_PHASE_END ? tone_phase - s->LOOP_PHASE_LENGTH : tone_phase;
-
-
- index = tone_phase >> (32 - s->INDEX_BITS);
- tmp1 = *((uint32_t*)(s->sample + index));
- phase_scale = (tone_phase << s->INDEX_BITS) >> 16;
- s2 = signed_multiply_32x16t(phase_scale, tmp1);
- s2 = signed_multiply_accumulate_32x16b(s2, 0xFFFF - phase_scale, tmp1);
- s2 = signed_multiply_32x16b(mod_amp, s2);
-
- tone_phase += tone_incr + tone_incr_offset;
- if (s->LOOP == false && tone_phase >= s->MAX_PHASE) break;
- tone_phase = s->LOOP && tone_phase >= s->LOOP_PHASE_END ? tone_phase - s->LOOP_PHASE_LENGTH : tone_phase;
-
-
- *p = pack_16b_16b(s2, s1);
- }
- }
-
- if (p < end) {
- env_state = STATE_IDLE;
- env_count = 0;
- while (p < end) *p++ = 0;
- }
-
-
- p = (uint32_t *)block->data;
- end = p + AUDIO_BLOCK_SAMPLES / 2;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- while (p < end) {
-
- if (env_count <= 0) switch (env_state) {
- case STATE_DELAY:
- env_state = STATE_ATTACK;
- env_count = s->ATTACK_COUNT;
- env_incr = UNITY_GAIN / (env_count * ENVELOPE_PERIOD);
- PRINT_ENV(STATE_ATTACK);
- continue;
- case STATE_ATTACK:
- env_mult = UNITY_GAIN;
- env_state = STATE_HOLD;
- env_count = s->HOLD_COUNT;
- env_incr = 0;
- PRINT_ENV(STATE_HOLD);
- continue;
- case STATE_HOLD:
- env_state = STATE_DECAY;
- env_count = s->DECAY_COUNT;
- env_incr = (-s->SUSTAIN_MULT) / (env_count * ENVELOPE_PERIOD);
- PRINT_ENV(STATE_DECAY);
- continue;
- case STATE_DECAY:
- env_mult = UNITY_GAIN - s->SUSTAIN_MULT;
-
-
- env_state = env_mult < UNITY_GAIN / UINT16_MAX ? STATE_RELEASE : STATE_SUSTAIN;
- env_incr = 0;
- continue;
- case STATE_SUSTAIN:
- env_count = INT32_MAX;
- PRINT_ENV(STATE_SUSTAIN);
- continue;
- case STATE_RELEASE:
- env_state = STATE_IDLE;
- for (; p < end; ++p) *p = 0;
- PRINT_ENV(STATE_IDLE);
- continue;
- default:
- p = end;
- PRINT_ENV(DEFAULT);
- continue;
- }
-
- env_mult += env_incr;
-
- tmp1 = signed_multiply_32x16b(env_mult, p[0]) << 1;
- env_mult += env_incr;
- tmp2 = signed_multiply_32x16t(env_mult, p[0]) << 1;
-
- p[0] = pack_16t_16t(tmp2, tmp1);
- env_mult += env_incr;
- tmp1 = signed_multiply_32x16b(env_mult, p[1]) << 1;
- env_mult += env_incr;
- tmp2 = signed_multiply_32x16t(env_mult, p[1]) << 1;
- p[1] = pack_16t_16t(tmp2, tmp1);
- env_mult += env_incr;
- tmp1 = signed_multiply_32x16b(env_mult, p[2]) << 1;
- env_mult += env_incr;
- tmp2 = signed_multiply_32x16t(env_mult, p[2]) << 1;
- p[2] = pack_16t_16t(tmp2, tmp1);
- env_mult += env_incr;
- tmp1 = signed_multiply_32x16b(env_mult, p[3]) << 1;
- env_mult += env_incr;
- tmp2 = signed_multiply_32x16t(env_mult, p[3]) << 1;
- p[3] = pack_16t_16t(tmp2, tmp1);
-
- p += ENVELOPE_PERIOD / 2;
- env_count--;
- }
-
-
- if (this->state_change == false) {
- this->tone_phase = tone_phase;
- this->env_state = env_state;
- this->env_count = env_count;
- this->env_mult = env_mult;
- this->env_incr = env_incr;
- if (this->env_state != STATE_IDLE) {
- this->vib_count = vib_count;
- this->vib_phase = vib_phase;
- this->mod_count = mod_count;
- this->mod_phase = mod_phase;
- }
- else {
- this->vib_count = this->mod_count = 0;
- this->vib_phase = this->mod_phase = TRIANGLE_INITIAL_PHASE;
- }
- }
-
- transmit(block);
- release(block);
- #endif
- }
|