| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- #include "do_effects.h"
- #include <furi.h>
- #include "../sound_engine/sound_engine_filter.h"
- #include "tracker_engine.h"
- void do_command(uint16_t opcode, TrackerEngine *tracker_engine, uint8_t channel, uint8_t tick, bool from_program)
- {
- UNUSED(from_program);
- TrackerEngineChannel *te_channel = &tracker_engine->channel[channel];
- SoundEngineChannel *se_channel = &tracker_engine->sound_engine->channel[channel];
- switch (opcode & 0x7f00)
- {
- case TE_EFFECT_ARPEGGIO:
- {
- if (tick == 0)
- {
- if (te_channel->fixed_note != 0xffff)
- {
- te_channel->note = te_channel->last_note;
- te_channel->fixed_note = 0xffff;
- }
- if ((opcode & 0xff) == 0xf0)
- te_channel->arpeggio_note = te_channel->extarp1;
- else if ((opcode & 0xff) == 0xf1)
- te_channel->arpeggio_note = te_channel->extarp2;
- else
- te_channel->arpeggio_note = (opcode & 0xff);
- }
- break;
- }
- case TE_EFFECT_PORTAMENTO_UP:
- {
- uint32_t prev = te_channel->note;
- te_channel->note += ((opcode & 0xff) << 2);
- if (prev > te_channel->note)
- te_channel->note = 0xffff;
- te_channel->target_note = te_channel->note;
- break;
- }
- case TE_EFFECT_PORTAMENTO_DOWN:
- {
- int32_t prev = te_channel->note;
- te_channel->note -= ((opcode & 0xff) << 2);
- if (prev < te_channel->note)
- te_channel->note = 0;
- te_channel->target_note = te_channel->note;
- break;
- }
- case TE_EFFECT_VIBRATO:
- {
- if (tick == 0)
- {
- if (opcode & 0xff)
- {
- te_channel->flags |= TE_ENABLE_VIBRATO;
- te_channel->vibrato_speed = (opcode & 0xf0);
- te_channel->vibrato_depth = ((opcode & 0x0f) << 4);
- }
- else
- {
- te_channel->flags &= ~(TE_ENABLE_VIBRATO);
- }
- }
- break;
- }
- case TE_EFFECT_PWM:
- {
- if (tick == 0)
- {
- if (opcode & 0xff)
- {
- te_channel->flags |= TE_ENABLE_PWM;
- te_channel->pwm_speed = (opcode & 0xf0);
- te_channel->pwm_depth = ((opcode & 0x0f) << 4);
- }
- else
- {
- te_channel->flags &= ~(TE_ENABLE_PWM);
- }
- }
- break;
- }
- case TE_EFFECT_SET_PW:
- {
- if (tick == 0)
- {
- te_channel->pw = ((opcode & 0xff) << 4);
- }
- break;
- }
- case TE_EFFECT_PW_UP:
- {
- int16_t temp_pw = te_channel->pw + (int16_t)(opcode & 0xff);
- if (temp_pw < 0)
- temp_pw = 0;
- if (temp_pw > 0xfff)
- temp_pw = 0xfff;
- te_channel->pw = temp_pw;
- break;
- }
- case TE_EFFECT_PW_DOWN:
- {
- int16_t temp_pw = te_channel->pw - (int16_t)(opcode & 0xff);
- if (temp_pw < 0)
- temp_pw = 0;
- if (temp_pw > 0xfff)
- temp_pw = 0xfff;
- te_channel->pw = temp_pw;
- break;
- }
- case TE_EFFECT_SET_CUTOFF:
- {
- if (tick == 0)
- {
- te_channel->filter_cutoff = ((opcode & 0xff) << 3);
- sound_engine_filter_set_coeff(&se_channel->filter, te_channel->filter_cutoff, te_channel->filter_resonance);
- }
- break;
- }
- case TE_EFFECT_VOLUME_FADE:
- {
- if (!(te_channel->channel_flags & TEC_DISABLED))
- {
- te_channel->volume -= opcode & 0xf;
- if (te_channel->volume > MAX_ADSR_VOLUME)
- te_channel->volume = 0;
- te_channel->volume += (opcode >> 4) & 0xf;
- if (te_channel->volume > MAX_ADSR_VOLUME)
- te_channel->volume = MAX_ADSR_VOLUME;
- se_channel->adsr.volume = (int32_t)se_channel->adsr.volume * (int32_t)te_channel->volume / MAX_ADSR_VOLUME * (int32_t)te_channel->instrument->adsr.volume / MAX_ADSR_VOLUME;
- se_channel->adsr.volume = (int32_t)se_channel->adsr.volume * (int32_t)tracker_engine->master_volume / MAX_ADSR_VOLUME;
- }
- break;
- }
- case TE_EFFECT_SET_WAVEFORM:
- {
- if (tick == 0)
- {
- se_channel->waveform = (opcode & 0x3f);
- }
- break;
- }
- case TE_EFFECT_SET_VOLUME:
- {
- if (tick == 0)
- {
- if (!(te_channel->channel_flags & TEC_DISABLED))
- {
- te_channel->volume = opcode & 0xff;
- se_channel->adsr.volume = (int32_t)se_channel->adsr.volume * (int32_t)te_channel->volume / MAX_ADSR_VOLUME * (int32_t)te_channel->instrument->adsr.volume / MAX_ADSR_VOLUME;
- se_channel->adsr.volume = (int32_t)se_channel->adsr.volume * (int32_t)tracker_engine->master_volume / MAX_ADSR_VOLUME;
- }
- }
- break;
- }
- case TE_EFFECT_EXT:
- {
- switch (opcode & 0x7ff0)
- {
- case TE_EFFECT_EXT_TOGGLE_FILTER:
- {
- if (tick == 0)
- {
- if (opcode & 0xf)
- {
- se_channel->flags |= SE_ENABLE_FILTER;
- }
- else
- {
- se_channel->flags &= ~SE_ENABLE_FILTER;
- }
- }
- break;
- }
- case TE_EFFECT_EXT_PORTA_DN:
- {
- if (tick == 0)
- {
- int32_t prev = te_channel->note;
- te_channel->note -= opcode & 0xf;
- if (prev < te_channel->note)
- te_channel->note = 0;
- te_channel->target_note = te_channel->note;
- }
- break;
- }
- case TE_EFFECT_EXT_PORTA_UP:
- {
- if (tick == 0)
- {
- uint32_t prev = te_channel->note;
- te_channel->note += opcode & 0xf;
- if (prev > te_channel->note)
- te_channel->note = 0xffff;
- te_channel->target_note = te_channel->note;
- }
- break;
- }
- case TE_EFFECT_EXT_RETRIGGER:
- {
- if ((opcode & 0xf) > 0 && (tick % (opcode & 0xf)) == 0)
- {
- uint8_t prev_vol_tr = te_channel->volume;
- uint8_t prev_vol_cyd = se_channel->adsr.volume;
- tracker_engine_trigger_instrument_internal(tracker_engine, channel, te_channel->instrument, te_channel->last_note);
- te_channel->volume = prev_vol_tr;
- se_channel->adsr.volume = prev_vol_cyd;
- }
- break;
- }
- case TE_EFFECT_EXT_FINE_VOLUME_DOWN:
- {
- if (tick == 0)
- {
- te_channel->volume -= opcode & 0xf;
- if (te_channel->volume > MAX_ADSR_VOLUME)
- te_channel->volume = 0;
- se_channel->adsr.volume = (int32_t)se_channel->adsr.volume * (int32_t)te_channel->volume / MAX_ADSR_VOLUME * (int32_t)te_channel->instrument->adsr.volume / MAX_ADSR_VOLUME;
- se_channel->adsr.volume = (int32_t)se_channel->adsr.volume * (int32_t)tracker_engine->master_volume / MAX_ADSR_VOLUME;
- }
- break;
- }
- case TE_EFFECT_EXT_FINE_VOLUME_UP:
- {
- if (tick == 0)
- {
- te_channel->volume += opcode & 0xf;
- if (te_channel->volume > MAX_ADSR_VOLUME)
- te_channel->volume = MAX_ADSR_VOLUME;
- se_channel->adsr.volume = (int32_t)se_channel->adsr.volume * (int32_t)te_channel->volume / MAX_ADSR_VOLUME * (int32_t)te_channel->instrument->adsr.volume / MAX_ADSR_VOLUME;
- se_channel->adsr.volume = (int32_t)se_channel->adsr.volume * (int32_t)tracker_engine->master_volume / MAX_ADSR_VOLUME;
- }
- break;
- }
- case TE_EFFECT_EXT_NOTE_CUT:
- {
- if ((opcode & 0xf) <= tick)
- {
- se_channel->adsr.volume = 0;
- te_channel->volume = 0;
- }
- break;
- }
- case TE_EFFECT_EXT_PHASE_RESET:
- {
- if (tick == (opcode & 0xf))
- {
- se_channel->accumulator = 0;
- se_channel->lfsr = RANDOM_SEED;
- }
- break;
- }
- }
- break;
- }
- case TE_EFFECT_SET_SPEED_PROG_PERIOD:
- {
- if (tick == 0)
- {
- if (from_program)
- {
- te_channel->program_period = opcode & 0xff;
- }
- else
- {
- tracker_engine->song->speed = opcode & 0xff;
- }
- }
- break;
- }
- case TE_EFFECT_CUTOFF_UP:
- {
- te_channel->filter_cutoff += (opcode & 0xff);
- if (te_channel->filter_cutoff > 0x7ff)
- {
- te_channel->filter_cutoff = 0x7ff;
- }
- sound_engine_filter_set_coeff(&se_channel->filter, te_channel->filter_cutoff, te_channel->filter_resonance);
- break;
- }
- case TE_EFFECT_CUTOFF_DOWN:
- {
- te_channel->filter_cutoff -= (opcode & 0xff);
- if (te_channel->filter_cutoff > 0x7ff) // unsigned int overflow
- {
- te_channel->filter_cutoff = 0;
- }
- sound_engine_filter_set_coeff(&se_channel->filter, te_channel->filter_cutoff, te_channel->filter_resonance);
- break;
- }
- default:
- break;
- }
- }
|