output_embedded.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #include "Configurator.h"
  14. extern log_level log_level_from_sys_level(sys_DebugLevelEnum level);
  15. static sys_Squeezelite* config = NULL;
  16. extern unsigned int* get_rates() ;
  17. extern struct outputstate output;
  18. extern struct buffer* outputbuf;
  19. static bool (*slimp_handler_chain)(u8_t* data, int len);
  20. #define FRAME_BLOCK MAX_SILENCE_FRAMES
  21. #define LOCK mutex_lock(outputbuf->mutex)
  22. #define UNLOCK mutex_unlock(outputbuf->mutex)
  23. // output_bt.c
  24. extern void output_init_bt(unsigned rates[]);
  25. extern void output_close_bt(void);
  26. // output_i2s.c
  27. extern void output_init_i2s(unsigned rates[]);
  28. extern bool output_volume_i2s(unsigned left, unsigned right);
  29. extern void output_close_i2s(void);
  30. // controls.c
  31. extern void cli_controls_init(void);
  32. static log_level loglevel;
  33. static bool (*volume_cb)(unsigned left, unsigned right);
  34. static void (*close_cb)(void);
  35. #pragma pack(push, 1)
  36. struct eqlz_packet {
  37. char opcode[4];
  38. };
  39. struct loud_packet {
  40. char opcode[4];
  41. u8_t loudness;
  42. };
  43. #pragma pack(pop)
  44. static bool handler(u8_t* data, int len) {
  45. bool res = true;
  46. if (!strncmp((char*)data, "eqlz", 4)) {
  47. s8_t* gain = (s8_t*)(data + sizeof(struct eqlz_packet));
  48. // update will be done at next opportunity
  49. equalizer_set_gain(gain);
  50. } else if (!strncmp((char*)data, "loud", 4)) {
  51. struct loud_packet* packet = (struct loud_packet*)data;
  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() {
  62. config = &platform->services.squeezelite;
  63. loglevel = log_level_from_sys_level(config->log.output);
  64. LOG_INFO("init device: %s", sys_OutputTypeEnum_name(config->output_type));
  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(loglevel, sys_OutputTypeEnum_name(config->output_type),
  72. config->buffers.output, get_rates(), config->amp_gpio_timeout);
  73. output.start_frames = FRAME_BLOCK;
  74. #pragma message("Rate delay logic incomplete")
  75. output.rate_delay = 0;
  76. #if CONFIG_BT_SINK
  77. if (config->output_type == sys_OutputTypeEnum_OUTPUT_Bluetooth) {
  78. LOG_INFO("init Bluetooth");
  79. close_cb = &output_close_bt;
  80. output_init_bt(get_rates());
  81. } else
  82. #endif
  83. {
  84. close_cb = &output_close_i2s;
  85. volume_cb = &output_volume_i2s;
  86. output_init_i2s(get_rates());
  87. }
  88. output_visu_init(loglevel);
  89. LOG_INFO("init completed.");
  90. }
  91. void output_close_embedded(void) {
  92. LOG_INFO("close output");
  93. if (close_cb) (*close_cb)();
  94. output_close_common();
  95. output_visu_close();
  96. }
  97. void set_volume(unsigned left, unsigned right) {
  98. LOG_DEBUG("setting internal gain left: %u right: %u", left, right);
  99. if (!volume_cb || !(*volume_cb)(left, right)) {
  100. LOCK;
  101. output.gainL = left;
  102. output.gainR = right;
  103. UNLOCK;
  104. }
  105. equalizer_set_volume(left, right);
  106. }
  107. bool test_open(const char* device, unsigned rates[], bool userdef_rates) {
  108. memset(rates, 0, MAX_SUPPORTED_SAMPLERATES * sizeof(unsigned));
  109. if (config->output_type == sys_OutputTypeEnum_OUTPUT_I2S) {
  110. unsigned _rates[] = {
  111. #if BYTES_PER_FRAME == 4
  112. 192000,
  113. 176400,
  114. #endif
  115. 96000,
  116. 88200,
  117. 48000,
  118. 44100,
  119. 32000,
  120. 24000,
  121. 22050,
  122. 16000,
  123. 12000,
  124. 11025,
  125. 8000,
  126. 0
  127. };
  128. memcpy(rates, _rates, sizeof(_rates));
  129. } else if (config->output_type == sys_OutputTypeEnum_OUTPUT_SPDIF) {
  130. unsigned _rates[] = {
  131. 96000, 88200, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 0};
  132. memcpy(rates, _rates, sizeof(_rates));
  133. } else {
  134. rates[0] = 44100;
  135. }
  136. return true;
  137. }
  138. char* output_state_str(void) {
  139. output_state state;
  140. LOCK;
  141. state = output.state;
  142. UNLOCK;
  143. switch (state) {
  144. case OUTPUT_OFF:
  145. return STR(OUTPUT_OFF);
  146. case OUTPUT_STOPPED:
  147. return STR(OUTPUT_STOPPED);
  148. case OUTPUT_BUFFER:
  149. return STR(OUTPUT_BUFFER);
  150. case OUTPUT_RUNNING:
  151. return STR(OUTPUT_RUNNING);
  152. case OUTPUT_PAUSE_FRAMES:
  153. return STR(OUTPUT_PAUSE_FRAMES);
  154. case OUTPUT_SKIP_FRAMES:
  155. return STR(OUTPUT_SKIP_FRAMES);
  156. case OUTPUT_START_AT:
  157. return STR(OUTPUT_START_AT);
  158. default:
  159. return "OUTPUT_UNKNOWN_STATE";
  160. }
  161. }
  162. bool output_stopped(void) {
  163. output_state state;
  164. LOCK;
  165. state = output.state;
  166. UNLOCK;
  167. return state <= OUTPUT_STOPPED;
  168. }