I_Mihalache_AudioPlayer.ino 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #include <Audio.h>
  2. #include <Wire.h>
  3. #include <SPI.h>
  4. #include <SD.h>
  5. #include <SerialFlash.h>
  6. #include <Metro.h>
  7. //Sensor smoothing and hysteresys
  8. #include <SetPoint.h>
  9. #include <ResponsiveAnalogRead.h>
  10. /* ESP commands
  11. * AT+MODE=1
  12. * AT+CWLAP lists wifi AP
  13. * AT+CWJAP="IM_AP","raspberry" connect to IM ap
  14. * AT+CIFSR get IP
  15. * AT+CIPSTART="UDP","192.168.3.1",8000 connect to **
  16. *
  17. */
  18. /* SdWav1 : sample voix |
  19. * SdWav2 : sample attente |-> mix1 -> Lout & RMS1
  20. *
  21. * SdWav3 : sample ambiance -> mix2 -> Rout & RMS2
  22. */
  23. // GUItool: begin automatically generated code
  24. AudioPlaySdWav LoopPlayer; //xy=152,429 LOOP
  25. AudioPlaySdWav PresencePlayer; //xy=153,217 PRESENCE
  26. AudioPlaySdWav WaitPlayer; //xy=155,307 WAIT
  27. AudioMixer4 mixer1; //xy=572,215
  28. AudioMixer4 mixer2; //xy=579,344
  29. AudioAnalyzeRMS rms1; //xy=875,315
  30. AudioOutputI2S i2s1; //xy=876,225
  31. AudioAnalyzeRMS rms2; //xy=876,365
  32. AudioConnection patchCord1(LoopPlayer, 0, mixer2, 0);
  33. AudioConnection patchCord2(PresencePlayer, 0, mixer1, 0);
  34. AudioConnection patchCord3(WaitPlayer, 0, mixer1, 1);
  35. AudioConnection patchCord4(mixer1, 0, i2s1, 0);
  36. AudioConnection patchCord5(PresencePlayer, rms1);
  37. AudioConnection patchCord6(mixer2, 0, i2s1, 1);
  38. AudioConnection patchCord7(mixer2, rms2);
  39. AudioControlSGTL5000 sgtl5000_1; //xy=573,537
  40. // GUItool: end automatically generated code
  41. bool UDPalive ;
  42. Metro checkUDP = Metro(1000);
  43. Metro checkESPserial = Metro(50);
  44. Metro sendRMS = Metro(50);
  45. #define LOOP_WAV "LOOP.WAV"
  46. #define PRESENCE_WAV "PRESENCE.WAV"
  47. #define WAIT_WAV "WAIT.WAV"
  48. int WAIT_TIME=10000 ;
  49. const int LDR_PIN = A7;
  50. ResponsiveAnalogRead ldr(LDR_PIN, false);
  51. int threshold = 700;
  52. int hysteresis = 100;
  53. int sensorPin = A7;
  54. SetPoint setPoint;
  55. bool presence ;
  56. uint32_t LastPresence ;
  57. // Use these with the Teensy Audio Shield
  58. #define SDCARD_CS_PIN 10
  59. #define SDCARD_MOSI_PIN 7
  60. #define SDCARD_SCK_PIN 14
  61. void ESPSerial(){
  62. // Send bytes from ESP8266 -> Teensy to Computer
  63. bool printLine ;
  64. while ( Serial1.available() ) {
  65. //String buf = Serial1.read();
  66. //Serial.write(F(buf));
  67. Serial.write( Serial1.read() );
  68. // if(buf.readStringUntil("\n")=="OK") {Serial.println("trouvé");}
  69. printLine = 1 ;
  70. }
  71. // if (printLine) {Serial.println("");}
  72. printLine = 0;
  73. // Send bytes from Computer -> Teensy back to ESP8266
  74. while ( Serial.available() ) {
  75. Serial1.write( Serial.read() );
  76. }
  77. }
  78. /////////////////////////////// SETUP /////////////////////////
  79. void setup() {
  80. Serial.begin(115200);
  81. Serial1.begin(115200);
  82. //LDR digital inputs
  83. pinMode(16, INPUT);
  84. pinMode(20,INPUT);
  85. AudioMemory(16);
  86. sgtl5000_1.enable();
  87. sgtl5000_1.lineOutLevel(13); // set output to 3.16 Vpp
  88. sgtl5000_1.volume(1); //set volume to maximum
  89. SPI.setMOSI(SDCARD_MOSI_PIN);
  90. SPI.setSCK(SDCARD_SCK_PIN);
  91. if (!(SD.begin(SDCARD_CS_PIN))) {
  92. // stop here, but print a message repetitively
  93. while (1) {
  94. Serial.println("Unable to access the SD card");
  95. delay(500);
  96. }
  97. }
  98. Serial.println("Playing LOOP and WAIT file");
  99. LoopPlayer.play(LOOP_WAV);
  100. WaitPlayer.play(WAIT_WAV);
  101. // A brief delay for the library read WAV info
  102. delay(10);
  103. setPoint.begin(threshold, hysteresis);
  104. setPoint.attach(RISING_EDGE, Presence);
  105. setPoint.attach(FALLING_EDGE, Absence);
  106. //ESP8266
  107. Serial1.println("AT+GMR");
  108. //while (!Serial1.available()) {};
  109. delay(200);
  110. ESPSerial();
  111. Serial.println("");
  112. Serial.println("");
  113. while(millis()<10000){}
  114. Serial1.println("AT+CIFSR");
  115. ESPSerial();
  116. delay(200);
  117. Serial.println ("");
  118. Serial1.println("AT+CIPSTART=\"UDP\",\"192.168.3.1\",8002");
  119. ESPSerial();
  120. }
  121. ///////////////////////////////////////////////////////////////////
  122. void playFile(const char *filename)
  123. {
  124. Serial.print("Playing file: ");
  125. Serial.println(filename);
  126. PresencePlayer.play(filename);
  127. // A brief delay for the library read WAV info
  128. delay(5);
  129. }
  130. void printLDR(){
  131. Serial.print(analogRead(A3));
  132. Serial.print(" ");
  133. Serial.print(digitalRead(16));
  134. Serial.print(" ");
  135. Serial.print(analogRead(A7));
  136. Serial.print(" ");
  137. Serial.print(digitalRead(20));
  138. Serial.println();
  139. }
  140. bool checkPresence(int Pin1, int Pin2) {
  141. bool isPresent = analogRead(A7)>1010;
  142. //digitalRead(Pin2); //|| digitalRead(Pin2);
  143. Serial.println(isPresent);
  144. return isPresent ;
  145. }
  146. bool checkUDPconnected() {
  147. Serial1.println("AT+CIPSTATUS");
  148. delay(50);
  149. while ( Serial1.available() ) {
  150. String buf = Serial1.readStringUntil('\n'); //StringUntil('\n') ;
  151. if(buf.startsWith("STATUS:2")){
  152. Serial.println("connected");
  153. return true ;
  154. }
  155. else return false;
  156. }
  157. }
  158. void sendUDP() {
  159. if(rms1.available() ) { //&& UDPalive
  160. Serial1.println("AT+CIPSEND=3");
  161. delay(20);
  162. Serial1.write(byte(rms1.read()*255));
  163. Serial1.println("");
  164. //Serial.println("udp");
  165. }
  166. }
  167. /////////////////////////////// LOOP ////////////////////////////////////
  168. void loop() {
  169. // LOOP FILE
  170. ldr.update();
  171. int value = ldr.getValue();
  172. setPoint.update(value);
  173. // LOOP is alwaiys playing
  174. if (!LoopPlayer.isPlaying()) {
  175. Serial.println("Playing LOOP file");
  176. LoopPlayer.play(LOOP_WAV);
  177. // A brief delay for the library read WAV info
  178. delay(10);
  179. }
  180. if ( (millis() - LastPresence > WAIT_TIME) && !WaitPlayer.isPlaying() && !PresencePlayer.isPlaying()) {
  181. WaitPlayer.play(WAIT_WAV);
  182. mixer1.gain (1, 1.0);
  183. Serial.println("Playing wait file");
  184. delay(10);
  185. }
  186. if (presence) {
  187. LastPresence = millis() ;
  188. }
  189. //WAIT fades up if PresencePlayer stops (
  190. if (millis()-LastPresence > WAIT_TIME && PresencePlayer.isPlaying() && presence == false) {
  191. Serial.println("Stopping PRESENCE file");
  192. PresencePlayer.stop();
  193. for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
  194. mixer1.gain(1, fadeRamp/100) ;
  195. }
  196. }
  197. // // PRESENCE FILE
  198. // presence = checkPresence(16, 20);
  199. // //printLDR();
  200. // //Serial.println(presence);
  201. //
  202. // //PRESENCE DETECTED
  203. // if (presence ) {
  204. //
  205. // else if (!presence ) {
  206. // if (checkUDP.check() ==1) {
  207. // UDPalive = checkUDPconnected();
  208. // }
  209. // if (checkESPserial.check() == 1) {
  210. //
  211. // }
  212. // ESPSerial();
  213. //
  214. // if (sendRMS.check() == 1) {
  215. // sendUDP();
  216. // }
  217. //
  218. }
  219. ////////////////////// LDR event handlers /////////////////////////////
  220. void Presence() {
  221. Serial.println("Presence detected");
  222. presence = true ;
  223. if (!PresencePlayer.isPlaying()) {
  224. //stop WAIT_WAV if playing
  225. if (WaitPlayer.isPlaying()) {
  226. //fade out WaitPlayer
  227. Serial.println("Fade out Wait file");
  228. for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
  229. mixer1.gain(1, 1.0 - fadeRamp/100) ;
  230. }
  231. // WaitPlayer.stop() ;
  232. // mixer1.gain (1, 1.0); // set volume for next play
  233. // delay(2000) ;
  234. }
  235. PresencePlayer.play(PRESENCE_WAV) ;
  236. delay(15);
  237. Serial.println("Playing presence file");
  238. }
  239. // if presence and playing, don't do anything
  240. for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
  241. mixer1.gain(0, fadeRamp/100) ;
  242. }
  243. }
  244. void Absence() {
  245. LastPresence = millis() ;
  246. presence = false ;
  247. Serial.println("No presence detected");
  248. if (PresencePlayer.isPlaying()) {
  249. //what if presence gone while playing ?
  250. Serial.println("Fading out Presence file");
  251. for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
  252. mixer1.gain(0, 1.0 - fadeRamp/100) ;
  253. delay(10);
  254. }
  255. // while(rms1.read() > 0) { }
  256. }
  257. }