music_player_worker.c 13 KB

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