Arduino: PCM Audio Play

From OnnoWiki
Jump to navigation Jump to search

Install TMRpcm library

cd ~/Arduino/libraries/
wget https://github.com/TMRh20/TMRpcm/archive/master.zip
unzip master.zip
mv TMRpcm-master/ TMRpcm

Edit

cd ~/Arduino/libraries/TMRpcm
vi pcmConfig.h

Pastikan,

#define DISABLE_SPEAKER2


Code

#include "TMRpcm.h"
#include <SPI.h>
#include <SD.h>

char mychar;
const int CS_PIN = 4;
TMRpcm audio;

void setup() {
  audio.speakerPin = 9;
  audio.setVolume(3);  // volume 3 supaya suara tidak pecah
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Initializing Card");
  pinMode(CS_PIN, OUTPUT);
  if(!SD.begin(CS_PIN)) {
    Serial.println("Card Failure");
    return;
  }
  Serial.println("Card Ready");
  audio.play("MORAT.WAV");
  Serial.println("Done");
}

void loop() {
}

Fungsi TMPpcm

TMRpcm audio;
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.


Pada saat lagu dimainkan aplikasi loop akan jalan terus.