equalizer.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. LOG_INFO("equalizer initialized");
  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. }
  46. } else {
  47. LOG_WARN("can't init equalizer");
  48. }
  49. }
  50. /****************************************************************************************
  51. * close equalizer
  52. */
  53. void equalizer_close(void) {
  54. if (equalizer.handle) {
  55. esp_equalizer_uninit(equalizer.handle);
  56. equalizer.handle = NULL;
  57. }
  58. }
  59. /****************************************************************************************
  60. * update equalizer gain
  61. */
  62. void equalizer_update(s8_t *gain) {
  63. for (int i = 0; i < EQ_BANDS; i++) equalizer.gain[i] = gain[i];
  64. equalizer.update = true;
  65. }
  66. /****************************************************************************************
  67. * process equalizer
  68. */
  69. void equalizer_process(u8_t *buf, u32_t bytes, u32_t sample_rate) {
  70. // don't want to process with output locked, so tak ethe small risk to miss one parametric update
  71. if (equalizer.update) {
  72. equalizer_close();
  73. equalizer_open(sample_rate);
  74. }
  75. if (equalizer.handle) {
  76. esp_equalizer_process(equalizer.handle, buf, bytes, sample_rate, 2);
  77. }
  78. }