equalizer.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "squeezelite.h"
  11. #include "equalizer.h"
  12. #include "esp_equalizer.h"
  13. #define EQ_BANDS 10
  14. static log_level loglevel = lINFO;
  15. static struct {
  16. void *handle;
  17. float gain[EQ_BANDS];
  18. bool update;
  19. } equalizer = { .update = true };
  20. /****************************************************************************************
  21. * open equalizer
  22. */
  23. void equalizer_open(u32_t sample_rate) {
  24. // in any case, need to clear update flag
  25. equalizer.update = false;
  26. if (sample_rate != 11025 && sample_rate != 22050 && sample_rate != 44100 && sample_rate != 48000) {
  27. LOG_WARN("equalizer only supports 11025, 22050, 44100 and 48000 sample rates, not %u", sample_rate);
  28. return;
  29. }
  30. equalizer.handle = esp_equalizer_init(2, sample_rate, EQ_BANDS, 0);
  31. if (equalizer.handle) {
  32. bool active = false;
  33. for (int i = 0; i < EQ_BANDS; i++) {
  34. esp_equalizer_set_band_value(equalizer.handle, equalizer.gain[i], i, 0);
  35. esp_equalizer_set_band_value(equalizer.handle, equalizer.gain[i], i, 1);
  36. active |= equalizer.gain[i] != 0;
  37. }
  38. // do not activate equalizer if all gain are 0
  39. if (!active) equalizer_close();
  40. LOG_INFO("equalizer initialized %u", active);
  41. } else {
  42. LOG_WARN("can't init equalizer");
  43. }
  44. }
  45. /****************************************************************************************
  46. * close equalizer
  47. */
  48. void equalizer_close(void) {
  49. if (equalizer.handle) {
  50. esp_equalizer_uninit(equalizer.handle);
  51. equalizer.handle = NULL;
  52. }
  53. }
  54. /****************************************************************************************
  55. * update equalizer gain
  56. */
  57. void equalizer_update(s8_t *gain) {
  58. for (int i = 0; i < EQ_BANDS; i++) equalizer.gain[i] = gain[i];
  59. equalizer.update = true;
  60. }
  61. /****************************************************************************************
  62. * process equalizer
  63. */
  64. void equalizer_process(u8_t *buf, u32_t bytes, u32_t sample_rate) {
  65. // don't want to process with output locked, so tak ethe small risk to miss one parametric update
  66. if (equalizer.update) {
  67. equalizer_close();
  68. equalizer_open(sample_rate);
  69. }
  70. if (equalizer.handle) {
  71. esp_equalizer_process(equalizer.handle, buf, bytes, sample_rate, 2);
  72. }
  73. }