tracker_engine.c 23 KB

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