output_embedded.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. struct loud_packet {
  38. char opcode[4];
  39. u8_t loudness;
  40. };
  41. #pragma pack(pop)
  42. static bool handler(u8_t *data, int len){
  43. bool res = true;
  44. if (!strncmp((char*) data, "eqlz", 4)) {
  45. s8_t *gain = (s8_t*) (data + sizeof(struct eqlz_packet));
  46. LOG_INFO("got equalizer %d", len);
  47. // update will be done at next opportunity
  48. equalizer_set_gain(gain);
  49. } else if (!strncmp((char*) data, "loud", 4)) {
  50. struct loud_packet *packet = (struct loud_packet*) data;
  51. LOG_INFO("got loudness %d", packet->loudness);
  52. // update will be done at next opportunity
  53. equalizer_set_loudness(packet->loudness);
  54. } else {
  55. res = false;
  56. }
  57. // chain protocol handlers (bitwise or is fine)
  58. if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
  59. return res;
  60. }
  61. void output_init_embedded(log_level level, char *device, unsigned output_buf_size, char *params,
  62. unsigned rates[], unsigned rate_delay, unsigned idle) {
  63. loglevel = level;
  64. LOG_INFO("init device: %s", device);
  65. // chain handlers
  66. slimp_handler_chain = slimp_handler;
  67. slimp_handler = handler;
  68. // init equalizer before backends
  69. equalizer_init();
  70. memset(&output, 0, sizeof(output));
  71. output_init_common(level, device, output_buf_size, rates, idle);
  72. output.start_frames = FRAME_BLOCK;
  73. output.rate_delay = rate_delay;
  74. #if CONFIG_BT_SINK
  75. if (strcasestr(device, "BT")) {
  76. LOG_INFO("init Bluetooth");
  77. close_cb = &output_close_bt;
  78. output_init_bt(level, device, output_buf_size, params, rates, rate_delay, idle);
  79. } else
  80. #endif
  81. {
  82. LOG_INFO("init I2S/SPDIF");
  83. close_cb = &output_close_i2s;
  84. volume_cb = &output_volume_i2s;
  85. output_init_i2s(level, device, output_buf_size, params, rates, rate_delay, idle);
  86. }
  87. output_visu_init(level);
  88. LOG_INFO("init completed.");
  89. }
  90. void output_close_embedded(void) {
  91. LOG_INFO("close output");
  92. if (close_cb) (*close_cb)();
  93. output_close_common();
  94. output_visu_close();
  95. }
  96. void set_volume(unsigned left, unsigned right) {
  97. LOG_DEBUG("setting internal gain left: %u right: %u", left, right);
  98. if (!volume_cb || !(*volume_cb)(left, right)) {
  99. LOCK;
  100. output.gainL = left;
  101. output.gainR = right;
  102. UNLOCK;
  103. }
  104. equalizer_set_volume(left, right);
  105. }
  106. bool test_open(const char *device, unsigned rates[], bool userdef_rates) {
  107. memset(rates, 0, MAX_SUPPORTED_SAMPLERATES * sizeof(unsigned));
  108. if (!strcasecmp(device, "I2S")) {
  109. unsigned _rates[] = {
  110. #if BYTES_PER_FRAME == 4
  111. 192000, 176400,
  112. #endif
  113. 96000, 88200, 48000,
  114. 44100, 32000, 24000, 22050, 16000,
  115. 12000, 11025, 8000, 0 };
  116. memcpy(rates, _rates, sizeof(_rates));
  117. } else if (!strcasecmp(device, "SPDIF")) {
  118. unsigned _rates[] = { 96000, 88200, 48000,
  119. 44100, 32000, 24000, 22050, 16000,
  120. 12000, 11025, 8000, 0 };
  121. memcpy(rates, _rates, sizeof(_rates));
  122. } else {
  123. rates[0] = 44100;
  124. }
  125. return true;
  126. }
  127. char* output_state_str(void){
  128. output_state state;
  129. LOCK;
  130. state = output.state;
  131. UNLOCK;
  132. switch (state) {
  133. case OUTPUT_OFF: return STR(OUTPUT_OFF);
  134. case OUTPUT_STOPPED: return STR(OUTPUT_STOPPED);
  135. case OUTPUT_BUFFER: return STR(OUTPUT_BUFFER);
  136. case OUTPUT_RUNNING: return STR(OUTPUT_RUNNING);
  137. case OUTPUT_PAUSE_FRAMES: return STR(OUTPUT_PAUSE_FRAMES);
  138. case OUTPUT_SKIP_FRAMES: return STR(OUTPUT_SKIP_FRAMES);
  139. case OUTPUT_START_AT: return STR(OUTPUT_START_AT);
  140. default: return "OUTPUT_UNKNOWN_STATE";
  141. }
  142. }
  143. bool output_stopped(void) {
  144. output_state state;
  145. LOCK;
  146. state = output.state;
  147. UNLOCK;
  148. return state <= OUTPUT_STOPPED;
  149. }