output_dac.c.nocompile 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #if REPACK && BYTES_PER_FRAMES == 4
  8. #error "REPACK is not compatible with BYTES_PER_FRAME=4"
  9. #endif
  10. #define LOCK mutex_lock(outputbuf->mutex)
  11. #define UNLOCK mutex_unlock(outputbuf->mutex)
  12. #define FRAME_BLOCK MAX_SILENCE_FRAMES
  13. extern u8_t *silencebuf;
  14. static u8_t *optr;
  15. static int bytes_per_frame;
  16. static thread_type thread;
  17. static int _dac_write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  18. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr);
  19. static void *output_thread();
  20. void set_volume(unsigned left, unsigned right) {
  21. LOG_DEBUG("setting internal gain left: %u right: %u", left, right);
  22. LOCK;
  23. output.gainL = left;
  24. output.gainR = right;
  25. UNLOCK;
  26. }
  27. void output_init_dac(log_level level, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle) {
  28. loglevel = level;
  29. LOG_INFO("init output DAC");
  30. memset(&output, 0, sizeof(output));
  31. #if BYTES_PER_FRAME == 4
  32. output.format = S16_LE;
  33. #else
  34. output.format = S32_LE;
  35. #endif
  36. output.start_frames = FRAME_BLOCK * 2;
  37. output.write_cb = &_dac_write_frames;
  38. output.rate_delay = rate_delay;
  39. if (params) {
  40. if (!strcmp(params, "32")) output.format = S32_LE;
  41. if (!strcmp(params, "24")) output.format = S24_3LE;
  42. if (!strcmp(params, "16")) output.format = S16_LE;
  43. }
  44. // ensure output rate is specified to avoid test open
  45. if (!rates[0]) {
  46. rates[0] = 44100;
  47. }
  48. output_init_common(level, "-", output_buf_size, rates, idle);
  49. #if LINUX || OSX || FREEBSD || POSIX
  50. pthread_attr_t attr;
  51. pthread_attr_init(&attr);
  52. #ifdef PTHREAD_STACK_MIN
  53. pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + OUTPUT_THREAD_STACK_SIZE);
  54. #endif
  55. pthread_create(&thread, &attr, output_thread, NULL);
  56. pthread_attr_destroy(&attr);
  57. #endif
  58. #if WIN
  59. thread = CreateThread(NULL, OUTPUT_THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)&output_thread, NULL, 0, NULL);
  60. #endif
  61. }
  62. void output_close_dac(void) {
  63. LOG_INFO("close output");
  64. LOCK;
  65. running = false;
  66. UNLOCK;
  67. output_close_common();
  68. }
  69. static int _dac_write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  70. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr) {
  71. u8_t *obuf;
  72. if (!silence) {
  73. if (output.fade == FADE_ACTIVE && output.fade_dir == FADE_CROSS && *cross_ptr) {
  74. _apply_cross(outputbuf, out_frames, cross_gain_in, cross_gain_out, cross_ptr);
  75. }
  76. #if !REPACK
  77. if (gainL != FIXED_ONE || gainR!= FIXED_ONE) {
  78. _apply_gain(outputbuf, out_frames, gainL, gainR);
  79. }
  80. IF_DSD(
  81. if (output.outfmt == DOP) {
  82. update_dop((u32_t *) outputbuf->readp, out_frames, output.invert);
  83. } else if (output.outfmt != PCM && output.invert)
  84. dsd_invert((u32_t *) outputbuf->readp, out_frames);
  85. )
  86. memcpy(optr, outputbuf->readp, out_frames * BYTES_PER_FRAME);
  87. #else
  88. obuf = outputbuf->readp;
  89. #endif
  90. } else {
  91. obuf = silencebuf;
  92. #if !REPACK
  93. IF_DSD(
  94. if (output.outfmt != PCM) {
  95. obuf = silencebuf_dsd;
  96. update_dop((u32_t *) obuf, out_frames, false); // don't invert silence
  97. }
  98. )
  99. memcpy(optr, obuf, out_frames * BYTES_PER_FRAME);
  100. #endif
  101. }
  102. #if REPACK
  103. _scale_and_pack_frames(optr, (s32_t *)(void *)obuf, out_frames, gainL, gainR, output.format);
  104. #endif
  105. return (int)out_frames;
  106. }
  107. static void *output_thread() {
  108. // buffer to hold output data so we can block on writing outside of output lock, allocated on init
  109. u8_t *obuf = malloc(FRAME_BLOCK * BYTES_PER_FRAME);
  110. unsigned frames = 0;
  111. #if REPACK
  112. LOCK;
  113. switch (output.format) {
  114. case S32_LE:
  115. bytes_per_frame = 4 * 2; break;
  116. case S24_3LE:
  117. bytes_per_frame = 3 * 2; break;
  118. case S16_LE:
  119. bytes_per_frame = 2 * 2; break;
  120. default:
  121. bytes_per_frame = 4 * 2; break;
  122. break;
  123. }
  124. UNLOCK;
  125. #endif
  126. while (running) {
  127. LOCK;
  128. if (output.state == OUTPUT_OFF) {
  129. UNLOCK;
  130. usleep(500000);
  131. continue;
  132. }
  133. output.device_frames = 0;
  134. output.updated = gettime_ms();
  135. output.frames_played_dmp = output.frames_played;
  136. optr = obuf + frames * bytes_per_frame;
  137. frames += _output_frames(FRAME_BLOCK);
  138. UNLOCK;
  139. if (frames) {
  140. // do something with some of these frames...
  141. usleep((frames * 1000 * 1000) / output.current_sample_rate);
  142. frames = 0;
  143. } else {
  144. usleep((FRAME_BLOCK * 1000 * 1000) / output.current_sample_rate);
  145. }
  146. }
  147. return 0;
  148. }