music_player_worker.c 13 KB

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