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