mp3
player.cpp@4:5b1bd3cca8dc, 2017-12-11 (annotated)
- Committer:
- csggreen
- Date:
- Mon Dec 11 12:29:47 2017 +0000
- Revision:
- 4:5b1bd3cca8dc
- Parent:
- 3:94b538ace77a
- Child:
- 5:ce3a3ce60f30
new type
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 |
csggreen | 4:5b1bd3cca8dc | 6 | Serial aa(USBTX, USBRX); |
csggreen | 4:5b1bd3cca8dc | 7 | Serial pc(D1, D0); |
cha45689 | 0:58524d569dfd | 8 | playerStatetype playerState; |
csggreen | 4:5b1bd3cca8dc | 9 | static unsigned char fileBuf[48000]; |
cha45689 | 0:58524d569dfd | 10 | unsigned char *bufptr; |
csggreen | 2:a59255e80c0d | 11 | char green; |
csggreen | 4:5b1bd3cca8dc | 12 | bool startplaysong; |
csggreen | 4:5b1bd3cca8dc | 13 | bool playsong; |
cha45689 | 0:58524d569dfd | 14 | |
csggreen | 2:a59255e80c0d | 15 | char list[20][50]; //song list |
csggreen | 2:a59255e80c0d | 16 | |
csggreen | 1:17913cff05a2 | 17 | char data ; |
csggreen | 1:17913cff05a2 | 18 | DigitalIn sw_in(USER_BUTTON); |
cha45689 | 0:58524d569dfd | 19 | |
cha45689 | 0:58524d569dfd | 20 | void Player::begin(void) |
cha45689 | 0:58524d569dfd | 21 | { |
cha45689 | 0:58524d569dfd | 22 | DirHandle *dir; |
cha45689 | 0:58524d569dfd | 23 | struct dirent *ptr; |
cha45689 | 0:58524d569dfd | 24 | FileHandle *fp; |
cha45689 | 0:58524d569dfd | 25 | |
cha45689 | 0:58524d569dfd | 26 | vs1053.reset(); |
cha45689 | 0:58524d569dfd | 27 | dir = opendir("/sd"); |
csggreen | 4:5b1bd3cca8dc | 28 | //printf("\r\n**********playing list**********\r\n"); |
cha45689 | 0:58524d569dfd | 29 | unsigned char i = 0,j=0; |
cha45689 | 0:58524d569dfd | 30 | while(((ptr = dir->readdir()) != NULL)&&(i <20)) |
cha45689 | 0:58524d569dfd | 31 | { |
csggreen | 1:17913cff05a2 | 32 | if(strstr(ptr->d_name,".mp3")||strstr(ptr->d_name,".MP3")) |
cha45689 | 0:58524d569dfd | 33 | { |
cha45689 | 0:58524d569dfd | 34 | fp =sd.open(ptr->d_name, O_RDONLY); |
cha45689 | 0:58524d569dfd | 35 | if(fp != NULL) |
cha45689 | 0:58524d569dfd | 36 | { |
cha45689 | 0:58524d569dfd | 37 | char *byte = ptr->d_name; |
csggreen | 1:17913cff05a2 | 38 | j=0; |
csggreen | 1:17913cff05a2 | 39 | while(*byte){ |
csggreen | 1:17913cff05a2 | 40 | list[i][j++] = *byte++; |
csggreen | 2:a59255e80c0d | 41 | green=i; |
cha45689 | 0:58524d569dfd | 42 | } |
csggreen | 4:5b1bd3cca8dc | 43 | aa.printf("%2d . %s\r\n", i,list[i++]); |
csggreen | 1:17913cff05a2 | 44 | //fp->close(); |
cha45689 | 0:58524d569dfd | 45 | } |
cha45689 | 0:58524d569dfd | 46 | } |
cha45689 | 0:58524d569dfd | 47 | } |
cha45689 | 0:58524d569dfd | 48 | dir->closedir(); |
cha45689 | 0:58524d569dfd | 49 | } |
cha45689 | 0:58524d569dfd | 50 | |
csggreen | 3:94b538ace77a | 51 | |
csggreen | 3:94b538ace77a | 52 | |
csggreen | 4:5b1bd3cca8dc | 53 | |
csggreen | 4:5b1bd3cca8dc | 54 | |
csggreen | 4:5b1bd3cca8dc | 55 | /* select file by num. */ |
csggreen | 4:5b1bd3cca8dc | 56 | void Player::selectfile(char n){ |
csggreen | 4:5b1bd3cca8dc | 57 | startplaysong=false; |
csggreen | 4:5b1bd3cca8dc | 58 | playerState =PS_STOP; |
csggreen | 4:5b1bd3cca8dc | 59 | playsong=false; |
csggreen | 4:5b1bd3cca8dc | 60 | green = n; |
csggreen | 4:5b1bd3cca8dc | 61 | } |
csggreen | 4:5b1bd3cca8dc | 62 | |
csggreen | 4:5b1bd3cca8dc | 63 | |
csggreen | 4:5b1bd3cca8dc | 64 | |
csggreen | 4:5b1bd3cca8dc | 65 | /* This function stop an audio file. */ |
csggreen | 1:17913cff05a2 | 66 | void Player::stop(void){ |
csggreen | 4:5b1bd3cca8dc | 67 | startplaysong=true; |
csggreen | 1:17913cff05a2 | 68 | playerState =PS_STOP; |
csggreen | 1:17913cff05a2 | 69 | } |
csggreen | 2:a59255e80c0d | 70 | |
csggreen | 4:5b1bd3cca8dc | 71 | void Player::run(void){ |
csggreen | 4:5b1bd3cca8dc | 72 | if(pc.readable()){ |
csggreen | 4:5b1bd3cca8dc | 73 | |
csggreen | 4:5b1bd3cca8dc | 74 | char datainput; |
csggreen | 4:5b1bd3cca8dc | 75 | |
csggreen | 4:5b1bd3cca8dc | 76 | if(datainput=='1'){ |
csggreen | 4:5b1bd3cca8dc | 77 | selectfile(1); |
csggreen | 4:5b1bd3cca8dc | 78 | stop(); |
csggreen | 4:5b1bd3cca8dc | 79 | } |
csggreen | 4:5b1bd3cca8dc | 80 | if(datainput=='2'){ |
csggreen | 4:5b1bd3cca8dc | 81 | selectfile(2); |
csggreen | 4:5b1bd3cca8dc | 82 | stop(); |
csggreen | 4:5b1bd3cca8dc | 83 | } |
csggreen | 4:5b1bd3cca8dc | 84 | } |
csggreen | 4:5b1bd3cca8dc | 85 | } |
csggreen | 1:17913cff05a2 | 86 | |
csggreen | 2:a59255e80c0d | 87 | |
csggreen | 4:5b1bd3cca8dc | 88 | |
csggreen | 4:5b1bd3cca8dc | 89 | void Player::read(void){ |
csggreen | 4:5b1bd3cca8dc | 90 | pc.readable(); |
csggreen | 4:5b1bd3cca8dc | 91 | |
csggreen | 4:5b1bd3cca8dc | 92 | } |
csggreen | 4:5b1bd3cca8dc | 93 | |
csggreen | 1:17913cff05a2 | 94 | |
csggreen | 4:5b1bd3cca8dc | 95 | |
csggreen | 4:5b1bd3cca8dc | 96 | /* This function plays audio file. */ |
cha45689 | 0:58524d569dfd | 97 | void Player::playFile(char *file) { |
cha45689 | 0:58524d569dfd | 98 | int bytes; // How many bytes in buffer left |
cha45689 | 0:58524d569dfd | 99 | char n; |
cha45689 | 0:58524d569dfd | 100 | |
cha45689 | 0:58524d569dfd | 101 | playerState = PS_PLAY; |
cha45689 | 0:58524d569dfd | 102 | |
cha45689 | 0:58524d569dfd | 103 | vs1053.setFreq(24000000); //hight speed |
csggreen | 1:17913cff05a2 | 104 | |
csggreen | 2:a59255e80c0d | 105 | FileHandle *fp =sd.open(file, O_RDONLY); |
cha45689 | 0:58524d569dfd | 106 | |
cha45689 | 0:58524d569dfd | 107 | if(fp == NULL) { |
cha45689 | 0:58524d569dfd | 108 | printf("Could not open %s\r\n",file); |
cha45689 | 0:58524d569dfd | 109 | |
cha45689 | 0:58524d569dfd | 110 | } |
cha45689 | 0:58524d569dfd | 111 | else |
cha45689 | 0:58524d569dfd | 112 | { |
cha45689 | 0:58524d569dfd | 113 | printf("Playing %s ...\r\n",file); |
cha45689 | 0:58524d569dfd | 114 | |
cha45689 | 0:58524d569dfd | 115 | /* Main playback loop */ |
csggreen | 4:5b1bd3cca8dc | 116 | while((bytes = fp->read(fileBuf,48000)) > 0) { |
cha45689 | 0:58524d569dfd | 117 | { |
cha45689 | 0:58524d569dfd | 118 | bufptr = fileBuf; |
cha45689 | 0:58524d569dfd | 119 | // actual audio data gets sent to VS10xx. |
cha45689 | 0:58524d569dfd | 120 | while(bytes > 0) |
cha45689 | 0:58524d569dfd | 121 | { |
cha45689 | 0:58524d569dfd | 122 | n = (bytes < 32)?bytes:32; |
cha45689 | 0:58524d569dfd | 123 | vs1053.writeData(bufptr,n); |
cha45689 | 0:58524d569dfd | 124 | bytes -= n; |
cha45689 | 0:58524d569dfd | 125 | bufptr += n; |
csggreen | 1:17913cff05a2 | 126 | |
cha45689 | 0:58524d569dfd | 127 | } |
csggreen | 1:17913cff05a2 | 128 | uint8_t vol = 0x00;//set vlume |
csggreen | 1:17913cff05a2 | 129 | vs1053.setVolume(vol); //set vlume |
csggreen | 2:a59255e80c0d | 130 | |
cha45689 | 0:58524d569dfd | 131 | if(playerState != PS_PLAY) //stop |
cha45689 | 0:58524d569dfd | 132 | { |
cha45689 | 0:58524d569dfd | 133 | fp->close(); |
cha45689 | 0:58524d569dfd | 134 | vs1053.softReset(); |
cha45689 | 0:58524d569dfd | 135 | } |
csggreen | 1:17913cff05a2 | 136 | |
csggreen | 1:17913cff05a2 | 137 | } |
cha45689 | 0:58524d569dfd | 138 | } |
cha45689 | 0:58524d569dfd | 139 | } |
cha45689 | 0:58524d569dfd | 140 | |
cha45689 | 0:58524d569dfd | 141 | } |
cha45689 | 0:58524d569dfd | 142 |