output_visu.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 512
  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(void *frames, frames_t out_frames, u32_t rate, bool silence, u32_t gain) {
  28. // no data to process
  29. if (silence) {
  30. visu->running = false;
  31. return;
  32. }
  33. // do not block, try to stuff data but 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) * BYTES_PER_FRAME;
  40. memcpy(visu->buffer + visu->level, frames, space);
  41. visu->level += space / BYTES_PER_FRAME;
  42. visu->running = true;
  43. visu->rate = rate ? rate : 44100;
  44. visu->gain = gain;
  45. }
  46. // mutex must be released
  47. pthread_mutex_unlock(&visu->mutex);
  48. }
  49. }
  50. void output_visu_close(void) {
  51. pthread_mutex_lock(&visu->mutex);
  52. visu->running = false;
  53. free(visu->buffer);
  54. pthread_mutex_unlock(&visu->mutex);
  55. }
  56. void output_visu_init(log_level level) {
  57. loglevel = level;
  58. pthread_mutex_init(&visu->mutex, NULL);
  59. visu->size = VISUEXPORT_SIZE;
  60. visu->running = false;
  61. visu->rate = 44100;
  62. visu->buffer = malloc(VISUEXPORT_SIZE * BYTES_PER_FRAME);
  63. LOG_INFO("Initialize VISUEXPORT %u %u bits samples", VISUEXPORT_SIZE, BYTES_PER_FRAME * 4);
  64. }