JOSResampleReader.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*******************************************************************************
  2. * Copyright 2009-2016 Jörg Müller
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. ******************************************************************************/
  16. #pragma once
  17. /**
  18. * @file JOSResampleReader.h
  19. * @ingroup respec
  20. * The JOSResampleReader class.
  21. */
  22. #include "respec/ResampleReader.h"
  23. #include "util/Buffer.h"
  24. AUD_NAMESPACE_BEGIN
  25. /**
  26. * This resampling reader uses Julius O. Smith's resampling algorithm.
  27. */
  28. class AUD_API JOSResampleReader : public ResampleReader
  29. {
  30. private:
  31. typedef void (JOSResampleReader::*resample_f)(double target_factor, int length, sample_t* buffer);
  32. /**
  33. * The half filter length.
  34. */
  35. static const int m_len;
  36. /**
  37. * The sample step size for the filter.
  38. */
  39. static const int m_L;
  40. /**
  41. * The filter coefficients.
  42. */
  43. static const float m_coeff[];
  44. /**
  45. * The reader channels.
  46. */
  47. Channels m_channels;
  48. /**
  49. * The sample position in the cache.
  50. */
  51. unsigned int m_n;
  52. /**
  53. * The subsample position in the cache.
  54. */
  55. double m_P;
  56. /**
  57. * The input data buffer.
  58. */
  59. Buffer m_buffer;
  60. /**
  61. * Double buffer for the sums.
  62. */
  63. Buffer m_sums;
  64. /**
  65. * How many samples in the cache are valid.
  66. */
  67. int m_cache_valid;
  68. /**
  69. * Resample function.
  70. */
  71. resample_f m_resample;
  72. /**
  73. * Last resampling factor.
  74. */
  75. double m_last_factor;
  76. // delete copy constructor and operator=
  77. JOSResampleReader(const JOSResampleReader&) = delete;
  78. JOSResampleReader& operator=(const JOSResampleReader&) = delete;
  79. /**
  80. * Resets the resampler to its initial state.
  81. */
  82. void AUD_LOCAL reset();
  83. /**
  84. * Updates the buffer to be as small as possible for the coming reading.
  85. * \param size The size of samples to be read.
  86. * \param factor The next resampling factor.
  87. * \param samplesize The size of a sample.
  88. */
  89. void AUD_LOCAL updateBuffer(int size, double factor, int samplesize);
  90. void AUD_LOCAL resample(double target_factor, int length, sample_t* buffer);
  91. void AUD_LOCAL resample_mono(double target_factor, int length, sample_t* buffer);
  92. void AUD_LOCAL resample_stereo(double target_factor, int length, sample_t* buffer);
  93. public:
  94. /**
  95. * Creates a resampling reader.
  96. * \param reader The reader to mix.
  97. * \param rate The target sampling rate.
  98. */
  99. JOSResampleReader(std::shared_ptr<IReader> reader, SampleRate rate);
  100. virtual void seek(int position);
  101. virtual int getLength() const;
  102. virtual int getPosition() const;
  103. virtual Specs getSpecs() const;
  104. virtual void read(int& length, bool& eos, sample_t* buffer);
  105. };
  106. AUD_NAMESPACE_END