tracker_engine.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. #include "tracker_engine.h"
  2. #include "../flizzer_tracker_hal.h"
  3. #include "../macros.h"
  4. #include "../sound_engine/sound_engine_osc.h"
  5. #include <furi_hal.h>
  6. void tracker_engine_init(TrackerEngine* tracker_engine, uint8_t rate, SoundEngine* sound_engine) {
  7. memset(tracker_engine, 0, sizeof(TrackerEngine));
  8. furi_hal_interrupt_set_isr_ex(
  9. FuriHalInterruptIdTIM2, 14, tracker_engine_timer_isr, (void*)tracker_engine);
  10. tracker_engine_init_hardware(rate);
  11. tracker_engine->sound_engine = sound_engine;
  12. tracker_engine->rate = rate;
  13. }
  14. void tracker_engine_deinit_song(TrackerSong* song, bool free_song) {
  15. for(int i = 0; i < MAX_PATTERNS; i++) {
  16. if(song->pattern[i].step != NULL) {
  17. free(song->pattern[i].step);
  18. }
  19. }
  20. for(int i = 0; i < MAX_INSTRUMENTS; i++) {
  21. if(song->instrument[i] != NULL) {
  22. free(song->instrument[i]);
  23. }
  24. }
  25. if(free_song) {
  26. free(song);
  27. }
  28. }
  29. void tracker_engine_deinit(TrackerEngine* tracker_engine, bool free_song) {
  30. tracker_engine_deinit_song(tracker_engine->song, free_song);
  31. furi_hal_interrupt_set_isr_ex(FuriHalInterruptIdTIM2, 13, NULL, NULL);
  32. tracker_engine_stop();
  33. }
  34. void set_note(TrackerSongPatternStep* step, uint8_t note) {
  35. step->note &= 0x80;
  36. step->note |= (note & 0x7f);
  37. }
  38. void set_instrument(TrackerSongPatternStep* step, uint8_t inst) {
  39. step->note &= 0x7f;
  40. step->inst_vol &= 0x0f;
  41. step->note |= ((inst & 0x10) << 3);
  42. step->inst_vol |= ((inst & 0xf) << 4);
  43. }
  44. void set_volume(TrackerSongPatternStep* step, uint8_t vol) {
  45. step->command &= 0x7fff;
  46. step->inst_vol &= 0xf0;
  47. step->command |= ((vol & 0x10) << 11);
  48. step->inst_vol |= (vol & 0xf);
  49. }
  50. void set_command(TrackerSongPatternStep* step, uint16_t command) {
  51. step->command &= 0x8000;
  52. step->command |= command & (0x7fff);
  53. }
  54. void set_default_instrument(Instrument* inst) {
  55. memset(inst, 0, sizeof(Instrument));
  56. inst->flags = TE_SET_CUTOFF | TE_SET_PW | TE_ENABLE_VIBRATO;
  57. inst->sound_engine_flags = SE_ENABLE_KEYDOWN_SYNC;
  58. inst->base_note = MIDDLE_C;
  59. inst->waveform = SE_WAVEFORM_PULSE;
  60. inst->pw = 0x80;
  61. inst->adsr.a = 0x4;
  62. inst->adsr.d = 0x28;
  63. inst->adsr.volume = 0x80;
  64. inst->filter_type = FIL_OUTPUT_LOWPASS;
  65. inst->filter_cutoff = 0xff;
  66. inst->program_period = 1;
  67. for(int i = 0; i < INST_PROG_LEN; i++) {
  68. inst->program[i] = TE_PROGRAM_NOP;
  69. }
  70. inst->vibrato_speed = 0x60;
  71. inst->vibrato_depth = 0x20;
  72. inst->vibrato_delay = 0x20;
  73. }
  74. void set_empty_pattern(TrackerSongPattern* pattern, uint16_t pattern_length) {
  75. for(uint16_t i = 0; i < pattern_length; i++) {
  76. TrackerSongPatternStep* step = &pattern->step[i];
  77. set_note(step, MUS_NOTE_NONE);
  78. set_instrument(step, MUS_NOTE_INSTRUMENT_NONE);
  79. set_volume(step, MUS_NOTE_VOLUME_NONE);
  80. set_command(step, 0);
  81. }
  82. }
  83. uint8_t tracker_engine_get_note(TrackerSongPatternStep* step) {
  84. return (step->note & 0x7f);
  85. }
  86. uint8_t tracker_engine_get_instrument(TrackerSongPatternStep* step) {
  87. return ((step->note & 0x80) >> 3) | ((step->inst_vol & 0xf0) >> 4);
  88. }
  89. uint8_t tracker_engine_get_volume(TrackerSongPatternStep* step) {
  90. return (step->inst_vol & 0xf) | ((step->command & 0x8000) >> 11);
  91. }
  92. uint16_t tracker_engine_get_command(TrackerSongPatternStep* step) {
  93. return (step->command & 0x7fff);
  94. }
  95. void tracker_engine_set_note(
  96. TrackerEngine* tracker_engine,
  97. uint8_t chan,
  98. uint16_t note,
  99. bool update_note) {
  100. if(update_note) tracker_engine->channel[chan].note = note;
  101. sound_engine_set_channel_frequency(
  102. tracker_engine->sound_engine, &tracker_engine->sound_engine->channel[chan], note);
  103. }
  104. void tracker_engine_set_song(TrackerEngine* tracker_engine, TrackerSong* song) {
  105. tracker_engine->song = song;
  106. }
  107. void tracker_engine_trigger_instrument_internal(
  108. TrackerEngine* tracker_engine,
  109. uint8_t chan,
  110. Instrument* pinst,
  111. uint16_t note) {
  112. SoundEngineChannel* se_channel = &tracker_engine->sound_engine->channel[chan];
  113. TrackerEngineChannel* te_channel = &tracker_engine->channel[chan];
  114. te_channel->channel_flags = TEC_PLAYING | (te_channel->channel_flags & TEC_DISABLED);
  115. te_channel->program_period = pinst->program_period;
  116. if(!(pinst->flags & TE_PROG_NO_RESTART) && pinst->program_period > 0) {
  117. te_channel->channel_flags |= TEC_PROGRAM_RUNNING;
  118. te_channel->program_counter = 0;
  119. te_channel->program_loop = 1;
  120. te_channel->program_tick = 0;
  121. }
  122. te_channel->instrument = pinst;
  123. se_channel->waveform = pinst->waveform;
  124. se_channel->flags = pinst->sound_engine_flags;
  125. te_channel->flags = pinst->flags;
  126. te_channel->arpeggio_note = 0;
  127. te_channel->fixed_note = 0xffff;
  128. note += (uint16_t)(((int16_t)pinst->base_note - MIDDLE_C) << 8);
  129. tracker_engine_set_note(tracker_engine, chan, note + (int16_t)pinst->finetune, true);
  130. te_channel->last_note = te_channel->target_note = note + (int16_t)pinst->finetune;
  131. te_channel->extarp1 = te_channel->extarp2 = 0;
  132. if(pinst->flags & TE_ENABLE_VIBRATO) {
  133. te_channel->vibrato_speed = pinst->vibrato_speed;
  134. te_channel->vibrato_depth = pinst->vibrato_depth;
  135. te_channel->vibrato_delay = pinst->vibrato_delay;
  136. }
  137. if(pinst->flags & TE_ENABLE_PWM) {
  138. te_channel->pwm_speed = pinst->pwm_speed;
  139. te_channel->pwm_depth = pinst->pwm_depth;
  140. te_channel->pwm_delay = pinst->pwm_delay;
  141. }
  142. if(pinst->sound_engine_flags & SE_ENABLE_KEYDOWN_SYNC) {
  143. te_channel->vibrato_position = ((ACC_LENGTH / 2 / 2) << 9);
  144. te_channel->pwm_position = ((ACC_LENGTH / 2 / 2) << 9);
  145. se_channel->accumulator = 0;
  146. se_channel->lfsr = RANDOM_SEED;
  147. }
  148. if(pinst->flags & TE_SET_CUTOFF) {
  149. te_channel->filter_cutoff = ((uint16_t)pinst->filter_cutoff << 3);
  150. te_channel->filter_resonance = (uint16_t)pinst->filter_resonance;
  151. se_channel->filter.low = 0;
  152. se_channel->filter.high = 0;
  153. se_channel->filter.band = 0;
  154. sound_engine_filter_set_coeff(
  155. &se_channel->filter, te_channel->filter_cutoff, te_channel->filter_resonance);
  156. }
  157. if(pinst->sound_engine_flags & SE_ENABLE_FILTER) {
  158. te_channel->filter_type = pinst->filter_type;
  159. se_channel->filter_mode = te_channel->filter_type;
  160. }
  161. if(pinst->flags & TE_SET_PW) {
  162. te_channel->pw = (pinst->pw << 4);
  163. se_channel->pw = (pinst->pw << 4);
  164. }
  165. se_channel->ring_mod = pinst->ring_mod;
  166. se_channel->hard_sync = pinst->hard_sync;
  167. te_channel->slide_speed = pinst->slide_speed;
  168. se_channel->adsr.a = pinst->adsr.a;
  169. se_channel->adsr.d = pinst->adsr.d;
  170. se_channel->adsr.s = pinst->adsr.s;
  171. se_channel->adsr.r = pinst->adsr.r;
  172. se_channel->adsr.volume = pinst->adsr.volume;
  173. se_channel->adsr.volume = (int32_t)se_channel->adsr.volume *
  174. (int32_t)tracker_engine->master_volume / MAX_ADSR_VOLUME;
  175. te_channel->volume = pinst->adsr.volume;
  176. te_channel->volume =
  177. (int32_t)te_channel->volume * (int32_t)tracker_engine->master_volume / MAX_ADSR_VOLUME;
  178. sound_engine_enable_gate(
  179. tracker_engine->sound_engine, &tracker_engine->sound_engine->channel[chan], true);
  180. }
  181. void tracker_engine_execute_track_command(
  182. TrackerEngine* tracker_engine,
  183. uint8_t chan,
  184. TrackerSongPatternStep* step,
  185. bool first_tick) {
  186. UNUSED(first_tick);
  187. UNUSED(tracker_engine);
  188. UNUSED(chan);
  189. uint8_t vol = tracker_engine_get_volume(step);
  190. uint16_t opcode = tracker_engine_get_command(step);
  191. if(vol != MUS_NOTE_VOLUME_NONE &&
  192. !(tracker_engine->channel[chan].channel_flags & TEC_DISABLED)) {
  193. tracker_engine->sound_engine->channel[chan].adsr.volume =
  194. (int32_t)tracker_engine->channel[chan].volume * (int32_t)vol / (MUS_NOTE_VOLUME_NONE);
  195. // tracker_engine->sound_engine->channel[chan].adsr.volume = (int32_t)tracker_engine->sound_engine->channel[chan].adsr.volume * (int32_t)tracker_engine->channel[chan].instrument->adsr.volume / MAX_ADSR_VOLUME * (int32_t)tracker_engine->master_volume / MAX_ADSR_VOLUME;
  196. }
  197. if(tracker_engine->channel[chan].instrument != NULL && opcode != 0) {
  198. if((opcode & 0x7f00) == TE_EFFECT_ARPEGGIO) {
  199. tracker_engine->channel[chan].extarp1 = ((opcode & 0xf0) >> 4);
  200. tracker_engine->channel[chan].extarp2 = (opcode & 0xf);
  201. }
  202. else {
  203. do_command(opcode, tracker_engine, chan, tracker_engine->current_tick, false);
  204. }
  205. }
  206. if(tracker_engine->channel[chan].channel_flags & TEC_DISABLED) {
  207. tracker_engine->sound_engine->channel[chan].adsr.volume = 0;
  208. }
  209. }
  210. void tracker_engine_execute_program_tick(
  211. TrackerEngine* tracker_engine,
  212. uint8_t chan,
  213. uint8_t advance) {
  214. TrackerEngineChannel* te_channel = &tracker_engine->channel[chan];
  215. uint8_t tick = te_channel->program_tick;
  216. uint8_t visited[INST_PROG_LEN] = {0};
  217. do_it_again:;
  218. const uint16_t inst = te_channel->instrument->program[tick];
  219. if((inst & 0x7fff) == TE_PROGRAM_END) {
  220. te_channel->channel_flags &= ~(TEC_PROGRAM_RUNNING);
  221. return;
  222. }
  223. uint8_t dont_reloop = 0;
  224. if((inst & 0x7fff) != TE_PROGRAM_NOP) {
  225. switch(inst & 0x7f00) {
  226. case TE_PROGRAM_JUMP: {
  227. if(!visited[tick]) {
  228. visited[tick] = 1;
  229. tick = inst & (INST_PROG_LEN - 1);
  230. }
  231. else
  232. return;
  233. break;
  234. }
  235. case TE_PROGRAM_LOOP_BEGIN:
  236. break;
  237. case TE_PROGRAM_LOOP_END: {
  238. if(te_channel->program_loop == (inst & 0xff)) {
  239. if(advance) te_channel->program_loop = 1;
  240. }
  241. else {
  242. if(advance) ++te_channel->program_loop;
  243. uint8_t l = 0;
  244. while((te_channel->instrument->program[tick] & 0x7f00) != TE_PROGRAM_LOOP_BEGIN &&
  245. tick > 0) {
  246. --tick;
  247. if(!(te_channel->instrument->program[tick] & 0x8000)) ++l;
  248. }
  249. --tick;
  250. dont_reloop = l <= 1;
  251. }
  252. break;
  253. }
  254. default: {
  255. do_command(inst, tracker_engine, chan, te_channel->program_counter, true);
  256. break;
  257. }
  258. }
  259. }
  260. if((inst & 0x7fff) == TE_PROGRAM_NOP || (inst & 0x7f00) != TE_PROGRAM_JUMP) {
  261. ++tick;
  262. if(tick >= INST_PROG_LEN) {
  263. tick = 0;
  264. }
  265. }
  266. // skip to next on msb
  267. if(((inst & 0x8000) || ((inst & 0x7f00) == TE_PROGRAM_LOOP_BEGIN) ||
  268. ((inst & 0x7f00) == TE_PROGRAM_JUMP)) &&
  269. (inst & 0x7fff) != TE_PROGRAM_NOP && !dont_reloop) {
  270. goto do_it_again;
  271. }
  272. if(advance) {
  273. te_channel->program_tick = tick;
  274. }
  275. }
  276. void tracker_engine_advance_channel(TrackerEngine* tracker_engine, uint8_t chan) {
  277. SoundEngineChannel* se_channel = &tracker_engine->sound_engine->channel[chan];
  278. TrackerEngineChannel* te_channel = &tracker_engine->channel[chan];
  279. if(te_channel->channel_flags & TEC_PLAYING) {
  280. if(!(se_channel->flags & SE_ENABLE_GATE)) {
  281. te_channel->flags &= ~(TEC_PLAYING);
  282. }
  283. if(te_channel->slide_speed != 0) {
  284. if(te_channel->target_note > te_channel->note) {
  285. te_channel->note += my_min(
  286. te_channel->slide_speed * 4, te_channel->target_note - te_channel->note);
  287. }
  288. else if(te_channel->target_note < te_channel->note) {
  289. te_channel->note -= my_min(
  290. te_channel->slide_speed * 4, te_channel->note - te_channel->target_note);
  291. }
  292. }
  293. if(te_channel->channel_flags & TEC_PROGRAM_RUNNING) {
  294. uint8_t u = (te_channel->program_counter + 1) >= te_channel->program_period;
  295. tracker_engine_execute_program_tick(tracker_engine, chan, u);
  296. ++te_channel->program_counter;
  297. if(u) te_channel->program_counter = 0;
  298. }
  299. int16_t vib = 0;
  300. int32_t pwm = 0;
  301. if(te_channel->flags & TE_ENABLE_VIBRATO) {
  302. if(te_channel->vibrato_delay > 0) {
  303. te_channel->vibrato_delay--;
  304. }
  305. else {
  306. te_channel->vibrato_position += ((uint32_t)te_channel->vibrato_speed << 21);
  307. vib =
  308. (int32_t)(sound_engine_triangle(te_channel->vibrato_position >> 9) - WAVE_AMP / 2) *
  309. (int32_t)te_channel->vibrato_depth / (256 * 128);
  310. }
  311. }
  312. if(te_channel->flags & TE_ENABLE_PWM) {
  313. if(te_channel->pwm_delay > 0) {
  314. te_channel->pwm_delay--;
  315. }
  316. else {
  317. te_channel->pwm_position +=
  318. ((uint32_t)te_channel->pwm_speed
  319. << 20); // so minimum PWM speed is even lower than minimum vibrato speed
  320. pwm = ((int32_t)sound_engine_triangle((te_channel->pwm_position) >> 9) -
  321. WAVE_AMP / 2) *
  322. (int32_t)te_channel->pwm_depth / (256 * 16);
  323. }
  324. int16_t final_pwm = (int16_t)tracker_engine->channel[chan].pw + pwm;
  325. if(final_pwm < 0) {
  326. final_pwm = 0;
  327. }
  328. if(final_pwm > 0xfff) {
  329. final_pwm = 0xfff;
  330. }
  331. tracker_engine->sound_engine->channel[chan].pw = final_pwm;
  332. }
  333. else {
  334. tracker_engine->sound_engine->channel[chan].pw = tracker_engine->channel[chan].pw;
  335. }
  336. int32_t chn_note =
  337. (int16_t)(te_channel->fixed_note != 0xffff ? te_channel->fixed_note : te_channel->note) +
  338. vib + ((int16_t)te_channel->arpeggio_note << 8);
  339. if(chn_note < 0) {
  340. chn_note = 0;
  341. }
  342. if(chn_note > ((12 * 7 + 11) << 8)) {
  343. chn_note = ((12 * 7 + 11) << 8); // highest note is B-7
  344. }
  345. tracker_engine_set_note(tracker_engine, chan, (uint16_t)chn_note, false);
  346. }
  347. if(tracker_engine->channel[chan].channel_flags &
  348. TEC_DISABLED) // so we can't set some non-zero volme from inst program too
  349. {
  350. tracker_engine->sound_engine->channel[chan].adsr.volume = 0;
  351. }
  352. }
  353. void tracker_engine_advance_tick(TrackerEngine* tracker_engine) {
  354. if(!(tracker_engine->playing)) return;
  355. if(!(tracker_engine->sound_engine)) return;
  356. TrackerSong* song = tracker_engine->song;
  357. uint16_t opcode = 0;
  358. for(uint8_t chan = 0; chan < SONG_MAX_CHANNELS; chan++) {
  359. SoundEngineChannel* se_channel = &tracker_engine->sound_engine->channel[chan];
  360. TrackerEngineChannel* te_channel = &tracker_engine->channel[chan];
  361. if(tracker_engine->song) {
  362. uint16_t sequence_position = tracker_engine->sequence_position;
  363. uint8_t current_pattern =
  364. song->sequence.sequence_step[sequence_position].pattern_indices[chan];
  365. uint8_t pattern_step = tracker_engine->pattern_position;
  366. TrackerSongPattern* pattern = &song->pattern[current_pattern];
  367. uint8_t note_delay = 0;
  368. opcode = tracker_engine_get_command(&pattern->step[pattern_step]);
  369. if((opcode & 0x7ff0) == TE_EFFECT_EXT_NOTE_DELAY) {
  370. note_delay = (opcode & 0xf);
  371. }
  372. if(tracker_engine->current_tick == note_delay) {
  373. uint8_t note = tracker_engine_get_note(&pattern->step[pattern_step]);
  374. uint8_t inst = tracker_engine_get_instrument(&pattern->step[pattern_step]);
  375. Instrument* pinst = NULL;
  376. if(inst == MUS_NOTE_INSTRUMENT_NONE) {
  377. pinst = te_channel->instrument;
  378. }
  379. else {
  380. if(inst < song->num_instruments) {
  381. pinst = song->instrument[inst];
  382. te_channel->instrument = pinst;
  383. }
  384. }
  385. if(note == MUS_NOTE_CUT) {
  386. sound_engine_enable_gate(tracker_engine->sound_engine, se_channel, 0);
  387. se_channel->adsr.volume = 0;
  388. te_channel->volume = 0;
  389. }
  390. if(note == MUS_NOTE_RELEASE) {
  391. sound_engine_enable_gate(tracker_engine->sound_engine, se_channel, 0);
  392. }
  393. else if(
  394. pinst && note != MUS_NOTE_RELEASE && note != MUS_NOTE_CUT &&
  395. note != MUS_NOTE_NONE) {
  396. uint8_t prev_adsr_volume = se_channel->adsr.volume;
  397. if((opcode & 0x7f00) == TE_EFFECT_SLIDE) {
  398. if(pinst->flags & TE_RETRIGGER_ON_SLIDE) {
  399. uint16_t temp_note = te_channel->note;
  400. tracker_engine_trigger_instrument_internal(
  401. tracker_engine, chan, pinst, note << 8);
  402. te_channel->note = temp_note;
  403. }
  404. te_channel->target_note =
  405. ((note + pinst->base_note - MIDDLE_C) << 8) + pinst->finetune;
  406. te_channel->slide_speed = (opcode & 0xff);
  407. }
  408. else if((opcode & 0x7f00) == TE_EFFECT_LEGATO) {
  409. te_channel->note = te_channel->target_note = te_channel->last_note =
  410. ((note + pinst->base_note - MIDDLE_C) << 8) + pinst->finetune;
  411. }
  412. else {
  413. tracker_engine_trigger_instrument_internal(
  414. tracker_engine, chan, pinst, note << 8);
  415. te_channel->note =
  416. ((note + pinst->base_note - MIDDLE_C) << 8) + pinst->finetune;
  417. te_channel->target_note =
  418. ((note + pinst->base_note - MIDDLE_C) << 8) + pinst->finetune;
  419. }
  420. if(inst == MUS_NOTE_INSTRUMENT_NONE) {
  421. se_channel->adsr.volume = prev_adsr_volume;
  422. }
  423. }
  424. }
  425. tracker_engine_execute_track_command(
  426. tracker_engine,
  427. chan,
  428. &pattern->step[pattern_step],
  429. tracker_engine->current_tick == note_delay);
  430. }
  431. tracker_engine_advance_channel(
  432. tracker_engine,
  433. chan); // this will be executed even if the song pointer is NULL; handy for live instrument playback from inst editor ("jams")
  434. }
  435. if(tracker_engine->song) {
  436. tracker_engine->current_tick++;
  437. if(tracker_engine->current_tick >= song->speed) {
  438. bool flag = true;
  439. for(int chan = 0; chan < SONG_MAX_CHANNELS; ++chan) {
  440. uint16_t sequence_position = tracker_engine->sequence_position;
  441. uint8_t current_pattern =
  442. song->sequence.sequence_step[sequence_position].pattern_indices[chan];
  443. uint8_t pattern_step = tracker_engine->pattern_position;
  444. TrackerSongPattern* pattern = &song->pattern[current_pattern];
  445. opcode = tracker_engine_get_command(&pattern->step[pattern_step]);
  446. if((opcode & 0x7ff0) == TE_EFFECT_EXT_PATTERN_LOOP) {
  447. if(opcode & 0xf) // loop end
  448. {
  449. if(!(tracker_engine->in_loop)) {
  450. tracker_engine->loops_left = (opcode & 0xf);
  451. tracker_engine->in_loop = true;
  452. for(int j = tracker_engine->pattern_position; j >= 0; j--) {
  453. if(tracker_engine_get_command(&pattern->step[j]) ==
  454. TE_EFFECT_EXT_PATTERN_LOOP) // search for loop start
  455. {
  456. tracker_engine->pattern_position =
  457. fmax((int16_t)j - 1, 0); // jump to loop start
  458. goto out;
  459. }
  460. }
  461. }
  462. else {
  463. tracker_engine->loops_left--;
  464. if(tracker_engine->loops_left == 0) {
  465. tracker_engine->in_loop = false;
  466. goto out;
  467. }
  468. for(int j = tracker_engine->pattern_position; j >= 0; j--) {
  469. if(tracker_engine_get_command(&pattern->step[j]) ==
  470. TE_EFFECT_EXT_PATTERN_LOOP) // search for loop start
  471. {
  472. tracker_engine->pattern_position =
  473. fmax((int16_t)j - 1, 0); // jump to loop start
  474. goto out;
  475. }
  476. }
  477. }
  478. }
  479. else // loop start
  480. {
  481. }
  482. out:;
  483. }
  484. if((opcode & 0x7f00) == TE_EFFECT_SKIP_PATTERN) {
  485. tracker_engine->sequence_position++;
  486. tracker_engine->pattern_position = 0;
  487. flag = false;
  488. if(tracker_engine->sequence_position >= song->num_sequence_steps) {
  489. tracker_engine->playing = false;
  490. tracker_engine->sequence_position--;
  491. tracker_engine->pattern_position = song->pattern_length - 1;
  492. for(int i = 0; i < SONG_MAX_CHANNELS; i++) {
  493. sound_engine_enable_gate(
  494. tracker_engine->sound_engine,
  495. &tracker_engine->sound_engine->channel[i],
  496. false);
  497. }
  498. goto end_process;
  499. }
  500. }
  501. }
  502. if(flag) {
  503. tracker_engine->pattern_position++;
  504. }
  505. tracker_engine->current_tick = 0;
  506. if(tracker_engine->pattern_position >= song->pattern_length) {
  507. tracker_engine->pattern_position = 0;
  508. if(song->loop_start != 0 || song->loop_end != 0) {
  509. if(tracker_engine->sequence_position == song->loop_end) {
  510. tracker_engine->sequence_position =
  511. song->loop_start; // infinite loop between loop start and loop end
  512. }
  513. else {
  514. tracker_engine->sequence_position++;
  515. }
  516. }
  517. else {
  518. tracker_engine->sequence_position++;
  519. }
  520. if(tracker_engine->sequence_position >= song->num_sequence_steps) {
  521. tracker_engine->playing = false;
  522. tracker_engine->sequence_position--;
  523. tracker_engine->pattern_position = song->pattern_length - 1;
  524. for(int i = 0; i < SONG_MAX_CHANNELS; i++) {
  525. sound_engine_enable_gate(
  526. tracker_engine->sound_engine,
  527. &tracker_engine->sound_engine->channel[i],
  528. false);
  529. }
  530. }
  531. }
  532. }
  533. }
  534. end_process:;
  535. }