Stuntmunky
Active member
More than likely the problem is me
I have been trying to add an mp3 player to a small busking amp I have made. I managed to get an external mp3 module working with it but was getting unwanted noise . I put an audio loop isolation transformer in but it didn't sound great and so I started to look for alternatives.
I have been using the Complete_MP3_AAC_MP4_M4A example in the Audiocodecs library to try and implement a player into my existing code. I have two audio shields and a teensy 3.2. The code compiles and the existing lines in/out and controls all function as usual but the mp3 is not working.
When I open the serial monitor it looks like it is just skipping through all the tracks without giving them time to play.
I have tried putting in a delay after playMp31.play(filename); but it didn't help
I also changed the mp3 files in audacity to 16 bit but no joy either
I will keep plugging away at it but if anyone can see where I may have gone wrong I would be very grateful, I'm not an absolute beginner but not far off
Here's the code:
I have been trying to add an mp3 player to a small busking amp I have made. I managed to get an external mp3 module working with it but was getting unwanted noise . I put an audio loop isolation transformer in but it didn't sound great and so I started to look for alternatives.
I have been using the Complete_MP3_AAC_MP4_M4A example in the Audiocodecs library to try and implement a player into my existing code. I have two audio shields and a teensy 3.2. The code compiles and the existing lines in/out and controls all function as usual but the mp3 is not working.
When I open the serial monitor it looks like it is just skipping through all the tracks without giving them time to play.
I have tried putting in a delay after playMp31.play(filename); but it didn't help
I also changed the mp3 files in audacity to 16 bit but no joy either
I will keep plugging away at it but if anyone can see where I may have gone wrong I would be very grateful, I'm not an absolute beginner but not far off
Here's the code:
Code:
#include <IRremote.h>
#include <Encoder.h>
#include <Bounce.h>
#include <EEPROM.h>
#include <play_sd_mp3.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioInputI2SQuad i2s_quad1; //xy=72,136
AudioPlaySdWav playSdWav1; //xy=83,349
AudioMixer4 mixer5; //xy=202,218
AudioMixer4 mixer1; //xy=220,76
AudioMixer4 mixer2; //xy=344,140
AudioMixer4 mixer4; //xy=442,57
AudioMixer4 mixer3; //xy=469,196
AudioFilterStateVariable filter1; //xy=583,32
AudioFilterStateVariable filter2; //xy=722,35
AudioOutputI2SQuad i2s_quad2; //xy=789,187
AudioConnection patchCord1(i2s_quad1, 0, mixer1, 0);
AudioConnection patchCord2(i2s_quad1, 1, mixer1, 1);
AudioConnection patchCord3(i2s_quad1, 2, mixer2, 1);
AudioConnection patchCord4(playSdWav1, 0, mixer3, 1);
AudioConnection patchCord5(playSdWav1, 1, mixer3, 2);
AudioConnection patchCord6(mixer5, 0, mixer2, 2);
AudioConnection patchCord7(mixer1, 0, mixer2, 0);
AudioConnection patchCord8(mixer2, 0, mixer3, 0);
AudioConnection patchCord9(mixer4, 0, filter1, 0);
AudioConnection patchCord10(mixer4, 0, i2s_quad2, 1);
AudioConnection patchCord11(mixer4, 0, i2s_quad2, 0);
AudioConnection patchCord12(mixer3, 0, mixer4, 0);
AudioConnection patchCord13(filter1, 0, filter2, 0);
AudioConnection patchCord14(filter2, 2, i2s_quad2, 3);
AudioControlSGTL5000 sgtl5000_1; //xy=67,20
AudioControlSGTL5000 sgtl5000_2; //xy=195,20
// GUItool: end automatically generated code
AudioPlaySdMp3 playMp31; //needs manual patching into sys
AudioConnection patch1(playMp31,0,mixer5,0);
AudioConnection patch2(playMp31,1,mixer5,1);
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14
#define NUM_BUTTONS 7 // number of remote buttons
#define RECV_PIN 2
int track;
int tracknum;
int trackext[255]; // 0= nothing, 1= mp3, 2= aac, 3= wav.
String tracklist[255];
File root;
char playthis[15];
boolean trackchange;
boolean paused;
const int ledPin1 = 3;
const int ledPin2 = 4;
const int ledPin3 = 5;
uint16_t lastCode = 0;
const uint16_t BUTTON_volup = 0xFFA857; //ir remote codes
const uint16_t BUTTON_voldown = 0xFFE01F;
const uint16_t BUTTON_back = 0xFF22DD;
const uint16_t BUTTON_pp = 0xFFC23D;
const uint16_t BUTTON_fwd = 0xFF02FD;
const uint16_t BUTTON_1 = 0xFF30CF;
const uint16_t BUTTON_2 = 0xFF18E7;
Bounce button1 = Bounce(1, 15); // 15 = 15 ms debounce time
IRrecv irrecv(RECV_PIN);
decode_results results;
Encoder myEnc(0, 21);
int tonevolsel = 1;
float bass = 0.0;
float treble = 0.0;
float vol = 0.0;
float oldPosition1 = 0.0;
//long newPosition;
void setup() {
AudioMemory(60);
Serial.begin(115200);
irrecv.enableIRIn(); // Start the receiver
pinMode (RECV_PIN, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(1, INPUT_PULLUP);
track = EEPROM.read(0);
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");
delay(500);
}
}
sgtl5000_1.setAddress(LOW);
sgtl5000_1.enable();
sgtl5000_1.lineOutLevel(13);
sgtl5000_1.lineInLevel(9);
sgtl5000_1.muteHeadphone();
sgtl5000_1.audioPostProcessorEnable();
// sgtl5000_1.eqBands(bass,treble);
sgtl5000_2.setAddress(HIGH);
sgtl5000_2.enable();
sgtl5000_2.lineOutLevel(13);
sgtl5000_2.lineInLevel(9);
sgtl5000_2.muteHeadphone();
sgtl5000_2.audioPostProcessorEnable();
// sgtl5000_2.eqBands(bass,treble);
filter1.frequency(9000);
filter2.frequency(70);
// Stereo linein/bluetooth to mono
mixer1.gain(0, 0.5);
mixer1.gain(1, 0.5);
// Control for line/mp3/mic
mixer2.gain(0, 0.0);
mixer2.gain(1, 0.0);
mixer2.gain(2, 0.0);
// Turns sd to mono and mixes with product of mixer2
mixer3.gain(0, 0.5);
mixer3.gain(1, 0.25);
mixer3.gain(2, 0.25);
// Master volume
mixer4.gain(0,0);
// Turns mp3 stereo into mono
mixer5.gain(0,0.45);
mixer5.gain(1,0.45);
root = SD.open("/");
while(true) {
File files = root.openNextFile();
if (!files) {
//If no more files, break out.
break;
}
String curfile = files.name(); //put file in string
//look for MP3 or AAC files
int m = curfile.lastIndexOf(".MP3");
int a = curfile.lastIndexOf(".AAC");
int a1 = curfile.lastIndexOf(".MP4");
int a2 = curfile.lastIndexOf(".M4A");
//int w = curfile.lastIndexOf(".WAV");
// if returned results is more then 0 add them to the list.
if(m > 0 || a > 0 || a1 > 0 || a2 > 0 ){
tracklist[tracknum] = files.name();
if(m > 0) trackext[tracknum] = 1;
if(a > 0) trackext[tracknum] = 2;
if(a1 > 0) trackext[tracknum] = 2;
if(a2 > 0) trackext[tracknum] = 2;
// if(w > 0) trackext[tracknum] = 3;
Serial.print(m);
tracknum++;
}
// close
files.close();
}
if(track > tracknum){
//if it is too big, reset to 0
EEPROM.write(0,0);
track = 0;
}
tracklist[track].toCharArray(playthis, sizeof(tracklist[track]));
}
void playFileMP3(const char *filename)
{
Serial.print("Playing file: ");
Serial.print(track);
Serial.print(" - ");
Serial.println(filename);
trackchange = true; //auto track change is allowed.
// Start playing the file. This sketch continues to
// run while the file plays.
EEPROM.write(0,track); //meanwhile write the track position to eeprom address 0
playMp31.play(filename);
delay(10);
// Simply wait for the file to finish playing.
while (playMp31.isPlaying()) {
// update controls!
controls();
}
}
void controls() {
// ir buttons and decoder
{
if (irrecv.decode(&results)) {
uint16_t resultCode = (results.value & 0xFFFF);
if (resultCode == 0xFFFF)
resultCode = lastCode;
else
lastCode = resultCode;
switch (resultCode)
{
case BUTTON_volup:
vol = vol + 0.1;
if (vol>1){
vol = 1;}
if (vol<0){
vol = 0;}
Serial.println(vol);
mixer4.gain(0,vol);
break;
case BUTTON_voldown:
vol = vol - 0.1;
if (vol>1){
vol = 1;}
if (vol<0){
vol = 0;}
Serial.println(vol);
mixer4.gain(0,vol);
break;
case BUTTON_back:
prevtrack();
break;
case BUTTON_pp:
pausetrack();
// Serial.println("play/pause");
break;
case BUTTON_fwd:
nexttrack();
break;
case BUTTON_1:
playSdWav1.stop();
// Serial.println("wav1");
playSdWav1.play("1.WAV");
delay(10); // wait for library to parse WAV info
break;
case BUTTON_2:
playSdWav1.stop();
// Serial.println("wav2");
playSdWav1.play("2.WAV");
delay(10); // wait for library to parse WAV info
break;
default:
Serial.print("Unrecognized code received: 0x");
Serial.println(results.value, HEX);
break;
}
irrecv.resume();
}
}
}
void loop() {
Serial.println();
Serial.println();
Serial.println(tracknum);
Serial.println(track);
Serial.println(trackext[track]);
Serial.println(tracklist[track]);
if(trackext[track] == 1){
Serial.println("MP3" );
playFileMP3(playthis);
delay(10);
}
if(trackchange == true){ //when your track is finished, go to the next one. when using buttons, it will not skip twice.
nexttrack();
}
delay(100);
button1.update();
if (button1.fallingEdge()) {
tonevolsel ++ ;
if (tonevolsel == 4) tonevolsel = 1;
}
if (tonevolsel == 1){
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
float newPosition = myEnc.read()/4;
if (newPosition != oldPosition1) {
oldPosition1 = newPosition;
//Serial.println(newPosition);
vol = vol + newPosition / 20;
if (vol>1.0){
vol = 1.0;}
if (vol<0.0){
vol = 0.0;}
// Serial.println("vol");
// Serial.println(vol);
mixer4.gain(0,vol);
myEnc.write(0);
}
}
if (tonevolsel == 2){
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
float newPosition = myEnc.read()/4;
if (newPosition != oldPosition1) {
oldPosition1 = newPosition;
// Serial.println(newPosition);
bass = bass + newPosition / 20;
if (bass>1){
bass = 1;}
if (bass<-1){
bass = -1;}
// Serial.println("bass");
// Serial.println(bass);
sgtl5000_1.eqBands(bass,treble);
sgtl5000_2.eqBands(bass,treble);
myEnc.write(0);}
}
if (tonevolsel == 3){
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
float newPosition = myEnc.read()/4;
if (newPosition != oldPosition1) {
oldPosition1 = newPosition;
// Serial.println(newPosition);
treble = treble + newPosition / 20;
if (treble>1){
treble = 1;}
if (treble<-1){
treble = -1;}
// Serial.println("treble");
// Serial.println(treble);
sgtl5000_1.eqBands(bass,treble);
sgtl5000_2.eqBands(bass,treble);
myEnc.write(0);}
}
int mic = analogRead(A2);
float micvol = (float)mic / 1023.0;
mixer2.gain(0,micvol);
//Serial.print("mic volume = ");
// Serial.println(micvol);
int line = analogRead(A3);
float linevol = (float)line / 1023.0;
mixer2.gain(1,linevol);
// Serial.print("line volume = ");
//Serial.println(linevol);
int oompah3 = analogRead(A6);
float mp3vol = (float)oompah3 / 1023.0;
mixer2.gain(2,mp3vol);
// Serial.print("mp3 volume = ");
//Serial.println(mp3vol);
controls();
}
void nexttrack(){
Serial.println("Next track!");
trackchange=false; // we are doing a track change here, so the auto trackchange will not skip another one.
playMp31.stop();
track++;
if(track >= tracknum){ // keeps in tracklist.
track = 0;
}
tracklist[track].toCharArray(playthis, sizeof(tracklist[track])); //since we have to convert String to Char will do this
}
void prevtrack(){
Serial.println("Previous track! ");
trackchange=false; // we are doing a track change here, so the auto trackchange will not skip another one.
playMp31.stop();
track--;
if(track <0){ // keeps in tracklist.
track = tracknum-1;
}
tracklist[track].toCharArray(playthis, sizeof(tracklist[track])); //since we have to convert String to Char will do this
}
void pausetrack(){
if(paused){
Serial.println("Playing!");
}
else{
Serial.println("Paused!");
}
paused = playMp31.pause(!paused);
}