zero_tracker.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #define EFFECT_ARPEGGIO 0x00
  79. #define EFFECT_SLIDE_UP 0x01
  80. #define EFFECT_SLIDE_DOWN 0x02
  81. #define EFFECT_DATA_NONE 0
  82. #define EFFECT_DATA_2(x, y) ((x) | ((y) << 3))
  83. uint8_t record_get_note(NoteRecord note) {
  84. return note & 0x3F;
  85. }
  86. uint8_t record_get_effect(NoteRecord note) {
  87. return (note >> 6) & 0xF;
  88. }
  89. uint8_t record_get_effect_data(NoteRecord note) {
  90. return (note >> 10) & 0x3F;
  91. }
  92. #define RECORD_MAKE(note, effect, data) \
  93. ((NoteRecord)(((note)&0x3F) | (((effect)&0xF) << 6) | (((data)&0x3F) << 10)))
  94. #define NOTES_PER_OCT 12
  95. const float notes_oct[NOTES_PER_OCT] = {
  96. 130.813f,
  97. 138.591f,
  98. 146.832f,
  99. 155.563f,
  100. 164.814f,
  101. 174.614f,
  102. 184.997f,
  103. 195.998f,
  104. 207.652f,
  105. 220.00f,
  106. 233.082f,
  107. 246.942f,
  108. };
  109. float note_to_freq(uint8_t note) {
  110. if(note == NOTE_NONE) return 0.0f;
  111. note = note - NOTE_C2;
  112. uint8_t octave = note / NOTES_PER_OCT;
  113. uint8_t note_in_oct = note % NOTES_PER_OCT;
  114. return notes_oct[note_in_oct] * (1 << octave);
  115. }
  116. float frequency_offset(float frequency, uint8_t semitones) {
  117. return frequency * (1.0f + ((1.0f / 12.0f) * semitones));
  118. }
  119. #define PATTERN_SIZE 64
  120. typedef struct {
  121. NoteRecord notes[PATTERN_SIZE];
  122. } NoteRow;
  123. typedef struct {
  124. uint8_t row_count;
  125. NoteRow* rows;
  126. } NotePattern;
  127. NoteRow _row = {
  128. .notes =
  129. {
  130. //
  131. RECORD_MAKE(NOTE_A4, 0, 0),
  132. RECORD_MAKE(NOTE_C3, 0, 0),
  133. RECORD_MAKE(NOTE_F2, 0, 0),
  134. RECORD_MAKE(NOTE_C3, 0, 0),
  135. //
  136. RECORD_MAKE(NOTE_E4, 0, 0),
  137. RECORD_MAKE(NOTE_C3, 0, 0),
  138. RECORD_MAKE(NOTE_E4, 0, 0),
  139. RECORD_MAKE(NOTE_OFF, 0, 0),
  140. //
  141. RECORD_MAKE(NOTE_A4, 0, 0),
  142. RECORD_MAKE(NOTE_A4, 0, 0),
  143. RECORD_MAKE(NOTE_A4, 0, 0),
  144. RECORD_MAKE(NOTE_OFF, 0, 0),
  145. //
  146. RECORD_MAKE(NOTE_E5, 0, 0),
  147. RECORD_MAKE(NOTE_E5, 0, 0),
  148. RECORD_MAKE(NOTE_E5, 0, 0),
  149. RECORD_MAKE(NOTE_OFF, 0, 0),
  150. //
  151. RECORD_MAKE(NOTE_D5, 0, 0),
  152. RECORD_MAKE(NOTE_C3, 0, 0),
  153. RECORD_MAKE(NOTE_F2, 0, 0),
  154. RECORD_MAKE(NOTE_C3, 0, 0),
  155. //
  156. RECORD_MAKE(NOTE_C5, 0, 0),
  157. RECORD_MAKE(NOTE_C3, 0, 0),
  158. RECORD_MAKE(NOTE_C5, 0, 0),
  159. RECORD_MAKE(NOTE_OFF, 0, 0),
  160. //
  161. RECORD_MAKE(NOTE_A4, 0, 0),
  162. RECORD_MAKE(0, 0, 0),
  163. RECORD_MAKE(0, 0, 0),
  164. RECORD_MAKE(0, 0, 0),
  165. //
  166. RECORD_MAKE(NOTE_A4, 0, 0),
  167. RECORD_MAKE(NOTE_A4, 0, 0),
  168. RECORD_MAKE(NOTE_A4, 0, 0),
  169. RECORD_MAKE(NOTE_OFF, 0, 0),
  170. //
  171. RECORD_MAKE(NOTE_B4, 0, 0),
  172. RECORD_MAKE(NOTE_D3, 0, 0),
  173. RECORD_MAKE(NOTE_G2, 0, 0),
  174. RECORD_MAKE(NOTE_D3, 0, 0),
  175. //
  176. RECORD_MAKE(NOTE_E4, 0, 0),
  177. RECORD_MAKE(NOTE_D3, 0, 0),
  178. RECORD_MAKE(NOTE_E4, 0, 0),
  179. RECORD_MAKE(NOTE_OFF, 0, 0),
  180. //
  181. RECORD_MAKE(NOTE_A4, 0, 0),
  182. RECORD_MAKE(NOTE_A4, 0, 0),
  183. RECORD_MAKE(NOTE_A4, 0, 0),
  184. RECORD_MAKE(NOTE_OFF, 0, 0),
  185. //
  186. RECORD_MAKE(NOTE_E5, 0, 0),
  187. RECORD_MAKE(NOTE_E5, 0, 0),
  188. RECORD_MAKE(NOTE_E5, 0, 0),
  189. RECORD_MAKE(NOTE_OFF, 0, 0),
  190. //
  191. RECORD_MAKE(NOTE_D5, 0, 0),
  192. RECORD_MAKE(NOTE_D3, 0, 0),
  193. RECORD_MAKE(NOTE_G2, 0, 0),
  194. RECORD_MAKE(NOTE_D3, 0, 0),
  195. //
  196. RECORD_MAKE(NOTE_C5, 0, 0),
  197. RECORD_MAKE(NOTE_D3, 0, 0),
  198. RECORD_MAKE(NOTE_C5, 0, 0),
  199. RECORD_MAKE(NOTE_OFF, 0, 0),
  200. //
  201. RECORD_MAKE(NOTE_A4, 0, 0),
  202. RECORD_MAKE(0, 0, 0),
  203. RECORD_MAKE(0, 0, 0),
  204. RECORD_MAKE(0, 0, 0),
  205. //
  206. RECORD_MAKE(NOTE_A4, 0, 0),
  207. RECORD_MAKE(NOTE_A4, 0, 0),
  208. RECORD_MAKE(NOTE_A4, 0, 0),
  209. RECORD_MAKE(NOTE_OFF, 0, 0),
  210. },
  211. };
  212. NoteRow row = {
  213. .notes =
  214. {
  215. //
  216. RECORD_MAKE(NOTE_C4, EFFECT_SLIDE_DOWN, 0x3F),
  217. RECORD_MAKE(0, EFFECT_SLIDE_DOWN, 0x3F),
  218. RECORD_MAKE(0, EFFECT_SLIDE_DOWN, 0x3F),
  219. RECORD_MAKE(NOTE_OFF, 0, 0),
  220. //
  221. RECORD_MAKE(0, 0, 0),
  222. RECORD_MAKE(0, 0, 0),
  223. RECORD_MAKE(0, 0, 0),
  224. RECORD_MAKE(0, 0, 0),
  225. //
  226. RECORD_MAKE(NOTE_C4, EFFECT_ARPEGGIO, EFFECT_DATA_2(3, 5)),
  227. RECORD_MAKE(0, EFFECT_ARPEGGIO, EFFECT_DATA_2(3, 5)),
  228. RECORD_MAKE(0, EFFECT_ARPEGGIO, EFFECT_DATA_2(3, 5)),
  229. RECORD_MAKE(NOTE_OFF, 0, 0),
  230. //
  231. RECORD_MAKE(0, 0, 0),
  232. RECORD_MAKE(0, 0, 0),
  233. RECORD_MAKE(0, 0, 0),
  234. RECORD_MAKE(0, 0, 0),
  235. //
  236. RECORD_MAKE(NOTE_C4, EFFECT_SLIDE_DOWN, 0x3F),
  237. RECORD_MAKE(0, EFFECT_SLIDE_DOWN, 0x3F),
  238. RECORD_MAKE(0, EFFECT_SLIDE_DOWN, 0x3F),
  239. RECORD_MAKE(NOTE_OFF, 0, 0),
  240. //
  241. RECORD_MAKE(0, 0, 0),
  242. RECORD_MAKE(0, 0, 0),
  243. RECORD_MAKE(0, 0, 0),
  244. RECORD_MAKE(0, 0, 0),
  245. //
  246. RECORD_MAKE(NOTE_C4, EFFECT_ARPEGGIO, EFFECT_DATA_2(3, 5)),
  247. RECORD_MAKE(0, EFFECT_ARPEGGIO, EFFECT_DATA_2(3, 5)),
  248. RECORD_MAKE(0, EFFECT_ARPEGGIO, EFFECT_DATA_2(3, 5)),
  249. RECORD_MAKE(NOTE_OFF, 0, 0),
  250. //
  251. RECORD_MAKE(0, 0, 0),
  252. RECORD_MAKE(0, 0, 0),
  253. RECORD_MAKE(0, 0, 0),
  254. RECORD_MAKE(0, 0, 0),
  255. //
  256. RECORD_MAKE(NOTE_C4, EFFECT_SLIDE_DOWN, 0x3F),
  257. RECORD_MAKE(0, EFFECT_SLIDE_DOWN, 0x3F),
  258. RECORD_MAKE(0, EFFECT_SLIDE_DOWN, 0x3F),
  259. RECORD_MAKE(NOTE_OFF, 0, 0),
  260. //
  261. RECORD_MAKE(0, 0, 0),
  262. RECORD_MAKE(0, 0, 0),
  263. RECORD_MAKE(0, 0, 0),
  264. RECORD_MAKE(0, 0, 0),
  265. //
  266. RECORD_MAKE(NOTE_D4, EFFECT_ARPEGGIO, EFFECT_DATA_2(3, 5)),
  267. RECORD_MAKE(0, EFFECT_ARPEGGIO, EFFECT_DATA_2(3, 5)),
  268. RECORD_MAKE(0, EFFECT_ARPEGGIO, EFFECT_DATA_2(3, 5)),
  269. RECORD_MAKE(NOTE_OFF, 0, 0),
  270. //
  271. RECORD_MAKE(0, 0, 0),
  272. RECORD_MAKE(0, 0, 0),
  273. RECORD_MAKE(0, 0, 0),
  274. RECORD_MAKE(0, 0, 0),
  275. //
  276. RECORD_MAKE(NOTE_C4, EFFECT_SLIDE_DOWN, 0x3F),
  277. RECORD_MAKE(0, EFFECT_SLIDE_DOWN, 0x3F),
  278. RECORD_MAKE(0, EFFECT_SLIDE_DOWN, 0x3F),
  279. RECORD_MAKE(NOTE_OFF, 0, 0),
  280. //
  281. RECORD_MAKE(0, 0, 0),
  282. RECORD_MAKE(0, 0, 0),
  283. RECORD_MAKE(0, 0, 0),
  284. RECORD_MAKE(0, 0, 0),
  285. //
  286. RECORD_MAKE(NOTE_A3, EFFECT_ARPEGGIO, EFFECT_DATA_2(3, 5)),
  287. RECORD_MAKE(0, EFFECT_ARPEGGIO, EFFECT_DATA_2(3, 5)),
  288. RECORD_MAKE(0, EFFECT_ARPEGGIO, EFFECT_DATA_2(3, 5)),
  289. RECORD_MAKE(NOTE_OFF, 0, 0),
  290. //
  291. RECORD_MAKE(0, 0, 0),
  292. RECORD_MAKE(0, 0, 0),
  293. RECORD_MAKE(0, 0, 0),
  294. RECORD_MAKE(0, 0, 0),
  295. },
  296. };
  297. uint8_t tick_counter = 0;
  298. uint8_t tick_limit = 2;
  299. uint8_t row_counter = 0;
  300. float note_frequency = 0;
  301. bool note_play = false;
  302. void tracker_interrupt_body() {
  303. uint8_t note = record_get_note(row.notes[row_counter]);
  304. uint8_t effect = record_get_effect(row.notes[row_counter]);
  305. uint8_t data = record_get_effect_data(row.notes[row_counter]);
  306. // load frequency from note at tick 0
  307. if(tick_counter == 0) {
  308. if(note == NOTE_OFF) {
  309. note_play = false;
  310. } else if((note > 0) && (note < NOTE_OFF)) {
  311. note_play = true;
  312. note_frequency = note_to_freq(note);
  313. }
  314. }
  315. if(note_play) {
  316. if((effect == EFFECT_SLIDE_UP || effect == EFFECT_SLIDE_DOWN) &&
  317. data != EFFECT_DATA_NONE) {
  318. note_frequency += (effect == EFFECT_SLIDE_UP ? 1 : -1) * data;
  319. }
  320. float frequency = note_frequency;
  321. // apply arpeggio effect
  322. if(effect == EFFECT_ARPEGGIO && data != EFFECT_DATA_NONE) {
  323. if((tick_counter % 3) == 1) {
  324. uint8_t note_offset = data & 0b000111;
  325. frequency = frequency_offset(frequency, note_offset);
  326. } else if((tick_counter % 3) == 2) {
  327. uint8_t note_offset = (data >> 3) & 0b000111;
  328. frequency = frequency_offset(frequency, note_offset);
  329. }
  330. }
  331. tracker_speaker_play(frequency, 0.5f);
  332. } else {
  333. tracker_speaker_stop();
  334. }
  335. tick_counter++;
  336. if(tick_counter >= tick_limit) {
  337. tick_counter = 0;
  338. // next note
  339. row_counter = (row_counter + 1) % PATTERN_SIZE;
  340. }
  341. }
  342. void tracker_interrupt_cb() {
  343. tracker_debug_set(true);
  344. tracker_interrupt_body();
  345. tracker_debug_set(false);
  346. }
  347. void log_record(NoteRecord record) {
  348. uint8_t note = record_get_note(record);
  349. uint8_t effect = record_get_effect(record);
  350. uint8_t data = record_get_effect_data(record);
  351. printf("Note: %u, Effect: %u, Data: %u", note, effect, data);
  352. if(effect == EFFECT_ARPEGGIO) {
  353. uint8_t note_offset = data & 0b000111;
  354. uint8_t note_offset2 = (data >> 3) & 0b000111;
  355. printf(" (Arpeggio: %u, %u)", note_offset, note_offset2);
  356. float frequency = note_to_freq(note);
  357. float new_frequency = frequency_offset(frequency, note_offset);
  358. float new_frequency2 = frequency_offset(frequency, note_offset2);
  359. printf(
  360. " (Freq: %f, %f, %f)",
  361. (double)frequency,
  362. (double)new_frequency,
  363. (double)new_frequency2);
  364. }
  365. printf("\r\n");
  366. }
  367. int32_t zero_tracker_app(void* p) {
  368. UNUSED(p);
  369. tracker_debug_init();
  370. tracker_speaker_init();
  371. tracker_interrupt_init(60.0f, tracker_interrupt_cb, NULL);
  372. // log_record(row.notes[0]);
  373. while(1) {
  374. furi_delay_ms(1000);
  375. }
  376. return 0;
  377. }