output_embedded.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. extern struct outputstate output;
  23. extern struct buffer *outputbuf;
  24. #define FRAME_BLOCK MAX_SILENCE_FRAMES
  25. #define LOCK mutex_lock(outputbuf->mutex)
  26. #define UNLOCK mutex_unlock(outputbuf->mutex)
  27. extern void output_init_bt(log_level level, char *device, unsigned output_buf_size, char *params,
  28. unsigned rates[], unsigned rate_delay, unsigned idle);
  29. extern void output_init_i2s(log_level level, char *device, unsigned output_buf_size, char *params,
  30. unsigned rates[], unsigned rate_delay, unsigned idle);
  31. extern void output_close_i2s(void);
  32. extern void output_close_bt(void);
  33. static log_level loglevel;
  34. static void (*volume_cb)(unsigned left, unsigned right);
  35. static void (*close_cb)(void);
  36. void output_init_embedded(log_level level, char *device, unsigned output_buf_size, char *params,
  37. unsigned rates[], unsigned rate_delay, unsigned idle) {
  38. loglevel = level;
  39. LOG_INFO("init device: %s", device);
  40. memset(&output, 0, sizeof(output));
  41. output_init_common(level, device, output_buf_size, rates, idle);
  42. output.start_frames = FRAME_BLOCK;
  43. output.rate_delay = rate_delay;
  44. if (strstr(device, "BT ")) {
  45. LOG_INFO("init Bluetooth");
  46. close_cb = &output_close_bt;
  47. output_init_bt(level, device, output_buf_size, params, rates, rate_delay, idle);
  48. } else {
  49. LOG_INFO("init I2S");
  50. close_cb = &output_close_i2s;
  51. output_init_i2s(level, device, output_buf_size, params, rates, rate_delay, idle);
  52. }
  53. LOG_INFO("init completed.");
  54. }
  55. void output_close_embedded(void) {
  56. LOG_INFO("close output");
  57. if (close_cb) (*close_cb)();
  58. output_close_common();
  59. }
  60. void set_volume(unsigned left, unsigned right) {
  61. LOG_DEBUG("setting internal gain left: %u right: %u", left, right);
  62. if (!volume_cb) {
  63. LOCK;
  64. output.gainL = left;
  65. output.gainR = right;
  66. UNLOCK;
  67. } else (*volume_cb)(left, right);
  68. }
  69. bool test_open(const char *device, unsigned rates[], bool userdef_rates) {
  70. memset(rates, 0, MAX_SUPPORTED_SAMPLERATES * sizeof(unsigned));
  71. if (!strcmp(device, "BT")) {
  72. rates[0] = 44100;
  73. } else {
  74. unsigned _rates[] = { 96000, 88200, 48000, 44100, 32000, 0 };
  75. memcpy(rates, _rates, sizeof(_rates));
  76. }
  77. return true;
  78. }
  79. char* output_state_str(void){
  80. output_state state;
  81. LOCK;
  82. state = output.state;
  83. UNLOCK;
  84. switch (state) {
  85. case OUTPUT_OFF: return STR(OUTPUT_OFF);
  86. case OUTPUT_STOPPED: return STR(OUTPUT_STOPPED);
  87. case OUTPUT_BUFFER: return STR(OUTPUT_BUFFER);
  88. case OUTPUT_RUNNING: return STR(OUTPUT_RUNNING);
  89. case OUTPUT_PAUSE_FRAMES: return STR(OUTPUT_PAUSE_FRAMES);
  90. case OUTPUT_SKIP_FRAMES: return STR(OUTPUT_SKIP_FRAMES);
  91. case OUTPUT_START_AT: return STR(OUTPUT_START_AT);
  92. default: return "OUTPUT_UNKNOWN_STATE";
  93. }
  94. }
  95. bool output_stopped(void) {
  96. output_state state;
  97. LOCK;
  98. state = output.state;
  99. UNLOCK;
  100. return state <= OUTPUT_STOPPED;
  101. }