wav_player.c 16 KB

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