music_player_worker.c 13 KB

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