// Advanced Microcontroller-based Audio Workshop
//
// http://www.pjrc.com/store/audio_tutorial_kit.html
// https://hackaday.io/project/8292-microcontroller-audio-workshop-had-supercon-2015
// 
// Part 1-3: First "Hello World" program, play a music file
//
// WAV files for this and other Tutorials are here:
// http://www.pjrc.com/teensy/td_libs_AudioDataFiles.html

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav7;     //xy=91,352.00000953674316
AudioPlaySdWav           playSdWav6;     //xy=102,307.00000953674316
AudioPlaySdWav           playSdWav5;     //xy=108,261.0000286102295
AudioPlaySdWav           playSdWav3;     //xy=111.00000381469727,148.00000381469727
AudioPlaySdWav           playSdWav4;     //xy=113.00000381469727,210.0000057220459
AudioPlaySdWav           playSdWav1;     //xy=114,43.000003814697266
AudioPlaySdWav           playSdWav2;     //xy=114,95.00000190734863
AudioMixer4              mixer1;         //xy=308.0000114440918,118.00000381469727
AudioMixer4              mixer2;         //xy=473.0000534057617,227.0000057220459
AudioOutputI2S           i2s1;           //xy=640.0000991821289,266.0000276565552
AudioConnection          patchCord1(playSdWav7, 0, mixer2, 3);
AudioConnection          patchCord2(playSdWav6, 0, mixer2, 2);
AudioConnection          patchCord3(playSdWav5, 0, mixer2, 1);
AudioConnection          patchCord4(playSdWav3, 0, mixer1, 2);
AudioConnection          patchCord5(playSdWav4, 0, mixer1, 3);
AudioConnection          patchCord6(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord7(playSdWav2, 0, mixer1, 1);
AudioConnection          patchCord8(mixer1, 0, mixer2, 0);
AudioConnection          patchCord9(mixer2, 0, i2s1, 0);
AudioConnection          patchCord10(mixer2, 0, i2s1, 1);
// GUItool: end automatically generated code

AudioControlSGTL5000     sgtl5000_1;


///* Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_SCK_PIN   14
//*/

/*// Use these with the Teensy 4.1 onboard SD card
#define SDCARD_CS_PIN    BUILTIN_SDCARD
#define SDCARD_MOSI_PIN  11  // not actually used
#define SDCARD_SCK_PIN   13  // not actually used
//*/

/*
//file names of guitar strings(gs):
const char* gs6[] ={"g600E.wav","g601F.wav","g602F#.wav","g603G.wav","g604G#.wav","g605A.wav","g606A#.wav","g607B.wav","g608C.wav","g609C#.wav","g610D.wav","g611D#.wav"};
const char* gs5[] ={"g500A.wav","g501A#.wav","g502B.wav","g503C.wav","g504C#.wav","g505D.wav","g506D#.wav","g507E.wav","g508F.wav","g509F#.wav"};
const char* gs4[] ={"g400D.wav","g401D#.wav","g402E.wav","g403F.wav","g404F#.wav","g405G.wav","g406G#.wav","g407A.wav","g408A#.wav","g409B.wav"};
const char* gs3[] ={"g300G.wav","g301G#.wav","g302A.wav","g303A#.wav","g304B.wav","g305C.wav","g306C#.wav","g307D.wav","g308D#.wav","g309E.wav"};
const char* gs2[] ={"g200B.wav","g201C.wav","g202C#.wav","g203D.wav","g204D#.wav","g205E.wav","g206F.wav","g207F#.wav","g208G.wav","g209G#.wav"};
const char* gs1[] ={"g100E.wav","g101F.wav","g102F#.wav","g103G.wav","g104G#.wav","g105A.wav","g106A#.wav","g107B.wav","g108C.wav","g109C#.wav"};
//C:C,E,G:{8,7,5,5,5,3}
//F:F,A,C:{1,0,3,2,1,1}
//*/

const char* gstr[] ={
 "g600E.wav"/*0*/,"g601F.wav"/*1*/,"g602F#.wav"/*2*/,"g603G.wav"/*3*/,"g604G#.wav"/*4*/
,"g500A.wav"/*5*/,"g501A#.wav"/*6*/,"g502B.wav"/*7*/,"g503C.wav"/*8*/,"g504C#.wav"/*9*/
,"g400D.wav"/*10*/,"g401D#.wav"/*11*/,"g402E.wav"/*12*/,"g403F.wav"/*13*/,"g404F#.wav"/*14*/
,"g300G.wav"/*15*/,"g301G#.wav"/*16*/,"g302A.wav"/*17*/,"g303A#.wav"/*18*/                
,"g200B.wav"/*19*/,"g201C.wav"/*20*/,"g202C#.wav"/*21*/,"g203D.wav"/*22*/,"g204D#.wav"/*23*/
,"g100E.wav"/*24*/,"g101F.wav"/*25*/,"g102F#.wav"/*26*/,"g103G.wav"/*27*/,"g104G#.wav"/*28*/
//아래는 1번 줄의 고음을 sampling한 것들
,"g105A.wav"/*29*/,"g106A#.wav"/*30*/,"g107B.wav"/*31*/,"g108C.wav"/*32*/,"g109C#.wav"/*33*/
,"g110D.wav"/*34*/,"g111D#.wav"/*35*/,"g112E.wav"/*36*/,"g113F.wav"/*37*/,"g114F#.wav"/*38*/,"g115G.wav"/*39*/
,"g116G#.wav"/*40*/,"g117A.wav"/*41*/,"g118A#.wav"/*42*/,"g119B.wav"/*43*/
};

//C:C,E,G:{8(C),0(E),3(G),12(E),15(G),20(C)}
//F:F,A,C:{1(F),5(A),8(C),13(F),17(A),20(C)}

//주의: 13번핀(LED)는 다른 용도(SCK)로 예약되어 있으므로 사용하면 안됨!
void setup() {
  
  Serial.begin(9600);
  
  // AudioMemoryUsageMax() 함수로 조사한 것보다 커야 한다
  // 이 예제의 경우에 7값이 찍혔다
  // https://www.pjrc.com/teensy/td_libs_AudioConnection.html
  AudioMemory(50);
  
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.7);
  
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      while(1);
    }
  }

  
  delay(1000);

