output_embedded.c 4.6 KB

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