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 <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 && app->bits_per_sample == 8)
  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 == 1 && app->bits_per_sample == 16)
  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. int16_t int_16 = (((int16_t)app->tmp_buffer[i + 1] << 8) + (int16_t)app->tmp_buffer[i]);
  149. float data = ((float)int_16 / 256.0 + 127.0);
  150. data -= UINT8_MAX / 2; // to signed
  151. data /= UINT8_MAX / 2; // scale -1..1
  152. data *= app->volume; // volume
  153. data = tanhf(data); // hyperbolic tangent limiter
  154. data *= UINT8_MAX / 2; // scale -128..127
  155. data += UINT8_MAX / 2; // to unsigned
  156. if(data < 0) {
  157. data = 0;
  158. }
  159. if(data > 255) {
  160. data = 255;
  161. }
  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. if(app->num_channels == 2 && app->bits_per_sample == 16)
  168. {
  169. uint16_t* sample_buffer_start = &app->sample_buffer[index];
  170. size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
  171. for(size_t i = 0; i < app->samples_count; i += 4)
  172. {
  173. int16_t L = (((int16_t)app->tmp_buffer[i + 1] << 8) + (int16_t)app->tmp_buffer[i]);
  174. int16_t R = (((int16_t)app->tmp_buffer[i + 3] << 8) + (int16_t)app->tmp_buffer[i + 2]);
  175. int32_t int_16 = L / 2 + R / 2; // (L + R) / 2
  176. float data = ((float)int_16 / 256.0 + 127.0);
  177. data -= UINT8_MAX / 2; // to signed
  178. data /= UINT8_MAX / 2; // scale -1..1
  179. data *= app->volume; // volume
  180. data = tanhf(data); // hyperbolic tangent limiter
  181. data *= UINT8_MAX / 2; // scale -128..127
  182. data += UINT8_MAX / 2; // to unsigned
  183. if(data < 0) {
  184. data = 0;
  185. }
  186. if(data > 255) {
  187. data = 255;
  188. }
  189. sample_buffer_start[i / 4] = data;
  190. }
  191. count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
  192. for(size_t i = 0; i < app->samples_count; i += 4)
  193. {
  194. int16_t L = (((int16_t)app->tmp_buffer[i + 1] << 8) + (int16_t)app->tmp_buffer[i]);
  195. int16_t R = (((int16_t)app->tmp_buffer[i + 3] << 8) + (int16_t)app->tmp_buffer[i + 2]);
  196. int32_t int_16 = L / 2 + R / 2; // (L + R) / 2
  197. float data = ((float)int_16 / 256.0 + 127.0);
  198. data -= UINT8_MAX / 2; // to signed
  199. data /= UINT8_MAX / 2; // scale -1..1
  200. data *= app->volume; // volume
  201. data = tanhf(data); // hyperbolic tangent limiter
  202. data *= UINT8_MAX / 2; // scale -128..127
  203. data += UINT8_MAX / 2; // to unsigned
  204. if(data < 0) {
  205. data = 0;
  206. }
  207. if(data > 255) {
  208. data = 255;
  209. }
  210. sample_buffer_start[i / 4 + app->samples_count / 4] = data;
  211. }
  212. wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
  213. return count != app->samples_count;
  214. }
  215. if(app->num_channels == 2 && app->bits_per_sample == 8)
  216. {
  217. uint16_t* sample_buffer_start = &app->sample_buffer[index];
  218. size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
  219. for(size_t i = count; i < app->samples_count; i++) {
  220. app->tmp_buffer[i] = 0;
  221. }
  222. for(size_t i = 0; i < app->samples_count; i += 2)
  223. {
  224. float data = (app->tmp_buffer[i] + app->tmp_buffer[i + 1]) / 2; // (L + R) / 2
  225. data -= UINT8_MAX / 2; // to signed
  226. data /= UINT8_MAX / 2; // scale -1..1
  227. data *= app->volume; // volume
  228. data = tanhf(data); // hyperbolic tangent limiter
  229. data *= UINT8_MAX / 2; // scale -128..127
  230. data += UINT8_MAX / 2; // to unsigned
  231. if(data < 0) {
  232. data = 0;
  233. }
  234. if(data > 255) {
  235. data = 255;
  236. }
  237. //uint8_t data = app->tmp_buffer[i];
  238. sample_buffer_start[i / 2] = data;
  239. }
  240. wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
  241. return count != app->samples_count;
  242. }
  243. return true;
  244. }
  245. static void ctrl_callback(WavPlayerCtrl ctrl, void* ctx) {
  246. FuriMessageQueue* event_queue = ctx;
  247. WavPlayerEvent event;
  248. switch(ctrl) {
  249. case WavPlayerCtrlVolUp:
  250. event.type = WavPlayerEventCtrlVolUp;
  251. furi_message_queue_put(event_queue, &event, 0);
  252. break;
  253. case WavPlayerCtrlVolDn:
  254. event.type = WavPlayerEventCtrlVolDn;
  255. furi_message_queue_put(event_queue, &event, 0);
  256. break;
  257. case WavPlayerCtrlMoveL:
  258. event.type = WavPlayerEventCtrlMoveL;
  259. furi_message_queue_put(event_queue, &event, 0);
  260. break;
  261. case WavPlayerCtrlMoveR:
  262. event.type = WavPlayerEventCtrlMoveR;
  263. furi_message_queue_put(event_queue, &event, 0);
  264. break;
  265. case WavPlayerCtrlOk:
  266. event.type = WavPlayerEventCtrlOk;
  267. furi_message_queue_put(event_queue, &event, 0);
  268. break;
  269. case WavPlayerCtrlBack:
  270. event.type = WavPlayerEventCtrlBack;
  271. furi_message_queue_put(event_queue, &event, 0);
  272. break;
  273. default:
  274. break;
  275. }
  276. }
  277. static void app_run(WavPlayerApp* app) {
  278. if(!open_wav_stream(app->stream)) return;
  279. if(!wav_parser_parse(app->parser, app->stream, app)) return;
  280. wav_player_view_set_volume(app->view, app->volume);
  281. wav_player_view_set_start(app->view, wav_parser_get_data_start(app->parser));
  282. wav_player_view_set_current(app->view, stream_tell(app->stream));
  283. wav_player_view_set_end(app->view, wav_parser_get_data_end(app->parser));
  284. wav_player_view_set_play(app->view, app->play);
  285. wav_player_view_set_context(app->view, app->queue);
  286. wav_player_view_set_ctrl_callback(app->view, ctrl_callback);
  287. bool eof = fill_data(app, 0);
  288. eof = fill_data(app, app->samples_count_half);
  289. wav_player_speaker_init(app->sample_rate);
  290. wav_player_dma_init((uint32_t)app->sample_buffer, app->samples_count);
  291. furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, wav_player_dma_isr, app->queue);
  292. if(furi_hal_speaker_acquire(1000)) {
  293. wav_player_dma_start();
  294. wav_player_speaker_start();
  295. WavPlayerEvent event;
  296. while(1) {
  297. if(furi_message_queue_get(app->queue, &event, FuriWaitForever) == FuriStatusOk) {
  298. if(event.type == WavPlayerEventHalfTransfer) {
  299. wav_player_view_set_chans(app->view, app->num_channels);
  300. wav_player_view_set_bits(app->view, app->bits_per_sample);
  301. eof = fill_data(app, 0);
  302. wav_player_view_set_current(app->view, stream_tell(app->stream));
  303. if(eof) {
  304. stream_seek(
  305. app->stream,
  306. wav_parser_get_data_start(app->parser),
  307. StreamOffsetFromStart);
  308. }
  309. } else if(event.type == WavPlayerEventFullTransfer) {
  310. wav_player_view_set_chans(app->view, app->num_channels);
  311. wav_player_view_set_bits(app->view, app->bits_per_sample);
  312. eof = fill_data(app, app->samples_count_half);
  313. wav_player_view_set_current(app->view, stream_tell(app->stream));
  314. if(eof) {
  315. stream_seek(
  316. app->stream,
  317. wav_parser_get_data_start(app->parser),
  318. StreamOffsetFromStart);
  319. }
  320. } else if(event.type == WavPlayerEventCtrlVolUp) {
  321. if(app->volume < 9.9) app->volume += 0.4;
  322. wav_player_view_set_volume(app->view, app->volume);
  323. } else if(event.type == WavPlayerEventCtrlVolDn) {
  324. if(app->volume > 0.01) app->volume -= 0.4;
  325. wav_player_view_set_volume(app->view, app->volume);
  326. } else if(event.type == WavPlayerEventCtrlMoveL) {
  327. int32_t seek =
  328. stream_tell(app->stream) - wav_parser_get_data_start(app->parser);
  329. seek =
  330. MIN(seek, (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100) % 2 ? ((int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100) - 1) : (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100));
  331. stream_seek(app->stream, -seek, StreamOffsetFromCurrent);
  332. wav_player_view_set_current(app->view, stream_tell(app->stream));
  333. } else if(event.type == WavPlayerEventCtrlMoveR) {
  334. int32_t seek = wav_parser_get_data_end(app->parser) - stream_tell(app->stream);
  335. seek =
  336. MIN(seek, (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100) % 2 ? ((int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100) - 1) : (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100));
  337. stream_seek(app->stream, seek, StreamOffsetFromCurrent);
  338. wav_player_view_set_current(app->view, stream_tell(app->stream));
  339. } else if(event.type == WavPlayerEventCtrlOk) {
  340. app->play = !app->play;
  341. wav_player_view_set_play(app->view, app->play);
  342. if(!app->play) {
  343. wav_player_speaker_stop();
  344. } else {
  345. wav_player_speaker_start();
  346. }
  347. } else if(event.type == WavPlayerEventCtrlBack) {
  348. break;
  349. }
  350. }
  351. }
  352. wav_player_speaker_stop();
  353. wav_player_dma_stop();
  354. furi_hal_speaker_release();
  355. }
  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. }