You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.6KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2019, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. /*
  27. by Alexander Walch
  28. */
  29. #ifndef async_input_spdif3_h_
  30. #define async_input_spdif3_h_
  31. #include "./Resampler.h"
  32. #include "./Quantizer.h"
  33. #include <core/Arduino.h>
  34. #include <core/AudioStream.h>
  35. #include <core/DMAChannel.h>
  36. #include <arm_math.h>
  37. //#define DEBUG_SPDIF_IN //activates debug output
  38. class AsyncAudioInputSPDIF3 : public AudioStream
  39. {
  40. public:
  41. ///@param attenuation target attenuation [dB] of the anti-aliasing filter. Only used if AUDIO_SAMPLE_RATE_EXACT < input sample rate (input fs). The attenuation can't be reached if the needed filter length exceeds 2*MAX_FILTER_SAMPLES+1
  42. ///@param minHalfFilterLength If AUDIO_SAMPLE_RATE_EXACT >= input fs), the filter length of the resampling filter is 2*minHalfFilterLength+1. If AUDIO_SAMPLE_RATE_EXACT < input fs the filter is maybe longer to reach the desired attenuation
  43. ///@param maxHalfFilterLength Can be used to restrict the maximum filter length at the cost of a lower attenuation
  44. AsyncAudioInputSPDIF3(bool dither=false, bool noiseshaping=false,float attenuation=100, int32_t minHalfFilterLength=20, int32_t maxHalfFilterLength=80);
  45. ~AsyncAudioInputSPDIF3();
  46. void begin();
  47. virtual void update(void);
  48. double getBufferedTime() const;
  49. double getInputFrequency() const;
  50. static bool isLocked();
  51. double getTargetLantency() const;
  52. double getAttenuation() const;
  53. int32_t getHalfFilterLength() const;
  54. protected:
  55. static DMAChannel dma;
  56. static void isr(void);
  57. private:
  58. void resample(int16_t* data_left, int16_t* data_right, int32_t& block_offset);
  59. void monitorResampleBuffer();
  60. void configure();
  61. double getNewValidInputFrequ();
  62. void config_spdifIn();
  63. //accessed in isr ====
  64. static volatile int32_t buffer_offset;
  65. static int32_t resample_offset;
  66. static volatile uint32_t microsLast;
  67. //====================
  68. Resampler _resampler;
  69. Quantizer* quantizer[2];
  70. arm_biquad_cascade_df2T_instance_f32 _bufferLPFilter;
  71. volatile double _bufferedTime;
  72. volatile double _lastValidInputFrequ;
  73. double _inputFrequency=0.;
  74. double _targetLatencyS; //target latency [seconds]
  75. const double _blockDuration=AUDIO_BLOCK_SAMPLES/AUDIO_SAMPLE_RATE_EXACT; //[seconds]
  76. double _maxLatency=2.*_blockDuration;
  77. #ifdef DEBUG_SPDIF_IN
  78. static volatile bool bufferOverflow;
  79. #endif
  80. };
  81. #endif