tracker_engine.c 22 KB

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