2
0

output_bt.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "squeezelite.h"
  2. static log_level loglevel;
  3. static bool running = true;
  4. extern struct outputstate output;
  5. extern struct buffer *outputbuf;
  6. extern struct buffer *streambuf;
  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. extern u8_t *bt_optr;
  12. void hal_bluetooth_init(log_level);
  13. static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  14. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr);
  15. #if BTAUDIO
  16. void set_volume(unsigned left, unsigned right) {
  17. LOG_DEBUG("setting internal gain left: %u right: %u", left, right);
  18. LOCK;
  19. output.gainL = left;
  20. output.gainR = right;
  21. // TODO
  22. output.gainL = FIXED_ONE;
  23. output.gainR = FIXED_ONE;
  24. UNLOCK;
  25. }
  26. #endif
  27. void output_init_bt(log_level level, char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle) {
  28. loglevel = level;
  29. LOG_INFO("init output BT");
  30. memset(&output, 0, sizeof(output));
  31. output.start_frames = 0; //CONFIG_ //FRAME_BLOCK * 2;
  32. output.write_cb = &_write_frames;
  33. output.rate_delay = rate_delay;
  34. // ensure output rate is specified to avoid test open
  35. if (!rates[0]) {
  36. rates[0] = 44100;
  37. }
  38. hal_bluetooth_init(loglevel);
  39. /*
  40. * Bluetooth audio source init Start
  41. */
  42. device = "BT";
  43. output_init_common(level, device, output_buf_size, rates, idle);
  44. }
  45. void output_close_bt(void) {
  46. LOG_INFO("close output");
  47. LOCK;
  48. running = false;
  49. UNLOCK;
  50. output_close_common();
  51. }
  52. static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  53. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr) {
  54. if (!silence ) {
  55. DEBUG_LOG_TIMED(200,"Not silence, Writing audio out.");
  56. /* TODO need 16 bit fix
  57. if (output.fade == FADE_ACTIVE && output.fade_dir == FADE_CROSS && *cross_ptr) {
  58. _apply_cross(outputbuf, out_frames, cross_gain_in, cross_gain_out, cross_ptr);
  59. }
  60. if (gainL != FIXED_ONE || gainR!= FIXED_ONE) {
  61. _apply_gain(outputbuf, out_frames, gainL, gainR);
  62. }
  63. */
  64. #if BYTES_PER_FRAME == 4
  65. memcpy(bt_optr, outputbuf->readp, out_frames * BYTES_PER_FRAME);
  66. #else
  67. {
  68. frames_t count = out_frames;
  69. s32_t *_iptr = (s32_t*) outputbuf->readp;
  70. s16_t *_optr = (s16_t*) bt_optr;
  71. while (count--) {
  72. *_optr++ = *_iptr++ >> 16;
  73. *_optr++ = *_iptr++ >> 16;
  74. }
  75. }
  76. #endif
  77. } else {
  78. DEBUG_LOG_TIMED(200,"Silence flag true. Writing silence to audio out.");
  79. u8_t *buf = silencebuf;
  80. memcpy(bt_optr, buf, out_frames * 4);
  81. }
  82. bt_optr += out_frames * 4;
  83. return (int)out_frames;
  84. }