output_embedded.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This software is released under the MIT License.
  8. * https://opensource.org/licenses/MIT
  9. *
  10. */
  11. #include "squeezelite.h"
  12. #include "equalizer.h"
  13. extern struct outputstate output;
  14. extern struct buffer *outputbuf;
  15. static bool (*slimp_handler_chain)(u8_t *data, int len);
  16. #define FRAME_BLOCK MAX_SILENCE_FRAMES
  17. #define LOCK mutex_lock(outputbuf->mutex)
  18. #define UNLOCK mutex_unlock(outputbuf->mutex)
  19. // output_bt.c
  20. extern void output_init_bt(log_level level, char *device, unsigned output_buf_size, char *params,
  21. unsigned rates[], unsigned rate_delay, unsigned idle);
  22. extern void output_close_bt(void);
  23. // output_i2s.c
  24. extern void output_init_i2s(log_level level, char *device, unsigned output_buf_size, char *params,
  25. unsigned rates[], unsigned rate_delay, unsigned idle);
  26. extern bool output_volume_i2s(unsigned left, unsigned right);
  27. extern void output_close_i2s(void);
  28. // controls.c
  29. extern void cli_controls_init(void);
  30. static log_level loglevel;
  31. static bool (*volume_cb)(unsigned left, unsigned right);
  32. static void (*close_cb)(void);
  33. #pragma pack(push, 1)
  34. struct eqlz_packet {
  35. char opcode[4];
  36. };
  37. #pragma pack(pop)
  38. static bool handler(u8_t *data, int len){
  39. bool res = true;
  40. if (!strncmp((char*) data, "eqlz", 4)) {
  41. s8_t *gain = (s8_t*) (data + sizeof(struct eqlz_packet));
  42. LOG_INFO("got equalizer %d", len);
  43. // update will be done at next opportunity
  44. equalizer_update(gain);
  45. } else {
  46. res = false;
  47. }
  48. // chain protocol handlers (bitwise or is fine)
  49. if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
  50. return res;
  51. }
  52. void output_init_embedded(log_level level, char *device, unsigned output_buf_size, char *params,
  53. unsigned rates[], unsigned rate_delay, unsigned idle) {
  54. loglevel = level;
  55. LOG_INFO("init device: %s", device);
  56. // chain handlers
  57. slimp_handler_chain = slimp_handler;
  58. slimp_handler = handler;
  59. // init equalizer before backends
  60. equalizer_init();
  61. memset(&output, 0, sizeof(output));
  62. output_init_common(level, device, output_buf_size, rates, idle);
  63. output.start_frames = FRAME_BLOCK;
  64. output.rate_delay = rate_delay;
  65. #if CONFIG_BT_SINK
  66. if (strcasestr(device, "BT")) {
  67. LOG_INFO("init Bluetooth");
  68. close_cb = &output_close_bt;
  69. output_init_bt(level, device, output_buf_size, params, rates, rate_delay, idle);
  70. } else
  71. #endif
  72. {
  73. LOG_INFO("init I2S/SPDIF");
  74. close_cb = &output_close_i2s;
  75. volume_cb = &output_volume_i2s;
  76. output_init_i2s(level, device, output_buf_size, params, rates, rate_delay, idle);
  77. }
  78. output_visu_init(level);
  79. LOG_INFO("init completed.");
  80. }
  81. void output_close_embedded(void) {
  82. LOG_INFO("close output");
  83. if (close_cb) (*close_cb)();
  84. output_close_common();
  85. output_visu_close();
  86. }
  87. void set_volume(unsigned left, unsigned right) {
  88. LOG_DEBUG("setting internal gain left: %u right: %u", left, right);
  89. if (!volume_cb || !(*volume_cb)(left, right)) {
  90. LOCK;
  91. output.gainL = left;
  92. output.gainR = right;
  93. UNLOCK;
  94. }
  95. }
  96. bool test_open(const char *device, unsigned rates[], bool userdef_rates) {
  97. memset(rates, 0, MAX_SUPPORTED_SAMPLERATES * sizeof(unsigned));
  98. if (!strcasecmp(device, "I2S")) {
  99. unsigned _rates[] = {
  100. #if BYTES_PER_FRAME == 4
  101. 192000, 176400,
  102. #endif
  103. 96000, 88200, 48000,
  104. 44100, 32000, 24000, 22050, 16000,
  105. 12000, 11025, 8000, 0 };
  106. memcpy(rates, _rates, sizeof(_rates));
  107. } else if (!strcasecmp(device, "SPDIF")) {
  108. unsigned _rates[] = { 96000, 88200, 48000,
  109. 44100, 32000, 24000, 22050, 16000,
  110. 12000, 11025, 8000, 0 };
  111. memcpy(rates, _rates, sizeof(_rates));
  112. } else {
  113. rates[0] = 44100;
  114. }
  115. return true;
  116. }
  117. char* output_state_str(void){
  118. output_state state;
  119. LOCK;
  120. state = output.state;
  121. UNLOCK;
  122. switch (state) {
  123. case OUTPUT_OFF: return STR(OUTPUT_OFF);
  124. case OUTPUT_STOPPED: return STR(OUTPUT_STOPPED);
  125. case OUTPUT_BUFFER: return STR(OUTPUT_BUFFER);
  126. case OUTPUT_RUNNING: return STR(OUTPUT_RUNNING);
  127. case OUTPUT_PAUSE_FRAMES: return STR(OUTPUT_PAUSE_FRAMES);
  128. case OUTPUT_SKIP_FRAMES: return STR(OUTPUT_SKIP_FRAMES);
  129. case OUTPUT_START_AT: return STR(OUTPUT_START_AT);
  130. default: return "OUTPUT_UNKNOWN_STATE";
  131. }
  132. }
  133. bool output_stopped(void) {
  134. output_state state;
  135. LOCK;
  136. state = output.state;
  137. UNLOCK;
  138. return state <= OUTPUT_STOPPED;
  139. }