您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

137 行
4.2KB

  1. // Waveform Modulation Example - Create waveforms with
  2. // modulated frequency
  3. //
  4. // This example is meant to be used with 3 buttons (pin 0,
  5. // 1, 2) and 2 knobs (pins 16/A2, 17/A3), which are present
  6. // on the audio tutorial kit.
  7. // https://www.pjrc.com/store/audio_tutorial_kit.html
  8. //
  9. // Use an oscilloscope to view the 2 waveforms.
  10. //
  11. // Button0 changes the waveform shape
  12. //
  13. // Knob A2 changes the amount of frequency modulation
  14. //
  15. // Knob A3 varies the shape (only for Pulse & Variable Triangle)
  16. //
  17. // This example code is in the public domain.
  18. #include <Audio.h>
  19. #include <Wire.h>
  20. #include <SPI.h>
  21. #include <SD.h>
  22. #include <SerialFlash.h>
  23. #include <Bounce.h>
  24. AudioSynthWaveformSine sine1; //xy=131,97
  25. AudioSynthWaveformSine sine2; //xy=152,170
  26. AudioSynthWaveformModulated waveformMod1; //xy=354,69
  27. AudioOutputAnalogStereo dacs1; //xy=490,209
  28. AudioOutputI2S i2s1; //xy=532,140
  29. AudioConnection patchCord1(sine1, 0, i2s1, 1);
  30. AudioConnection patchCord2(sine1, 0, dacs1, 1);
  31. AudioConnection patchCord3(sine1, 0, waveformMod1, 0);
  32. AudioConnection patchCord4(sine2, 0, waveformMod1, 1);
  33. AudioConnection patchCord5(waveformMod1, 0, i2s1, 0);
  34. AudioConnection patchCord6(waveformMod1, 0, dacs1, 0);
  35. AudioControlSGTL5000 sgtl5000_1; //xy=286,240
  36. Bounce button0 = Bounce(0, 15);
  37. Bounce button1 = Bounce(1, 15);
  38. Bounce button2 = Bounce(2, 15);
  39. int current_waveform=0;
  40. extern const int16_t myWaveform[256]; // defined in myWaveform.ino
  41. void setup() {
  42. Serial.begin(9600);
  43. pinMode(0, INPUT_PULLUP);
  44. pinMode(1, INPUT_PULLUP);
  45. pinMode(2, INPUT_PULLUP);
  46. delay(300);
  47. Serial.println("Waveform Modulation Test");
  48. // Audio connections require memory to work. For more
  49. // detailed information, see the MemoryAndCpuUsage example
  50. AudioMemory(12);
  51. // Comment these out if not using the audio adaptor board.
  52. sgtl5000_1.enable();
  53. sgtl5000_1.volume(0.8); // caution: very loud - use oscilloscope only!
  54. // Confirgure both to use "myWaveform" for WAVEFORM_ARBITRARY
  55. waveformMod1.arbitraryWaveform(myWaveform, 172.0);
  56. // Configure for middle C note without modulation
  57. waveformMod1.frequency(261.63);
  58. waveformMod1.amplitude(1.0);
  59. sine1.frequency(20.3); // Sine waves are low frequency oscillators (LFO)
  60. sine2.frequency(1.2);
  61. current_waveform = WAVEFORM_TRIANGLE_VARIABLE;
  62. waveformMod1.begin(current_waveform);
  63. // uncomment to try modulating phase instead of frequency
  64. //waveformMod1.phaseModulation(720.0);
  65. }
  66. void loop() {
  67. // Read the buttons and knobs, scale knobs to 0-1.0
  68. button0.update();
  69. button1.update();
  70. button2.update();
  71. float knob_A2 = (float)analogRead(A2) / 1023.0;
  72. float knob_A3 = (float)analogRead(A3) / 1023.0;
  73. // use Knobsto adjust the amount of modulation
  74. sine1.amplitude(knob_A2);
  75. sine2.amplitude(knob_A3);
  76. // Button 0 or 2 changes the waveform type
  77. if (button0.fallingEdge() || button2.fallingEdge()) {
  78. switch (current_waveform) {
  79. case WAVEFORM_SINE:
  80. current_waveform = WAVEFORM_SAWTOOTH;
  81. Serial.println("Sawtooth");
  82. break;
  83. case WAVEFORM_SAWTOOTH:
  84. current_waveform = WAVEFORM_SAWTOOTH_REVERSE;
  85. Serial.println("Reverse Sawtooth");
  86. break;
  87. case WAVEFORM_SAWTOOTH_REVERSE:
  88. current_waveform = WAVEFORM_SQUARE;
  89. Serial.println("Square");
  90. break;
  91. case WAVEFORM_SQUARE:
  92. current_waveform = WAVEFORM_TRIANGLE;
  93. Serial.println("Triangle");
  94. break;
  95. case WAVEFORM_TRIANGLE:
  96. current_waveform = WAVEFORM_TRIANGLE_VARIABLE;
  97. Serial.println("Variable Triangle");
  98. break;
  99. case WAVEFORM_TRIANGLE_VARIABLE:
  100. current_waveform = WAVEFORM_ARBITRARY;
  101. Serial.println("Arbitary Waveform");
  102. break;
  103. case WAVEFORM_ARBITRARY:
  104. current_waveform = WAVEFORM_PULSE;
  105. Serial.println("Pulse");
  106. break;
  107. case WAVEFORM_PULSE:
  108. current_waveform = WAVEFORM_SAMPLE_HOLD;
  109. Serial.println("Sample & Hold");
  110. break;
  111. case WAVEFORM_SAMPLE_HOLD:
  112. current_waveform = WAVEFORM_SINE;
  113. Serial.println("Sine");
  114. break;
  115. }
  116. waveformMod1.begin(current_waveform);
  117. }
  118. }