wav_player.c 12 KB

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