equalizer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Philippe G. 2020, philippe_44@outlook.com
  5. *
  6. * This software is released under the MIT License.
  7. * https://opensource.org/licenses/MIT
  8. *
  9. */
  10. #include "platform_config.h"
  11. #include "squeezelite.h"
  12. #include "equalizer.h"
  13. #include "esp_equalizer.h"
  14. #define EQ_BANDS 10
  15. static log_level loglevel = lINFO;
  16. static struct {
  17. void *handle;
  18. float gain[EQ_BANDS];
  19. bool update;
  20. } equalizer = { .update = true };
  21. /****************************************************************************************
  22. * initialize equalizer
  23. */
  24. void equalizer_init(void) {
  25. s8_t gain[EQ_BANDS] = { };
  26. char *config = config_alloc_get(NVS_TYPE_STR, "equalizer");
  27. char *p = strtok(config, ", !");
  28. for (int i = 0; p && i < EQ_BANDS; i++) {
  29. gain[i] = atoi(p);
  30. p = strtok(NULL, ", :");
  31. }
  32. free(config);
  33. equalizer_update(gain);
  34. LOG_INFO("initializing equalizer");
  35. }
  36. /****************************************************************************************
  37. * open equalizer
  38. */
  39. void equalizer_open(u32_t sample_rate) {
  40. // in any case, need to clear update flag
  41. equalizer.update = false;
  42. if (sample_rate != 11025 && sample_rate != 22050 && sample_rate != 44100 && sample_rate != 48000) {
  43. LOG_WARN("equalizer only supports 11025, 22050, 44100 and 48000 sample rates, not %u", sample_rate);
  44. return;
  45. }
  46. equalizer.handle = esp_equalizer_init(2, sample_rate, EQ_BANDS, 0);
  47. if (equalizer.handle) {
  48. bool active = false;
  49. for (int i = 0; i < EQ_BANDS; i++) {
  50. esp_equalizer_set_band_value(equalizer.handle, equalizer.gain[i], i, 0);
  51. esp_equalizer_set_band_value(equalizer.handle, equalizer.gain[i], i, 1);
  52. active |= equalizer.gain[i] != 0;
  53. }
  54. // do not activate equalizer if all gain are 0
  55. if (!active) equalizer_close();
  56. LOG_INFO("equalizer initialized %u", active);
  57. } else {
  58. LOG_WARN("can't init equalizer");
  59. }
  60. }
  61. /****************************************************************************************
  62. * close equalizer
  63. */
  64. void equalizer_close(void) {
  65. if (equalizer.handle) {
  66. esp_equalizer_uninit(equalizer.handle);
  67. equalizer.handle = NULL;
  68. }
  69. }
  70. /****************************************************************************************
  71. * update equalizer gain
  72. */
  73. void equalizer_update(s8_t *gain) {
  74. char config[EQ_BANDS * 4 + 1] = { };
  75. char *p = config;
  76. for (int i = 0; i < EQ_BANDS; i++) {
  77. equalizer.gain[i] = gain[i];
  78. if (gain[i] < 0) *p++ = '-';
  79. *p++ = (gain[i] / 10) + 0x30;
  80. *p++ = (gain[i] % 10) + 0x30;
  81. if (i < EQ_BANDS - 1) *p++ = ',';
  82. }
  83. config_set_value(NVS_TYPE_STR, "equalizer", config);
  84. equalizer.update = true;
  85. }
  86. /****************************************************************************************
  87. * process equalizer
  88. */
  89. void equalizer_process(u8_t *buf, u32_t bytes, u32_t sample_rate) {
  90. // don't want to process with output locked, so take the small risk to miss one parametric update
  91. if (equalizer.update) {
  92. equalizer_close();
  93. equalizer_open(sample_rate);
  94. }
  95. if (equalizer.handle) {
  96. esp_equalizer_process(equalizer.handle, buf, bytes, sample_rate, 2);
  97. }
  98. }