music_player_worker.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. furi_delay_ms(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 * furi_kernel_get_tick_frequency() * 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_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_get_tick() < next_tick) {
  64. volume *= 0.9945679;
  65. furi_hal_speaker_set_volume(volume);
  66. furi_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. FuriString* temp_str;
  278. temp_str = furi_string_alloc();
  279. Storage* storage = furi_record_open(RECORD_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(furi_string_cmp_str(temp_str, MUSIC_PLAYER_FILETYPE) ||
  286. (version != MUSIC_PLAYER_VERSION)) {
  287. FURI_LOG_E(TAG, "Incorrect file format or version");
  288. break;
  289. }
  290. if(!flipper_format_read_uint32(file, "BPM", &instance->bpm, 1)) {
  291. FURI_LOG_E(TAG, "BPM is missing");
  292. break;
  293. }
  294. if(!flipper_format_read_uint32(file, "Duration", &instance->duration, 1)) {
  295. FURI_LOG_E(TAG, "Duration is missing");
  296. break;
  297. }
  298. if(!flipper_format_read_uint32(file, "Octave", &instance->octave, 1)) {
  299. FURI_LOG_E(TAG, "Octave is missing");
  300. break;
  301. }
  302. if(!flipper_format_read_string(file, "Notes", temp_str)) {
  303. FURI_LOG_E(TAG, "Notes is missing");
  304. break;
  305. }
  306. if(!music_player_worker_parse_notes(instance, furi_string_get_cstr(temp_str))) {
  307. break;
  308. }
  309. result = true;
  310. } while(false);
  311. furi_record_close(RECORD_STORAGE);
  312. flipper_format_free(file);
  313. furi_string_free(temp_str);
  314. return result;
  315. }
  316. bool music_player_worker_load_rtttl_from_file(MusicPlayerWorker* instance, const char* file_path) {
  317. furi_assert(instance);
  318. furi_assert(file_path);
  319. bool result = false;
  320. FuriString* content;
  321. content = furi_string_alloc();
  322. Storage* storage = furi_record_open(RECORD_STORAGE);
  323. File* file = storage_file_alloc(storage);
  324. do {
  325. if(!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  326. FURI_LOG_E(TAG, "Unable to open file");
  327. break;
  328. };
  329. uint16_t ret = 0;
  330. do {
  331. uint8_t buffer[65] = {0};
  332. ret = storage_file_read(file, buffer, sizeof(buffer) - 1);
  333. for(size_t i = 0; i < ret; i++) {
  334. furi_string_push_back(content, buffer[i]);
  335. }
  336. } while(ret > 0);
  337. furi_string_trim(content);
  338. if(!furi_string_size(content)) {
  339. FURI_LOG_E(TAG, "Empty file");
  340. break;
  341. }
  342. if(!music_player_worker_load_rtttl_from_string(instance, furi_string_get_cstr(content))) {
  343. FURI_LOG_E(TAG, "Invalid file content");
  344. break;
  345. }
  346. result = true;
  347. } while(0);
  348. storage_file_free(file);
  349. furi_record_close(RECORD_STORAGE);
  350. furi_string_free(content);
  351. return result;
  352. }
  353. bool music_player_worker_load_rtttl_from_string(MusicPlayerWorker* instance, const char* string) {
  354. furi_assert(instance);
  355. const char* cursor = string;
  356. // Skip name
  357. cursor += skip_till(cursor, ':');
  358. if(*cursor != ':') {
  359. return false;
  360. }
  361. // Duration
  362. cursor += skip_till(cursor, '=');
  363. if(*cursor != '=') {
  364. return false;
  365. }
  366. cursor++;
  367. cursor += extract_number(cursor, &instance->duration);
  368. // Octave
  369. cursor += skip_till(cursor, '=');
  370. if(*cursor != '=') {
  371. return false;
  372. }
  373. cursor++;
  374. cursor += extract_number(cursor, &instance->octave);
  375. // BPM
  376. cursor += skip_till(cursor, '=');
  377. if(*cursor != '=') {
  378. return false;
  379. }
  380. cursor++;
  381. cursor += extract_number(cursor, &instance->bpm);
  382. // Notes
  383. cursor += skip_till(cursor, ':');
  384. if(*cursor != ':') {
  385. return false;
  386. }
  387. cursor++;
  388. if(!music_player_worker_parse_notes(instance, cursor)) {
  389. return false;
  390. }
  391. return true;
  392. }
  393. void music_player_worker_set_callback(
  394. MusicPlayerWorker* instance,
  395. MusicPlayerWorkerCallback callback,
  396. void* context) {
  397. furi_assert(instance);
  398. instance->callback = callback;
  399. instance->callback_context = context;
  400. }
  401. void music_player_worker_set_volume(MusicPlayerWorker* instance, float volume) {
  402. furi_assert(instance);
  403. instance->volume = volume;
  404. }
  405. void music_player_worker_start(MusicPlayerWorker* instance) {
  406. furi_assert(instance);
  407. furi_assert(instance->should_work == false);
  408. instance->should_work = true;
  409. furi_thread_start(instance->thread);
  410. }
  411. void music_player_worker_stop(MusicPlayerWorker* instance) {
  412. furi_assert(instance);
  413. furi_assert(instance->should_work == true);
  414. instance->should_work = false;
  415. furi_thread_join(instance->thread);
  416. }