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.

control_sgtl5000.h 5.9KB

10 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, 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. #ifndef control_sgtl5000_h_
  27. #define control_sgtl5000_h_
  28. #include <AudioStream.h>
  29. #include "AudioControl.h"
  30. // SGTL5000-specific defines for headphones
  31. #define AUDIO_HEADPHONE_DAC 0
  32. #define AUDIO_HEADPHONE_LINEIN 1
  33. class AudioControlSGTL5000 : public AudioControl
  34. {
  35. public:
  36. AudioControlSGTL5000(void) : i2c_addr(0x0A) { }
  37. void setAddress(uint8_t level);
  38. bool enable(void);//For Teensy LC the SGTL acts as master, for all other Teensys as slave.
  39. bool enable(const unsigned extMCLK, const uint32_t pllFreq = (4096.0l * AUDIO_SAMPLE_RATE_EXACT) ); //With extMCLK > 0, the SGTL acts as Master
  40. bool disable(void) { return false; }
  41. bool volume(float n) { return volumeInteger(n * 129 + 0.499); }
  42. bool inputLevel(float n) {return false;}
  43. bool muteHeadphone(void) { return write(0x0024, ana_ctrl | (1<<4)); }
  44. bool unmuteHeadphone(void) { return write(0x0024, ana_ctrl & ~(1<<4)); }
  45. bool muteLineout(void) { return write(0x0024, ana_ctrl | (1<<8)); }
  46. bool unmuteLineout(void) { return write(0x0024, ana_ctrl & ~(1<<8)); }
  47. bool inputSelect(int n) {
  48. if (n == AUDIO_INPUT_LINEIN) {
  49. return write(0x0020, 0x055) // +7.5dB gain (1.3Vp-p full scale)
  50. && write(0x0024, ana_ctrl | (1<<2)); // enable linein
  51. } else if (n == AUDIO_INPUT_MIC) {
  52. return write(0x002A, 0x0173) // mic preamp gain = +40dB
  53. && write(0x0020, 0x088) // input gain +12dB (is this enough?)
  54. && write(0x0024, ana_ctrl & ~(1<<2)); // enable mic
  55. } else {
  56. return false;
  57. }
  58. }
  59. bool headphoneSelect(int n) {
  60. if (n == AUDIO_HEADPHONE_DAC) {
  61. return write(0x0024, ana_ctrl | (1<<6)); // route DAC to headphones out
  62. } else if (n == AUDIO_HEADPHONE_LINEIN) {
  63. return write(0x0024, ana_ctrl & ~(1<<6)); // route linein to headphones out
  64. } else {
  65. return false;
  66. }
  67. }
  68. bool volume(float left, float right);
  69. bool micGain(unsigned int dB);
  70. bool lineInLevel(uint8_t n) { return lineInLevel(n, n); }
  71. bool lineInLevel(uint8_t left, uint8_t right);
  72. unsigned short lineOutLevel(uint8_t n);
  73. unsigned short lineOutLevel(uint8_t left, uint8_t right);
  74. unsigned short dacVolume(float n);
  75. unsigned short dacVolume(float left, float right);
  76. bool dacVolumeRamp();
  77. bool dacVolumeRampLinear();
  78. bool dacVolumeRampDisable();
  79. unsigned short adcHighPassFilterEnable(void);
  80. unsigned short adcHighPassFilterFreeze(void);
  81. unsigned short adcHighPassFilterDisable(void);
  82. unsigned short audioPreProcessorEnable(void);
  83. unsigned short audioPostProcessorEnable(void);
  84. unsigned short audioProcessorDisable(void);
  85. unsigned short eqFilterCount(uint8_t n);
  86. unsigned short eqSelect(uint8_t n);
  87. unsigned short eqBand(uint8_t bandNum, float n);
  88. void eqBands(float bass, float mid_bass, float midrange, float mid_treble, float treble);
  89. void eqBands(float bass, float treble);
  90. void eqFilter(uint8_t filterNum, int *filterParameters);
  91. unsigned short autoVolumeControl(uint8_t maxGain, uint8_t lbiResponse, uint8_t hardLimit, float threshold, float attack, float decay);
  92. unsigned short autoVolumeEnable(void);
  93. unsigned short autoVolumeDisable(void);
  94. unsigned short enhanceBass(float lr_lev, float bass_lev);
  95. unsigned short enhanceBass(float lr_lev, float bass_lev, uint8_t hpf_bypass, uint8_t cutoff);
  96. unsigned short enhanceBassEnable(void);
  97. unsigned short enhanceBassDisable(void);
  98. unsigned short surroundSound(uint8_t width);
  99. unsigned short surroundSound(uint8_t width, uint8_t select);
  100. unsigned short surroundSoundEnable(void);
  101. unsigned short surroundSoundDisable(void);
  102. void killAutomation(void) { semi_automated=false; }
  103. void setMasterMode(uint32_t freqMCLK_in);
  104. protected:
  105. bool muted;
  106. bool volumeInteger(unsigned int n); // range: 0x00 to 0x80
  107. uint16_t ana_ctrl;
  108. uint8_t i2c_addr;
  109. unsigned char calcVol(float n, unsigned char range);
  110. unsigned int read(unsigned int reg);
  111. bool write(unsigned int reg, unsigned int val);
  112. unsigned int modify(unsigned int reg, unsigned int val, unsigned int iMask);
  113. unsigned short dap_audio_eq_band(uint8_t bandNum, float n);
  114. private:
  115. bool semi_automated;
  116. void automate(uint8_t dap, uint8_t eq);
  117. void automate(uint8_t dap, uint8_t eq, uint8_t filterCount);
  118. };
  119. //For Filter Type: 0 = LPF, 1 = HPF, 2 = BPF, 3 = NOTCH, 4 = PeakingEQ, 5 = LowShelf, 6 = HighShelf
  120. #define FILTER_LOPASS 0
  121. #define FILTER_HIPASS 1
  122. #define FILTER_BANDPASS 2
  123. #define FILTER_NOTCH 3
  124. #define FILTER_PARAEQ 4
  125. #define FILTER_LOSHELF 5
  126. #define FILTER_HISHELF 6
  127. //For frequency adjustment
  128. #define FLAT_FREQUENCY 0
  129. #define PARAMETRIC_EQUALIZER 1
  130. #define TONE_CONTROLS 2
  131. #define GRAPHIC_EQUALIZER 3
  132. void calcBiquad(uint8_t filtertype, float fC, float dB_Gain, float Q, uint32_t quantization_unit, uint32_t fS, int *coef);
  133. #endif