Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: MP3333 B18_MP3_PLAYER B18_MP3_PLAYER B18_MP3_PLAYER
player.cpp
- Committer:
- PKnevermind
- Date:
- 2015-12-07
- Revision:
- 0:928e5b21896c
- Child:
- 1:4afda9d22e34
File content as of revision 0:928e5b21896c:
#include "player.h"
#include "SDFileSystem.h"
SDFileSystem sd(D11, D12, D13, D10, "sd"); // the pinout on the mbed Cool
vs10xx vs1053(D11, D12, D13, D4, D5, D3, D6);//mosi,miso,sclk,xcs,xdcs,dreq,xreset
playerStatetype playerState;
ctrlStatetype ctrlState;
static unsigned char fileBuf[32768];
unsigned char *bufptr;
char list[20][50]; //song list
char index = 0; //song play index
char index_MAX; //how many song in all
unsigned char vlume = 0x40; //vlume
unsigned char vlumeflag = 0; //set vlume flag
void Player::begin(void)
{
DirHandle *dir;
struct dirent *ptr;
FileHandle *fp;
vs1053.reset();
dir = opendir("/sd");
printf("\r\n**********playing list**********\r\n");
unsigned char i = 0,j=0;
while(((ptr = dir->readdir()) != NULL)&&(i <20)) {
if(strstr(ptr->d_name,".mp3")||strstr(ptr->d_name,".MP3")) {
fp =sd.open(ptr->d_name, O_RDONLY);
if(fp != NULL) {
char *byte = ptr->d_name;
j=0;
while(*byte) {
list[i][j++] = *byte++;
}
printf("%2d . %s\r\n", i,list[i++]);
fp->close();
}
}
}
index_MAX = i-1;
dir->closedir();
printf("\r\n");
}
/* This function plays back an audio file. */
void Player::playFile(char *file)
{
int bytes; // How many bytes in buffer left
int n;
playerState = PS_PLAY;
vs1053.setFreq(24000000); //hight speed
FileHandle *fp =sd.open(file, O_RDONLY);
if(fp == NULL) {
printf("Could not open %s\r\n",file);
} else {
printf("Playing %s ...\r\n",file);
/* Main playback loop */
while((bytes = fp->read(fileBuf,32768)) > 0) {
bufptr = fileBuf;
// actual audio data gets sent to VS10xx.
while(bytes > 0) {
n = (bytes < 1)?bytes:1;
vs1053.writeData(bufptr,n);
bytes -= n;
bufptr += n;
}
}
/*while(playerState == PS_PAUSE); //Pause
if(vlumeflag) { //set vlume
vs1053.setFreq(12000000); //low speed
vs1053.writeRegister(SPI_VOL, vlume*0x101); //Set volume level
vs1053.setFreq(24000000); //higth speed
vlumeflag = 0; //clear flag;
}
if(playerState != PS_PLAY) { //stop
fp->close();
vs1053.softReset();
return;
}*/
fp->close();
vs1053.softReset();
printf("[done!]\r\n");
}
if(index != index_MAX)index++;
else index = 0;
}
void Set32(unsigned char *d, unsigned int n)
{
int i;
for (i=0; i<4; i++) {
*d++ = (unsigned char)n;
n >>= 8;
}
}