output_bt.c 2.5 KB

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