| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600 |
- #include "instrument.h"
- #include "songinfo.h"
- void edit_instrument_param(FlizzerTrackerApp *tracker, uint8_t selected_param, int8_t delta)
- {
- if (!(tracker->current_digit))
- {
- delta *= 16;
- }
- Instrument *inst = tracker->song.instrument[tracker->current_instrument];
- switch (selected_param)
- {
- case INST_CURRENTINSTRUMENT:
- {
- int16_t inst = tracker->current_instrument;
- int8_t inst_delta = delta > 0 ? 1 : -1;
- inst += inst_delta;
- clamp(inst, 0, 0, tracker->song.num_instruments);
- if(check_and_allocate_instrument(&tracker->song, (uint8_t)inst))
- {
- tracker->current_instrument = inst;
- }
- break;
- }
- case INST_INSTRUMENTNAME:
- {
- text_input_set_header_text(tracker->text_input, "Instrument name:");
- text_input_set_result_callback(tracker->text_input, return_from_keyboard_callback, tracker, (char *)&inst->name, MUS_INST_NAME_LEN + 1, false);
- view_dispatcher_switch_to_view(tracker->view_dispatcher, VIEW_KEYBOARD);
- break;
- }
- case INST_CURRENT_NOTE:
- {
- int8_t note_delta = 0;
- if (delta < 0)
- {
- if (tracker->current_digit)
- {
- note_delta = -12;
- }
- else
- {
- note_delta = -1;
- }
- }
- if (delta > 0)
- {
- if (tracker->current_digit)
- {
- note_delta = 12;
- }
- else
- {
- note_delta = 1;
- }
- }
- clamp(inst->base_note, note_delta, 0, MAX_NOTE);
- break;
- }
- case INST_FINETUNE:
- {
- int8_t fine_delta = 0;
- if (delta < 0)
- {
- if (tracker->current_digit)
- {
- fine_delta = -1;
- }
- else
- {
- fine_delta = -10;
- }
- }
- if (delta > 0)
- {
- if (tracker->current_digit)
- {
- fine_delta = 1;
- }
- else
- {
- fine_delta = 10;
- }
- }
- inst->finetune += fine_delta;
- break;
- }
- case INST_SLIDESPEED:
- {
- if ((int16_t)inst->slide_speed + (int16_t)delta >= 0 && (int16_t)inst->slide_speed + (int16_t)delta <= 0xff)
- {
- inst->slide_speed += delta;
- }
- break;
- }
- case INST_SETPW:
- {
- flipbit(inst->flags, TE_SET_PW);
- break;
- }
- case INST_PW:
- {
- if ((int16_t)inst->pw + (int16_t)delta >= 0 && (int16_t)inst->pw + (int16_t)delta <= 0xff)
- {
- inst->pw += delta;
- }
- break;
- }
- case INST_SETCUTOFF:
- {
- flipbit(inst->flags, TE_SET_CUTOFF);
- break;
- }
- case INST_WAVE_NOISE:
- {
- flipbit(inst->waveform, SE_WAVEFORM_NOISE);
- break;
- }
- case INST_WAVE_PULSE:
- {
- flipbit(inst->waveform, SE_WAVEFORM_PULSE);
- break;
- }
- case INST_WAVE_TRIANGLE:
- {
- flipbit(inst->waveform, SE_WAVEFORM_TRIANGLE);
- break;
- }
- case INST_WAVE_SAWTOOTH:
- {
- flipbit(inst->waveform, SE_WAVEFORM_SAW);
- break;
- }
- case INST_WAVE_NOISE_METAL:
- {
- flipbit(inst->waveform, SE_WAVEFORM_NOISE_METAL);
- break;
- }
- case INST_WAVE_SINE:
- {
- flipbit(inst->waveform, SE_WAVEFORM_SINE);
- break;
- }
- case INST_ATTACK:
- {
- if ((int16_t)inst->adsr.a + (int16_t)delta >= 0 && (int16_t)inst->adsr.a + (int16_t)delta <= 0xff)
- {
- inst->adsr.a += delta;
- }
- break;
- }
- case INST_DECAY:
- {
- if ((int16_t)inst->adsr.d + (int16_t)delta >= 0 && (int16_t)inst->adsr.d + (int16_t)delta <= 0xff)
- {
- inst->adsr.d += delta;
- }
- break;
- }
- case INST_SUSTAIN:
- {
- if ((int16_t)inst->adsr.s + (int16_t)delta >= 0 && (int16_t)inst->adsr.s + (int16_t)delta <= 0xff)
- {
- inst->adsr.s += delta;
- }
- break;
- }
- case INST_RELEASE:
- {
- if ((int16_t)inst->adsr.r + (int16_t)delta >= 0 && (int16_t)inst->adsr.r + (int16_t)delta <= 0xff)
- {
- inst->adsr.r += delta;
- }
- break;
- }
- case INST_VOLUME:
- {
- if ((int16_t)inst->adsr.volume + (int16_t)delta >= 0 && (int16_t)inst->adsr.volume + (int16_t)delta <= 0xff)
- {
- inst->adsr.volume += delta;
- }
- break;
- }
- case INST_ENABLEFILTER:
- {
- flipbit(inst->sound_engine_flags, SE_ENABLE_FILTER);
- break;
- }
- case INST_FILTERCUTOFF:
- {
- if ((int16_t)inst->filter_cutoff + (int16_t)delta >= 0 && (int16_t)inst->filter_cutoff + (int16_t)delta <= 0xff)
- {
- inst->filter_cutoff += delta;
- }
- break;
- }
- case INST_FILTERRESONANCE:
- {
- if ((int16_t)inst->filter_resonance + (int16_t)delta >= 0 && (int16_t)inst->filter_resonance + (int16_t)delta <= 0xff)
- {
- inst->filter_resonance += delta;
- }
- break;
- }
- case INST_FILTERTYPE:
- {
- int8_t flt_delta = (delta > 0 ? 1 : -1);
- if ((int16_t)inst->filter_type + (int16_t)flt_delta >= 0 && (int16_t)inst->filter_type + (int16_t)flt_delta < FIL_MODES)
- {
- inst->filter_type += flt_delta;
- }
- break;
- }
- case INST_ENABLERINGMOD:
- {
- flipbit(inst->sound_engine_flags, SE_ENABLE_RING_MOD);
- break;
- }
- case INST_RINGMODSRC:
- {
- if ((int16_t)inst->ring_mod + (int16_t)delta >= 0 && (int16_t)inst->ring_mod + (int16_t)delta < SONG_MAX_CHANNELS)
- {
- inst->ring_mod += delta;
- }
- if ((int16_t)inst->ring_mod + (int16_t)delta < 0)
- {
- inst->ring_mod = 0xff; // 0xff = self
- }
- if ((int16_t)inst->ring_mod == 0xff && (int16_t)delta > 0)
- {
- inst->ring_mod = 0;
- }
- break;
- }
- case INST_ENABLEHARDSYNC:
- {
- flipbit(inst->sound_engine_flags, SE_ENABLE_HARD_SYNC);
- break;
- }
- case INST_HARDSYNCSRC:
- {
- if ((int16_t)inst->hard_sync + (int16_t)delta >= 0 && (int16_t)inst->hard_sync + (int16_t)delta < SONG_MAX_CHANNELS)
- {
- inst->hard_sync += delta;
- }
- if ((int16_t)inst->hard_sync + (int16_t)delta < 0)
- {
- inst->hard_sync = 0xff; // 0xff = self
- }
- if ((int16_t)inst->hard_sync == 0xff && (int16_t)delta > 0)
- {
- inst->hard_sync = 0;
- }
- break;
- }
- case INST_RETRIGGERONSLIDE:
- {
- flipbit(inst->flags, TE_RETRIGGER_ON_SLIDE);
- break;
- }
- case INST_ENABLEKEYSYNC:
- {
- flipbit(inst->sound_engine_flags, SE_ENABLE_KEYDOWN_SYNC);
- break;
- }
- case INST_ENABLEVIBRATO:
- {
- flipbit(inst->flags, TE_ENABLE_VIBRATO);
- break;
- }
- case INST_VIBRATOSPEED:
- {
- if ((int16_t)inst->vibrato_speed + (int16_t)delta >= 0 && (int16_t)inst->vibrato_speed + (int16_t)delta <= 0xff)
- {
- inst->vibrato_speed += delta;
- }
- break;
- }
- case INST_VIBRATODEPTH:
- {
- if ((int16_t)inst->vibrato_depth + (int16_t)delta >= 0 && (int16_t)inst->vibrato_depth + (int16_t)delta <= 0xff)
- {
- inst->vibrato_depth += delta;
- }
- break;
- }
- case INST_VIBRATODELAY:
- {
- if ((int16_t)inst->vibrato_delay + (int16_t)delta >= 0 && (int16_t)inst->vibrato_delay + (int16_t)delta <= 0xff)
- {
- inst->vibrato_delay += delta;
- }
- break;
- }
- case INST_ENABLEPWM:
- {
- flipbit(inst->flags, TE_ENABLE_PWM);
- break;
- }
- case INST_PWMSPEED:
- {
- if ((int16_t)inst->pwm_speed + (int16_t)delta >= 0 && (int16_t)inst->pwm_speed + (int16_t)delta <= 0xff)
- {
- inst->pwm_speed += delta;
- }
- break;
- }
- case INST_PWMDEPTH:
- {
- if ((int16_t)inst->pwm_depth + (int16_t)delta >= 0 && (int16_t)inst->pwm_depth + (int16_t)delta <= 0xff)
- {
- inst->pwm_depth += delta;
- }
- break;
- }
- case INST_PWMDELAY:
- {
- if ((int16_t)inst->pwm_delay + (int16_t)delta >= 0 && (int16_t)inst->pwm_delay + (int16_t)delta <= 0xff)
- {
- inst->pwm_delay += delta;
- }
- break;
- }
- case INST_PROGRESTART:
- {
- flipbit(inst->flags, TE_PROG_NO_RESTART);
- break;
- }
- }
- }
- void instrument_edit_event(FlizzerTrackerApp *tracker, FlizzerTrackerEvent *event)
- {
- if (event->input.key == InputKeyOk && event->input.type == InputTypeShort && !tracker->tracker_engine.playing)
- {
- tracker->editing = !(tracker->editing);
- return;
- }
- if (event->input.key == InputKeyOk && event->input.type == InputTypeLong && !tracker->editing)
- {
- reset_buffer(&tracker->sound_engine);
- tracker_engine_set_song(&tracker->tracker_engine, NULL);
- for(int i = 1; i < SONG_MAX_CHANNELS; i++)
- {
- tracker->tracker_engine.channel[i].channel_flags &= TEC_PLAYING;
- tracker->tracker_engine.sound_engine->channel[i].frequency = 0;
- tracker->tracker_engine.sound_engine->channel[i].waveform = 0;
- }
- Instrument *inst = tracker->song.instrument[tracker->current_instrument];
- tracker_engine_trigger_instrument_internal(&tracker->tracker_engine, 0, inst, (MIDDLE_C << 8));
- tracker->tracker_engine.playing = true;
- play();
- return;
- }
- if (event->input.key == InputKeyOk && event->input.type == InputTypeRelease && !tracker->editing)
- {
- SoundEngineChannel *se_channel = &tracker->sound_engine.channel[0];
- sound_engine_enable_gate(&tracker->sound_engine, se_channel, false);
- return;
- }
- if (event->input.key == InputKeyRight && event->input.type == InputTypeShort && tracker->editing)
- {
- switch (tracker->selected_param)
- {
- default:
- {
- tracker->current_digit++;
- if (tracker->current_digit > 1)
- {
- tracker->selected_param++;
- tracker->current_digit = 0;
- if (tracker->selected_param > INST_PARAMS - 1)
- {
- tracker->selected_param = 0;
- }
- }
- break;
- }
- case INST_CURRENTINSTRUMENT:
- case INST_INSTRUMENTNAME:
- case INST_SETPW:
- case INST_SETCUTOFF:
- case INST_WAVE_NOISE:
- case INST_WAVE_PULSE:
- case INST_WAVE_TRIANGLE:
- case INST_WAVE_SAWTOOTH:
- case INST_WAVE_NOISE_METAL:
- case INST_WAVE_SINE:
- case INST_ENABLEFILTER:
- case INST_FILTERTYPE:
- case INST_ENABLERINGMOD:
- case INST_RINGMODSRC:
- case INST_ENABLEHARDSYNC:
- case INST_HARDSYNCSRC:
- case INST_RETRIGGERONSLIDE:
- case INST_ENABLEKEYSYNC:
- case INST_ENABLEVIBRATO:
- case INST_ENABLEPWM:
- case INST_PROGRESTART:
- {
- tracker->selected_param++;
- tracker->current_digit = 1;
- if (tracker->selected_param > INST_PARAMS - 1)
- {
- tracker->selected_param = 0;
- }
- break;
- }
- }
- }
- if (event->input.key == InputKeyLeft && event->input.type == InputTypeShort && tracker->editing)
- {
- switch (tracker->selected_param)
- {
- default:
- {
- tracker->current_digit--;
- if (tracker->current_digit > 1) // unsigned int overflow
- {
- tracker->selected_param--;
- tracker->current_digit = 1;
- if (tracker->selected_param > INST_PARAMS - 1) // unsigned int overflow
- {
- tracker->selected_param = INST_PARAMS - 1;
- }
- }
- break;
- }
- case INST_CURRENTINSTRUMENT:
- case INST_INSTRUMENTNAME:
- case INST_SETPW:
- case INST_SETCUTOFF:
- case INST_WAVE_NOISE:
- case INST_WAVE_PULSE:
- case INST_WAVE_TRIANGLE:
- case INST_WAVE_SAWTOOTH:
- case INST_WAVE_NOISE_METAL:
- case INST_WAVE_SINE:
- case INST_ENABLEFILTER:
- case INST_FILTERTYPE:
- case INST_ENABLERINGMOD:
- case INST_RINGMODSRC:
- case INST_ENABLEHARDSYNC:
- case INST_HARDSYNCSRC:
- case INST_RETRIGGERONSLIDE:
- case INST_ENABLEKEYSYNC:
- case INST_ENABLEVIBRATO:
- case INST_ENABLEPWM:
- case INST_PROGRESTART:
- {
- tracker->selected_param--;
- tracker->current_digit = 1;
- if (tracker->selected_param > INST_PARAMS - 1) // unsigned int overflow
- {
- tracker->selected_param = INST_PARAMS - 1;
- }
- break;
- }
- }
- return;
- }
- if (event->input.key == InputKeyDown && event->input.type == InputTypeShort)
- {
- if (tracker->editing)
- {
- edit_instrument_param(tracker, tracker->selected_param, -1);
- }
- return;
- }
- if (event->input.key == InputKeyUp && event->input.type == InputTypeShort)
- {
- if (tracker->editing)
- {
- edit_instrument_param(tracker, tracker->selected_param, 1);
- }
- return;
- }
- if (tracker->selected_param > INST_VIBRATODELAY)
- {
- tracker->inst_editor_shift = 6;
- }
- if (tracker->selected_param > INST_PWMDELAY)
- {
- tracker->inst_editor_shift = 12;
- }
- if (tracker->selected_param < INST_CURRENT_NOTE)
- {
- tracker->inst_editor_shift = 0;
- }
- }
|