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.

98 lines
2.6KB

  1. // The SGTL5000 has a secondard "dacVolume" setting, in addition to normal volume.
  2. //
  3. // Normally dacVolume defaults to 1.0, to pass your sound directly to the normal
  4. // volume control. The actual volume you hear depends on both settings.
  5. //
  6. // The dacVolume control has an option to gracefully ramp (change) its setting over
  7. // time, to prevent a sudden pop or click sound. You could achieve this by adding
  8. // code to change the volume setting in very small steps over time, but the
  9. // SGTL5000 can do it for you automatically.
  10. #include <Audio.h>
  11. #include <Wire.h>
  12. #include <SPI.h>
  13. #include <SD.h>
  14. // GUItool: begin automatically generated code
  15. AudioSynthWaveformSine sine1; //xy=203,233
  16. AudioOutputI2S i2s1; //xy=441,233
  17. AudioConnection patchCord1(sine1, 0, i2s1, 0);
  18. AudioConnection patchCord2(sine1, 0, i2s1, 1);
  19. AudioControlSGTL5000 sgtl5000_1; //xy=452,162
  20. // GUItool: end automatically generated code
  21. elapsedMillis msec;
  22. float vol;
  23. float inc;
  24. int rampType;
  25. const char *rampName[] = {
  26. "No Ramp (instant)", // loud pop due to instant change
  27. "Normal Ramp", // graceful transition between volume levels
  28. "Linear Ramp" // slight click/chirp
  29. };
  30. void setup(void) {
  31. Serial.begin(9600);
  32. AudioMemory(4);
  33. vol = 0.0;
  34. inc = 0.2;
  35. rampType = 0;
  36. sine1.amplitude(1.0);
  37. sine1.frequency(440);
  38. sgtl5000_1.enable();
  39. sgtl5000_1.volume(0.5); // set the main volume...
  40. sgtl5000_1.dacVolume(0); // set the "dac" volume (extra control)
  41. sgtl5000_1.dacVolumeRampDisable();
  42. Serial.println("setup done");
  43. }
  44. void loop(void) {
  45. if (msec > 1000) { // change the volume every second
  46. // increment or decrement the volume variable
  47. vol += inc;
  48. if (vol >= 1.0) {
  49. vol = 1.0;
  50. inc = -inc;
  51. }
  52. if (vol < 0.01) {
  53. vol = 0.0;
  54. inc = -inc;
  55. }
  56. Serial.print("Volume: ");
  57. Serial.print(vol);
  58. Serial.print(" ");
  59. Serial.println(rampName[rampType]);
  60. // configure which type of volume transition to use
  61. if (rampType == 0) {
  62. sgtl5000_1.dacVolumeRampDisable();
  63. } else if (rampType == 1) {
  64. sgtl5000_1.dacVolumeRamp();
  65. } else {
  66. sgtl5000_1.dacVolumeRampLinear();
  67. }
  68. // set the dacVolume. The actual change make take place over time, if ramping
  69. // this is a logarithmic volume,
  70. // that is, the range 0.0 to 1.0 gets converted to -90dB to 0dB in 0.5dB steps
  71. sgtl5000_1.dacVolume(vol);
  72. // if we turned the volume off, advance to the next ramp type
  73. if (vol < 0.01) {
  74. rampType = rampType + 1;
  75. if (rampType > 2) rampType = 0;
  76. }
  77. msec = 0;
  78. }
  79. }