output_dac.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "squeezelite.h"
  2. #include <signal.h>
  3. static log_level loglevel;
  4. static bool running = true;
  5. extern struct outputstate output;
  6. extern struct buffer *outputbuf;
  7. #define LOCK mutex_lock(outputbuf->mutex)
  8. #define UNLOCK mutex_unlock(outputbuf->mutex)
  9. #define FRAME_BLOCK MAX_SILENCE_FRAMES
  10. extern u8_t *silencebuf;
  11. // buffer to hold output data so we can block on writing outside of output lock, allocated on init
  12. static u8_t *buf;
  13. static unsigned buffill;
  14. static int bytes_per_frame;
  15. static thread_type thread;
  16. static int _dac_write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  17. s32_t cross_gain_in, s32_t cross_gain_out, s32_t **cross_ptr);
  18. static void *output_thread();
  19. void set_volume(unsigned left, unsigned right) {}
  20. void output_init_dac(log_level level, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle) {
  21. loglevel = level;
  22. LOG_INFO("init output DAC");
  23. buf = malloc(FRAME_BLOCK * BYTES_PER_FRAME);
  24. if (!buf) {
  25. LOG_ERROR("unable to malloc buf");
  26. return;
  27. }
  28. buffill = 0;
  29. memset(&output, 0, sizeof(output));
  30. output.format = S32_LE;
  31. output.start_frames = FRAME_BLOCK * 2;
  32. output.write_cb = &_dac_write_frames;
  33. output.rate_delay = rate_delay;
  34. if (params) {
  35. if (!strcmp(params, "32")) output.format = S32_LE;
  36. if (!strcmp(params, "24")) output.format = S24_3LE;
  37. if (!strcmp(params, "16")) output.format = S16_LE;
  38. }
  39. // ensure output rate is specified to avoid test open
  40. if (!rates[0]) {
  41. rates[0] = 44100;
  42. }
  43. output_init_common(level, "-", output_buf_size, rates, 0);
  44. #if LINUX || OSX || FREEBSD || POSIX
  45. pthread_attr_t attr;
  46. pthread_attr_init(&attr);
  47. #ifdef PTHREAD_STACK_MIN
  48. pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + OUTPUT_THREAD_STACK_SIZE);
  49. #endif
  50. pthread_create(&thread, &attr, output_thread, NULL);
  51. pthread_attr_destroy(&attr);
  52. #endif
  53. #if WIN
  54. thread = CreateThread(NULL, OUTPUT_THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)&output_thread, NULL, 0, NULL);
  55. #endif
  56. }
  57. void output_close_dac(void) {
  58. LOG_INFO("close output");
  59. LOCK;
  60. running = false;
  61. UNLOCK;
  62. free(buf);
  63. output_close_common();
  64. }
  65. static int _dac_write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  66. s32_t cross_gain_in, s32_t cross_gain_out, s32_t **cross_ptr) {
  67. u8_t *obuf;
  68. if (!silence) {
  69. if (output.fade == FADE_ACTIVE && output.fade_dir == FADE_CROSS && *cross_ptr) {
  70. _apply_cross(outputbuf, out_frames, cross_gain_in, cross_gain_out, cross_ptr);
  71. }
  72. obuf = outputbuf->readp;
  73. } else {
  74. obuf = silencebuf;
  75. }
  76. _scale_and_pack_frames(buf + buffill * bytes_per_frame, (s32_t *)(void *)obuf, out_frames, gainL, gainR, output.format);
  77. buffill += out_frames;
  78. return (int)out_frames;
  79. }
  80. static void *output_thread() {
  81. LOCK;
  82. switch (output.format) {
  83. case S32_LE:
  84. bytes_per_frame = 4 * 2; break;
  85. case S24_3LE:
  86. bytes_per_frame = 3 * 2; break;
  87. case S16_LE:
  88. bytes_per_frame = 2 * 2; break;
  89. default:
  90. bytes_per_frame = 4 * 2; break;
  91. break;
  92. }
  93. UNLOCK;
  94. while (running) {
  95. LOCK;
  96. output.device_frames = 0;
  97. output.updated = gettime_ms();
  98. output.frames_played_dmp = output.frames_played;
  99. _output_frames(FRAME_BLOCK);
  100. UNLOCK;
  101. if (buffill) {
  102. //fwrite(buf, bytes_per_frame, buffill, stdout);
  103. buffill = 0;
  104. }
  105. }
  106. return 0;
  107. }