doom_music_player_worker.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. #include "doom_music_player_worker.h"
  2. #include <furi_hal.h>
  3. #include <furi.h>
  4. #include <storage/storage.h>
  5. #include <lib/flipper_format/flipper_format.h>
  6. #include <m-array.h>
  7. #define TAG "MusicPlayerWorker"
  8. #define MUSIC_PLAYER_FILETYPE "Flipper Music Format"
  9. #define MUSIC_PLAYER_VERSION 0
  10. #define SEMITONE_PAUSE 0xFF
  11. #define NOTE_C4 261.63f
  12. #define NOTE_C4_SEMITONE (4.0f * 12.0f)
  13. #define TWO_POW_TWELTH_ROOT 1.059463094359f
  14. typedef struct {
  15. uint8_t semitone;
  16. uint8_t duration;
  17. uint8_t dots;
  18. } NoteBlock;
  19. ARRAY_DEF(NoteBlockArray, NoteBlock, M_POD_OPLIST);
  20. struct MusicPlayerWorker {
  21. FuriThread* thread;
  22. bool should_work;
  23. MusicPlayerWorkerCallback callback;
  24. void* callback_context;
  25. float volume;
  26. uint32_t bpm;
  27. uint32_t duration;
  28. uint32_t octave;
  29. NoteBlockArray_t notes;
  30. };
  31. static int32_t music_player_worker_thread_callback(void* context) {
  32. furi_assert(context);
  33. MusicPlayerWorker* instance = context;
  34. NoteBlockArray_it_t it;
  35. NoteBlockArray_it(it, instance->notes);
  36. if(furi_hal_speaker_acquire(1000)) {
  37. while(instance->should_work) {
  38. if(NoteBlockArray_end_p(it)) {
  39. NoteBlockArray_it(it, instance->notes);
  40. furi_delay_ms(10);
  41. } else {
  42. NoteBlock* note_block = NoteBlockArray_ref(it);
  43. float note_from_a4 = (float)note_block->semitone - NOTE_C4_SEMITONE;
  44. float frequency = NOTE_C4 * powf(TWO_POW_TWELTH_ROOT, note_from_a4);
  45. float duration = 60.0 * furi_kernel_get_tick_frequency() * 4 / instance->bpm /
  46. note_block->duration;
  47. uint32_t dots = note_block->dots;
  48. while(dots > 0) {
  49. duration += duration / 2;
  50. dots--;
  51. }
  52. uint32_t next_tick = furi_get_tick() + duration;
  53. float volume = instance->volume;
  54. if(instance->callback) {
  55. instance->callback(
  56. note_block->semitone,
  57. note_block->dots,
  58. note_block->duration,
  59. 0.0,
  60. instance->callback_context);
  61. }
  62. furi_hal_speaker_stop();
  63. furi_hal_speaker_start(frequency, volume);
  64. while(instance->should_work && furi_get_tick() < next_tick) {
  65. volume *= 0.9945679;
  66. furi_hal_speaker_set_volume(volume);
  67. furi_delay_ms(2);
  68. }
  69. NoteBlockArray_next(it);
  70. }
  71. }
  72. furi_hal_speaker_stop();
  73. furi_hal_speaker_release();
  74. } else {
  75. FURI_LOG_E(TAG, "Speaker system is busy with another process.");
  76. }
  77. return 0;
  78. }
  79. MusicPlayerWorker* music_player_worker_alloc() {
  80. MusicPlayerWorker* instance = malloc(sizeof(MusicPlayerWorker));
  81. NoteBlockArray_init(instance->notes);
  82. instance->thread = furi_thread_alloc();
  83. furi_thread_set_name(instance->thread, "MusicPlayerWorker");
  84. furi_thread_set_stack_size(instance->thread, 1024);
  85. furi_thread_set_context(instance->thread, instance);
  86. furi_thread_set_callback(instance->thread, music_player_worker_thread_callback);
  87. instance->volume = 1.0f;
  88. return instance;
  89. }
  90. void music_player_worker_free(MusicPlayerWorker* instance) {
  91. furi_assert(instance);
  92. furi_thread_free(instance->thread);
  93. NoteBlockArray_clear(instance->notes);
  94. free(instance);
  95. }
  96. static bool is_digit(const char c) {
  97. return isdigit(c) != 0;
  98. }
  99. static bool is_letter(const char c) {
  100. return islower(c) != 0 || isupper(c) != 0;
  101. }
  102. static bool is_space(const char c) {
  103. return c == ' ' || c == '\t';
  104. }
  105. static size_t extract_number(const char* string, uint32_t* number) {
  106. size_t ret = 0;
  107. while(is_digit(*string)) {
  108. *number *= 10;
  109. *number += (*string - '0');
  110. string++;
  111. ret++;
  112. }
  113. return ret;
  114. }
  115. static size_t extract_dots(const char* string, uint32_t* number) {
  116. size_t ret = 0;
  117. while(*string == '.') {
  118. *number += 1;
  119. string++;
  120. ret++;
  121. }
  122. return ret;
  123. }
  124. static size_t extract_char(const char* string, char* symbol) {
  125. if(is_letter(*string)) {
  126. *symbol = *string;
  127. return 1;
  128. } else {
  129. return 0;
  130. }
  131. }
  132. static size_t extract_sharp(const char* string, char* symbol) {
  133. if(*string == '#' || *string == '_') {
  134. *symbol = '#';
  135. return 1;
  136. } else {
  137. return 0;
  138. }
  139. }
  140. static size_t skip_till(const char* string, const char symbol) {
  141. size_t ret = 0;
  142. while(*string != '\0' && *string != symbol) {
  143. string++;
  144. ret++;
  145. }
  146. if(*string != symbol) {
  147. ret = 0;
  148. }
  149. return ret;
  150. }
  151. static bool music_player_worker_add_note(
  152. MusicPlayerWorker* instance,
  153. uint8_t semitone,
  154. uint8_t duration,
  155. uint8_t dots) {
  156. NoteBlock note_block;
  157. note_block.semitone = semitone;
  158. note_block.duration = duration;
  159. note_block.dots = dots;
  160. NoteBlockArray_push_back(instance->notes, note_block);
  161. return true;
  162. }
  163. static int8_t note_to_semitone(const char note) {
  164. switch(note) {
  165. case 'C':
  166. return 0;
  167. // C#
  168. case 'D':
  169. return 2;
  170. // D#
  171. case 'E':
  172. return 4;
  173. case 'F':
  174. return 5;
  175. // F#
  176. case 'G':
  177. return 7;
  178. // G#
  179. case 'A':
  180. return 9;
  181. // A#
  182. case 'B':
  183. return 11;
  184. default:
  185. return 0;
  186. }
  187. }
  188. static bool music_player_worker_parse_notes(MusicPlayerWorker* instance, const char* string) {
  189. const char* cursor = string;
  190. bool result = true;
  191. while(*cursor != '\0') {
  192. if(!is_space(*cursor)) {
  193. uint32_t duration = 0;
  194. char note_char = '\0';
  195. char sharp_char = '\0';
  196. uint32_t octave = 0;
  197. uint32_t dots = 0;
  198. // Parsing
  199. cursor += extract_number(cursor, &duration);
  200. cursor += extract_char(cursor, &note_char);
  201. cursor += extract_sharp(cursor, &sharp_char);
  202. cursor += extract_number(cursor, &octave);
  203. cursor += extract_dots(cursor, &dots);
  204. // Post processing
  205. note_char = toupper(note_char);
  206. if(!duration) {
  207. duration = instance->duration;
  208. }
  209. if(!octave) {
  210. octave = instance->octave;
  211. }
  212. // Validation
  213. bool is_valid = true;
  214. is_valid &= (duration >= 1 && duration <= 128);
  215. is_valid &= ((note_char >= 'A' && note_char <= 'G') || note_char == 'P');
  216. is_valid &= (sharp_char == '#' || sharp_char == '\0');
  217. is_valid &= (octave <= 16);
  218. is_valid &= (dots <= 16);
  219. if(!is_valid) {
  220. FURI_LOG_E(
  221. TAG,
  222. "Invalid note: %lu%c%c%lu.%lu",
  223. duration,
  224. note_char == '\0' ? '_' : note_char,
  225. sharp_char == '\0' ? '_' : sharp_char,
  226. octave,
  227. dots);
  228. result = false;
  229. break;
  230. }
  231. // Note to semitones
  232. uint8_t semitone = 0;
  233. if(note_char == 'P') {
  234. semitone = SEMITONE_PAUSE;
  235. } else {
  236. semitone += octave * 12;
  237. semitone += note_to_semitone(note_char);
  238. semitone += sharp_char == '#' ? 1 : 0;
  239. }
  240. if(music_player_worker_add_note(instance, semitone, duration, dots)) {
  241. FURI_LOG_D(
  242. TAG,
  243. "Added note: %c%c%lu.%lu = %u %lu",
  244. note_char == '\0' ? '_' : note_char,
  245. sharp_char == '\0' ? '_' : sharp_char,
  246. octave,
  247. dots,
  248. semitone,
  249. duration);
  250. } else {
  251. FURI_LOG_E(
  252. TAG,
  253. "Invalid note: %c%c%lu.%lu = %u %lu",
  254. note_char == '\0' ? '_' : note_char,
  255. sharp_char == '\0' ? '_' : sharp_char,
  256. octave,
  257. dots,
  258. semitone,
  259. duration);
  260. }
  261. cursor += skip_till(cursor, ',');
  262. }
  263. if(*cursor != '\0') cursor++;
  264. }
  265. return result;
  266. }
  267. bool music_player_worker_load(MusicPlayerWorker* instance, const char* file_path) {
  268. furi_assert(instance);
  269. furi_assert(file_path);
  270. bool ret = false;
  271. if(strcasestr(file_path, ".fmf")) {
  272. ret = music_player_worker_load_fmf_from_file(instance, file_path);
  273. } else {
  274. ret = music_player_worker_load_rtttl_from_file(instance, file_path);
  275. }
  276. return ret;
  277. }
  278. bool music_player_worker_load_fmf_from_file(MusicPlayerWorker* instance, const char* file_path) {
  279. furi_assert(instance);
  280. furi_assert(file_path);
  281. bool result = false;
  282. FuriString* temp_str;
  283. temp_str = furi_string_alloc();
  284. Storage* storage = furi_record_open(RECORD_STORAGE);
  285. FlipperFormat* file = flipper_format_file_alloc(storage);
  286. do {
  287. if(!flipper_format_file_open_existing(file, file_path)) break;
  288. uint32_t version = 0;
  289. if(!flipper_format_read_header(file, temp_str, &version)) break;
  290. if(furi_string_cmp_str(temp_str, MUSIC_PLAYER_FILETYPE) ||
  291. (version != MUSIC_PLAYER_VERSION)) {
  292. FURI_LOG_E(TAG, "Incorrect file format or version");
  293. break;
  294. }
  295. if(!flipper_format_read_uint32(file, "BPM", &instance->bpm, 1)) {
  296. FURI_LOG_E(TAG, "BPM is missing");
  297. break;
  298. }
  299. if(!flipper_format_read_uint32(file, "Duration", &instance->duration, 1)) {
  300. FURI_LOG_E(TAG, "Duration is missing");
  301. break;
  302. }
  303. if(!flipper_format_read_uint32(file, "Octave", &instance->octave, 1)) {
  304. FURI_LOG_E(TAG, "Octave is missing");
  305. break;
  306. }
  307. if(!flipper_format_read_string(file, "Notes", temp_str)) {
  308. FURI_LOG_E(TAG, "Notes is missing");
  309. break;
  310. }
  311. if(!music_player_worker_parse_notes(instance, furi_string_get_cstr(temp_str))) {
  312. break;
  313. }
  314. result = true;
  315. } while(false);
  316. furi_record_close(RECORD_STORAGE);
  317. flipper_format_free(file);
  318. furi_string_free(temp_str);
  319. return result;
  320. }
  321. bool music_player_worker_load_rtttl_from_file(MusicPlayerWorker* instance, const char* file_path) {
  322. furi_assert(instance);
  323. furi_assert(file_path);
  324. bool result = false;
  325. FuriString* content;
  326. content = furi_string_alloc();
  327. Storage* storage = furi_record_open(RECORD_STORAGE);
  328. File* file = storage_file_alloc(storage);
  329. do {
  330. if(!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  331. FURI_LOG_E(TAG, "Unable to open file");
  332. break;
  333. };
  334. uint16_t ret = 0;
  335. do {
  336. uint8_t buffer[65] = {0};
  337. ret = storage_file_read(file, buffer, sizeof(buffer) - 1);
  338. for(size_t i = 0; i < ret; i++) {
  339. furi_string_push_back(content, buffer[i]);
  340. }
  341. } while(ret > 0);
  342. furi_string_trim(content);
  343. if(!furi_string_size(content)) {
  344. FURI_LOG_E(TAG, "Empty file");
  345. break;
  346. }
  347. if(!music_player_worker_load_rtttl_from_string(instance, furi_string_get_cstr(content))) {
  348. FURI_LOG_E(TAG, "Invalid file content");
  349. break;
  350. }
  351. result = true;
  352. } while(0);
  353. storage_file_free(file);
  354. furi_record_close(RECORD_STORAGE);
  355. furi_string_free(content);
  356. return result;
  357. }
  358. bool music_player_worker_load_rtttl_from_string(MusicPlayerWorker* instance, const char* string) {
  359. furi_assert(instance);
  360. const char* cursor = string;
  361. // Skip name
  362. cursor += skip_till(cursor, ':');
  363. if(*cursor != ':') {
  364. return false;
  365. }
  366. // Duration
  367. cursor += skip_till(cursor, '=');
  368. if(*cursor != '=') {
  369. return false;
  370. }
  371. cursor++;
  372. cursor += extract_number(cursor, &instance->duration);
  373. // Octave
  374. cursor += skip_till(cursor, '=');
  375. if(*cursor != '=') {
  376. return false;
  377. }
  378. cursor++;
  379. cursor += extract_number(cursor, &instance->octave);
  380. // BPM
  381. cursor += skip_till(cursor, '=');
  382. if(*cursor != '=') {
  383. return false;
  384. }
  385. cursor++;
  386. cursor += extract_number(cursor, &instance->bpm);
  387. // Notes
  388. cursor += skip_till(cursor, ':');
  389. if(*cursor != ':') {
  390. return false;
  391. }
  392. cursor++;
  393. if(!music_player_worker_parse_notes(instance, cursor)) {
  394. return false;
  395. }
  396. return true;
  397. }
  398. void music_player_worker_set_callback(
  399. MusicPlayerWorker* instance,
  400. MusicPlayerWorkerCallback callback,
  401. void* context) {
  402. furi_assert(instance);
  403. instance->callback = callback;
  404. instance->callback_context = context;
  405. }
  406. void music_player_worker_set_volume(MusicPlayerWorker* instance, float volume) {
  407. furi_assert(instance);
  408. instance->volume = volume;
  409. }
  410. void music_player_worker_start(MusicPlayerWorker* instance) {
  411. furi_assert(instance);
  412. furi_assert(instance->should_work == false);
  413. instance->should_work = true;
  414. furi_thread_start(instance->thread);
  415. }
  416. void music_player_worker_stop(MusicPlayerWorker* instance) {
  417. furi_assert(instance);
  418. furi_assert(instance->should_work == true);
  419. instance->should_work = false;
  420. furi_thread_join(instance->thread);
  421. }