wav_player.c 16 KB

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