test1
Fork of VS1053 by
player.cpp@2:a59255e80c0d, 2017-12-09 (annotated)
- Committer:
- csggreen
- Date:
- Sat Dec 09 15:07:27 2017 +0000
- Revision:
- 2:a59255e80c0d
- Parent:
- 1:17913cff05a2
- Child:
- 3:683fad085072
green_mp3;
Who changed what in which revision?
User | Revision | Line number | New 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; |
cha45689 | 0:58524d569dfd | 8 | ctrlStatetype ctrlState; |
csggreen | 1:17913cff05a2 | 9 | static unsigned char fileBuf[32768]; |
cha45689 | 0:58524d569dfd | 10 | unsigned char *bufptr; |
csggreen | 2:a59255e80c0d | 11 | char green; |
cha45689 | 0:58524d569dfd | 12 | |
csggreen | 2:a59255e80c0d | 13 | char list[20][50]; //song list |
csggreen | 2:a59255e80c0d | 14 | |
csggreen | 1:17913cff05a2 | 15 | char data ; |
csggreen | 1:17913cff05a2 | 16 | DigitalIn sw_in(USER_BUTTON); |
cha45689 | 0:58524d569dfd | 17 | |
cha45689 | 0:58524d569dfd | 18 | void Player::begin(void) |
cha45689 | 0:58524d569dfd | 19 | { |
cha45689 | 0:58524d569dfd | 20 | DirHandle *dir; |
cha45689 | 0:58524d569dfd | 21 | struct dirent *ptr; |
cha45689 | 0:58524d569dfd | 22 | FileHandle *fp; |
cha45689 | 0:58524d569dfd | 23 | |
cha45689 | 0:58524d569dfd | 24 | vs1053.reset(); |
cha45689 | 0:58524d569dfd | 25 | dir = opendir("/sd"); |
cha45689 | 0:58524d569dfd | 26 | printf("\r\n**********playing list**********\r\n"); |
cha45689 | 0:58524d569dfd | 27 | unsigned char i = 0,j=0; |
cha45689 | 0:58524d569dfd | 28 | while(((ptr = dir->readdir()) != NULL)&&(i <20)) |
cha45689 | 0:58524d569dfd | 29 | { |
csggreen | 1:17913cff05a2 | 30 | if(strstr(ptr->d_name,".mp3")||strstr(ptr->d_name,".MP3")) |
cha45689 | 0:58524d569dfd | 31 | { |
cha45689 | 0:58524d569dfd | 32 | fp =sd.open(ptr->d_name, O_RDONLY); |
cha45689 | 0:58524d569dfd | 33 | if(fp != NULL) |
cha45689 | 0:58524d569dfd | 34 | { |
cha45689 | 0:58524d569dfd | 35 | char *byte = ptr->d_name; |
csggreen | 1:17913cff05a2 | 36 | j=0; |
csggreen | 1:17913cff05a2 | 37 | while(*byte){ |
csggreen | 1:17913cff05a2 | 38 | list[i][j++] = *byte++; |
csggreen | 2:a59255e80c0d | 39 | green=i; |
cha45689 | 0:58524d569dfd | 40 | } |
cha45689 | 0:58524d569dfd | 41 | pc.printf("%2d . %s\r\n", i,list[i++]); |
csggreen | 1:17913cff05a2 | 42 | //fp->close(); |
cha45689 | 0:58524d569dfd | 43 | } |
cha45689 | 0:58524d569dfd | 44 | } |
cha45689 | 0:58524d569dfd | 45 | } |
cha45689 | 0:58524d569dfd | 46 | dir->closedir(); |
cha45689 | 0:58524d569dfd | 47 | } |
cha45689 | 0:58524d569dfd | 48 | |
csggreen | 1:17913cff05a2 | 49 | void Player::stop(void){ |
csggreen | 1:17913cff05a2 | 50 | playerState =PS_STOP; |
csggreen | 1:17913cff05a2 | 51 | } |
csggreen | 2:a59255e80c0d | 52 | |
csggreen | 1:17913cff05a2 | 53 | |
csggreen | 2:a59255e80c0d | 54 | |
csggreen | 1:17913cff05a2 | 55 | |
cha45689 | 0:58524d569dfd | 56 | /* This function plays back an audio file. */ |
cha45689 | 0:58524d569dfd | 57 | void Player::playFile(char *file) { |
cha45689 | 0:58524d569dfd | 58 | int bytes; // How many bytes in buffer left |
cha45689 | 0:58524d569dfd | 59 | char n; |
cha45689 | 0:58524d569dfd | 60 | |
cha45689 | 0:58524d569dfd | 61 | playerState = PS_PLAY; |
cha45689 | 0:58524d569dfd | 62 | |
cha45689 | 0:58524d569dfd | 63 | vs1053.setFreq(24000000); //hight speed |
csggreen | 1:17913cff05a2 | 64 | |
csggreen | 2:a59255e80c0d | 65 | FileHandle *fp =sd.open(file, O_RDONLY); |
cha45689 | 0:58524d569dfd | 66 | |
cha45689 | 0:58524d569dfd | 67 | if(fp == NULL) { |
cha45689 | 0:58524d569dfd | 68 | printf("Could not open %s\r\n",file); |
cha45689 | 0:58524d569dfd | 69 | |
cha45689 | 0:58524d569dfd | 70 | } |
cha45689 | 0:58524d569dfd | 71 | else |
cha45689 | 0:58524d569dfd | 72 | { |
cha45689 | 0:58524d569dfd | 73 | printf("Playing %s ...\r\n",file); |
cha45689 | 0:58524d569dfd | 74 | |
cha45689 | 0:58524d569dfd | 75 | /* Main playback loop */ |
csggreen | 1:17913cff05a2 | 76 | while((bytes = fp->read(fileBuf,32768)) > 0) { |
csggreen | 1:17913cff05a2 | 77 | while((bytes = fp->read(fileBuf,32000)) > 0) { |
cha45689 | 0:58524d569dfd | 78 | { |
cha45689 | 0:58524d569dfd | 79 | bufptr = fileBuf; |
cha45689 | 0:58524d569dfd | 80 | // actual audio data gets sent to VS10xx. |
cha45689 | 0:58524d569dfd | 81 | while(bytes > 0) |
cha45689 | 0:58524d569dfd | 82 | { |
cha45689 | 0:58524d569dfd | 83 | n = (bytes < 32)?bytes:32; |
cha45689 | 0:58524d569dfd | 84 | vs1053.writeData(bufptr,n); |
cha45689 | 0:58524d569dfd | 85 | bytes -= n; |
cha45689 | 0:58524d569dfd | 86 | bufptr += n; |
csggreen | 1:17913cff05a2 | 87 | |
cha45689 | 0:58524d569dfd | 88 | } |
csggreen | 1:17913cff05a2 | 89 | uint8_t vol = 0x00;//set vlume |
csggreen | 1:17913cff05a2 | 90 | vs1053.setVolume(vol); //set vlume |
csggreen | 2:a59255e80c0d | 91 | |
cha45689 | 0:58524d569dfd | 92 | if(playerState != PS_PLAY) //stop |
cha45689 | 0:58524d569dfd | 93 | { |
cha45689 | 0:58524d569dfd | 94 | fp->close(); |
cha45689 | 0:58524d569dfd | 95 | vs1053.softReset(); |
cha45689 | 0:58524d569dfd | 96 | } |
csggreen | 1:17913cff05a2 | 97 | |
csggreen | 1:17913cff05a2 | 98 | } |
csggreen | 1:17913cff05a2 | 99 | } |
cha45689 | 0:58524d569dfd | 100 | } |
cha45689 | 0:58524d569dfd | 101 | } |
cha45689 | 0:58524d569dfd | 102 | |
cha45689 | 0:58524d569dfd | 103 | } |
cha45689 | 0:58524d569dfd | 104 |