music_player_worker.c 13 KB

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