output_embedded.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 (strcasestr(device, "BT ") || !strcasecmp(device, "BT")) {
  66. LOG_INFO("init Bluetooth");
  67. close_cb = &output_close_bt;
  68. output_init_bt(level, device, output_buf_size, params, rates, rate_delay, idle);
  69. } else {
  70. LOG_INFO("init I2S/SPDIF");
  71. close_cb = &output_close_i2s;
  72. volume_cb = &output_volume_i2s;
  73. output_init_i2s(level, device, output_buf_size, params, rates, rate_delay, idle);
  74. }
  75. output_visu_init(level);
  76. LOG_INFO("init completed.");
  77. }
  78. void output_close_embedded(void) {
  79. LOG_INFO("close output");
  80. if (close_cb) (*close_cb)();
  81. output_close_common();
  82. output_visu_close();
  83. }
  84. void set_volume(unsigned left, unsigned right) {
  85. LOG_DEBUG("setting internal gain left: %u right: %u", left, right);
  86. if (!volume_cb || !(*volume_cb)(left, right)) {
  87. LOCK;
  88. output.gainL = left;
  89. output.gainR = right;
  90. UNLOCK;
  91. }
  92. }
  93. bool test_open(const char *device, unsigned rates[], bool userdef_rates) {
  94. memset(rates, 0, MAX_SUPPORTED_SAMPLERATES * sizeof(unsigned));
  95. if (!strcasecmp(device, "I2S")) {
  96. unsigned _rates[] = {
  97. #if BYTES_PER_FRAME == 4
  98. 192000, 176400,
  99. #endif
  100. 96000, 88200, 48000,
  101. 44100, 32000, 24000, 22050, 16000,
  102. 12000, 11025, 8000, 0 };
  103. memcpy(rates, _rates, sizeof(_rates));
  104. } else if (!strcasecmp(device, "SPDIF")) {
  105. unsigned _rates[] = { 96000, 88200, 48000,
  106. 44100, 32000, 24000, 22050, 16000,
  107. 12000, 11025, 8000, 0 };
  108. memcpy(rates, _rates, sizeof(_rates));
  109. } else {
  110. rates[0] = 44100;
  111. }
  112. return true;
  113. }
  114. char* output_state_str(void){
  115. output_state state;
  116. LOCK;
  117. state = output.state;
  118. UNLOCK;
  119. switch (state) {
  120. case OUTPUT_OFF: return STR(OUTPUT_OFF);
  121. case OUTPUT_STOPPED: return STR(OUTPUT_STOPPED);
  122. case OUTPUT_BUFFER: return STR(OUTPUT_BUFFER);
  123. case OUTPUT_RUNNING: return STR(OUTPUT_RUNNING);
  124. case OUTPUT_PAUSE_FRAMES: return STR(OUTPUT_PAUSE_FRAMES);
  125. case OUTPUT_SKIP_FRAMES: return STR(OUTPUT_SKIP_FRAMES);
  126. case OUTPUT_START_AT: return STR(OUTPUT_START_AT);
  127. default: return "OUTPUT_UNKNOWN_STATE";
  128. }
  129. }
  130. bool output_stopped(void) {
  131. output_state state;
  132. LOCK;
  133. state = output.state;
  134. UNLOCK;
  135. return state <= OUTPUT_STOPPED;
  136. }