Wednesday, March 25, 2015

miniSD + sonido / sound

En la últimas semanas descubrí una librería de arduino muy interesante, TMRpcm. Estaba buscando una forma de reproducir y manipular sonidos de audio sin la necesidad de usar un MP3 o un computador. Me gustaría poder manipular diferentes señales de audio de archivos WAV. 

Esta librería permite que arduino pueda reproducir archivos WAV en diferentes calidades desde una miniSD usando un modulo y un speaker de bajo costo. 

-----

From the last few weeks I've been discovered an interesting library from arduino, it is called TMRpcm. I was looking for a form to reproduce and manipulate sound files without a MP3 or computer, I would like to manipulate different mono audio signals from files WAV. 

In fact, this library contributes to an arduino the capacity to reproduce WAV sound files from a miniSD, using a cheaper and tiny SDmodule and a speaker. 















Puedo operar sencillos comandos, aunque suficientes para mi propósito experimental.

 -----

I can operate very simple commands, although enough for my experimental plan:

Simple mode
audio.play("filename");    plays a file
audio.play("filename",30); plays a file starting at 30 seconds into the track
audio.speakerPin = 11;     set to 5,6,11 or 46 for Mega, 9 for Uno, Nano, etc.
audio.disable();           disables the timer on output pin and stops the music
audio.stopPlayback();      stops the music, but leaves the timer running
audio.isPlaying();         returns 1 if music playing, 0 if not
audio.pause();             pauses/unpauses playback
audio.quality(1);          Set 1 for 2x oversampling
audio.volume(0);           1(up) or 0(down) to control volume
audio.setVolume(0);        0 to 7. Set volume level 
audio.loop(1); 0 or 1. Can be changed during playback for full control of looping.   

Primer código / first code

#include <SdFat.h>
SdFat sd;

#define SD_ChipSelectPin 4  // Pin CS 
#include <TMRpcm.h>         
#include <SPI.h>

TMRpcm audio; 

void setup(){
  audio.speakerPin = 9;  // speaker
  pinMode(10,OUTPUT);   // speaker 
  Serial.begin(9600);
  if (!sd.begin(4, SPI_HALF_SPEED)) {Serial.println("Something wrong"); return; 
  }else{ Serial.println("SD OK"); }
}

//con las teclas p,o,i cambia el archivo WAV 

void loop(){  
  if(Serial.available()){   
    switch(Serial.read()){
      case 'p': audio.play("track1.wav"); break;
      case 'o': audio.play("track2.wav"); break;
      case 'i': audio.play("track3.wav"); break;
      case 'l': audio.pause(); break;
      default: break;
    }
  }
}

---------

//  aleatorialmente cambia una posición de el tiempo de la reproducción en un mismo archivo ---- randomness second position at the same track.

void loop(){  
 audio.play("track3.wav",random(0,14)); delay(1000);audio.pause();
}




mapa // map pin

cs           -     4 (SD_ChipSelectPin)
clock      -   13
mosi       -   11
miso       -   12
vcc         -   3.3
gnd        -   ground
speaker -   10


No comments:

Post a Comment