wav_player.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. #include <assets_icons.h>
  16. #define TAG "WavPlayer"
  17. #define WAVPLAYER_FOLDER "/ext/wav_player"
  18. static bool open_wav_stream(WavPlayerApp* app) {
  19. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  20. bool result = false;
  21. if(furi_string_empty(app->path)) {
  22. furi_string_set(app->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, app->path, app->path, &browser_options);
  28. furi_record_close(RECORD_DIALOGS);
  29. if(!ret) return false;
  30. }
  31. if(!file_stream_open(
  32. app->stream, furi_string_get_cstr(app->path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  33. FURI_LOG_E(TAG, "Cannot open file \"%s\"", furi_string_get_cstr(app->path));
  34. } else {
  35. result = true;
  36. }
  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. app->path = furi_string_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. furi_string_free(app->path);
  104. notification_message(app->notification, &sequence_display_backlight_enforce_auto);
  105. furi_record_close(RECORD_NOTIFICATION);
  106. free(app);
  107. }
  108. // TODO: that works only with 8-bit 2ch audio
  109. static bool fill_data(WavPlayerApp* app, size_t index) {
  110. if(app->num_channels == 1 && app->bits_per_sample == 8) {
  111. uint16_t* sample_buffer_start = &app->sample_buffer[index];
  112. size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count_half);
  113. for(size_t i = count; i < app->samples_count_half; i++) {
  114. app->tmp_buffer[i] = 0;
  115. }
  116. //for(size_t i = 0; i < app->samples_count; i += 2)
  117. for(size_t i = 0; i < app->samples_count_half; i++) //now works with mono!
  118. {
  119. float data = app->tmp_buffer[i];
  120. data -= UINT8_MAX / 2; // to signed
  121. data /= UINT8_MAX / 2; // scale -1..1
  122. data *= app->volume; // volume
  123. data = tanhf(data); // hyperbolic tangent limiter
  124. data *= UINT8_MAX / 2; // scale -128..127
  125. data += UINT8_MAX / 2; // to unsigned
  126. if(data < 0) {
  127. data = 0;
  128. }
  129. if(data > 255) {
  130. data = 255;
  131. }
  132. //uint8_t data = app->tmp_buffer[i];
  133. sample_buffer_start[i] = data;
  134. }
  135. wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
  136. return count != app->samples_count_half;
  137. }
  138. if(app->num_channels == 1 && app->bits_per_sample == 16) {
  139. uint16_t* sample_buffer_start = &app->sample_buffer[index];
  140. size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
  141. for(size_t i = count; i < app->samples_count; i++) {
  142. //app->tmp_buffer[i] = 0;
  143. }
  144. for(size_t i = 0; i < app->samples_count; i += 2) {
  145. int16_t int_16 =
  146. (((int16_t)app->tmp_buffer[i + 1] << 8) + (int16_t)app->tmp_buffer[i]);
  147. float data = ((float)int_16 / 256.0 + 127.0);
  148. data -= UINT8_MAX / 2; // to signed
  149. data /= UINT8_MAX / 2; // scale -1..1
  150. data *= app->volume; // volume
  151. data = tanhf(data); // hyperbolic tangent limiter
  152. data *= UINT8_MAX / 2; // scale -128..127
  153. data += UINT8_MAX / 2; // to unsigned
  154. if(data < 0) {
  155. data = 0;
  156. }
  157. if(data > 255) {
  158. data = 255;
  159. }
  160. sample_buffer_start[i / 2] = data;
  161. }
  162. wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
  163. return count != app->samples_count;
  164. }
  165. if(app->num_channels == 2 && app->bits_per_sample == 16) {
  166. uint16_t* sample_buffer_start = &app->sample_buffer[index];
  167. size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
  168. for(size_t i = 0; i < app->samples_count; i += 4) {
  169. int16_t L = (((int16_t)app->tmp_buffer[i + 1] << 8) + (int16_t)app->tmp_buffer[i]);
  170. int16_t R = (((int16_t)app->tmp_buffer[i + 3] << 8) + (int16_t)app->tmp_buffer[i + 2]);
  171. int32_t int_16 = L / 2 + R / 2; // (L + R) / 2
  172. float data = ((float)int_16 / 256.0 + 127.0);
  173. data -= UINT8_MAX / 2; // to signed
  174. data /= UINT8_MAX / 2; // scale -1..1
  175. data *= app->volume; // volume
  176. data = tanhf(data); // hyperbolic tangent limiter
  177. data *= UINT8_MAX / 2; // scale -128..127
  178. data += UINT8_MAX / 2; // to unsigned
  179. if(data < 0) {
  180. data = 0;
  181. }
  182. if(data > 255) {
  183. data = 255;
  184. }
  185. sample_buffer_start[i / 4] = data;
  186. }
  187. count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
  188. for(size_t i = 0; i < app->samples_count; i += 4) {
  189. int16_t L = (((int16_t)app->tmp_buffer[i + 1] << 8) + (int16_t)app->tmp_buffer[i]);
  190. int16_t R = (((int16_t)app->tmp_buffer[i + 3] << 8) + (int16_t)app->tmp_buffer[i + 2]);
  191. int32_t int_16 = L / 2 + R / 2; // (L + R) / 2
  192. float data = ((float)int_16 / 256.0 + 127.0);
  193. data -= UINT8_MAX / 2; // to signed
  194. data /= UINT8_MAX / 2; // scale -1..1
  195. data *= app->volume; // volume
  196. data = tanhf(data); // hyperbolic tangent limiter
  197. data *= UINT8_MAX / 2; // scale -128..127
  198. data += UINT8_MAX / 2; // to unsigned
  199. if(data < 0) {
  200. data = 0;
  201. }
  202. if(data > 255) {
  203. data = 255;
  204. }
  205. sample_buffer_start[i / 4 + app->samples_count / 4] = data;
  206. }
  207. wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
  208. return count != app->samples_count;
  209. }
  210. if(app->num_channels == 2 && app->bits_per_sample == 8) {
  211. uint16_t* sample_buffer_start = &app->sample_buffer[index];
  212. size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
  213. for(size_t i = count; i < app->samples_count; i++) {
  214. app->tmp_buffer[i] = 0;
  215. }
  216. for(size_t i = 0; i < app->samples_count; i += 2) {
  217. float data = (app->tmp_buffer[i] + app->tmp_buffer[i + 1]) / 2; // (L + R) / 2
  218. data -= UINT8_MAX / 2; // to signed
  219. data /= UINT8_MAX / 2; // scale -1..1
  220. data *= app->volume; // volume
  221. data = tanhf(data); // hyperbolic tangent limiter
  222. data *= UINT8_MAX / 2; // scale -128..127
  223. data += UINT8_MAX / 2; // to unsigned
  224. if(data < 0) {
  225. data = 0;
  226. }
  227. if(data > 255) {
  228. data = 255;
  229. }
  230. //uint8_t data = app->tmp_buffer[i];
  231. sample_buffer_start[i / 2] = data;
  232. }
  233. wav_player_view_set_data(app->view, sample_buffer_start, app->samples_count_half);
  234. return count != app->samples_count;
  235. }
  236. return true;
  237. }
  238. static void ctrl_callback(WavPlayerCtrl ctrl, void* ctx) {
  239. FuriMessageQueue* event_queue = ctx;
  240. WavPlayerEvent event;
  241. switch(ctrl) {
  242. case WavPlayerCtrlVolUp:
  243. event.type = WavPlayerEventCtrlVolUp;
  244. furi_message_queue_put(event_queue, &event, 0);
  245. break;
  246. case WavPlayerCtrlVolDn:
  247. event.type = WavPlayerEventCtrlVolDn;
  248. furi_message_queue_put(event_queue, &event, 0);
  249. break;
  250. case WavPlayerCtrlMoveL:
  251. event.type = WavPlayerEventCtrlMoveL;
  252. furi_message_queue_put(event_queue, &event, 0);
  253. break;
  254. case WavPlayerCtrlMoveR:
  255. event.type = WavPlayerEventCtrlMoveR;
  256. furi_message_queue_put(event_queue, &event, 0);
  257. break;
  258. case WavPlayerCtrlOk:
  259. event.type = WavPlayerEventCtrlOk;
  260. furi_message_queue_put(event_queue, &event, 0);
  261. break;
  262. case WavPlayerCtrlBack:
  263. event.type = WavPlayerEventCtrlBack;
  264. furi_message_queue_put(event_queue, &event, 0);
  265. break;
  266. default:
  267. break;
  268. }
  269. }
  270. static void app_run(WavPlayerApp* app) {
  271. if(!open_wav_stream(app)) return;
  272. if(!wav_parser_parse(app->parser, app->stream, app)) return;
  273. wav_player_view_set_volume(app->view, app->volume);
  274. wav_player_view_set_start(app->view, wav_parser_get_data_start(app->parser));
  275. wav_player_view_set_current(app->view, stream_tell(app->stream));
  276. wav_player_view_set_end(app->view, wav_parser_get_data_end(app->parser));
  277. wav_player_view_set_play(app->view, app->play);
  278. wav_player_view_set_context(app->view, app->queue);
  279. wav_player_view_set_ctrl_callback(app->view, ctrl_callback);
  280. bool eof = fill_data(app, 0);
  281. eof = fill_data(app, app->samples_count_half);
  282. if(furi_hal_speaker_acquire(1000)) {
  283. wav_player_speaker_init(app->sample_rate);
  284. wav_player_dma_init((uint32_t)app->sample_buffer, app->samples_count);
  285. furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, wav_player_dma_isr, app->queue);
  286. wav_player_dma_start();
  287. wav_player_speaker_start();
  288. WavPlayerEvent event;
  289. while(1) {
  290. if(furi_message_queue_get(app->queue, &event, FuriWaitForever) == FuriStatusOk) {
  291. if(event.type == WavPlayerEventHalfTransfer) {
  292. wav_player_view_set_chans(app->view, app->num_channels);
  293. wav_player_view_set_bits(app->view, app->bits_per_sample);
  294. eof = fill_data(app, 0);
  295. wav_player_view_set_current(app->view, stream_tell(app->stream));
  296. if(eof) {
  297. stream_seek(
  298. app->stream,
  299. wav_parser_get_data_start(app->parser),
  300. StreamOffsetFromStart);
  301. }
  302. } else if(event.type == WavPlayerEventFullTransfer) {
  303. wav_player_view_set_chans(app->view, app->num_channels);
  304. wav_player_view_set_bits(app->view, app->bits_per_sample);
  305. eof = fill_data(app, app->samples_count_half);
  306. wav_player_view_set_current(app->view, stream_tell(app->stream));
  307. if(eof) {
  308. stream_seek(
  309. app->stream,
  310. wav_parser_get_data_start(app->parser),
  311. StreamOffsetFromStart);
  312. }
  313. } else if(event.type == WavPlayerEventCtrlVolUp) {
  314. if(app->volume < 9.9) app->volume += 0.4;
  315. wav_player_view_set_volume(app->view, app->volume);
  316. } else if(event.type == WavPlayerEventCtrlVolDn) {
  317. if(app->volume > 0.01) app->volume -= 0.4;
  318. wav_player_view_set_volume(app->view, app->volume);
  319. } else if(event.type == WavPlayerEventCtrlMoveL) {
  320. int32_t seek =
  321. stream_tell(app->stream) - wav_parser_get_data_start(app->parser);
  322. seek = MIN(
  323. seek,
  324. (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100) % 2 ?
  325. ((int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100) - 1) :
  326. (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100));
  327. stream_seek(app->stream, -seek, StreamOffsetFromCurrent);
  328. wav_player_view_set_current(app->view, stream_tell(app->stream));
  329. } else if(event.type == WavPlayerEventCtrlMoveR) {
  330. int32_t seek = wav_parser_get_data_end(app->parser) - stream_tell(app->stream);
  331. seek = MIN(
  332. seek,
  333. (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100) % 2 ?
  334. ((int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100) - 1) :
  335. (int32_t)(wav_parser_get_data_len(app->parser) / (size_t)100));
  336. stream_seek(app->stream, seek, StreamOffsetFromCurrent);
  337. wav_player_view_set_current(app->view, stream_tell(app->stream));
  338. } else if(event.type == WavPlayerEventCtrlOk) {
  339. app->play = !app->play;
  340. wav_player_view_set_play(app->view, app->play);
  341. if(!app->play) {
  342. wav_player_speaker_stop();
  343. } else {
  344. wav_player_speaker_start();
  345. }
  346. } else if(event.type == WavPlayerEventCtrlBack) {
  347. break;
  348. }
  349. }
  350. }
  351. wav_player_speaker_stop();
  352. wav_player_dma_stop();
  353. furi_hal_speaker_release();
  354. }
  355. // Reset GPIO pin and bus states
  356. wav_player_hal_deinit();
  357. furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, NULL, NULL);
  358. }
  359. int32_t wav_player_app(void* p) {
  360. const char* args = p;
  361. WavPlayerApp* app = app_alloc();
  362. Storage* storage = furi_record_open(RECORD_STORAGE);
  363. if(!storage_simply_mkdir(storage, WAVPLAYER_FOLDER)) {
  364. FURI_LOG_E(TAG, "Could not create folder %s", WAVPLAYER_FOLDER);
  365. }
  366. furi_record_close(RECORD_STORAGE);
  367. if(args && strlen(args)) {
  368. furi_string_set(app->path, args);
  369. }
  370. app_run(app);
  371. app_free(app);
  372. return 0;
  373. }