Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

134 lines
3.8KB

  1. // Simple WAV file player example
  2. //
  3. // Three types of output may be used, by configuring the code below.
  4. //
  5. // 1: Digital I2S - Normally used with the audio shield:
  6. // http://www.pjrc.com/store/teensy3_audio.html
  7. //
  8. // 2: Digital S/PDIF - Connect pin 22 to a S/PDIF transmitter
  9. // https://www.oshpark.com/shared_projects/KcDBKHta
  10. //
  11. // 3: Analog DAC - Connect the DAC pin to an amplified speaker
  12. // http://www.pjrc.com/teensy/gui/?info=AudioOutputAnalog
  13. //
  14. // To configure the output type, first uncomment one of the three
  15. // output objects. If not using the audio shield, comment out
  16. // the sgtl5000_1 lines in setup(), so it does not wait forever
  17. // trying to configure the SGTL5000 codec chip.
  18. //
  19. // The SD card may connect to different pins, depending on the
  20. // hardware you are using. Uncomment or configure the SD card
  21. // pins to match your hardware.
  22. //
  23. // Data files to put on your SD card can be downloaded here:
  24. // http://www.pjrc.com/teensy/td_libs_AudioDataFiles.html
  25. //
  26. // This example code is in the public domain.
  27. #include <Audio.h>
  28. #include <Wire.h>
  29. #include <SPI.h>
  30. #include <SD.h>
  31. #include <SerialFlash.h>
  32. AudioPlaySdWav playWav1;
  33. // Use one of these 3 output types: Digital I2S, Digital S/PDIF, or Analog DAC
  34. AudioOutputI2S audioOutput;
  35. //AudioOutputSPDIF audioOutput;
  36. //AudioOutputAnalog audioOutput;
  37. AudioConnection patchCord1(playWav1, 0, audioOutput, 0);
  38. AudioConnection patchCord2(playWav1, 1, audioOutput, 1);
  39. AudioControlSGTL5000 sgtl5000_1;
  40. // Use these with the Teensy Audio Shield
  41. #define SDCARD_CS_PIN 10
  42. #define SDCARD_MOSI_PIN 7
  43. #define SDCARD_SCK_PIN 14
  44. // Use these with the Teensy 3.5 & 3.6 SD card
  45. //#define SDCARD_CS_PIN BUILTIN_SDCARD
  46. //#define SDCARD_MOSI_PIN 11 // not actually used
  47. //#define SDCARD_SCK_PIN 13 // not actually used
  48. // Use these for the SD+Wiz820 or other adaptors
  49. //#define SDCARD_CS_PIN 4
  50. //#define SDCARD_MOSI_PIN 11
  51. //#define SDCARD_SCK_PIN 13
  52. void setup() {
  53. Serial.begin(9600);
  54. // Audio connections require memory to work. For more
  55. // detailed information, see the MemoryAndCpuUsage example
  56. AudioMemory(8);
  57. // Comment these out if not using the audio adaptor board.
  58. // This may wait forever if the SDA & SCL pins lack
  59. // pullup resistors
  60. sgtl5000_1.enable();
  61. sgtl5000_1.volume(0.5);
  62. SPI.setMOSI(SDCARD_MOSI_PIN);
  63. SPI.setSCK(SDCARD_SCK_PIN);
  64. if (!(SD.begin(SDCARD_CS_PIN))) {
  65. // stop here, but print a message repetitively
  66. while (1) {
  67. Serial.println("Unable to access the SD card");
  68. delay(500);
  69. }
  70. }
  71. }
  72. void playFile(const char *filename)
  73. {
  74. Serial.print("Playing file: ");
  75. Serial.println(filename);
  76. // Start playing the file. This sketch continues to
  77. // run while the file plays.
  78. playWav1.play(filename);
  79. // A brief delay for the library read WAV info
  80. delay(5);
  81. // Simply wait for the file to finish playing.
  82. while (!playWav1.isStopped()) {
  83. if(playWav1.isPlaying()) {
  84. // play for 10 seconds
  85. delay(10000);
  86. }
  87. else if(playWav1.isPaused()) {
  88. // pause for 2 seconds
  89. delay(2000);
  90. }
  91. // toggle play/pause state
  92. playWav1.togglePlayPause();
  93. Serial.println(filename);
  94. Serial.print("positionMillis() = ");
  95. Serial.println(playWav1.positionMillis());
  96. Serial.print("lengthMillis() = ");
  97. Serial.println(playWav1.lengthMillis());
  98. Serial.print("isPlaying() = ");
  99. Serial.println(playWav1.isPlaying());
  100. Serial.print("isPaused() = ");
  101. Serial.println(playWav1.isPaused());
  102. Serial.print("isStopped() = ");
  103. Serial.println(playWav1.isStopped());
  104. Serial.println();
  105. }
  106. }
  107. void loop() {
  108. playFile("SDTEST1.WAV"); // filenames are always uppercase 8.3 format
  109. delay(500);
  110. playFile("SDTEST2.WAV");
  111. delay(500);
  112. playFile("SDTEST3.WAV");
  113. delay(500);
  114. playFile("SDTEST4.WAV");
  115. delay(1500);
  116. }