wav_player.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <stm32wbxx_ll_dma.h>
  5. #include "wav_player_hal.h"
  6. #include "wav_parser.h"
  7. #include "wav_player_view.h"
  8. #include <math.h>
  9. #include <wav_player_icons.h>
  10. #define TAG "WavPlayer"
  11. #define WAVPLAYER_FOLDER "/ext/wav_player"
  12. static bool open_wav_stream(Stream* stream) {
  13. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  14. bool result = false;
  15. FuriString* path;
  16. path = furi_string_alloc();
  17. furi_string_set(path, WAVPLAYER_FOLDER);
  18. DialogsFileBrowserOptions browser_options;
  19. dialog_file_browser_set_basic_options(&browser_options, ".wav", &I_music_10px);
  20. browser_options.base_path = WAVPLAYER_FOLDER;
  21. browser_options.hide_ext = false;
  22. bool ret = dialog_file_browser_show(dialogs, path, path, &browser_options);
  23. furi_record_close(RECORD_DIALOGS);
  24. if(ret) {
  25. if(!file_stream_open(stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  26. FURI_LOG_E(TAG, "Cannot open file \"%s\"", furi_string_get_cstr(path));
  27. } else {
  28. result = true;
  29. }
  30. }
  31. furi_string_free(path);
  32. return result;
  33. }
  34. typedef enum {
  35. WavPlayerEventHalfTransfer,
  36. WavPlayerEventFullTransfer,
  37. WavPlayerEventCtrlVolUp,
  38. WavPlayerEventCtrlVolDn,
  39. WavPlayerEventCtrlMoveL,
  40. WavPlayerEventCtrlMoveR,
  41. WavPlayerEventCtrlOk,
  42. WavPlayerEventCtrlBack,
  43. } WavPlayerEventType;
  44. typedef struct {
  45. WavPlayerEventType type;
  46. } WavPlayerEvent;
  47. static void wav_player_dma_isr(void* ctx) {
  48. FuriMessageQueue* event_queue = ctx;
  49. // half of transfer
  50. if(LL_DMA_IsActiveFlag_HT1(DMA1)) {
  51. LL_DMA_ClearFlag_HT1(DMA1);
  52. // fill first half of buffer
  53. WavPlayerEvent event = {.type = WavPlayerEventHalfTransfer};
  54. furi_message_queue_put(event_queue, &event, 0);
  55. }
  56. // transfer complete
  57. if(LL_DMA_IsActiveFlag_TC1(DMA1)) {
  58. LL_DMA_ClearFlag_TC1(DMA1);
  59. // fill second half of buffer
  60. WavPlayerEvent event = {.type = WavPlayerEventFullTransfer};
  61. furi_message_queue_put(event_queue, &event, 0);
  62. }
  63. }
  64. static void exit_callback(void* ctx) {
  65. FuriMessageQueue* event_queue = ctx;
  66. WavPlayerEvent event;
  67. event.type = WavPlayerEventCtrlBack;
  68. furi_message_queue_put(event_queue, &event, 0);
  69. }
  70. static bool thread_exit_signal_callback(uint32_t signal, void* arg, void* ctx) {
  71. UNUSED(arg);
  72. if(signal == FuriSignalExit) {
  73. exit_callback(ctx);
  74. return true;
  75. }
  76. return false;
  77. }
  78. static WavPlayerApp* app_alloc() {
  79. WavPlayerApp* app = malloc(sizeof(WavPlayerApp));
  80. app->samples_count_half = 1024 * 4;
  81. app->samples_count = app->samples_count_half * 2;
  82. app->storage = furi_record_open(RECORD_STORAGE);
  83. app->stream = file_stream_alloc(app->storage);
  84. app->parser = wav_parser_alloc();
  85. app->sample_buffer = malloc(sizeof(*app->sample_buffer) * app->samples_count);
  86. app->tmp_buffer = malloc(sizeof(*app->tmp_buffer) * app->samples_count);
  87. app->queue = furi_message_queue_alloc(10, sizeof(WavPlayerEvent));
  88. app->volume = 10.0f;
  89. app->play = true;
  90. app->gui = furi_record_open(RECORD_GUI);
  91. app->view_holder = view_holder_alloc();
  92. app->view = wav_player_view_alloc();
  93. view_holder_set_back_callback(app->view_holder, exit_callback, app->queue);
  94. view_holder_attach_to_gui(app->view_holder, app->gui);
  95. view_holder_set_view(app->view_holder, wav_player_view_get_view(app->view));
  96. view_holder_send_to_front(app->view_holder);
  97. app->notification = furi_record_open(RECORD_NOTIFICATION);
  98. notification_message(app->notification, &sequence_display_backlight_enforce_on);
  99. furi_thread_set_signal_callback(
  100. furi_thread_get_current(), thread_exit_signal_callback, app->queue);
  101. return app;
  102. }
  103. static void app_free(WavPlayerApp* app) {
  104. view_holder_set_view(app->view_holder, NULL);
  105. view_holder_free(app->view_holder);
  106. wav_player_view_free(app->view);
  107. furi_record_close(RECORD_GUI);
  108. furi_message_queue_free(app->queue);
  109. free(app->tmp_buffer);
  110. free(app->sample_buffer);
  111. wav_parser_free(app->parser);
  112. stream_free(app->stream);
  113. furi_record_close(RECORD_STORAGE);
  114. notification_message(app->notification, &sequence_display_backlight_enforce_auto);
  115. furi_record_close(RECORD_NOTIFICATION);
  116. free(app);
  117. }
  118. // TODO: that works only with 8-bit 2ch audio
  119. static bool fill_data(WavPlayerApp* app, size_t index) {
  120. if(app->num_channels == 1 && app->bits_per_sample == 8) {
  121. uint8_t* sample_buffer_start = &app->sample_buffer[index];
  122. size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count_half);
  123. for(size_t i = count; i < app->samples_count_half; i++) {
  124. app->tmp_buffer[i] = 0;
  125. }
  126. //for(size_t i = 0; i < app->samples_count; i += 2)
  127. for(size_t i = 0; i < app->samples_count_half; i++) //now works with mono!
  128. {
  129. float data = app->tmp_buffer[i];
  130. data -= UINT8_MAX / 2; // to signed
  131. data /= UINT8_MAX / 2; // scale -1..1
  132. data *= app->volume; // volume
  133. data = tanhf(data); // hyperbolic tangent limiter
  134. data *= UINT8_MAX / 2; // scale -128..127
  135. data += UINT8_MAX / 2; // to unsigned
  136. if(data < 0) {
  137. data = 0;
  138. }
  139. if(data > 255) {
  140. data = 255;
  141. }
  142. //uint8_t data = app->tmp_buffer[i];
  143. sample_buffer_start[i] = data;
  144. }
  145. wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
  146. return count != app->samples_count_half;
  147. }
  148. if(app->num_channels == 1 && app->bits_per_sample == 16) {
  149. uint8_t* sample_buffer_start = &app->sample_buffer[index];
  150. size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
  151. for(size_t i = count; i < app->samples_count; i++) {
  152. //app->tmp_buffer[i] = 0;
  153. }
  154. for(size_t i = 0; i < app->samples_count; i += 2) {
  155. int16_t int_16 =
  156. (((int16_t)app->tmp_buffer[i + 1] << 8) + (int16_t)app->tmp_buffer[i]);
  157. float data = ((float)int_16 / 256.0 + 127.0);
  158. data -= UINT8_MAX / 2; // to signed
  159. data /= UINT8_MAX / 2; // scale -1..1
  160. data *= app->volume; // volume
  161. data = tanhf(data); // hyperbolic tangent limiter
  162. data *= UINT8_MAX / 2; // scale -128..127
  163. data += UINT8_MAX / 2; // to unsigned
  164. if(data < 0) {
  165. data = 0;
  166. }
  167. if(data > 255) {
  168. data = 255;
  169. }
  170. sample_buffer_start[i / 2] = data;
  171. }
  172. wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
  173. return count != app->samples_count;
  174. }
  175. if(app->num_channels == 2 && app->bits_per_sample == 16) {
  176. uint8_t* sample_buffer_start = &app->sample_buffer[index];
  177. size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
  178. for(size_t i = 0; i < app->samples_count; i += 4) {
  179. int16_t L = (((int16_t)app->tmp_buffer[i + 1] << 8) + (int16_t)app->tmp_buffer[i]);
  180. int16_t R = (((int16_t)app->tmp_buffer[i + 3] << 8) + (int16_t)app->tmp_buffer[i + 2]);
  181. int32_t int_16 = L / 2 + R / 2; // (L + R) / 2
  182. float data = ((float)int_16 / 256.0 + 127.0);
  183. data -= UINT8_MAX / 2; // to signed
  184. data /= UINT8_MAX / 2; // scale -1..1
  185. data *= app->volume; // volume
  186. data = tanhf(data); // hyperbolic tangent limiter
  187. data *= UINT8_MAX / 2; // scale -128..127
  188. data += UINT8_MAX / 2; // to unsigned
  189. if(data < 0) {
  190. data = 0;
  191. }
  192. if(data > 255) {
  193. data = 255;
  194. }
  195. sample_buffer_start[i / 4] = data;
  196. }
  197. count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
  198. for(size_t i = 0; i < app->samples_count; i += 4) {
  199. int16_t L = (((int16_t)app->tmp_buffer[i + 1] << 8) + (int16_t)app->tmp_buffer[i]);
  200. int16_t R = (((int16_t)app->tmp_buffer[i + 3] << 8) + (int16_t)app->tmp_buffer[i + 2]);
  201. int32_t int_16 = L / 2 + R / 2; // (L + R) / 2
  202. float data = ((float)int_16 / 256.0 + 127.0);
  203. data -= UINT8_MAX / 2; // to signed
  204. data /= UINT8_MAX / 2; // scale -1..1
  205. data *= app->volume; // volume
  206. data = tanhf(data); // hyperbolic tangent limiter
  207. data *= UINT8_MAX / 2; // scale -128..127
  208. data += UINT8_MAX / 2; // to unsigned
  209. if(data < 0) {
  210. data = 0;
  211. }
  212. if(data > 255) {
  213. data = 255;
  214. }
  215. sample_buffer_start[i / 4 + app->samples_count / 4] = data;
  216. }
  217. wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
  218. return count != app->samples_count;
  219. }
  220. if(app->num_channels == 2 && app->bits_per_sample == 8) {
  221. uint8_t* sample_buffer_start = &app->sample_buffer[index];
  222. size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
  223. for(size_t i = count; i < app->samples_count; i++) {
  224. app->tmp_buffer[i] = 0;
  225. }
  226. for(size_t i = 0; i < app->samples_count; i += 2) {
  227. float data = (app->tmp_buffer[i] + app->tmp_buffer[i + 1]) / 2; // (L + R) / 2
  228. data -= UINT8_MAX / 2; // to signed
  229. data /= UINT8_MAX / 2; // scale -1..1
  230. data *= app->volume; // volume
  231. data = tanhf(data); // hyperbolic tangent limiter
  232. data *= UINT8_MAX / 2; // scale -128..127
  233. data += UINT8_MAX / 2; // to unsigned
  234. if(data < 0) {
  235. data = 0;
  236. }
  237. if(data > 255) {
  238. data = 255;
  239. }
  240. //uint8_t data = app->tmp_buffer[i];
  241. sample_buffer_start[i / 2] = data;
  242. }
  243. wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
  244. return count != app->samples_count;
  245. }
  246. return true;
  247. }
  248. static void ctrl_callback(WavPlayerCtrl ctrl, void* ctx) {
  249. FuriMessageQueue* event_queue = ctx;
  250. WavPlayerEvent event;
  251. switch(ctrl) {
  252. case WavPlayerCtrlVolUp:
  253. event.type = WavPlayerEventCtrlVolUp;
  254. furi_message_queue_put(event_queue, &event, 0);
  255. break;
  256. case WavPlayerCtrlVolDn:
  257. event.type = WavPlayerEventCtrlVolDn;
  258. furi_message_queue_put(event_queue, &event, 0);
  259. break;
  260. case WavPlayerCtrlMoveL:
  261. event.type = WavPlayerEventCtrlMoveL;
  262. furi_message_queue_put(event_queue, &event, 0);
  263. break;
  264. case WavPlayerCtrlMoveR:
  265. event.type = WavPlayerEventCtrlMoveR;
  266. furi_message_queue_put(event_queue, &event, 0);
  267. break;
  268. case WavPlayerCtrlOk:
  269. event.type = WavPlayerEventCtrlOk;
  270. furi_message_queue_put(event_queue, &event, 0);
  271. break;
  272. default:
  273. break;
  274. }
  275. }
  276. static void app_run(WavPlayerApp* app) {
  277. if(!open_wav_stream(app->stream)) return;
  278. if(!wav_parser_parse(app->parser, app->stream, app)) return;
  279. wav_player_view_set_volume(app->view, app->volume);
  280. wav_player_view_set_start(app->view, wav_parser_get_data_start(app->parser));
  281. wav_player_view_set_current(app->view, stream_tell(app->stream));
  282. wav_player_view_set_end(app->view, wav_parser_get_data_end(app->parser));
  283. wav_player_view_set_play(app->view, app->play);
  284. wav_player_view_set_context(app->view, app->queue);
  285. wav_player_view_set_ctrl_callback(app->view, ctrl_callback);
  286. bool eof = fill_data(app, 0);
  287. eof = fill_data(app, app->samples_count_half);
  288. if(furi_hal_speaker_acquire(1000)) {
  289. wav_player_speaker_init(app->sample_rate);
  290. wav_player_dma_init((uint32_t)app->sample_buffer, app->samples_count);
  291. furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, wav_player_dma_isr, app->queue);
  292. wav_player_dma_start();
  293. wav_player_speaker_start();
  294. WavPlayerEvent event;
  295. while(1) {
  296. if(furi_message_queue_get(app->queue, &event, FuriWaitForever) == FuriStatusOk) {
  297. if(event.type == WavPlayerEventHalfTransfer) {
  298. wav_player_view_set_chans(app->view, app->num_channels);
  299. wav_player_view_set_bits(app->view, app->bits_per_sample);
  300. eof = fill_data(app, 0);
  301. wav_player_view_set_current(app->view, stream_tell(app->stream));
  302. if(eof) {
  303. stream_seek(
  304. app->stream,
  305. wav_parser_get_data_start(app->parser),
  306. StreamOffsetFromStart);
  307. }
  308. } else if(event.type == WavPlayerEventFullTransfer) {
  309. wav_player_view_set_chans(app->view, app->num_channels);
  310. wav_player_view_set_bits(app->view, app->bits_per_sample);
  311. eof = fill_data(app, app->samples_count_half);
  312. wav_player_view_set_current(app->view, stream_tell(app->stream));
  313. if(eof) {
  314. stream_seek(
  315. app->stream,
  316. wav_parser_get_data_start(app->parser),
  317. StreamOffsetFromStart);
  318. }
  319. } else if(event.type == WavPlayerEventCtrlVolUp) {
  320. if(app->volume < 9.9) app->volume += 0.4;
  321. wav_player_view_set_volume(app->view, app->volume);
  322. } else if(event.type == WavPlayerEventCtrlVolDn) {
  323. if(app->volume > 0.01) app->volume -= 0.4;
  324. wav_player_view_set_volume(app->view, app->volume);
  325. } else if(event.type == WavPlayerEventCtrlMoveL) {
  326. int32_t seek =
  327. stream_tell(app->stream) - wav_parser_get_data_start(app->parser);
  328. seek = MIN(
  329. seek,
  330. (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100) % 2 ?
  331. ((int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100) - 1) :
  332. (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100));
  333. stream_seek(app->stream, -seek, StreamOffsetFromCurrent);
  334. wav_player_view_set_current(app->view, stream_tell(app->stream));
  335. } else if(event.type == WavPlayerEventCtrlMoveR) {
  336. int32_t seek = wav_parser_get_data_end(app->parser) - stream_tell(app->stream);
  337. seek = MIN(
  338. seek,
  339. (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100) % 2 ?
  340. ((int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100) - 1) :
  341. (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100));
  342. stream_seek(app->stream, seek, StreamOffsetFromCurrent);
  343. wav_player_view_set_current(app->view, stream_tell(app->stream));
  344. } else if(event.type == WavPlayerEventCtrlOk) {
  345. app->play = !app->play;
  346. wav_player_view_set_play(app->view, app->play);
  347. if(!app->play) {
  348. wav_player_speaker_stop();
  349. } else {
  350. wav_player_speaker_start();
  351. }
  352. } else if(event.type == WavPlayerEventCtrlBack) {
  353. break;
  354. }
  355. }
  356. }
  357. wav_player_speaker_stop();
  358. wav_player_dma_stop();
  359. furi_hal_speaker_release();
  360. }
  361. // Reset GPIO pin and bus states
  362. wav_player_hal_deinit();
  363. furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, NULL, NULL);
  364. }
  365. int32_t wav_player_app(void* p) {
  366. UNUSED(p);
  367. WavPlayerApp* app = app_alloc();
  368. Storage* storage = furi_record_open(RECORD_STORAGE);
  369. if(!storage_simply_mkdir(storage, WAVPLAYER_FOLDER)) {
  370. FURI_LOG_E(TAG, "Could not create folder %s", WAVPLAYER_FOLDER);
  371. }
  372. furi_record_close(RECORD_STORAGE);
  373. app_run(app);
  374. app_free(app);
  375. return 0;
  376. }