wav_player.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. 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. uint16_t* sample_buffer_start = &app->sample_buffer[index];
  110. size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
  111. for(size_t i = count; i < app->samples_count; i++) {
  112. app->tmp_buffer[i] = 0;
  113. }
  114. for(size_t i = 0; i < app->samples_count; i += 2) {
  115. float data = app->tmp_buffer[i];
  116. data -= UINT8_MAX / 2; // to signed
  117. data /= UINT8_MAX / 2; // scale -1..1
  118. data *= app->volume; // volume
  119. data = tanhf(data); // hyperbolic tangent limiter
  120. data *= UINT8_MAX / 2; // scale -128..127
  121. data += UINT8_MAX / 2; // to unsigned
  122. if(data < 0) {
  123. data = 0;
  124. }
  125. if(data > 255) {
  126. data = 255;
  127. }
  128. sample_buffer_start[i / 2] = data;
  129. }
  130. wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
  131. return count != app->samples_count;
  132. }
  133. static void ctrl_callback(WavPlayerCtrl ctrl, void* ctx) {
  134. FuriMessageQueue* event_queue = ctx;
  135. WavPlayerEvent event;
  136. switch(ctrl) {
  137. case WavPlayerCtrlVolUp:
  138. event.type = WavPlayerEventCtrlVolUp;
  139. furi_message_queue_put(event_queue, &event, 0);
  140. break;
  141. case WavPlayerCtrlVolDn:
  142. event.type = WavPlayerEventCtrlVolDn;
  143. furi_message_queue_put(event_queue, &event, 0);
  144. break;
  145. case WavPlayerCtrlMoveL:
  146. event.type = WavPlayerEventCtrlMoveL;
  147. furi_message_queue_put(event_queue, &event, 0);
  148. break;
  149. case WavPlayerCtrlMoveR:
  150. event.type = WavPlayerEventCtrlMoveR;
  151. furi_message_queue_put(event_queue, &event, 0);
  152. break;
  153. case WavPlayerCtrlOk:
  154. event.type = WavPlayerEventCtrlOk;
  155. furi_message_queue_put(event_queue, &event, 0);
  156. break;
  157. case WavPlayerCtrlBack:
  158. event.type = WavPlayerEventCtrlBack;
  159. furi_message_queue_put(event_queue, &event, 0);
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. static void app_run(WavPlayerApp* app) {
  166. if(!open_wav_stream(app->stream)) return;
  167. if(!wav_parser_parse(app->parser, app->stream, app)) return;
  168. wav_player_view_set_volume(app->view, app->volume);
  169. wav_player_view_set_start(app->view, wav_parser_get_data_start(app->parser));
  170. wav_player_view_set_current(app->view, stream_tell(app->stream));
  171. wav_player_view_set_end(app->view, wav_parser_get_data_end(app->parser));
  172. wav_player_view_set_play(app->view, app->play);
  173. wav_player_view_set_context(app->view, app->queue);
  174. wav_player_view_set_ctrl_callback(app->view, ctrl_callback);
  175. bool eof = fill_data(app, 0);
  176. eof = fill_data(app, app->samples_count_half);
  177. wav_player_speaker_init(app->sample_rate);
  178. wav_player_dma_init((uint32_t)app->sample_buffer, app->samples_count);
  179. furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, wav_player_dma_isr, app->queue);
  180. if(furi_hal_speaker_acquire(1000)) {
  181. wav_player_dma_start();
  182. wav_player_speaker_start();
  183. WavPlayerEvent event;
  184. while(1) {
  185. if(furi_message_queue_get(app->queue, &event, FuriWaitForever) == FuriStatusOk) {
  186. if(event.type == WavPlayerEventHalfTransfer) {
  187. eof = fill_data(app, 0);
  188. wav_player_view_set_current(app->view, stream_tell(app->stream));
  189. if(eof) {
  190. stream_seek(
  191. app->stream,
  192. wav_parser_get_data_start(app->parser),
  193. StreamOffsetFromStart);
  194. }
  195. } else if(event.type == WavPlayerEventFullTransfer) {
  196. eof = fill_data(app, app->samples_count_half);
  197. wav_player_view_set_current(app->view, stream_tell(app->stream));
  198. if(eof) {
  199. stream_seek(
  200. app->stream,
  201. wav_parser_get_data_start(app->parser),
  202. StreamOffsetFromStart);
  203. }
  204. } else if(event.type == WavPlayerEventCtrlVolUp) {
  205. if(app->volume < 9.9) app->volume += 0.4;
  206. wav_player_view_set_volume(app->view, app->volume);
  207. } else if(event.type == WavPlayerEventCtrlVolDn) {
  208. if(app->volume > 0.01) app->volume -= 0.4;
  209. wav_player_view_set_volume(app->view, app->volume);
  210. } else if(event.type == WavPlayerEventCtrlMoveL) {
  211. int32_t seek =
  212. stream_tell(app->stream) - wav_parser_get_data_start(app->parser);
  213. seek =
  214. MIN(seek, (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100));
  215. stream_seek(app->stream, -seek, StreamOffsetFromCurrent);
  216. wav_player_view_set_current(app->view, stream_tell(app->stream));
  217. } else if(event.type == WavPlayerEventCtrlMoveR) {
  218. int32_t seek = wav_parser_get_data_end(app->parser) - stream_tell(app->stream);
  219. seek =
  220. MIN(seek, (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100));
  221. stream_seek(app->stream, seek, StreamOffsetFromCurrent);
  222. wav_player_view_set_current(app->view, stream_tell(app->stream));
  223. } else if(event.type == WavPlayerEventCtrlOk) {
  224. app->play = !app->play;
  225. wav_player_view_set_play(app->view, app->play);
  226. if(!app->play) {
  227. wav_player_speaker_stop();
  228. } else {
  229. wav_player_speaker_start();
  230. }
  231. } else if(event.type == WavPlayerEventCtrlBack) {
  232. break;
  233. }
  234. }
  235. }
  236. wav_player_speaker_stop();
  237. wav_player_dma_stop();
  238. furi_hal_speaker_release();
  239. }
  240. furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, NULL, NULL);
  241. }
  242. int32_t wav_player_app(void* p) {
  243. UNUSED(p);
  244. WavPlayerApp* app = app_alloc();
  245. Storage* storage = furi_record_open(RECORD_STORAGE);
  246. if(!storage_simply_mkdir(storage, WAVPLAYER_FOLDER)) {
  247. FURI_LOG_E(TAG, "Could not create folder %s", WAVPLAYER_FOLDER);
  248. }
  249. furi_record_close(RECORD_STORAGE);
  250. app_run(app);
  251. app_free(app);
  252. return 0;
  253. }