Natthaphon Sudadech
/
PJ04_MASTER
+mp3
Fork of VS1053 by
player.cpp@1:17913cff05a2, 2017-12-09 (annotated)
- Committer:
- csggreen
- Date:
- Sat Dec 09 14:21:59 2017 +0000
- Revision:
- 1:17913cff05a2
- Parent:
- 0:58524d569dfd
- Child:
- 2:a59255e80c0d
gwev
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; |
cha45689 | 0:58524d569dfd | 11 | |
cha45689 | 0:58524d569dfd | 12 | char list[20][14]; //song list |
csggreen | 1:17913cff05a2 | 13 | char index; //song play index |
cha45689 | 0:58524d569dfd | 14 | char index_MAX; //how many song in all |
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++; |
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 | index_MAX = i-1; |
cha45689 | 0:58524d569dfd | 46 | dir->closedir(); |
cha45689 | 0:58524d569dfd | 47 | printf("\r\n"); |
cha45689 | 0:58524d569dfd | 48 | } |
cha45689 | 0:58524d569dfd | 49 | |
csggreen | 1:17913cff05a2 | 50 | void Player::stop(void){ |
csggreen | 1:17913cff05a2 | 51 | playerState =PS_STOP; |
csggreen | 1:17913cff05a2 | 52 | } |
csggreen | 1:17913cff05a2 | 53 | void Player::playfile(char n){ |
csggreen | 1:17913cff05a2 | 54 | playerState =PS_STOP; |
csggreen | 1:17913cff05a2 | 55 | index = n; |
csggreen | 1:17913cff05a2 | 56 | } |
csggreen | 1:17913cff05a2 | 57 | |
csggreen | 1:17913cff05a2 | 58 | void Player::getsong(void){ |
csggreen | 1:17913cff05a2 | 59 | playfile(2); |
csggreen | 1:17913cff05a2 | 60 | } |
csggreen | 1:17913cff05a2 | 61 | |
cha45689 | 0:58524d569dfd | 62 | /* This function plays back an audio file. */ |
cha45689 | 0:58524d569dfd | 63 | void Player::playFile(char *file) { |
cha45689 | 0:58524d569dfd | 64 | int bytes; // How many bytes in buffer left |
cha45689 | 0:58524d569dfd | 65 | char n; |
cha45689 | 0:58524d569dfd | 66 | |
cha45689 | 0:58524d569dfd | 67 | playerState = PS_PLAY; |
cha45689 | 0:58524d569dfd | 68 | |
cha45689 | 0:58524d569dfd | 69 | vs1053.setFreq(24000000); //hight speed |
csggreen | 1:17913cff05a2 | 70 | |
csggreen | 1:17913cff05a2 | 71 | FileHandle *fp =sd.open(file, index); |
cha45689 | 0:58524d569dfd | 72 | |
cha45689 | 0:58524d569dfd | 73 | if(fp == NULL) { |
cha45689 | 0:58524d569dfd | 74 | printf("Could not open %s\r\n",file); |
cha45689 | 0:58524d569dfd | 75 | |
cha45689 | 0:58524d569dfd | 76 | } |
cha45689 | 0:58524d569dfd | 77 | else |
cha45689 | 0:58524d569dfd | 78 | { |
cha45689 | 0:58524d569dfd | 79 | printf("Playing %s ...\r\n",file); |
cha45689 | 0:58524d569dfd | 80 | |
cha45689 | 0:58524d569dfd | 81 | /* Main playback loop */ |
csggreen | 1:17913cff05a2 | 82 | while((bytes = fp->read(fileBuf,32768)) > 0) { |
csggreen | 1:17913cff05a2 | 83 | while((bytes = fp->read(fileBuf,32000)) > 0) { |
cha45689 | 0:58524d569dfd | 84 | { |
cha45689 | 0:58524d569dfd | 85 | bufptr = fileBuf; |
cha45689 | 0:58524d569dfd | 86 | // actual audio data gets sent to VS10xx. |
cha45689 | 0:58524d569dfd | 87 | while(bytes > 0) |
cha45689 | 0:58524d569dfd | 88 | { |
cha45689 | 0:58524d569dfd | 89 | n = (bytes < 32)?bytes:32; |
cha45689 | 0:58524d569dfd | 90 | vs1053.writeData(bufptr,n); |
cha45689 | 0:58524d569dfd | 91 | bytes -= n; |
cha45689 | 0:58524d569dfd | 92 | bufptr += n; |
csggreen | 1:17913cff05a2 | 93 | |
cha45689 | 0:58524d569dfd | 94 | } |
csggreen | 1:17913cff05a2 | 95 | uint8_t vol = 0x00;//set vlume |
csggreen | 1:17913cff05a2 | 96 | vs1053.setVolume(vol); //set vlume |
cha45689 | 0:58524d569dfd | 97 | if(playerState != PS_PLAY) //stop |
cha45689 | 0:58524d569dfd | 98 | { |
cha45689 | 0:58524d569dfd | 99 | fp->close(); |
cha45689 | 0:58524d569dfd | 100 | vs1053.softReset(); |
cha45689 | 0:58524d569dfd | 101 | } |
csggreen | 1:17913cff05a2 | 102 | |
csggreen | 1:17913cff05a2 | 103 | } |
csggreen | 1:17913cff05a2 | 104 | } |
cha45689 | 0:58524d569dfd | 105 | } |
cha45689 | 0:58524d569dfd | 106 | } |
cha45689 | 0:58524d569dfd | 107 | |
cha45689 | 0:58524d569dfd | 108 | } |
cha45689 | 0:58524d569dfd | 109 |