wav_player.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 <My_WAV_Player_icons.h>
  15. #include <My_WAV_Player_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. typedef struct {
  71. Storage* storage;
  72. Stream* stream;
  73. WavParser* parser;
  74. uint16_t* sample_buffer;
  75. uint8_t* tmp_buffer;
  76. size_t samples_count_half;
  77. size_t samples_count;
  78. FuriMessageQueue* queue;
  79. float volume;
  80. bool play;
  81. WavPlayerView* view;
  82. ViewDispatcher* view_dispatcher;
  83. Gui* gui;
  84. NotificationApp* notification;
  85. } WavPlayerApp;
  86. static WavPlayerApp* app_alloc() {
  87. WavPlayerApp* app = malloc(sizeof(WavPlayerApp));
  88. app->samples_count_half = 1024 * 4;
  89. app->samples_count = app->samples_count_half * 2;
  90. app->storage = furi_record_open(RECORD_STORAGE);
  91. app->stream = file_stream_alloc(app->storage);
  92. app->parser = wav_parser_alloc();
  93. app->sample_buffer = malloc(sizeof(uint16_t) * app->samples_count);
  94. app->tmp_buffer = malloc(sizeof(uint8_t) * app->samples_count);
  95. app->queue = furi_message_queue_alloc(10, sizeof(WavPlayerEvent));
  96. app->volume = 10.0f;
  97. app->play = true;
  98. app->gui = furi_record_open(RECORD_GUI);
  99. app->view_dispatcher = view_dispatcher_alloc();
  100. app->view = wav_player_view_alloc();
  101. view_dispatcher_add_view(app->view_dispatcher, 0, wav_player_view_get_view(app->view));
  102. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  103. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  104. app->notification = furi_record_open(RECORD_NOTIFICATION);
  105. notification_message(app->notification, &sequence_display_backlight_enforce_on);
  106. return app;
  107. }
  108. static void app_free(WavPlayerApp* app) {
  109. view_dispatcher_remove_view(app->view_dispatcher, 0);
  110. view_dispatcher_free(app->view_dispatcher);
  111. wav_player_view_free(app->view);
  112. furi_record_close(RECORD_GUI);
  113. furi_message_queue_free(app->queue);
  114. free(app->tmp_buffer);
  115. free(app->sample_buffer);
  116. wav_parser_free(app->parser);
  117. stream_free(app->stream);
  118. furi_record_close(RECORD_STORAGE);
  119. notification_message(app->notification, &sequence_display_backlight_enforce_auto);
  120. furi_record_close(RECORD_NOTIFICATION);
  121. free(app);
  122. }
  123. // TODO: that works only with 8-bit 2ch audio
  124. static bool fill_data(WavPlayerApp* app, size_t index) {
  125. uint16_t* sample_buffer_start = &app->sample_buffer[index];
  126. size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
  127. for(size_t i = count; i < app->samples_count; i++) {
  128. app->tmp_buffer[i] = 0;
  129. }
  130. for(size_t i = 0; i < app->samples_count; i += 2) {
  131. float data = app->tmp_buffer[i];
  132. data -= UINT8_MAX / 2; // to signed
  133. data /= UINT8_MAX / 2; // scale -1..1
  134. data *= app->volume; // volume
  135. data = tanhf(data); // hyperbolic tangent limiter
  136. data *= UINT8_MAX / 2; // scale -128..127
  137. data += UINT8_MAX / 2; // to unsigned
  138. if(data < 0) {
  139. data = 0;
  140. }
  141. if(data > 255) {
  142. data = 255;
  143. }
  144. sample_buffer_start[i / 2] = data;
  145. }
  146. wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
  147. return count != app->samples_count;
  148. }
  149. static void ctrl_callback(WavPlayerCtrl ctrl, void* ctx) {
  150. FuriMessageQueue* event_queue = ctx;
  151. WavPlayerEvent event;
  152. switch(ctrl) {
  153. case WavPlayerCtrlVolUp:
  154. event.type = WavPlayerEventCtrlVolUp;
  155. furi_message_queue_put(event_queue, &event, 0);
  156. break;
  157. case WavPlayerCtrlVolDn:
  158. event.type = WavPlayerEventCtrlVolDn;
  159. furi_message_queue_put(event_queue, &event, 0);
  160. break;
  161. case WavPlayerCtrlMoveL:
  162. event.type = WavPlayerEventCtrlMoveL;
  163. furi_message_queue_put(event_queue, &event, 0);
  164. break;
  165. case WavPlayerCtrlMoveR:
  166. event.type = WavPlayerEventCtrlMoveR;
  167. furi_message_queue_put(event_queue, &event, 0);
  168. break;
  169. case WavPlayerCtrlOk:
  170. event.type = WavPlayerEventCtrlOk;
  171. furi_message_queue_put(event_queue, &event, 0);
  172. break;
  173. case WavPlayerCtrlBack:
  174. event.type = WavPlayerEventCtrlBack;
  175. furi_message_queue_put(event_queue, &event, 0);
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. static void app_run(WavPlayerApp* app) {
  182. if(!open_wav_stream(app->stream)) return;
  183. if(!wav_parser_parse(app->parser, app->stream)) return;
  184. wav_player_view_set_volume(app->view, app->volume);
  185. wav_player_view_set_start(app->view, wav_parser_get_data_start(app->parser));
  186. wav_player_view_set_current(app->view, stream_tell(app->stream));
  187. wav_player_view_set_end(app->view, wav_parser_get_data_end(app->parser));
  188. wav_player_view_set_play(app->view, app->play);
  189. wav_player_view_set_context(app->view, app->queue);
  190. wav_player_view_set_ctrl_callback(app->view, ctrl_callback);
  191. bool eof = fill_data(app, 0);
  192. eof = fill_data(app, app->samples_count_half);
  193. wav_player_speaker_init();
  194. wav_player_dma_init((uint32_t)app->sample_buffer, app->samples_count);
  195. furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, wav_player_dma_isr, app->queue);
  196. if(furi_hal_speaker_acquire(1000)) {
  197. wav_player_dma_start();
  198. wav_player_speaker_start();
  199. WavPlayerEvent event;
  200. while(1) {
  201. if(furi_message_queue_get(app->queue, &event, FuriWaitForever) == FuriStatusOk) {
  202. if(event.type == WavPlayerEventHalfTransfer) {
  203. eof = fill_data(app, 0);
  204. wav_player_view_set_current(app->view, stream_tell(app->stream));
  205. if(eof) {
  206. stream_seek(
  207. app->stream,
  208. wav_parser_get_data_start(app->parser),
  209. StreamOffsetFromStart);
  210. }
  211. } else if(event.type == WavPlayerEventFullTransfer) {
  212. eof = fill_data(app, app->samples_count_half);
  213. wav_player_view_set_current(app->view, stream_tell(app->stream));
  214. if(eof) {
  215. stream_seek(
  216. app->stream,
  217. wav_parser_get_data_start(app->parser),
  218. StreamOffsetFromStart);
  219. }
  220. } else if(event.type == WavPlayerEventCtrlVolUp) {
  221. if(app->volume < 9.9) app->volume += 0.4;
  222. wav_player_view_set_volume(app->view, app->volume);
  223. } else if(event.type == WavPlayerEventCtrlVolDn) {
  224. if(app->volume > 0.01) app->volume -= 0.4;
  225. wav_player_view_set_volume(app->view, app->volume);
  226. } else if(event.type == WavPlayerEventCtrlMoveL) {
  227. int32_t seek =
  228. stream_tell(app->stream) - wav_parser_get_data_start(app->parser);
  229. seek =
  230. MIN(seek, (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100));
  231. stream_seek(app->stream, -seek, StreamOffsetFromCurrent);
  232. wav_player_view_set_current(app->view, stream_tell(app->stream));
  233. } else if(event.type == WavPlayerEventCtrlMoveR) {
  234. int32_t seek = wav_parser_get_data_end(app->parser) - stream_tell(app->stream);
  235. seek =
  236. MIN(seek, (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100));
  237. stream_seek(app->stream, seek, StreamOffsetFromCurrent);
  238. wav_player_view_set_current(app->view, stream_tell(app->stream));
  239. } else if(event.type == WavPlayerEventCtrlOk) {
  240. app->play = !app->play;
  241. wav_player_view_set_play(app->view, app->play);
  242. if(!app->play) {
  243. wav_player_speaker_stop();
  244. } else {
  245. wav_player_speaker_start();
  246. }
  247. } else if(event.type == WavPlayerEventCtrlBack) {
  248. break;
  249. }
  250. }
  251. }
  252. wav_player_speaker_stop();
  253. wav_player_dma_stop();
  254. furi_hal_speaker_release();
  255. }
  256. furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, NULL, NULL);
  257. }
  258. int32_t wav_player_app(void* p) {
  259. UNUSED(p);
  260. WavPlayerApp* app = app_alloc();
  261. Storage* storage = furi_record_open(RECORD_STORAGE);
  262. if(!storage_simply_mkdir(storage, WAVPLAYER_FOLDER)) {
  263. FURI_LOG_E(TAG, "Could not create folder %s", WAVPLAYER_FOLDER);
  264. }
  265. furi_record_close(RECORD_STORAGE);
  266. app_run(app);
  267. app_free(app);
  268. return 0;
  269. }