equalizer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Philippe G. 2020, philippe_44@outlook.com
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "squeezelite.h"
  21. #include "equalizer.h"
  22. #include "esp_equalizer.h"
  23. #define EQ_BANDS 10
  24. static log_level loglevel = lINFO;
  25. static struct {
  26. void *handle;
  27. float gain[EQ_BANDS];
  28. bool update;
  29. } equalizer = { .update = true };
  30. /****************************************************************************************
  31. * open equalizer
  32. */
  33. void equalizer_open(u32_t sample_rate) {
  34. if (sample_rate != 11025 && sample_rate != 22050 && sample_rate != 44100 && sample_rate != 48000) {
  35. LOG_WARN("equalizer only supports 11025, 22050, 44100 and 48000 sample rates, not %u", sample_rate);
  36. return;
  37. }
  38. equalizer.handle = esp_equalizer_init(2, sample_rate, EQ_BANDS, 0);
  39. equalizer.update = false;
  40. if (equalizer.handle) {
  41. bool active = false;
  42. for (int i = 0; i < EQ_BANDS; i++) {
  43. esp_equalizer_set_band_value(equalizer.handle, equalizer.gain[i], i, 0);
  44. esp_equalizer_set_band_value(equalizer.handle, equalizer.gain[i], i, 1);
  45. active |= equalizer.gain[i] != 0;
  46. }
  47. // do not activate equalizer if all gain are 0
  48. if (!active) equalizer_close();
  49. LOG_INFO("equalizer initialized %u", active);
  50. } else {
  51. LOG_WARN("can't init equalizer");
  52. }
  53. }
  54. /****************************************************************************************
  55. * close equalizer
  56. */
  57. void equalizer_close(void) {
  58. if (equalizer.handle) {
  59. esp_equalizer_uninit(equalizer.handle);
  60. equalizer.handle = NULL;
  61. }
  62. }
  63. /****************************************************************************************
  64. * update equalizer gain
  65. */
  66. void equalizer_update(s8_t *gain) {
  67. for (int i = 0; i < EQ_BANDS; i++) equalizer.gain[i] = gain[i];
  68. equalizer.update = true;
  69. }
  70. /****************************************************************************************
  71. * process equalizer
  72. */
  73. void equalizer_process(u8_t *buf, u32_t bytes, u32_t sample_rate) {
  74. // don't want to process with output locked, so tak ethe small risk to miss one parametric update
  75. if (equalizer.update) {
  76. equalizer_close();
  77. equalizer_open(sample_rate);
  78. }
  79. if (equalizer.handle) {
  80. esp_equalizer_process(equalizer.handle, buf, bytes, sample_rate, 2);
  81. }
  82. }