resample16.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Squeezelite - lightweight headless squeezebox emulator
  3. *
  4. * (c) Adrian Smith 2012-2015, triode1@btinternet.com
  5. * Ralph Irving 2015-2017, ralph_irving@hotmail.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. // upsampling using libsoxr - only included if RESAMPLE set
  22. #include "squeezelite.h"
  23. #if RESAMPLE16
  24. #include <resample16.h>
  25. extern log_level loglevel;
  26. struct resample16 {
  27. struct resample16_s *resampler;
  28. bool max_rate;
  29. bool exception;
  30. bool interp;
  31. resample16_filter_e filter;
  32. };
  33. static struct resample16 r;
  34. void resample_samples(struct processstate *process) {
  35. ssize_t odone;
  36. odone = resample16(r.resampler, (HWORD*) process->inbuf, process->in_frames, (HWORD*) process->outbuf);
  37. if (odone < 0) {
  38. LOG_INFO("resample16 error");
  39. return;
  40. }
  41. process->out_frames = odone;
  42. process->total_in += process->in_frames;
  43. process->total_out += odone;
  44. }
  45. bool resample_drain(struct processstate *process) {
  46. process->out_frames = 0;
  47. LOG_INFO("resample track complete");
  48. resample16_delete(r.resampler);
  49. r.resampler = NULL;
  50. return true;
  51. }
  52. bool resample_newstream(struct processstate *process, unsigned raw_sample_rate, unsigned supported_rates[]) {
  53. unsigned outrate = 0;
  54. int i;
  55. if (r.exception) {
  56. // find direct match - avoid resampling
  57. for (i = 0; supported_rates[i]; i++) {
  58. if (raw_sample_rate == supported_rates[i]) {
  59. outrate = raw_sample_rate;
  60. break;
  61. }
  62. }
  63. // else find next highest sync sample rate
  64. while (!outrate && i >= 0) {
  65. if (supported_rates[i] > raw_sample_rate && supported_rates[i] % raw_sample_rate == 0) {
  66. outrate = supported_rates[i];
  67. break;
  68. }
  69. i--;
  70. }
  71. }
  72. if (!outrate) {
  73. if (r.max_rate) {
  74. // resample to max rate for device
  75. outrate = supported_rates[0];
  76. } else {
  77. // resample to max sync sample rate
  78. for (i = 0; supported_rates[i]; i++) {
  79. if (supported_rates[i] % raw_sample_rate == 0 || raw_sample_rate % supported_rates[i] == 0) {
  80. outrate = supported_rates[i];
  81. break;
  82. }
  83. }
  84. }
  85. if (!outrate) {
  86. outrate = supported_rates[0];
  87. }
  88. }
  89. process->in_sample_rate = raw_sample_rate;
  90. process->out_sample_rate = outrate;
  91. if (r.resampler) {
  92. resample16_delete(r.resampler);
  93. r.resampler = NULL;
  94. }
  95. if (raw_sample_rate != outrate) {
  96. LOG_INFO("resampling from %u -> %u", raw_sample_rate, outrate);
  97. r.resampler = resample16_create((float) outrate / raw_sample_rate, r.filter, NULL, false);
  98. return true;
  99. } else {
  100. LOG_INFO("disable resampling - rates match");
  101. return false;
  102. }
  103. }
  104. void resample_flush(void) {
  105. if (r.resampler) {
  106. resample16_delete(r.resampler);
  107. r.resampler = NULL;
  108. }
  109. }
  110. bool resample_init(char *opt) {
  111. char *filter = NULL, *interp = NULL;
  112. r.resampler = NULL;
  113. r.max_rate = false;
  114. r.exception = false;
  115. if (opt) {
  116. filter = next_param(opt, ':');
  117. interp = next_param(NULL, ':');
  118. }
  119. if (filter) {
  120. if (*filter == 'm') r.filter = RESAMPLE16_MED;
  121. else if (*filter == 'l') r.filter = RESAMPLE16_LOW;
  122. else r.filter = RESAMPLE16_BASIC;
  123. }
  124. if (interp && *interp == 'i') {
  125. r.interp = true;
  126. }
  127. LOG_INFO("Resampling with filter %d %s", r.filter, r.interp ? "(interpolated)" : "");
  128. return true;
  129. }
  130. #endif // #if RESAMPLE16