output_visu.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Squeezelite - lightweight headless squeezebox emulator
  3. *
  4. * (c) Adrian Smith 2012-2015, triode1@btinternet.com
  5. * Ralph Irving 2015-2017, ralph_irving@hotmail.com
  6. * Philippe_44 2020, philippe_44@outloook.com
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "squeezelite.h"
  23. #define VISUEXPORT_SIZE 2048
  24. EXT_BSS struct visu_export_s visu_export;
  25. static struct visu_export_s *visu = &visu_export;
  26. static log_level loglevel = lINFO;
  27. void output_visu_export(s16_t *frames, frames_t out_frames, u32_t rate, bool silence) {
  28. // no data to process
  29. if (silence) {
  30. visu->running = false;
  31. return;
  32. }
  33. // do not block, try to stuff data put wait for consumer to have used them
  34. if (!pthread_mutex_trylock(&visu->mutex)) {
  35. // don't mix sample rates
  36. if (visu->rate != rate) visu->level = 0;
  37. // stuff buffer up and wait for consumer to read it (should reset level)
  38. if (visu->level < visu->size) {
  39. u32_t space = min(visu->size - visu->level, out_frames * 2) * 2;
  40. memcpy(visu->buffer + visu->level, frames, space);
  41. visu->level += space / 2;
  42. visu->running = true;
  43. visu->rate = rate ? rate : 44100;
  44. }
  45. // mutex must be released
  46. pthread_mutex_unlock(&visu->mutex);
  47. }
  48. }
  49. void output_visu_close(void) {
  50. pthread_mutex_lock(&visu->mutex);
  51. visu->running = false;
  52. free(visu->buffer);
  53. pthread_mutex_unlock(&visu->mutex);
  54. }
  55. void output_visu_init(log_level level) {
  56. loglevel = level;
  57. pthread_mutex_init(&visu->mutex, NULL);
  58. visu->size = VISUEXPORT_SIZE;
  59. visu->running = false;
  60. visu->rate = 44100;
  61. visu->buffer = malloc(VISUEXPORT_SIZE * sizeof(s16_t) * 2);
  62. LOG_INFO("Initialize VISUEXPORT %u 16 bits samples", VISUEXPORT_SIZE);
  63. }