mp3

Committer:
csggreen
Date:
Sun Dec 10 07:14:12 2017 +0000
Revision:
3:94b538ace77a
Parent:
2:a59255e80c0d
Child:
4:5b1bd3cca8dc
use this

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cha45689 0:58524d569dfd 1 #include "player.h"
cha45689 0:58524d569dfd 2 #include"SDFileSystem.h"
cha45689 0:58524d569dfd 3
cha45689 0:58524d569dfd 4 SDFileSystem sd(D11, D12, D13, D9, "sd"); // the pinout on the mbed Cool
cha45689 0:58524d569dfd 5 vs10xx vs1053(D11, D12, D13, D6, D7, D2, D8);//mosi,miso,sclk,xcs,xdcs,dreq,xreset
cha45689 0:58524d569dfd 6 Serial pc(USBTX, USBRX);
cha45689 0:58524d569dfd 7 playerStatetype playerState;
csggreen 1:17913cff05a2 8 static unsigned char fileBuf[32768];
cha45689 0:58524d569dfd 9 unsigned char *bufptr;
csggreen 2:a59255e80c0d 10 char green;
cha45689 0:58524d569dfd 11
csggreen 2:a59255e80c0d 12 char list[20][50]; //song list
csggreen 2:a59255e80c0d 13
csggreen 1:17913cff05a2 14 char data ;
csggreen 1:17913cff05a2 15 DigitalIn sw_in(USER_BUTTON);
cha45689 0:58524d569dfd 16
cha45689 0:58524d569dfd 17 void Player::begin(void)
cha45689 0:58524d569dfd 18 {
cha45689 0:58524d569dfd 19 DirHandle *dir;
cha45689 0:58524d569dfd 20 struct dirent *ptr;
cha45689 0:58524d569dfd 21 FileHandle *fp;
cha45689 0:58524d569dfd 22
cha45689 0:58524d569dfd 23 vs1053.reset();
cha45689 0:58524d569dfd 24 dir = opendir("/sd");
cha45689 0:58524d569dfd 25 printf("\r\n**********playing list**********\r\n");
cha45689 0:58524d569dfd 26 unsigned char i = 0,j=0;
cha45689 0:58524d569dfd 27 while(((ptr = dir->readdir()) != NULL)&&(i <20))
cha45689 0:58524d569dfd 28 {
csggreen 1:17913cff05a2 29 if(strstr(ptr->d_name,".mp3")||strstr(ptr->d_name,".MP3"))
cha45689 0:58524d569dfd 30 {
cha45689 0:58524d569dfd 31 fp =sd.open(ptr->d_name, O_RDONLY);
cha45689 0:58524d569dfd 32 if(fp != NULL)
cha45689 0:58524d569dfd 33 {
cha45689 0:58524d569dfd 34 char *byte = ptr->d_name;
csggreen 1:17913cff05a2 35 j=0;
csggreen 1:17913cff05a2 36 while(*byte){
csggreen 1:17913cff05a2 37 list[i][j++] = *byte++;
csggreen 2:a59255e80c0d 38 green=i;
cha45689 0:58524d569dfd 39 }
cha45689 0:58524d569dfd 40 pc.printf("%2d . %s\r\n", i,list[i++]);
csggreen 1:17913cff05a2 41 //fp->close();
cha45689 0:58524d569dfd 42 }
cha45689 0:58524d569dfd 43 }
cha45689 0:58524d569dfd 44 }
cha45689 0:58524d569dfd 45 dir->closedir();
cha45689 0:58524d569dfd 46 }
cha45689 0:58524d569dfd 47
csggreen 3:94b538ace77a 48
csggreen 3:94b538ace77a 49
csggreen 1:17913cff05a2 50 void Player::stop(void){
csggreen 1:17913cff05a2 51 playerState =PS_STOP;
csggreen 1:17913cff05a2 52 }
csggreen 2:a59255e80c0d 53
csggreen 1:17913cff05a2 54
csggreen 2:a59255e80c0d 55
csggreen 1:17913cff05a2 56
cha45689 0:58524d569dfd 57 /* This function plays back an audio file. */
cha45689 0:58524d569dfd 58 void Player::playFile(char *file) {
cha45689 0:58524d569dfd 59 int bytes; // How many bytes in buffer left
cha45689 0:58524d569dfd 60 char n;
cha45689 0:58524d569dfd 61
cha45689 0:58524d569dfd 62 playerState = PS_PLAY;
cha45689 0:58524d569dfd 63
cha45689 0:58524d569dfd 64 vs1053.setFreq(24000000); //hight speed
csggreen 1:17913cff05a2 65
csggreen 2:a59255e80c0d 66 FileHandle *fp =sd.open(file, O_RDONLY);
cha45689 0:58524d569dfd 67
cha45689 0:58524d569dfd 68 if(fp == NULL) {
cha45689 0:58524d569dfd 69 printf("Could not open %s\r\n",file);
cha45689 0:58524d569dfd 70
cha45689 0:58524d569dfd 71 }
cha45689 0:58524d569dfd 72 else
cha45689 0:58524d569dfd 73 {
cha45689 0:58524d569dfd 74 printf("Playing %s ...\r\n",file);
cha45689 0:58524d569dfd 75
cha45689 0:58524d569dfd 76 /* Main playback loop */
csggreen 1:17913cff05a2 77 while((bytes = fp->read(fileBuf,32768)) > 0) {
csggreen 1:17913cff05a2 78 while((bytes = fp->read(fileBuf,32000)) > 0) {
cha45689 0:58524d569dfd 79 {
cha45689 0:58524d569dfd 80 bufptr = fileBuf;
cha45689 0:58524d569dfd 81 // actual audio data gets sent to VS10xx.
cha45689 0:58524d569dfd 82 while(bytes > 0)
cha45689 0:58524d569dfd 83 {
cha45689 0:58524d569dfd 84 n = (bytes < 32)?bytes:32;
cha45689 0:58524d569dfd 85 vs1053.writeData(bufptr,n);
cha45689 0:58524d569dfd 86 bytes -= n;
cha45689 0:58524d569dfd 87 bufptr += n;
csggreen 1:17913cff05a2 88
cha45689 0:58524d569dfd 89 }
csggreen 1:17913cff05a2 90 uint8_t vol = 0x00;//set vlume
csggreen 1:17913cff05a2 91 vs1053.setVolume(vol); //set vlume
csggreen 2:a59255e80c0d 92
cha45689 0:58524d569dfd 93 if(playerState != PS_PLAY) //stop
cha45689 0:58524d569dfd 94 {
cha45689 0:58524d569dfd 95 fp->close();
cha45689 0:58524d569dfd 96 vs1053.softReset();
cha45689 0:58524d569dfd 97 }
csggreen 1:17913cff05a2 98
csggreen 1:17913cff05a2 99 }
csggreen 1:17913cff05a2 100 }
cha45689 0:58524d569dfd 101 }
cha45689 0:58524d569dfd 102 }
cha45689 0:58524d569dfd 103
cha45689 0:58524d569dfd 104 }
cha45689 0:58524d569dfd 105