/*
  mixer1.gain(0, 0.5);
  mixer1.gain(1, 0.5);
  mixer1.gain(2, 0.5);
  mixer1.gain(3, 0.5);
  mixer2.gain(0, 0.5);
  mixer2.gain(1, 0.5);
  mixer2.gain(2, 0.5);
  */
}

#define DELAY 2
void loop() {
  //if (playSdWav1.isPlaying() == false) {
  
    Serial.println(AudioMemoryUsageMax());
/*
    playChord(8,0,3,12,15,20);// playChord(8,0,15,24,32,39);
    delay(random(5,10)*40); // wait for library to parse WAV info
    playChord(8,0,3,12,15,20);// playChord(8,0,15,24,32,39);
    delay(random(5,10)*40); // wait for library to parse WAV info
    playChord(8,24,27,32,36,39);//playChord(8,3,12,20,27,36);
    delay(random(5,10)*40); // wait for library to parse WAV info
    playChord(8,24,27,32,36,39);//playChord(8,3,12,20,27,36);
    delay(random(5,10)*40); // wait for library to parse WAV info
*/
    playChord(8,0,15,24,32,39);
    delay(random(5,10)*10); // wait for library to parse WAV info
    playChord(8,0,15,24,32,39);
    delay(random(5,10)*10); // wait for library to parse WAV info
    playChord(8,3,12,20,27,36);
    delay(random(5,10)*10); // wait for library to parse WAV info
    playChord(8,3,12,20,27,36);
    delay(random(5,10)*10); // wait for library to parse WAV info
  
  // do nothing while playing...
}

void playChord(byte s1, byte s2, byte s3, byte s4, byte s5, byte s6){
  
    playSdWav1.stop();//must be stopped first
    do {
      playSdWav1.play(gstr[s1]);
      delay(DELAY);
    } while (!playSdWav1.isPlaying());
    
    playSdWav2.stop();//must be stopped first
    do{
      playSdWav2.play(gstr[s2]);
      delay(DELAY);
    } while (!playSdWav2.isPlaying());
    
    playSdWav3.stop();//must be stopped first
    do {
      playSdWav3.play(gstr[s3]);
      delay(DELAY);
    } while (!playSdWav3.isPlaying());
    
    playSdWav4.stop();//must be stopped first
    do {playSdWav4.play(gstr[s4]);
    delay(DELAY);} while (!playSdWav4.isPlaying());
    
    playSdWav5.stop();//must be stopped first
    do {playSdWav5.play(gstr[s5]);
    delay(DELAY);} while (!playSdWav5.isPlaying());
    
    playSdWav6.stop();//must be stopped first
    do {playSdWav6.play(gstr[s6]);
    delay(DELAY);} while (!playSdWav6.isPlaying());
}

Posted by 살레시오

댓글을 달아 주세요