output_embedded.c 4.1 KB

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