output_embedded.c 4.6 KB

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