zero_tracker.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 EFFECT_DATA_GET_X(data) ((data)&0x07)
  88. #define EFFECT_DATA_GET_Y(data) (((data) >> 3) & 0x07)
  89. #define FREQUENCY_UNSET -1.0f
  90. uint8_t record_get_note(NoteRecord note) {
  91. return note & 0x3F;
  92. }
  93. uint8_t record_get_effect(NoteRecord note) {
  94. return (note >> 6) & 0xF;
  95. }
  96. uint8_t record_get_effect_data(NoteRecord note) {
  97. return (note >> 10) & 0x3F;
  98. }
  99. #define RECORD_MAKE(note, effect, data) \
  100. ((NoteRecord)(((note)&0x3F) | (((effect)&0xF) << 6) | (((data)&0x3F) << 10)))
  101. #define NOTES_PER_OCT 12
  102. const float notes_oct[NOTES_PER_OCT] = {
  103. 130.813f,
  104. 138.591f,
  105. 146.832f,
  106. 155.563f,
  107. 164.814f,
  108. 174.614f,
  109. 184.997f,
  110. 195.998f,
  111. 207.652f,
  112. 220.00f,
  113. 233.082f,
  114. 246.942f,
  115. };
  116. float note_to_freq(uint8_t note) {
  117. if(note == NOTE_NONE) return 0.0f;
  118. note = note - NOTE_C2;
  119. uint8_t octave = note / NOTES_PER_OCT;
  120. uint8_t note_in_oct = note % NOTES_PER_OCT;
  121. return notes_oct[note_in_oct] * (1 << octave);
  122. }
  123. float frequency_offset(float frequency, uint8_t semitones) {
  124. return frequency * (1.0f + ((1.0f / 12.0f) * semitones));
  125. }
  126. #define PATTERN_SIZE 64
  127. typedef struct {
  128. NoteRecord notes[PATTERN_SIZE];
  129. } NoteRow;
  130. typedef struct {
  131. uint8_t row_count;
  132. NoteRow* rows;
  133. } NotePattern;
  134. NoteRow _row = {
  135. .notes =
  136. {
  137. //
  138. RECORD_MAKE(NOTE_A4, 0, 0),
  139. RECORD_MAKE(NOTE_C3, 0, 0),
  140. RECORD_MAKE(NOTE_F2, 0, 0),
  141. RECORD_MAKE(NOTE_C3, 0, 0),
  142. //
  143. RECORD_MAKE(NOTE_E4, 0, 0),
  144. RECORD_MAKE(NOTE_C3, 0, 0),
  145. RECORD_MAKE(NOTE_E4, 0, 0),
  146. RECORD_MAKE(NOTE_OFF, 0, 0),
  147. //
  148. RECORD_MAKE(NOTE_A4, 0, 0),
  149. RECORD_MAKE(NOTE_A4, 0, 0),
  150. RECORD_MAKE(NOTE_A4, 0, 0),
  151. RECORD_MAKE(NOTE_OFF, 0, 0),
  152. //
  153. RECORD_MAKE(NOTE_E5, 0, 0),
  154. RECORD_MAKE(NOTE_E5, 0, 0),
  155. RECORD_MAKE(NOTE_E5, 0, 0),
  156. RECORD_MAKE(NOTE_OFF, 0, 0),
  157. //
  158. RECORD_MAKE(NOTE_D5, 0, 0),
  159. RECORD_MAKE(NOTE_C3, 0, 0),
  160. RECORD_MAKE(NOTE_F2, 0, 0),
  161. RECORD_MAKE(NOTE_C3, 0, 0),
  162. //
  163. RECORD_MAKE(NOTE_C5, 0, 0),
  164. RECORD_MAKE(NOTE_C3, 0, 0),
  165. RECORD_MAKE(NOTE_C5, 0, 0),
  166. RECORD_MAKE(NOTE_OFF, 0, 0),
  167. //
  168. RECORD_MAKE(NOTE_A4, 0, 0),
  169. RECORD_MAKE(0, 0, 0),
  170. RECORD_MAKE(0, 0, 0),
  171. RECORD_MAKE(0, 0, 0),
  172. //
  173. RECORD_MAKE(NOTE_A4, 0, 0),
  174. RECORD_MAKE(NOTE_A4, 0, 0),
  175. RECORD_MAKE(NOTE_A4, 0, 0),
  176. RECORD_MAKE(NOTE_OFF, 0, 0),
  177. //
  178. RECORD_MAKE(NOTE_B4, 0, 0),
  179. RECORD_MAKE(NOTE_D3, 0, 0),
  180. RECORD_MAKE(NOTE_G2, 0, 0),
  181. RECORD_MAKE(NOTE_D3, 0, 0),
  182. //
  183. RECORD_MAKE(NOTE_E4, 0, 0),
  184. RECORD_MAKE(NOTE_D3, 0, 0),
  185. RECORD_MAKE(NOTE_E4, 0, 0),
  186. RECORD_MAKE(NOTE_OFF, 0, 0),
  187. //
  188. RECORD_MAKE(NOTE_A4, 0, 0),
  189. RECORD_MAKE(NOTE_A4, 0, 0),
  190. RECORD_MAKE(NOTE_A4, 0, 0),
  191. RECORD_MAKE(NOTE_OFF, 0, 0),
  192. //
  193. RECORD_MAKE(NOTE_E5, 0, 0),
  194. RECORD_MAKE(NOTE_E5, 0, 0),
  195. RECORD_MAKE(NOTE_E5, 0, 0),
  196. RECORD_MAKE(NOTE_OFF, 0, 0),
  197. //
  198. RECORD_MAKE(NOTE_D5, 0, 0),
  199. RECORD_MAKE(NOTE_D3, 0, 0),
  200. RECORD_MAKE(NOTE_G2, 0, 0),
  201. RECORD_MAKE(NOTE_D3, 0, 0),
  202. //
  203. RECORD_MAKE(NOTE_C5, 0, 0),
  204. RECORD_MAKE(NOTE_D3, 0, 0),
  205. RECORD_MAKE(NOTE_C5, 0, 0),
  206. RECORD_MAKE(NOTE_OFF, 0, 0),
  207. //
  208. RECORD_MAKE(NOTE_A4, 0, 0),
  209. RECORD_MAKE(0, 0, 0),
  210. RECORD_MAKE(0, 0, 0),
  211. RECORD_MAKE(0, 0, 0),
  212. //
  213. RECORD_MAKE(NOTE_A4, 0, 0),
  214. RECORD_MAKE(NOTE_A4, 0, 0),
  215. RECORD_MAKE(NOTE_A4, 0, 0),
  216. RECORD_MAKE(NOTE_OFF, 0, 0),
  217. },
  218. };
  219. const uint8_t test = 0x20;
  220. NoteRow row = {
  221. .notes =
  222. {
  223. // 1/4
  224. RECORD_MAKE(NOTE_C3, EffectArpeggio, EFFECT_DATA_2(3, 5)),
  225. RECORD_MAKE(0, EffectArpeggio, EFFECT_DATA_2(3, 5)),
  226. RECORD_MAKE(NOTE_C4, EffectSlideToNote, test),
  227. RECORD_MAKE(0, EffectSlideToNote, test),
  228. //
  229. RECORD_MAKE(0, EffectSlideToNote, test),
  230. RECORD_MAKE(0, EffectSlideToNote, test),
  231. RECORD_MAKE(0, EffectSlideToNote, test),
  232. RECORD_MAKE(0, EffectSlideToNote, test),
  233. //
  234. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(1, 1)),
  235. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(1, 1)),
  236. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(1, 1)),
  237. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(1, 1)),
  238. //
  239. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(2, 2)),
  240. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(2, 2)),
  241. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(2, 2)),
  242. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(2, 2)),
  243. // 2/4
  244. RECORD_MAKE(NOTE_C3, EffectSlideDown, 0x20),
  245. RECORD_MAKE(0, EffectSlideDown, 0x20),
  246. RECORD_MAKE(NOTE_C4, EffectVibrato, EFFECT_DATA_2(3, 3)),
  247. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  248. //
  249. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  250. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  251. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  252. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  253. //
  254. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  255. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  256. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  257. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  258. //
  259. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  260. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  261. RECORD_MAKE(0, EffectVibrato, EFFECT_DATA_2(3, 3)),
  262. RECORD_MAKE(NOTE_OFF, EffectVibrato, EFFECT_DATA_2(3, 3)),
  263. // 3/4
  264. RECORD_MAKE(NOTE_C3, EffectArpeggio, EFFECT_DATA_2(3, 5)),
  265. RECORD_MAKE(0, EffectArpeggio, EFFECT_DATA_2(3, 5)),
  266. RECORD_MAKE(NOTE_OFF, 0, 0),
  267. RECORD_MAKE(0, 0, 0),
  268. //
  269. RECORD_MAKE(0, 0, 0),
  270. RECORD_MAKE(0, 0, 0),
  271. RECORD_MAKE(0, 0, 0),
  272. RECORD_MAKE(0, 0, 0),
  273. //
  274. RECORD_MAKE(NOTE_C2, 0, 0),
  275. RECORD_MAKE(0, 0, 0),
  276. RECORD_MAKE(0, 0, 0),
  277. RECORD_MAKE(NOTE_OFF, 0, 0),
  278. //
  279. RECORD_MAKE(0, 0, 0),
  280. RECORD_MAKE(0, 0, 0),
  281. RECORD_MAKE(0, 0, 0),
  282. RECORD_MAKE(0, 0, 0),
  283. // 4/4
  284. RECORD_MAKE(NOTE_C3, EffectSlideDown, 0x20),
  285. RECORD_MAKE(0, EffectSlideDown, 0x20),
  286. RECORD_MAKE(0, EffectSlideDown, 0x20),
  287. RECORD_MAKE(NOTE_OFF, 0, 0),
  288. //
  289. RECORD_MAKE(0, 0, 0),
  290. RECORD_MAKE(0, 0, 0),
  291. RECORD_MAKE(0, 0, 0),
  292. RECORD_MAKE(0, 0, 0),
  293. //
  294. RECORD_MAKE(NOTE_C2, 0, 0),
  295. RECORD_MAKE(0, 0, 0),
  296. RECORD_MAKE(0, 0, 0),
  297. RECORD_MAKE(NOTE_OFF, 0, 0),
  298. //
  299. RECORD_MAKE(0, 0, 0),
  300. RECORD_MAKE(0, 0, 0),
  301. RECORD_MAKE(0, 0, 0),
  302. RECORD_MAKE(0, 0, 0),
  303. },
  304. };
  305. typedef struct {
  306. uint8_t tick;
  307. uint8_t tick_limit;
  308. uint8_t row;
  309. } SongState;
  310. SongState song_state = {
  311. .tick = 0,
  312. .tick_limit = 2,
  313. .row = 0,
  314. };
  315. typedef struct {
  316. uint8_t speed;
  317. uint8_t depth;
  318. int8_t direction;
  319. int8_t value;
  320. } Vibrato;
  321. typedef struct {
  322. float frequency;
  323. float frequency_target;
  324. bool play;
  325. Vibrato vibrato;
  326. } ChannelState;
  327. ChannelState ch_state = {
  328. .frequency = 0,
  329. .frequency_target = FREQUENCY_UNSET,
  330. .play = false,
  331. };
  332. void tracker_interrupt_body() {
  333. uint8_t note = record_get_note(row.notes[song_state.row]);
  334. uint8_t effect = record_get_effect(row.notes[song_state.row]);
  335. uint8_t data = record_get_effect_data(row.notes[song_state.row]);
  336. // load frequency from note at tick 0
  337. if(song_state.tick == 0) {
  338. if(note == NOTE_OFF) {
  339. ch_state.play = false;
  340. } else if((note > NOTE_NONE) && (note < NOTE_OFF)) {
  341. ch_state.play = true;
  342. // reset vibrato
  343. ch_state.vibrato.speed = 0;
  344. ch_state.vibrato.depth = 0;
  345. ch_state.vibrato.value = 0;
  346. ch_state.vibrato.direction = 0;
  347. if(effect == EffectSlideToNote) {
  348. ch_state.frequency_target = note_to_freq(note);
  349. } else {
  350. ch_state.frequency = note_to_freq(note);
  351. ch_state.frequency_target = FREQUENCY_UNSET;
  352. }
  353. }
  354. }
  355. if(ch_state.play) {
  356. float frequency;
  357. if((effect == EffectSlideUp || effect == EffectSlideDown) && data != EFFECT_DATA_NONE) {
  358. // apply slide effect
  359. ch_state.frequency += (effect == EffectSlideUp ? 1 : -1) * data;
  360. } else if(effect == EffectSlideToNote) {
  361. // apply slide to note effect, if target frequency is set
  362. if(ch_state.frequency_target > 0) {
  363. if(ch_state.frequency_target > ch_state.frequency) {
  364. ch_state.frequency += data;
  365. if(ch_state.frequency > ch_state.frequency_target) {
  366. ch_state.frequency = ch_state.frequency_target;
  367. ch_state.frequency_target = FREQUENCY_UNSET;
  368. }
  369. } else if(ch_state.frequency_target < ch_state.frequency) {
  370. ch_state.frequency -= data;
  371. if(ch_state.frequency < ch_state.frequency_target) {
  372. ch_state.frequency = ch_state.frequency_target;
  373. ch_state.frequency_target = FREQUENCY_UNSET;
  374. }
  375. }
  376. }
  377. }
  378. frequency = ch_state.frequency;
  379. // apply arpeggio effect
  380. if(effect == EffectArpeggio) {
  381. if(data != EFFECT_DATA_NONE) {
  382. if((song_state.tick % 3) == 1) {
  383. uint8_t note_offset = EFFECT_DATA_GET_X(data);
  384. frequency = frequency_offset(frequency, note_offset);
  385. } else if((song_state.tick % 3) == 2) {
  386. uint8_t note_offset = EFFECT_DATA_GET_Y(data);
  387. frequency = frequency_offset(frequency, note_offset);
  388. }
  389. }
  390. } else if(effect == EffectVibrato) {
  391. // apply vibrato effect, data = speed, depth
  392. uint8_t vibrato_speed = EFFECT_DATA_GET_X(data);
  393. uint8_t vibrato_depth = EFFECT_DATA_GET_Y(data);
  394. // update vibrato parameters if speed or depth is non-zero
  395. if(vibrato_speed != 0) ch_state.vibrato.speed = vibrato_speed;
  396. if(vibrato_depth != 0) ch_state.vibrato.depth = vibrato_depth;
  397. // update vibrato value
  398. ch_state.vibrato.value += ch_state.vibrato.direction * ch_state.vibrato.speed;
  399. // change direction if value is at the limit
  400. if(ch_state.vibrato.value > ch_state.vibrato.depth) {
  401. ch_state.vibrato.direction = -1;
  402. } else if(ch_state.vibrato.value < -ch_state.vibrato.depth) {
  403. ch_state.vibrato.direction = 1;
  404. } else if(ch_state.vibrato.direction == 0) {
  405. // set initial direction, if it is not set
  406. ch_state.vibrato.direction = 1;
  407. }
  408. frequency += frequency * (((1.0f / 12.0f) / 7.0f) * ch_state.vibrato.value);
  409. }
  410. tracker_speaker_play(frequency, 0.5f);
  411. } else {
  412. tracker_speaker_stop();
  413. }
  414. song_state.tick++;
  415. if(song_state.tick >= song_state.tick_limit) {
  416. song_state.tick = 0;
  417. // next note
  418. song_state.row = (song_state.row + 1) % PATTERN_SIZE;
  419. }
  420. }
  421. void tracker_interrupt_cb() {
  422. tracker_debug_set(true);
  423. tracker_interrupt_body();
  424. tracker_debug_set(false);
  425. }
  426. void log_record(NoteRecord record) {
  427. uint8_t note = record_get_note(record);
  428. uint8_t effect = record_get_effect(record);
  429. uint8_t data = record_get_effect_data(record);
  430. printf("Note: %u, Effect: %u, Data: %u", note, effect, data);
  431. if(effect == EffectArpeggio) {
  432. uint8_t note_offset = EFFECT_DATA_GET_X(data);
  433. uint8_t note_offset2 = EFFECT_DATA_GET_Y(data);
  434. printf(" (Arpeggio: %u, %u)", note_offset, note_offset2);
  435. float frequency = note_to_freq(note);
  436. float new_frequency = frequency_offset(frequency, note_offset);
  437. float new_frequency2 = frequency_offset(frequency, note_offset2);
  438. printf(
  439. " (Freq: %f, %f, %f)",
  440. (double)frequency,
  441. (double)new_frequency,
  442. (double)new_frequency2);
  443. }
  444. printf("\r\n");
  445. }
  446. int32_t zero_tracker_app(void* p) {
  447. UNUSED(p);
  448. tracker_debug_init();
  449. tracker_speaker_init();
  450. tracker_interrupt_init(60.0f, tracker_interrupt_cb, NULL);
  451. // log_record(row.notes[0]);
  452. while(1) {
  453. furi_delay_ms(1000);
  454. }
  455. return 0;
  456. }