wav_player.c 16 KB

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