Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

997 lines
35KB

  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. #include "control_sgtl5000.h"
  27. #include "Wire.h"
  28. #define CHIP_ID 0x0000
  29. // 15:8 PARTID 0xA0 - 8 bit identifier for SGTL5000
  30. // 7:0 REVID 0x00 - revision number for SGTL5000.
  31. #define CHIP_DIG_POWER 0x0002
  32. // 6 ADC_POWERUP 1=Enable, 0=disable the ADC block, both digital & analog,
  33. // 5 DAC_POWERUP 1=Enable, 0=disable the DAC block, both analog and digital
  34. // 4 DAP_POWERUP 1=Enable, 0=disable the DAP block
  35. // 1 I2S_OUT_POWERUP 1=Enable, 0=disable the I2S data output
  36. // 0 I2S_IN_POWERUP 1=Enable, 0=disable the I2S data input
  37. #define CHIP_CLK_CTRL 0x0004
  38. // 5:4 RATE_MODE Sets the sample rate mode. MCLK_FREQ is still specified
  39. // relative to the rate in SYS_FS
  40. // 0x0 = SYS_FS specifies the rate
  41. // 0x1 = Rate is 1/2 of the SYS_FS rate
  42. // 0x2 = Rate is 1/4 of the SYS_FS rate
  43. // 0x3 = Rate is 1/6 of the SYS_FS rate
  44. // 3:2 SYS_FS Sets the internal system sample rate (default=2)
  45. // 0x0 = 32 kHz
  46. // 0x1 = 44.1 kHz
  47. // 0x2 = 48 kHz
  48. // 0x3 = 96 kHz
  49. // 1:0 MCLK_FREQ Identifies incoming SYS_MCLK frequency and if the PLL should be used
  50. // 0x0 = 256*Fs
  51. // 0x1 = 384*Fs
  52. // 0x2 = 512*Fs
  53. // 0x3 = Use PLL
  54. // The 0x3 (Use PLL) setting must be used if the SYS_MCLK is not
  55. // a standard multiple of Fs (256, 384, or 512). This setting can
  56. // also be used if SYS_MCLK is a standard multiple of Fs.
  57. // Before this field is set to 0x3 (Use PLL), the PLL must be
  58. // powered up by setting CHIP_ANA_POWER->PLL_POWERUP and
  59. // CHIP_ANA_POWER->VCOAMP_POWERUP. Also, the PLL dividers must
  60. // be calculated based on the external MCLK rate and
  61. // CHIP_PLL_CTRL register must be set (see CHIP_PLL_CTRL register
  62. // description details on how to calculate the divisors).
  63. #define CHIP_I2S_CTRL 0x0006
  64. // 8 SCLKFREQ Sets frequency of I2S_SCLK when in master mode (MS=1). When in slave
  65. // mode (MS=0), this field must be set appropriately to match SCLK input
  66. // rate.
  67. // 0x0 = 64Fs
  68. // 0x1 = 32Fs - Not supported for RJ mode (I2S_MODE = 1)
  69. // 7 MS Configures master or slave of I2S_LRCLK and I2S_SCLK.
  70. // 0x0 = Slave: I2S_LRCLK an I2S_SCLK are inputs
  71. // 0x1 = Master: I2S_LRCLK and I2S_SCLK are outputs
  72. // NOTE: If the PLL is used (CHIP_CLK_CTRL->MCLK_FREQ==0x3),
  73. // the SGTL5000 must be a master of the I2S port (MS==1)
  74. // 6 SCLK_INV Sets the edge that data (input and output) is clocked in on for I2S_SCLK
  75. // 0x0 = data is valid on rising edge of I2S_SCLK
  76. // 0x1 = data is valid on falling edge of I2S_SCLK
  77. // 5:4 DLEN I2S data length (default=1)
  78. // 0x0 = 32 bits (only valid when SCLKFREQ=0),
  79. // not valid for Right Justified Mode
  80. // 0x1 = 24 bits (only valid when SCLKFREQ=0)
  81. // 0x2 = 20 bits
  82. // 0x3 = 16 bits
  83. // 3:2 I2S_MODE Sets the mode for the I2S port
  84. // 0x0 = I2S mode or Left Justified (Use LRALIGN to select)
  85. // 0x1 = Right Justified Mode
  86. // 0x2 = PCM Format A/B
  87. // 0x3 = RESERVED
  88. // 1 LRALIGN I2S_LRCLK Alignment to data word. Not used for Right Justified mode
  89. // 0x0 = Data word starts 1 I2S_SCLK delay after I2S_LRCLK
  90. // transition (I2S format, PCM format A)
  91. // 0x1 = Data word starts after I2S_LRCLK transition (left
  92. // justified format, PCM format B)
  93. // 0 LRPOL I2S_LRCLK Polarity when data is presented.
  94. // 0x0 = I2S_LRCLK = 0 - Left, 1 - Right
  95. // 1x0 = I2S_LRCLK = 0 - Right, 1 - Left
  96. // The left subframe should be presented first regardless of
  97. // the setting of LRPOL.
  98. #define CHIP_SSS_CTRL 0x000A
  99. // 14 DAP_MIX_LRSWAP DAP Mixer Input Swap
  100. // 0x0 = Normal Operation
  101. // 0x1 = Left and Right channels for the DAP MIXER Input are swapped.
  102. // 13 DAP_LRSWAP DAP Mixer Input Swap
  103. // 0x0 = Normal Operation
  104. // 0x1 = Left and Right channels for the DAP Input are swapped
  105. // 12 DAC_LRSWAP DAC Input Swap
  106. // 0x0 = Normal Operation
  107. // 0x1 = Left and Right channels for the DAC are swapped
  108. // 10 I2S_LRSWAP I2S_DOUT Swap
  109. // 0x0 = Normal Operation
  110. // 0x1 = Left and Right channels for the I2S_DOUT are swapped
  111. // 9:8 DAP_MIX_SELECT Select data source for DAP mixer
  112. // 0x0 = ADC
  113. // 0x1 = I2S_IN
  114. // 0x2 = Reserved
  115. // 0x3 = Reserved
  116. // 7:6 DAP_SELECT Select data source for DAP
  117. // 0x0 = ADC
  118. // 0x1 = I2S_IN
  119. // 0x2 = Reserved
  120. // 0x3 = Reserved
  121. // 5:4 DAC_SELECT Select data source for DAC (default=1)
  122. // 0x0 = ADC
  123. // 0x1 = I2S_IN
  124. // 0x2 = Reserved
  125. // 0x3 = DAP
  126. // 1:0 I2S_SELECT Select data source for I2S_DOUT
  127. // 0x0 = ADC
  128. // 0x1 = I2S_IN
  129. // 0x2 = Reserved
  130. // 0x3 = DAP
  131. #define CHIP_ADCDAC_CTRL 0x000E
  132. // 13 VOL_BUSY_DAC_RIGHT Volume Busy DAC Right
  133. // 0x0 = Ready
  134. // 0x1 = Busy - This indicates the channel has not reached its
  135. // programmed volume/mute level
  136. // 12 VOL_BUSY_DAC_LEFT Volume Busy DAC Left
  137. // 0x0 = Ready
  138. // 0x1 = Busy - This indicates the channel has not reached its
  139. // programmed volume/mute level
  140. // 9 VOL_RAMP_EN Volume Ramp Enable (default=1)
  141. // 0x0 = Disables volume ramp. New volume settings take immediate
  142. // effect without a ramp
  143. // 0x1 = Enables volume ramp
  144. // This field affects DAC_VOL. The volume ramp effects both
  145. // volume settings and mute When set to 1 a soft mute is enabled.
  146. // 8 VOL_EXPO_RAMP Exponential Volume Ramp Enable
  147. // 0x0 = Linear ramp over top 4 volume octaves
  148. // 0x1 = Exponential ramp over full volume range
  149. // This bit only takes effect if VOL_RAMP_EN is 1.
  150. // 3 DAC_MUTE_RIGHT DAC Right Mute (default=1)
  151. // 0x0 = Unmute
  152. // 0x1 = Muted
  153. // If VOL_RAMP_EN = 1, this is a soft mute.
  154. // 2 DAC_MUTE_LEFT DAC Left Mute (default=1)
  155. // 0x0 = Unmute
  156. // 0x1 = Muted
  157. // If VOL_RAMP_EN = 1, this is a soft mute.
  158. // 1 ADC_HPF_FREEZE ADC High Pass Filter Freeze
  159. // 0x0 = Normal operation
  160. // 0x1 = Freeze the ADC high-pass filter offset register. The
  161. // offset continues to be subtracted from the ADC data stream.
  162. // 0 ADC_HPF_BYPASS ADC High Pass Filter Bypass
  163. // 0x0 = Normal operation
  164. // 0x1 = Bypassed and offset not updated
  165. #define CHIP_DAC_VOL 0x0010
  166. // 15:8 DAC_VOL_RIGHT DAC Right Channel Volume. Set the Right channel DAC volume
  167. // with 0.5017 dB steps from 0 to -90 dB
  168. // 0x3B and less = Reserved
  169. // 0x3C = 0 dB
  170. // 0x3D = -0.5 dB
  171. // 0xF0 = -90 dB
  172. // 0xFC and greater = Muted
  173. // If VOL_RAMP_EN = 1, there is an automatic ramp to the
  174. // new volume setting.
  175. // 7:0 DAC_VOL_LEFT DAC Left Channel Volume. Set the Left channel DAC volume
  176. // with 0.5017 dB steps from 0 to -90 dB
  177. // 0x3B and less = Reserved
  178. // 0x3C = 0 dB
  179. // 0x3D = -0.5 dB
  180. // 0xF0 = -90 dB
  181. // 0xFC and greater = Muted
  182. // If VOL_RAMP_EN = 1, there is an automatic ramp to the
  183. // new volume setting.
  184. #define CHIP_PAD_STRENGTH 0x0014
  185. // 9:8 I2S_LRCLK I2S LRCLK Pad Drive Strength (default=1)
  186. // Sets drive strength for output pads per the table below.
  187. // VDDIO 1.8 V 2.5 V 3.3 V
  188. // 0x0 = Disable
  189. // 0x1 = 1.66 mA 2.87 mA 4.02 mA
  190. // 0x2 = 3.33 mA 5.74 mA 8.03 mA
  191. // 0x3 = 4.99 mA 8.61 mA 12.05 mA
  192. // 7:6 I2S_SCLK I2S SCLK Pad Drive Strength (default=1)
  193. // 5:4 I2S_DOUT I2S DOUT Pad Drive Strength (default=1)
  194. // 3:2 CTRL_DATA I2C DATA Pad Drive Strength (default=3)
  195. // 1:0 CTRL_CLK I2C CLK Pad Drive Strength (default=3)
  196. // (all use same table as I2S_LRCLK)
  197. #define CHIP_ANA_ADC_CTRL 0x0020
  198. // 8 ADC_VOL_M6DB ADC Volume Range Reduction
  199. // This bit shifts both right and left analog ADC volume
  200. // range down by 6.0 dB.
  201. // 0x0 = No change in ADC range
  202. // 0x1 = ADC range reduced by 6.0 dB
  203. // 7:4 ADC_VOL_RIGHT ADC Right Channel Volume
  204. // Right channel analog ADC volume control in 1.5 dB steps.
  205. // 0x0 = 0 dB
  206. // 0x1 = +1.5 dB
  207. // ...
  208. // 0xF = +22.5 dB
  209. // This range is -6.0 dB to +16.5 dB if ADC_VOL_M6DB is set to 1.
  210. // 3:0 ADC_VOL_LEFT ADC Left Channel Volume
  211. // (same scale as ADC_VOL_RIGHT)
  212. #define CHIP_ANA_HP_CTRL 0x0022
  213. // 14:8 HP_VOL_RIGHT Headphone Right Channel Volume (default 0x18)
  214. // Right channel headphone volume control with 0.5 dB steps.
  215. // 0x00 = +12 dB
  216. // 0x01 = +11.5 dB
  217. // 0x18 = 0 dB
  218. // ...
  219. // 0x7F = -51.5 dB
  220. // 6:0 HP_VOL_LEFT Headphone Left Channel Volume (default 0x18)
  221. // (same scale as HP_VOL_RIGHT)
  222. #define CHIP_ANA_CTRL 0x0024
  223. // 8 MUTE_LO LINEOUT Mute, 0 = Unmute, 1 = Mute (default 1)
  224. // 6 SELECT_HP Select the headphone input, 0 = DAC, 1 = LINEIN
  225. // 5 EN_ZCD_HP Enable the headphone zero cross detector (ZCD)
  226. // 0x0 = HP ZCD disabled
  227. // 0x1 = HP ZCD enabled
  228. // 4 MUTE_HP Mute the headphone outputs, 0 = Unmute, 1 = Mute (default)
  229. // 2 SELECT_ADC Select the ADC input, 0 = Microphone, 1 = LINEIN
  230. // 1 EN_ZCD_ADC Enable the ADC analog zero cross detector (ZCD)
  231. // 0x0 = ADC ZCD disabled
  232. // 0x1 = ADC ZCD enabled
  233. // 0 MUTE_ADC Mute the ADC analog volume, 0 = Unmute, 1 = Mute (default)
  234. #define CHIP_LINREG_CTRL 0x0026
  235. // 6 VDDC_MAN_ASSN Determines chargepump source when VDDC_ASSN_OVRD is set.
  236. // 0x0 = VDDA
  237. // 0x1 = VDDIO
  238. // 5 VDDC_ASSN_OVRD Charge pump Source Assignment Override
  239. // 0x0 = Charge pump source is automatically assigned based
  240. // on higher of VDDA and VDDIO
  241. // 0x1 = the source of charge pump is manually assigned by
  242. // VDDC_MAN_ASSN If VDDIO and VDDA are both the same
  243. // and greater than 3.1 V, VDDC_ASSN_OVRD and
  244. // VDDC_MAN_ASSN should be used to manually assign
  245. // VDDIO as the source for charge pump.
  246. // 3:0 D_PROGRAMMING Sets the VDDD linear regulator output voltage in 50 mV steps.
  247. // Must clear the LINREG_SIMPLE_POWERUP and STARTUP_POWERUP bits
  248. // in the 0x0030 (CHIP_ANA_POWER) register after power-up, for
  249. // this setting to produce the proper VDDD voltage.
  250. // 0x0 = 1.60
  251. // 0xF = 0.85
  252. #define CHIP_REF_CTRL 0x0028 // bandgap reference bias voltage and currents
  253. // 8:4 VAG_VAL Analog Ground Voltage Control
  254. // These bits control the analog ground voltage in 25 mV steps.
  255. // This should usually be set to VDDA/2 or lower for best
  256. // performance (maximum output swing at minimum THD). This VAG
  257. // reference is also used for the DAC and ADC voltage reference.
  258. // So changing this voltage scales the output swing of the DAC
  259. // and the output signal of the ADC.
  260. // 0x00 = 0.800 V
  261. // 0x1F = 1.575 V
  262. // 3:1 BIAS_CTRL Bias control
  263. // These bits adjust the bias currents for all of the analog
  264. // blocks. By lowering the bias current a lower quiescent power
  265. // is achieved. It should be noted that this mode can affect
  266. // performance by 3-4 dB.
  267. // 0x0 = Nominal
  268. // 0x1-0x3=+12.5%
  269. // 0x4=-12.5%
  270. // 0x5=-25%
  271. // 0x6=-37.5%
  272. // 0x7=-50%
  273. // 0 SMALL_POP VAG Ramp Control
  274. // Setting this bit slows down the VAG ramp from ~200 to ~400 ms
  275. // to reduce the startup pop, but increases the turn on/off time.
  276. // 0x0 = Normal VAG ramp
  277. // 0x1 = Slow down VAG ramp
  278. #define CHIP_MIC_CTRL 0x002A // microphone gain & internal microphone bias
  279. // 9:8 BIAS_RESISTOR MIC Bias Output Impedance Adjustment
  280. // Controls an adjustable output impedance for the microphone bias.
  281. // If this is set to zero the micbias block is powered off and
  282. // the output is highZ.
  283. // 0x0 = Powered off
  284. // 0x1 = 2.0 kohm
  285. // 0x2 = 4.0 kohm
  286. // 0x3 = 8.0 kohm
  287. // 6:4 BIAS_VOLT MIC Bias Voltage Adjustment
  288. // Controls an adjustable bias voltage for the microphone bias
  289. // amp in 250 mV steps. This bias voltage setting should be no
  290. // more than VDDA-200 mV for adequate power supply rejection.
  291. // 0x0 = 1.25 V
  292. // ...
  293. // 0x7 = 3.00 V
  294. // 1:0 GAIN MIC Amplifier Gain
  295. // Sets the microphone amplifier gain. At 0 dB setting the THD
  296. // can be slightly higher than other paths- typically around
  297. // ~65 dB. At other gain settings the THD are better.
  298. // 0x0 = 0 dB
  299. // 0x1 = +20 dB
  300. // 0x2 = +30 dB
  301. // 0x3 = +40 dB
  302. #define CHIP_LINE_OUT_CTRL 0x002C
  303. // 11:8 OUT_CURRENT Controls the output bias current for the LINEOUT amplifiers. The
  304. // nominal recommended setting for a 10 kohm load with 1.0 nF load cap
  305. // is 0x3. There are only 5 valid settings.
  306. // 0x0=0.18 mA
  307. // 0x1=0.27 mA
  308. // 0x3=0.36 mA
  309. // 0x7=0.45 mA
  310. // 0xF=0.54 mA
  311. // 5:0 LO_VAGCNTRL LINEOUT Amplifier Analog Ground Voltage
  312. // Controls the analog ground voltage for the LINEOUT amplifiers
  313. // in 25 mV steps. This should usually be set to VDDIO/2.
  314. // 0x00 = 0.800 V
  315. // ...
  316. // 0x1F = 1.575 V
  317. // ...
  318. // 0x23 = 1.675 V
  319. // 0x24-0x3F are invalid
  320. #define CHIP_LINE_OUT_VOL 0x002E
  321. // 12:8 LO_VOL_RIGHT LINEOUT Right Channel Volume (default=4)
  322. // Controls the right channel LINEOUT volume in 0.5 dB steps.
  323. // Higher codes have more attenuation.
  324. // 4:0 LO_VOL_LEFT LINEOUT Left Channel Output Level (default=4)
  325. // Used to normalize the output level of the left line output
  326. // to full scale based on the values used to set
  327. // LINE_OUT_CTRL->LO_VAGCNTRL and CHIP_REF_CTRL->VAG_VAL.
  328. // In general this field should be set to:
  329. // 40*log((VAG_VAL)/(LO_VAGCNTRL)) + 15
  330. // Suggested values based on typical VDDIO and VDDA voltages.
  331. // VDDA VAG_VAL VDDIO LO_VAGCNTRL LO_VOL_*
  332. // 1.8 V 0.9 3.3 V 1.55 0x06
  333. // 1.8 V 0.9 1.8 V 0.9 0x0F
  334. // 3.3 V 1.55 1.8 V 0.9 0x19
  335. // 3.3 V 1.55 3.3 V 1.55 0x0F
  336. // After setting to the nominal voltage, this field can be used
  337. // to adjust the output level in +/-0.5 dB increments by using
  338. // values higher or lower than the nominal setting.
  339. #define CHIP_ANA_POWER 0x0030 // power down controls for the analog blocks.
  340. // The only other power-down controls are BIAS_RESISTOR in the MIC_CTRL register
  341. // and the EN_ZCD control bits in ANA_CTRL.
  342. // 14 DAC_MONO While DAC_POWERUP is set, this allows the DAC to be put into left only
  343. // mono operation for power savings. 0=mono, 1=stereo (default)
  344. // 13 LINREG_SIMPLE_POWERUP Power up the simple (low power) digital supply regulator.
  345. // After reset, this bit can be cleared IF VDDD is driven
  346. // externally OR the primary digital linreg is enabled with
  347. // LINREG_D_POWERUP
  348. // 12 STARTUP_POWERUP Power up the circuitry needed during the power up ramp and reset.
  349. // After reset this bit can be cleared if VDDD is coming from
  350. // an external source.
  351. // 11 VDDC_CHRGPMP_POWERUP Power up the VDDC charge pump block. If neither VDDA or VDDIO
  352. // is 3.0 V or larger this bit should be cleared before analog
  353. // blocks are powered up.
  354. // 10 PLL_POWERUP PLL Power Up, 0 = Power down, 1 = Power up
  355. // When cleared, the PLL is turned off. This must be set before
  356. // CHIP_CLK_CTRL->MCLK_FREQ is programmed to 0x3. The
  357. // CHIP_PLL_CTRL register must be configured correctly before
  358. // setting this bit.
  359. // 9 LINREG_D_POWERUP Power up the primary VDDD linear regulator, 0 = Power down, 1 = Power up
  360. // 8 VCOAMP_POWERUP Power up the PLL VCO amplifier, 0 = Power down, 1 = Power up
  361. // 7 VAG_POWERUP Power up the VAG reference buffer.
  362. // Setting this bit starts the power up ramp for the headphone
  363. // and LINEOUT. The headphone (and/or LINEOUT) powerup should
  364. // be set BEFORE clearing this bit. When this bit is cleared
  365. // the power-down ramp is started. The headphone (and/or LINEOUT)
  366. // powerup should stay set until the VAG is fully ramped down
  367. // (200 to 400 ms after clearing this bit).
  368. // 0x0 = Power down, 0x1 = Power up
  369. // 6 ADC_MONO While ADC_POWERUP is set, this allows the ADC to be put into left only
  370. // mono operation for power savings. This mode is useful when
  371. // only using the microphone input.
  372. // 0x0 = Mono (left only), 0x1 = Stereo
  373. // 5 REFTOP_POWERUP Power up the reference bias currents
  374. // 0x0 = Power down, 0x1 = Power up
  375. // This bit can be cleared when the part is a sleep state
  376. // to minimize analog power.
  377. // 4 HEADPHONE_POWERUP Power up the headphone amplifiers
  378. // 0x0 = Power down, 0x1 = Power up
  379. // 3 DAC_POWERUP Power up the DACs
  380. // 0x0 = Power down, 0x1 = Power up
  381. // 2 CAPLESS_HEADPHONE_POWERUP Power up the capless headphone mode
  382. // 0x0 = Power down, 0x1 = Power up
  383. // 1 ADC_POWERUP Power up the ADCs
  384. // 0x0 = Power down, 0x1 = Power up
  385. // 0 LINEOUT_POWERUP Power up the LINEOUT amplifiers
  386. // 0x0 = Power down, 0x1 = Power up
  387. #define CHIP_PLL_CTRL 0x0032
  388. // 15:11 INT_DIVISOR
  389. // 10:0 FRAC_DIVISOR
  390. #define CHIP_CLK_TOP_CTRL 0x0034
  391. // 11 ENABLE_INT_OSC Setting this bit enables an internal oscillator to be used for the
  392. // zero cross detectors, the short detect recovery, and the
  393. // charge pump. This allows the I2S clock to be shut off while
  394. // still operating an analog signal path. This bit can be kept
  395. // on when the I2S clock is enabled, but the I2S clock is more
  396. // accurate so it is preferred to clear this bit when I2S is present.
  397. // 3 INPUT_FREQ_DIV2 SYS_MCLK divider before PLL input
  398. // 0x0 = pass through
  399. // 0x1 = SYS_MCLK is divided by 2 before entering PLL
  400. // This must be set when the input clock is above 17 Mhz. This
  401. // has no effect when the PLL is powered down.
  402. #define CHIP_ANA_STATUS 0x0036
  403. // 9 LRSHORT_STS This bit is high whenever a short is detected on the left or right
  404. // channel headphone drivers.
  405. // 8 CSHORT_STS This bit is high whenever a short is detected on the capless headphone
  406. // common/center channel driver.
  407. // 4 PLL_IS_LOCKED This bit goes high after the PLL is locked.
  408. #define CHIP_ANA_TEST1 0x0038 // intended only for debug.
  409. #define CHIP_ANA_TEST2 0x003A // intended only for debug.
  410. #define CHIP_SHORT_CTRL 0x003C
  411. // 14:12 LVLADJR Right channel headphone short detector in 25 mA steps.
  412. // 0x3=25 mA
  413. // 0x2=50 mA
  414. // 0x1=75 mA
  415. // 0x0=100 mA
  416. // 0x4=125 mA
  417. // 0x5=150 mA
  418. // 0x6=175 mA
  419. // 0x7=200 mA
  420. // This trip point can vary by ~30% over process so leave plenty
  421. // of guard band to avoid false trips. This short detect trip
  422. // point is also effected by the bias current adjustments made
  423. // by CHIP_REF_CTRL->BIAS_CTRL and by CHIP_ANA_TEST1->HP_IALL_ADJ.
  424. // 10:8 LVLADJL Left channel headphone short detector in 25 mA steps.
  425. // (same scale as LVLADJR)
  426. // 6:4 LVLADJC Capless headphone center channel short detector in 50 mA steps.
  427. // 0x3=50 mA
  428. // 0x2=100 mA
  429. // 0x1=150 mA
  430. // 0x0=200 mA
  431. // 0x4=250 mA
  432. // 0x5=300 mA
  433. // 0x6=350 mA
  434. // 0x7=400 mA
  435. // 3:2 MODE_LR Behavior of left/right short detection
  436. // 0x0 = Disable short detector, reset short detect latch,
  437. // software view non-latched short signal
  438. // 0x1 = Enable short detector and reset the latch at timeout
  439. // (every ~50 ms)
  440. // 0x2 = This mode is not used/invalid
  441. // 0x3 = Enable short detector with only manual reset (have
  442. // to return to 0x0 to reset the latch)
  443. // 1:0 MODE_CM Behavior of capless headphone central short detection
  444. // (same settings as MODE_LR)
  445. #define DAP_CONTROL 0x0100
  446. #define DAP_PEQ 0x0102
  447. #define DAP_BASS_ENHANCE 0x0104
  448. #define DAP_BASS_ENHANCE_CTRL 0x0106
  449. #define DAP_AUDIO_EQ 0x0108
  450. #define DAP_SGTL_SURROUND 0x010A
  451. #define DAP_FILTER_COEF_ACCESS 0x010C
  452. #define DAP_COEF_WR_B0_MSB 0x010E
  453. #define DAP_COEF_WR_B0_LSB 0x0110
  454. #define DAP_AUDIO_EQ_BASS_BAND0 0x0116 // 115 Hz
  455. #define DAP_AUDIO_EQ_BAND1 0x0118 // 330 Hz
  456. #define DAP_AUDIO_EQ_BAND2 0x011A // 990 Hz
  457. #define DAP_AUDIO_EQ_BAND3 0x011C // 3000 Hz
  458. #define DAP_AUDIO_EQ_TREBLE_BAND4 0x011E // 9900 Hz
  459. #define DAP_MAIN_CHAN 0x0120
  460. #define DAP_MIX_CHAN 0x0122
  461. #define DAP_AVC_CTRL 0x0124
  462. #define DAP_AVC_THRESHOLD 0x0126
  463. #define DAP_AVC_ATTACK 0x0128
  464. #define DAP_AVC_DECAY 0x012A
  465. #define DAP_COEF_WR_B1_MSB 0x012C
  466. #define DAP_COEF_WR_B1_LSB 0x012E
  467. #define DAP_COEF_WR_B2_MSB 0x0130
  468. #define DAP_COEF_WR_B2_LSB 0x0132
  469. #define DAP_COEF_WR_A1_MSB 0x0134
  470. #define DAP_COEF_WR_A1_LSB 0x0136
  471. #define DAP_COEF_WR_A2_MSB 0x0138
  472. #define DAP_COEF_WR_A2_LSB 0x013A
  473. #define SGTL5000_I2C_ADDR 0x0A // CTRL_ADR0_CS pin low (normal configuration)
  474. //#define SGTL5000_I2C_ADDR 0x2A // CTRL_ADR0_CS pin high
  475. bool AudioControlSGTL5000::enable(void)
  476. {
  477. muted = true;
  478. Wire.begin();
  479. delay(5);
  480. //Serial.print("chip ID = ");
  481. //delay(5);
  482. //unsigned int n = read(CHIP_ID);
  483. //Serial.println(n, HEX);
  484. write(CHIP_ANA_POWER, 0x4060); // VDDD is externally driven with 1.8V
  485. write(CHIP_LINREG_CTRL, 0x006C); // VDDA & VDDIO both over 3.1V
  486. write(CHIP_REF_CTRL, 0x01F2); // VAG=1.575, normal ramp, +12.5% bias current
  487. write(CHIP_LINE_OUT_CTRL, 0x0F22); // LO_VAGCNTRL=1.65V, OUT_CURRENT=0.54mA
  488. write(CHIP_SHORT_CTRL, 0x4446); // allow up to 125mA
  489. write(CHIP_ANA_CTRL, 0x0137); // enable zero cross detectors
  490. write(CHIP_ANA_POWER, 0x40FF); // power up: lineout, hp, adc, dac
  491. write(CHIP_DIG_POWER, 0x0073); // power up all digital stuff
  492. delay(400);
  493. write(CHIP_LINE_OUT_VOL, 0x1D1D); // default approx 1.3 volts peak-to-peak
  494. write(CHIP_CLK_CTRL, 0x0004); // 44.1 kHz, 256*Fs
  495. write(CHIP_I2S_CTRL, 0x0130); // SCLK=32*Fs, 16bit, I2S format
  496. // default signal routing is ok?
  497. write(CHIP_SSS_CTRL, 0x0010); // ADC->I2S, I2S->DAC
  498. write(CHIP_ADCDAC_CTRL, 0x0000); // disable dac mute
  499. write(CHIP_DAC_VOL, 0x3C3C); // digital gain, 0dB
  500. write(CHIP_ANA_HP_CTRL, 0x7F7F); // set volume (lowest level)
  501. write(CHIP_ANA_CTRL, 0x0036); // enable zero cross detectors
  502. //mute = false;
  503. semi_automated = true;
  504. return true;
  505. }
  506. unsigned int AudioControlSGTL5000::read(unsigned int reg)
  507. {
  508. unsigned int val;
  509. Wire.beginTransmission(SGTL5000_I2C_ADDR);
  510. Wire.write(reg >> 8);
  511. Wire.write(reg);
  512. if (Wire.endTransmission(false) != 0) return 0;
  513. if (Wire.requestFrom(SGTL5000_I2C_ADDR, 2) < 2) return 0;
  514. val = Wire.read() << 8;
  515. val |= Wire.read();
  516. return val;
  517. }
  518. bool AudioControlSGTL5000::write(unsigned int reg, unsigned int val)
  519. {
  520. if (reg == CHIP_ANA_CTRL) ana_ctrl = val;
  521. Wire.beginTransmission(SGTL5000_I2C_ADDR);
  522. Wire.write(reg >> 8);
  523. Wire.write(reg);
  524. Wire.write(val >> 8);
  525. Wire.write(val);
  526. if (Wire.endTransmission() == 0) return true;
  527. return false;
  528. }
  529. unsigned int AudioControlSGTL5000::modify(unsigned int reg, unsigned int val, unsigned int iMask)
  530. {
  531. unsigned int val1 = (read(reg)&(~iMask))|val;
  532. if(!write(reg,val1)) return 0;
  533. return val1;
  534. }
  535. bool AudioControlSGTL5000::volumeInteger(unsigned int n)
  536. {
  537. if (n == 0) {
  538. muted = true;
  539. write(CHIP_ANA_HP_CTRL, 0x7F7F);
  540. return muteHeadphone();
  541. } else if (n > 0x80) {
  542. n = 0;
  543. } else {
  544. n = 0x80 - n;
  545. }
  546. if (muted) {
  547. muted = false;
  548. unmuteHeadphone();
  549. }
  550. n = n | (n << 8);
  551. return write(CHIP_ANA_HP_CTRL, n); // set volume
  552. }
  553. bool AudioControlSGTL5000::volume(float left, float right)
  554. {
  555. unsigned short m=((0x7F-calcVol(right,0x7F))<<8)|(0x7F-calcVol(left,0x7F));
  556. return write(CHIP_ANA_HP_CTRL, m);
  557. }
  558. bool AudioControlSGTL5000::micGain(unsigned int dB)
  559. {
  560. unsigned int preamp_gain, input_gain;
  561. if (dB >= 40) {
  562. preamp_gain = 3;
  563. dB -= 40;
  564. } else if (dB >= 30) {
  565. preamp_gain = 2;
  566. dB -= 30;
  567. } else if (dB >= 20) {
  568. preamp_gain = 1;
  569. dB -= 20;
  570. } else {
  571. preamp_gain = 0;
  572. }
  573. input_gain = (dB * 2) / 3;
  574. if (input_gain > 15) input_gain = 15;
  575. return write(CHIP_MIC_CTRL, 0x0170 | preamp_gain)
  576. && write(CHIP_ANA_ADC_CTRL, (input_gain << 4) | input_gain);
  577. }
  578. // CHIP_ANA_ADC_CTRL
  579. // Actual measured full-scale peak-to-peak sine wave input for max signal
  580. // 0: 3.12 Volts p-p
  581. // 1: 2.63 Volts p-p
  582. // 2: 2.22 Volts p-p
  583. // 3: 1.87 Volts p-p
  584. // 4: 1.58 Volts p-p
  585. // 5: 1.33 Volts p-p
  586. // 6: 1.11 Volts p-p
  587. // 7: 0.94 Volts p-p
  588. // 8: 0.79 Volts p-p
  589. // 9: 0.67 Volts p-p
  590. // 10: 0.56 Volts p-p
  591. // 11: 0.48 Volts p-p
  592. // 12: 0.40 Volts p-p
  593. // 13: 0.34 Volts p-p
  594. // 14: 0.29 Volts p-p
  595. // 15: 0.24 Volts p-p
  596. bool AudioControlSGTL5000::lineInLevel(uint8_t left, uint8_t right)
  597. {
  598. if (left > 15) left = 15;
  599. if (right > 15) right = 15;
  600. return write(CHIP_ANA_ADC_CTRL, (left << 4) | right);
  601. }
  602. // CHIP_LINE_OUT_VOL
  603. // Actual measured full-scale peak-to-peak sine wave output voltage:
  604. // 0-12: output has clipping
  605. // 13: 3.16 Volts p-p
  606. // 14: 2.98 Volts p-p
  607. // 15: 2.83 Volts p-p
  608. // 16: 2.67 Volts p-p
  609. // 17: 2.53 Volts p-p
  610. // 18: 2.39 Volts p-p
  611. // 19: 2.26 Volts p-p
  612. // 20: 2.14 Volts p-p
  613. // 21: 2.02 Volts p-p
  614. // 22: 1.91 Volts p-p
  615. // 23: 1.80 Volts p-p
  616. // 24: 1.71 Volts p-p
  617. // 25: 1.62 Volts p-p
  618. // 26: 1.53 Volts p-p
  619. // 27: 1.44 Volts p-p
  620. // 28: 1.37 Volts p-p
  621. // 29: 1.29 Volts p-p
  622. // 30: 1.22 Volts p-p
  623. // 31: 1.16 Volts p-p
  624. unsigned short AudioControlSGTL5000::lineOutLevel(uint8_t n)
  625. {
  626. if (n > 31) n = 31;
  627. else if (n < 13) n = 13;
  628. return modify(CHIP_LINE_OUT_VOL,(n<<8)|n,(31<<8)|31);
  629. }
  630. unsigned short AudioControlSGTL5000::lineOutLevel(uint8_t left, uint8_t right)
  631. {
  632. if (left > 31) left = 31;
  633. else if (left < 13) left = 13;
  634. if (right > 31) right = 31;
  635. else if (right < 13) right = 13;
  636. return modify(CHIP_LINE_OUT_VOL,(right<<8)|left,(31<<8)|31);
  637. }
  638. unsigned short AudioControlSGTL5000::dacVolume(float n) // set both directly
  639. {
  640. if ((read(CHIP_ADCDAC_CTRL)&(3<<2)) != ((n>0 ? 0:3)<<2)) {
  641. modify(CHIP_ADCDAC_CTRL,(n>0 ? 0:3)<<2,3<<2);
  642. }
  643. unsigned char m=calcVol(n,0xC0);
  644. return modify(CHIP_DAC_VOL,((0xFC-m)<<8)|(0xFC-m),65535);
  645. }
  646. unsigned short AudioControlSGTL5000::dacVolume(float left, float right)
  647. {
  648. unsigned short adcdac=((right>0 ? 0:2)|(left>0 ? 0:1))<<2;
  649. if ((read(CHIP_ADCDAC_CTRL)&(3<<2)) != adcdac) {
  650. modify(CHIP_ADCDAC_CTRL,adcdac,1<<2);
  651. }
  652. unsigned short m=(0xFC-calcVol(right,0xC0))<<8|(0xFC-calcVol(left,0xC0));
  653. return modify(CHIP_DAC_VOL,m,65535);
  654. }
  655. unsigned short AudioControlSGTL5000::adcHighPassFilterEnable(void)
  656. {
  657. return modify(CHIP_ADCDAC_CTRL, 0, 3);
  658. }
  659. unsigned short AudioControlSGTL5000::adcHighPassFilterFreeze(void)
  660. {
  661. return modify(CHIP_ADCDAC_CTRL, 2, 3);
  662. }
  663. unsigned short AudioControlSGTL5000::adcHighPassFilterDisable(void)
  664. {
  665. return modify(CHIP_ADCDAC_CTRL, 1, 3);
  666. }
  667. // DAP_CONTROL
  668. unsigned short AudioControlSGTL5000::audioPreProcessorEnable(void)
  669. {
  670. // audio processor used to pre-process analog input before Teensy
  671. return write(DAP_CONTROL, 1) && write(CHIP_SSS_CTRL, 0x0013);
  672. }
  673. unsigned short AudioControlSGTL5000::audioPostProcessorEnable(void)
  674. {
  675. // audio processor used to post-process Teensy output before headphones/lineout
  676. return write(DAP_CONTROL, 1) && write(CHIP_SSS_CTRL, 0x0070);
  677. }
  678. unsigned short AudioControlSGTL5000::audioProcessorDisable(void)
  679. {
  680. return write(CHIP_SSS_CTRL, 0x0010) && write(DAP_CONTROL, 0);
  681. }
  682. // DAP_PEQ
  683. unsigned short AudioControlSGTL5000::eqFilterCount(uint8_t n) // valid to n&7, 0 thru 7 filters enabled.
  684. {
  685. return modify(DAP_PEQ,(n&7),7);
  686. }
  687. // DAP_AUDIO_EQ
  688. unsigned short AudioControlSGTL5000::eqSelect(uint8_t n) // 0=NONE, 1=PEQ (7 IIR Biquad filters), 2=TONE (tone), 3=GEQ (5 band EQ)
  689. {
  690. return modify(DAP_AUDIO_EQ,n&3,3);
  691. }
  692. unsigned short AudioControlSGTL5000::eqBand(uint8_t bandNum, float n)
  693. {
  694. if(semi_automated) automate(1,3);
  695. return dap_audio_eq_band(bandNum, n);
  696. }
  697. void AudioControlSGTL5000::eqBands(float bass, float mid_bass, float midrange, float mid_treble, float treble)
  698. {
  699. if(semi_automated) automate(1,3);
  700. dap_audio_eq_band(0,bass);
  701. dap_audio_eq_band(1,mid_bass);
  702. dap_audio_eq_band(2,midrange);
  703. dap_audio_eq_band(3,mid_treble);
  704. dap_audio_eq_band(4,treble);
  705. }
  706. void AudioControlSGTL5000::eqBands(float bass, float treble) // dap_audio_eq(2);
  707. {
  708. if(semi_automated) automate(1,2);
  709. dap_audio_eq_band(0,bass);
  710. dap_audio_eq_band(4,treble);
  711. }
  712. // SGTL5000 PEQ Coefficient loader
  713. void AudioControlSGTL5000::eqFilter(uint8_t filterNum, int *filterParameters)
  714. {
  715. // TODO: add the part that selects 7 PEQ filters.
  716. if(semi_automated) automate(1,1,filterNum+1);
  717. modify(DAP_FILTER_COEF_ACCESS,(uint16_t)filterNum,15);
  718. write(DAP_COEF_WR_B0_MSB,(*filterParameters>>4)&65535);
  719. write(DAP_COEF_WR_B0_LSB,(*filterParameters++)&15);
  720. write(DAP_COEF_WR_B1_MSB,(*filterParameters>>4)&65535);
  721. write(DAP_COEF_WR_B1_LSB,(*filterParameters++)&15);
  722. write(DAP_COEF_WR_B2_MSB,(*filterParameters>>4)&65535);
  723. write(DAP_COEF_WR_B2_LSB,(*filterParameters++)&15);
  724. write(DAP_COEF_WR_A1_MSB,(*filterParameters>>4)&65535);
  725. write(DAP_COEF_WR_A1_LSB,(*filterParameters++)&15);
  726. write(DAP_COEF_WR_A2_MSB,(*filterParameters>>4)&65535);
  727. write(DAP_COEF_WR_A2_LSB,(*filterParameters++)&15);
  728. write(DAP_FILTER_COEF_ACCESS,(uint16_t)0x100|filterNum);
  729. }
  730. /* Valid values for dap_avc parameters
  731. maxGain; Maximum gain that can be applied
  732. 0 - 0 dB
  733. 1 - 6.0 dB
  734. 2 - 12 dB
  735. lbiResponse; Integrator Response
  736. 0 - 0 mS
  737. 1 - 25 mS
  738. 2 - 50 mS
  739. 3 - 100 mS
  740. hardLimit
  741. 0 - Hard limit disabled. AVC Compressor/Expander enabled.
  742. 1 - Hard limit enabled. The signal is limited to the programmed threshold (signal saturates at the threshold)
  743. threshold
  744. floating point in range 0 to -96 dB
  745. attack
  746. floating point figure is dB/s rate at which gain is increased
  747. decay
  748. floating point figure is dB/s rate at which gain is reduced
  749. */
  750. unsigned short AudioControlSGTL5000::autoVolumeControl(uint8_t maxGain, uint8_t lbiResponse, uint8_t hardLimit, float threshold, float attack, float decay)
  751. {
  752. //if(semi_automated&&(!read(DAP_CONTROL)&1)) audioProcessorEnable();
  753. if(maxGain>2) maxGain=2;
  754. lbiResponse&=3;
  755. hardLimit&=1;
  756. uint8_t thresh=(pow(10,threshold/20)*0.636)*pow(2,15);
  757. uint8_t att=(1-pow(10,-(attack/(20*44100))))*pow(2,19);
  758. uint8_t dec=(1-pow(10,-(decay/(20*44100))))*pow(2,23);
  759. write(DAP_AVC_THRESHOLD,thresh);
  760. write(DAP_AVC_ATTACK,att);
  761. write(DAP_AVC_DECAY,dec);
  762. return modify(DAP_AVC_CTRL,maxGain<<12|lbiResponse<<8|hardLimit<<5,3<<12|3<<8|1<<5);
  763. }
  764. unsigned short AudioControlSGTL5000::autoVolumeEnable(void)
  765. {
  766. return modify(DAP_AVC_CTRL, 1, 1);
  767. }
  768. unsigned short AudioControlSGTL5000::autoVolumeDisable(void)
  769. {
  770. return modify(DAP_AVC_CTRL, 0, 1);
  771. }
  772. unsigned short AudioControlSGTL5000::enhanceBass(float lr_lev, float bass_lev)
  773. {
  774. return modify(DAP_BASS_ENHANCE_CTRL,((0x3F-calcVol(lr_lev,0x3F))<<8) | (0x7F-calcVol(bass_lev,0x7F)), (0x3F<<8) | 0x7F);
  775. }
  776. unsigned short AudioControlSGTL5000::enhanceBass(float lr_lev, float bass_lev, uint8_t hpf_bypass, uint8_t cutoff)
  777. {
  778. modify(DAP_BASS_ENHANCE,(hpf_bypass&1)<<8|(cutoff&7)<<4,1<<8|7<<4);
  779. return enhanceBass(lr_lev,bass_lev);
  780. }
  781. unsigned short AudioControlSGTL5000::enhanceBassEnable(void)
  782. {
  783. return modify(DAP_BASS_ENHANCE, 1, 1);
  784. }
  785. unsigned short AudioControlSGTL5000::enhanceBassDisable(void)
  786. {
  787. return modify(DAP_BASS_ENHANCE, 0, 1);
  788. }
  789. unsigned short AudioControlSGTL5000::surroundSound(uint8_t width)
  790. {
  791. return modify(DAP_SGTL_SURROUND,(width&7)<<4,7<<4);
  792. }
  793. unsigned short AudioControlSGTL5000::surroundSound(uint8_t width, uint8_t select)
  794. {
  795. return modify(DAP_SGTL_SURROUND,((width&7)<<4)|(select&3), (7<<4)|3);
  796. }
  797. unsigned short AudioControlSGTL5000::surroundSoundEnable(void)
  798. {
  799. return modify(DAP_SGTL_SURROUND, 3, 3);
  800. }
  801. unsigned short AudioControlSGTL5000::surroundSoundDisable(void)
  802. {
  803. return modify(DAP_SGTL_SURROUND, 0, 3);
  804. }
  805. unsigned char AudioControlSGTL5000::calcVol(float n, unsigned char range)
  806. {
  807. // n=(n*(((float)range)/100))+0.499;
  808. n=(n*(float)range)+0.499;
  809. if ((unsigned char)n>range) n=range;
  810. return (unsigned char)n;
  811. }
  812. // DAP_AUDIO_EQ_BASS_BAND0 & DAP_AUDIO_EQ_BAND1 & DAP_AUDIO_EQ_BAND2 etc etc
  813. unsigned short AudioControlSGTL5000::dap_audio_eq_band(uint8_t bandNum, float n) // by signed percentage -100/+100; dap_audio_eq(3);
  814. {
  815. n=(n*48)+0.499;
  816. if(n<-47) n=-47;
  817. if(n>48) n=48;
  818. n+=47;
  819. return modify(DAP_AUDIO_EQ_BASS_BAND0+(bandNum*2),(unsigned int)n,127);
  820. }
  821. void AudioControlSGTL5000::automate(uint8_t dap, uint8_t eq)
  822. {
  823. //if((dap!=0)&&(!(read(DAP_CONTROL)&1))) audioProcessorEnable();
  824. if((read(DAP_AUDIO_EQ)&3) != eq) eqSelect(eq);
  825. }
  826. void AudioControlSGTL5000::automate(uint8_t dap, uint8_t eq, uint8_t filterCount)
  827. {
  828. automate(dap,eq);
  829. if (filterCount > (read(DAP_PEQ)&7)) eqFilterCount(filterCount);
  830. }
  831. // if(SGTL5000_PEQ) quantization_unit=524288; if(AudioFilterBiquad) quantization_unit=2147483648;
  832. void calcBiquad(uint8_t filtertype, float fC, float dB_Gain, float Q, uint32_t quantization_unit, uint32_t fS, int *coef)
  833. {
  834. // I used resources like http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
  835. // to make this routine, I tested most of the filter types and they worked. Such filters have limits and
  836. // before calling this routine with varying values the end user should check that those values are limited
  837. // to valid results.
  838. float A;
  839. if(filtertype<FILTER_PARAEQ) A=pow(10,dB_Gain/20); else A=pow(10,dB_Gain/40);
  840. float W0 = 2*3.14159265358979323846*fC/fS;
  841. float cosw=cosf(W0);
  842. float sinw=sinf(W0);
  843. //float alpha = sinw*sinh((log(2)/2)*BW*W0/sinw);
  844. //float beta = sqrt(2*A);
  845. float alpha = sinw / (2 * Q);
  846. float beta = sqrtf(A)/Q;
  847. float b0,b1,b2,a0,a1,a2;
  848. switch(filtertype) {
  849. case FILTER_LOPASS:
  850. b0 = (1.0F - cosw) * 0.5F; // =(1-COS($H$2))/2
  851. b1 = 1.0F - cosw;
  852. b2 = (1.0F - cosw) * 0.5F;
  853. a0 = 1.0F + alpha;
  854. a1 = 2.0F * cosw;
  855. a2 = alpha - 1.0F;
  856. break;
  857. case FILTER_HIPASS:
  858. b0 = (1.0F + cosw) * 0.5F;
  859. b1 = -(cosw + 1.0F);
  860. b2 = (1.0F + cosw) * 0.5F;
  861. a0 = 1.0F + alpha;
  862. a1 = 2.0F * cosw;
  863. a2 = alpha - 1.0F;
  864. break;
  865. case FILTER_BANDPASS:
  866. b0 = alpha;
  867. b1 = 0.0F;
  868. b2 = -alpha;
  869. a0 = 1.0F + alpha;
  870. a1 = 2.0F * cosw;
  871. a2 = alpha - 1.0F;
  872. break;
  873. case FILTER_NOTCH:
  874. b0=1;
  875. b1=-2*cosw;
  876. b2=1;
  877. a0=1+alpha;
  878. a1=2*cosw;
  879. a2=-(1-alpha);
  880. break;
  881. case FILTER_PARAEQ:
  882. b0 = 1 + (alpha*A);
  883. b1 =-2 * cosw;
  884. b2 = 1 - (alpha*A);
  885. a0 = 1 + (alpha/A);
  886. a1 = 2 * cosw;
  887. a2 =-(1-(alpha/A));
  888. break;
  889. case FILTER_LOSHELF:
  890. b0 = A * ((A+1.0F) - ((A-1.0F)*cosw) + (beta*sinw));
  891. b1 = 2.0F * A * ((A-1.0F) - ((A+1.0F)*cosw));
  892. b2 = A * ((A+1.0F) - ((A-1.0F)*cosw) - (beta*sinw));
  893. a0 = (A+1.0F) + ((A-1.0F)*cosw) + (beta*sinw);
  894. a1 = 2.0F * ((A-1.0F) + ((A+1.0F)*cosw));
  895. a2 = -((A+1.0F) + ((A-1.0F)*cosw) - (beta*sinw));
  896. break;
  897. case FILTER_HISHELF:
  898. b0 = A * ((A+1.0F) + ((A-1.0F)*cosw) + (beta*sinw));
  899. b1 = -2.0F * A * ((A-1.0F) + ((A+1.0F)*cosw));
  900. b2 = A * ((A+1.0F) + ((A-1.0F)*cosw) - (beta*sinw));
  901. a0 = (A+1.0F) - ((A-1.0F)*cosw) + (beta*sinw);
  902. a1 = -2.0F * ((A-1.0F) - ((A+1.0F)*cosw));
  903. a2 = -((A+1.0F) - ((A-1.0F)*cosw) - (beta*sinw));
  904. default:
  905. b0 = 0.5;
  906. b1 = 0.0;
  907. b2 = 0.0;
  908. a0 = 1.0;
  909. a1 = 0.0;
  910. a2 = 0.0;
  911. }
  912. a0=(a0*2)/(float)quantization_unit; // once here instead of five times there...
  913. b0/=a0;
  914. *coef++=(int)(b0+0.499);
  915. b1/=a0;
  916. *coef++=(int)(b1+0.499);
  917. b2/=a0;
  918. *coef++=(int)(b2+0.499);
  919. a1/=a0;
  920. *coef++=(int)(a1+0.499);
  921. a2/=a0;
  922. *coef++=(int)(a2+0.499);
  923. }