wav_player.c 17 KB

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