zero_tracker.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #include <furi.h>
  2. #include "speaker_hal.h"
  3. #include "zero_tracker.h"
  4. /**
  5. * @brief Note record
  6. *
  7. * AH AL
  8. * FEDCBA98 76543210
  9. * nnnnnnee eedddddd
  10. * -------- --------
  11. * nnnnnn = [0] do nothing, [1..60] note number, [61] note off, [62..63] not used
  12. * ee ee = [0..F] effect
  13. * 111222 = [0..63] or [0..7, 0..7] effect data
  14. */
  15. typedef uint16_t NoteRecord;
  16. #define NOTE_NONE 0
  17. #define NOTE_C2 1
  18. #define NOTE_Cs2 2
  19. #define NOTE_D2 3
  20. #define NOTE_Ds2 4
  21. #define NOTE_E2 5
  22. #define NOTE_F2 6
  23. #define NOTE_Fs2 7
  24. #define NOTE_G2 8
  25. #define NOTE_Gs2 9
  26. #define NOTE_A2 10
  27. #define NOTE_As2 11
  28. #define NOTE_B2 12
  29. #define NOTE_C3 13
  30. #define NOTE_Cs3 14
  31. #define NOTE_D3 15
  32. #define NOTE_Ds3 16
  33. #define NOTE_E3 17
  34. #define NOTE_F3 18
  35. #define NOTE_Fs3 19
  36. #define NOTE_G3 20
  37. #define NOTE_Gs3 21
  38. #define NOTE_A3 22
  39. #define NOTE_As3 23
  40. #define NOTE_B3 24
  41. #define NOTE_C4 25
  42. #define NOTE_Cs4 26
  43. #define NOTE_D4 27
  44. #define NOTE_Ds4 28
  45. #define NOTE_E4 29
  46. #define NOTE_F4 30
  47. #define NOTE_Fs4 31
  48. #define NOTE_G4 32
  49. #define NOTE_Gs4 33
  50. #define NOTE_A4 34
  51. #define NOTE_As4 35
  52. #define NOTE_B4 36
  53. #define NOTE_C5 37
  54. #define NOTE_Cs5 38
  55. #define NOTE_D5 39
  56. #define NOTE_Ds5 40
  57. #define NOTE_E5 41
  58. #define NOTE_F5 42
  59. #define NOTE_Fs5 43
  60. #define NOTE_G5 44
  61. #define NOTE_Gs5 45
  62. #define NOTE_A5 46
  63. #define NOTE_As5 47
  64. #define NOTE_B5 48
  65. #define NOTE_C6 49
  66. #define NOTE_Cs6 50
  67. #define NOTE_D6 51
  68. #define NOTE_Ds6 52
  69. #define NOTE_E6 53
  70. #define NOTE_F6 54
  71. #define NOTE_Fs6 55
  72. #define NOTE_G6 56
  73. #define NOTE_Gs6 57
  74. #define NOTE_A6 58
  75. #define NOTE_As6 59
  76. #define NOTE_B6 60
  77. #define NOTE_OFF 63
  78. typedef enum {
  79. EffectArpeggio = 0x00,
  80. EffectSlideUp = 0x01,
  81. EffectSlideDown = 0x02,
  82. EffectSlideToNote = 0x03,
  83. EffectVibrato = 0x04,
  84. } Effect;
  85. #define EFFECT_DATA_NONE 0
  86. #define EFFECT_DATA_2(x, y) ((x) | ((y) << 3))
  87. #define FREQUENCY_UNSET -1.0f
  88. uint8_t record_get_note(NoteRecord note) {
  89. return note & 0x3F;
  90. }
  91. uint8_t record_get_effect(NoteRecord note) {
  92. return (note >> 6) & 0xF;
  93. }
  94. uint8_t record_get_effect_data(NoteRecord note) {
  95. return (note >> 10) & 0x3F;
  96. }
  97. #define RECORD_MAKE(note, effect, data) \
  98. ((NoteRecord)(((note)&0x3F) | (((effect)&0xF) << 6) | (((data)&0x3F) << 10)))
  99. #define NOTES_PER_OCT 12
  100. const float notes_oct[NOTES_PER_OCT] = {
  101. 130.813f,
  102. 138.591f,
  103. 146.832f,
  104. 155.563f,
  105. 164.814f,
  106. 174.614f,
  107. 184.997f,
  108. 195.998f,
  109. 207.652f,
  110. 220.00f,
  111. 233.082f,
  112. 246.942f,
  113. };
  114. float note_to_freq(uint8_t note) {
  115. if(note == NOTE_NONE) return 0.0f;
  116. note = note - NOTE_C2;
  117. uint8_t octave = note / NOTES_PER_OCT;
  118. uint8_t note_in_oct = note % NOTES_PER_OCT;
  119. return notes_oct[note_in_oct] * (1 << octave);
  120. }
  121. float frequency_offset(float frequency, uint8_t semitones) {
  122. return frequency * (1.0f + ((1.0f / 12.0f) * semitones));
  123. }
  124. #define PATTERN_SIZE 64
  125. typedef struct {
  126. NoteRecord notes[PATTERN_SIZE];
  127. } NoteRow;
  128. typedef struct {
  129. uint8_t row_count;
  130. NoteRow* rows;
  131. } NotePattern;
  132. NoteRow _row = {
  133. .notes =
  134. {
  135. //
  136. RECORD_MAKE(NOTE_A4, 0, 0),
  137. RECORD_MAKE(NOTE_C3, 0, 0),
  138. RECORD_MAKE(NOTE_F2, 0, 0),
  139. RECORD_MAKE(NOTE_C3, 0, 0),
  140. //
  141. RECORD_MAKE(NOTE_E4, 0, 0),
  142. RECORD_MAKE(NOTE_C3, 0, 0),
  143. RECORD_MAKE(NOTE_E4, 0, 0),
  144. RECORD_MAKE(NOTE_OFF, 0, 0),
  145. //
  146. RECORD_MAKE(NOTE_A4, 0, 0),
  147. RECORD_MAKE(NOTE_A4, 0, 0),
  148. RECORD_MAKE(NOTE_A4, 0, 0),
  149. RECORD_MAKE(NOTE_OFF, 0, 0),
  150. //
  151. RECORD_MAKE(NOTE_E5, 0, 0),
  152. RECORD_MAKE(NOTE_E5, 0, 0),
  153. RECORD_MAKE(NOTE_E5, 0, 0),
  154. RECORD_MAKE(NOTE_OFF, 0, 0),
  155. //
  156. RECORD_MAKE(NOTE_D5, 0, 0),
  157. RECORD_MAKE(NOTE_C3, 0, 0),
  158. RECORD_MAKE(NOTE_F2, 0, 0),
  159. RECORD_MAKE(NOTE_C3, 0, 0),
  160. //
  161. RECORD_MAKE(NOTE_C5, 0, 0),
  162. RECORD_MAKE(NOTE_C3, 0, 0),
  163. RECORD_MAKE(NOTE_C5, 0, 0),
  164. RECORD_MAKE(NOTE_OFF, 0, 0),
  165. //
  166. RECORD_MAKE(NOTE_A4, 0, 0),
  167. RECORD_MAKE(0, 0, 0),
  168. RECORD_MAKE(0, 0, 0),
  169. RECORD_MAKE(0, 0, 0),
  170. //
  171. RECORD_MAKE(NOTE_A4, 0, 0),
  172. RECORD_MAKE(NOTE_A4, 0, 0),
  173. RECORD_MAKE(NOTE_A4, 0, 0),
  174. RECORD_MAKE(NOTE_OFF, 0, 0),
  175. //
  176. RECORD_MAKE(NOTE_B4, 0, 0),
  177. RECORD_MAKE(NOTE_D3, 0, 0),
  178. RECORD_MAKE(NOTE_G2, 0, 0),
  179. RECORD_MAKE(NOTE_D3, 0, 0),
  180. //
  181. RECORD_MAKE(NOTE_E4, 0, 0),
  182. RECORD_MAKE(NOTE_D3, 0, 0),
  183. RECORD_MAKE(NOTE_E4, 0, 0),
  184. RECORD_MAKE(NOTE_OFF, 0, 0),
  185. //
  186. RECORD_MAKE(NOTE_A4, 0, 0),
  187. RECORD_MAKE(NOTE_A4, 0, 0),
  188. RECORD_MAKE(NOTE_A4, 0, 0),
  189. RECORD_MAKE(NOTE_OFF, 0, 0),
  190. //
  191. RECORD_MAKE(NOTE_E5, 0, 0),
  192. RECORD_MAKE(NOTE_E5, 0, 0),
  193. RECORD_MAKE(NOTE_E5, 0, 0),
  194. RECORD_MAKE(NOTE_OFF, 0, 0),
  195. //
  196. RECORD_MAKE(NOTE_D5, 0, 0),
  197. RECORD_MAKE(NOTE_D3, 0, 0),
  198. RECORD_MAKE(NOTE_G2, 0, 0),
  199. RECORD_MAKE(NOTE_D3, 0, 0),
  200. //
  201. RECORD_MAKE(NOTE_C5, 0, 0),
  202. RECORD_MAKE(NOTE_D3, 0, 0),
  203. RECORD_MAKE(NOTE_C5, 0, 0),
  204. RECORD_MAKE(NOTE_OFF, 0, 0),
  205. //
  206. RECORD_MAKE(NOTE_A4, 0, 0),
  207. RECORD_MAKE(0, 0, 0),
  208. RECORD_MAKE(0, 0, 0),
  209. RECORD_MAKE(0, 0, 0),
  210. //
  211. RECORD_MAKE(NOTE_A4, 0, 0),
  212. RECORD_MAKE(NOTE_A4, 0, 0),
  213. RECORD_MAKE(NOTE_A4, 0, 0),
  214. RECORD_MAKE(NOTE_OFF, 0, 0),
  215. },
  216. };
  217. const uint8_t test = 0x20;
  218. NoteRow row = {
  219. .notes =
  220. {
  221. // 1/4
  222. RECORD_MAKE(NOTE_C3, EffectArpeggio, EFFECT_DATA_2(3, 5)),
  223. RECORD_MAKE(0, EffectArpeggio, EFFECT_DATA_2(3, 5)),
  224. RECORD_MAKE(NOTE_C4, EffectSlideToNote, test),
  225. RECORD_MAKE(0, EffectSlideToNote, test),
  226. //
  227. RECORD_MAKE(0, EffectSlideToNote, test),
  228. RECORD_MAKE(0, EffectSlideToNote, test),
  229. RECORD_MAKE(0, EffectSlideToNote, test),
  230. RECORD_MAKE(0, EffectSlideToNote, test),
  231. //
  232. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(1, 1)),
  233. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(1, 1)),
  234. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(1, 1)),
  235. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(1, 1)),
  236. //
  237. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(2, 2)),
  238. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(2, 2)),
  239. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(2, 2)),
  240. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(2, 2)),
  241. // 2/4
  242. RECORD_MAKE(NOTE_C3, EffectSlideDown, 0x20),
  243. RECORD_MAKE(0, EffectSlideDown, 0x20),
  244. RECORD_MAKE(NOTE_C4, EffectVibrato, EFFECT_DATA_2(3, 3)),
  245. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  246. //
  247. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  248. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  249. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  250. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  251. //
  252. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  253. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  254. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  255. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  256. //
  257. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  258. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  259. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  260. RECORD_MAKE(NOTE_OFF, EffectVibrato, EFFECT_DATA_2(3, 3)),
  261. // 3/4
  262. RECORD_MAKE(NOTE_C3, EffectArpeggio, EFFECT_DATA_2(3, 5)),
  263. RECORD_MAKE(0, EffectArpeggio, EFFECT_DATA_2(3, 5)),
  264. RECORD_MAKE(NOTE_OFF, 0, 0),
  265. RECORD_MAKE(0, 0, 0),
  266. //
  267. RECORD_MAKE(0, 0, 0),
  268. RECORD_MAKE(0, 0, 0),
  269. RECORD_MAKE(0, 0, 0),
  270. RECORD_MAKE(0, 0, 0),
  271. //
  272. RECORD_MAKE(NOTE_C2, 0, 0),
  273. RECORD_MAKE(0, 0, 0),
  274. RECORD_MAKE(0, 0, 0),
  275. RECORD_MAKE(NOTE_OFF, 0, 0),
  276. //
  277. RECORD_MAKE(0, 0, 0),
  278. RECORD_MAKE(0, 0, 0),
  279. RECORD_MAKE(0, 0, 0),
  280. RECORD_MAKE(0, 0, 0),
  281. // 4/4
  282. RECORD_MAKE(NOTE_C3, EffectSlideDown, 0x20),
  283. RECORD_MAKE(0, EffectSlideDown, 0x20),
  284. RECORD_MAKE(0, EffectSlideDown, 0x20),
  285. RECORD_MAKE(NOTE_OFF, 0, 0),
  286. //
  287. RECORD_MAKE(0, 0, 0),
  288. RECORD_MAKE(0, 0, 0),
  289. RECORD_MAKE(0, 0, 0),
  290. RECORD_MAKE(0, 0, 0),
  291. //
  292. RECORD_MAKE(NOTE_C2, 0, 0),
  293. RECORD_MAKE(0, 0, 0),
  294. RECORD_MAKE(0, 0, 0),
  295. RECORD_MAKE(NOTE_OFF, 0, 0),
  296. //
  297. RECORD_MAKE(0, 0, 0),
  298. RECORD_MAKE(0, 0, 0),
  299. RECORD_MAKE(0, 0, 0),
  300. RECORD_MAKE(0, 0, 0),
  301. },
  302. };
  303. typedef struct {
  304. uint8_t tick;
  305. uint8_t tick_limit;
  306. uint8_t row;
  307. } SongState;
  308. SongState song_state = {
  309. .tick = 0,
  310. .tick_limit = 2,
  311. .row = 0,
  312. };
  313. typedef struct {
  314. uint8_t speed;
  315. uint8_t depth;
  316. int8_t direction;
  317. int8_t value;
  318. } Vibrato;
  319. typedef struct {
  320. float frequency;
  321. float frequency_target;
  322. bool play;
  323. Vibrato vibrato;
  324. } ChannelState;
  325. ChannelState ch_state = {
  326. .frequency = 0,
  327. .frequency_target = FREQUENCY_UNSET,
  328. .play = false,
  329. };
  330. void tracker_interrupt_body() {
  331. uint8_t note = record_get_note(row.notes[song_state.row]);
  332. uint8_t effect = record_get_effect(row.notes[song_state.row]);
  333. uint8_t data = record_get_effect_data(row.notes[song_state.row]);
  334. // load frequency from note at tick 0
  335. if(song_state.tick == 0) {
  336. if(note == NOTE_OFF) {
  337. ch_state.play = false;
  338. } else if((note > NOTE_NONE) && (note < NOTE_OFF)) {
  339. ch_state.play = true;
  340. // reset vibrato
  341. ch_state.vibrato.speed = 0;
  342. ch_state.vibrato.depth = 0;
  343. ch_state.vibrato.value = 0;
  344. ch_state.vibrato.direction = 0;
  345. if(effect == EffectSlideToNote) {
  346. ch_state.frequency_target = note_to_freq(note);
  347. } else {
  348. ch_state.frequency = note_to_freq(note);
  349. ch_state.frequency_target = FREQUENCY_UNSET;
  350. }
  351. }
  352. }
  353. if(ch_state.play) {
  354. float frequency;
  355. if((effect == EffectSlideUp || effect == EffectSlideDown) && data != EFFECT_DATA_NONE) {
  356. // apply slide effect
  357. ch_state.frequency += (effect == EffectSlideUp ? 1 : -1) * data;
  358. } else if(effect == EffectSlideToNote) {
  359. // apply slide to note effect, if target frequency is set
  360. if(ch_state.frequency_target > 0) {
  361. if(ch_state.frequency_target > ch_state.frequency) {
  362. ch_state.frequency += data;
  363. if(ch_state.frequency > ch_state.frequency_target) {
  364. ch_state.frequency = ch_state.frequency_target;
  365. ch_state.frequency_target = FREQUENCY_UNSET;
  366. }
  367. } else if(ch_state.frequency_target < ch_state.frequency) {
  368. ch_state.frequency -= data;
  369. if(ch_state.frequency < ch_state.frequency_target) {
  370. ch_state.frequency = ch_state.frequency_target;
  371. ch_state.frequency_target = FREQUENCY_UNSET;
  372. }
  373. }
  374. }
  375. }
  376. frequency = ch_state.frequency;
  377. // apply arpeggio effect
  378. if(effect == EffectArpeggio) {
  379. if(data != EFFECT_DATA_NONE) {
  380. if((song_state.tick % 3) == 1) {
  381. uint8_t note_offset = data & 0b000111;
  382. frequency = frequency_offset(frequency, note_offset);
  383. } else if((song_state.tick % 3) == 2) {
  384. uint8_t note_offset = (data >> 3) & 0b000111;
  385. frequency = frequency_offset(frequency, note_offset);
  386. }
  387. }
  388. } else if(effect == EffectVibrato) {
  389. // apply vibrato effect, data = speed, depth
  390. uint8_t vibrato_speed = data & 0b000111;
  391. uint8_t vibrato_depth = (data >> 3) & 0b000111;
  392. // update vibrato parameters if speed or depth is non-zero
  393. if(vibrato_speed != 0) ch_state.vibrato.speed = vibrato_speed;
  394. if(vibrato_depth != 0) ch_state.vibrato.depth = vibrato_depth;
  395. // update vibrato value
  396. ch_state.vibrato.value += ch_state.vibrato.direction * ch_state.vibrato.speed;
  397. // change direction if value is at the limit
  398. if(ch_state.vibrato.value > ch_state.vibrato.depth) {
  399. ch_state.vibrato.direction = -1;
  400. } else if(ch_state.vibrato.value < -ch_state.vibrato.depth) {
  401. ch_state.vibrato.direction = 1;
  402. } else if(ch_state.vibrato.direction == 0) {
  403. // set initial direction, if it is not set
  404. ch_state.vibrato.direction = 1;
  405. }
  406. frequency += frequency * (((1.0f / 12.0f) / 7.0f) * ch_state.vibrato.value);
  407. }
  408. tracker_speaker_play(frequency, 0.5f);
  409. } else {
  410. tracker_speaker_stop();
  411. }
  412. song_state.tick++;
  413. if(song_state.tick >= song_state.tick_limit) {
  414. song_state.tick = 0;
  415. // next note
  416. song_state.row = (song_state.row + 1) % PATTERN_SIZE;
  417. }
  418. }
  419. void tracker_interrupt_cb() {
  420. tracker_debug_set(true);
  421. tracker_interrupt_body();
  422. tracker_debug_set(false);
  423. }
  424. void log_record(NoteRecord record) {
  425. uint8_t note = record_get_note(record);
  426. uint8_t effect = record_get_effect(record);
  427. uint8_t data = record_get_effect_data(record);
  428. printf("Note: %u, Effect: %u, Data: %u", note, effect, data);
  429. if(effect == EffectArpeggio) {
  430. uint8_t note_offset = data & 0b000111;
  431. uint8_t note_offset2 = (data >> 3) & 0b000111;
  432. printf(" (Arpeggio: %u, %u)", note_offset, note_offset2);
  433. float frequency = note_to_freq(note);
  434. float new_frequency = frequency_offset(frequency, note_offset);
  435. float new_frequency2 = frequency_offset(frequency, note_offset2);
  436. printf(
  437. " (Freq: %f, %f, %f)",
  438. (double)frequency,
  439. (double)new_frequency,
  440. (double)new_frequency2);
  441. }
  442. printf("\r\n");
  443. }
  444. int32_t zero_tracker_app(void* p) {
  445. UNUSED(p);
  446. tracker_debug_init();
  447. tracker_speaker_init();
  448. tracker_interrupt_init(60.0f, tracker_interrupt_cb, NULL);
  449. // log_record(row.notes[0]);
  450. while(1) {
  451. furi_delay_ms(1000);
  452. }
  453. return 0;
  454. }