VolumeRamp.ino 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. //
  11. // This example was originally contributed by Hedde Bosman
  12. #include <Audio.h>
  13. #include <Wire.h>
  14. #include <SPI.h>
  15. #include <SD.h>
  16. #include <SerialFlash.h>
  17. // GUItool: begin automatically generated code
  18. AudioSynthWaveformSine sine1; //xy=203,233
  19. AudioOutputI2S i2s1; //xy=441,233
  20. AudioConnection patchCord1(sine1, 0, i2s1, 0);
  21. AudioConnection patchCord2(sine1, 0, i2s1, 1);
  22. AudioControlSGTL5000 sgtl5000_1; //xy=452,162
  23. // GUItool: end automatically generated code
  24. elapsedMillis msec;
  25. float vol;
  26. float inc;
  27. int rampType;
  28. const char *rampName[] = {
  29. "No Ramp (instant)", // loud pop due to instant change
  30. "Normal Ramp", // graceful transition between volume levels
  31. "Linear Ramp" // slight click/chirp
  32. };
  33. void setup(void) {
  34. Serial.begin(9600);
  35. AudioMemory(4);
  36. vol = 0.0;
  37. inc = 0.2;
  38. rampType = 0;
  39. sine1.amplitude(1.0);
  40. sine1.frequency(440);
  41. sgtl5000_1.enable();
  42. sgtl5000_1.volume(0.5); // set the main volume...
  43. sgtl5000_1.dacVolume(0); // set the "dac" volume (extra control)
  44. sgtl5000_1.dacVolumeRampDisable();
  45. Serial.println("setup done");
  46. }
  47. void loop(void) {
  48. if (msec > 1000) { // change the volume every second
  49. // increment or decrement the volume variable
  50. vol += inc;
  51. if (vol >= 1.0) {
  52. vol = 1.0;
  53. inc = -inc;
  54. }
  55. if (vol < 0.01) {
  56. vol = 0.0;
  57. inc = -inc;
  58. }
  59. Serial.print("Volume: ");
  60. Serial.print(vol);
  61. Serial.print(" ");
  62. Serial.println(rampName[rampType]);
  63. // configure which type of volume transition to use
  64. if (rampType == 0) {
  65. sgtl5000_1.dacVolumeRampDisable();
  66. } else if (rampType == 1) {
  67. sgtl5000_1.dacVolumeRamp();
  68. } else {
  69. sgtl5000_1.dacVolumeRampLinear();
  70. }
  71. // set the dacVolume. The actual change make take place over time, if ramping
  72. // this is a logarithmic volume,
  73. // that is, the range 0.0 to 1.0 gets converted to -90dB to 0dB in 0.5dB steps
  74. sgtl5000_1.dacVolume(vol);
  75. // if we turned the volume off, advance to the next ramp type
  76. if (vol < 0.01) {
  77. rampType = rampType + 1;
  78. if (rampType > 2) rampType = 0;
  79. }
  80. msec = 0;
  81. }
  82. }