无损音乐

1 #include "Page.h"
2 #include "MusicPage.h"
3 #include "FileListPage.h"
4 #include "Resources.h"
5
6  static u32 ReadMusicDir(char *MusicDirPath)
7 {
8 FILINFO finfo;
9
10 gMusicTotalNum=0;
11
12 #if _USE_LFN
13 finfo.lfname = Fs_LongFileName;
14 finfo.lfsize = sizeof(Fs_LongFileName);
15 #endif
16
17 if (f_opendir(&gMusicDir, MusicDirPath) != FR_OK)
18 {
19 Debug("Open Music Dir error!\n\r");
20 return FALSE;
21 }
22
23 while (f_readdir(&gMusicDir, &finfo) == FR_OK)
24 {
25 if (finfo.fattrib & AM_ARC)
26 {
27 if(!finfo.fname[0]) break;
28 if(!( CheckSuffix(finfo.lfname,".wav")&CheckSuffix(finfo.lfname,".wma")&CheckSuffix(finfo.lfname,".mp3")) )
29 {
30 gMusicSet[gMusicTotalNum]=OS_Mallco(strlen(finfo.lfname)+1);
31 strcpy((void *)gMusicSet[gMusicTotalNum],(void *)finfo.lfname);
32 gMusicTotalNum++;
33 }
34 }
35 }
36
37 return TRUE;
38 }
39
40 static u32 PlayMusic(int Selection)
41 {
42 if(Selection==-1) return 0;
43
44 Debug("Select:%d.%s\n\r",Selection,gMusicSet[Selection]);
45
46 if(gCurrentSelection!=Selection)
47 {
48 if(gCurrentSelection!=-1)
49 {
50 if(f_close(&gMusicObj)!=FR_OK)
51 {
52 Debug("Close music file error!\n\r");
53 }
54 }
55
56 gCurrentSelection=Selection;
57 gPlayOperate=Play_Start;
58 }
59
60 DispPlayName(gMusicSet[gCurrentSelection]);
61 DispPlayMsg("正在播放...");
62
63 return 0;
64 }
65
66 //-----------------------本页系统函数----------------------
67 //当本页面成为系统当前页面时会运行的程序
68 //一旦本页面变成非当前页面,系统会等待此
69 //任务完成工作后暂停本线程。通过RunMutex实现
70 static char MusicBuf[512*8];
71 static void MusicPageRun_Task(void *Task_Parameters)
72 {
73 FRESULT Res;
74 UINT ReadByte,count,j;
75 //char MusicBuf[512*2];
76 char MusicPath[64];
77 char *pMusic;
78
79 Debug("Music Page Run Task\n\r");
80
81 while(1)
82 {
83 PageRunTaskLoopStart();
84 switch(gPlayOperate)
85 {
86 case Play_Nothing:
87 OS_TaskDelayMs(200);
88 break;
89 case Play_Start:
90 if((gCurrentSelection==-1)||gCurrentSelection>=gMusicTotalNum)
91 {
92 gPlayOperate=Play_Nothing;
93 break;
94 }
95
96 MusicPath[0]=0;
97 strcat((void *)MusicPath,(void *)MUSIC_DIR_PATH"/");
98 strcat((void *)MusicPath,(void *)gMusicSet[gCurrentSelection]);
99 if ((Res=f_open(&gMusicObj ,MusicPath, FA_OPEN_EXISTING | FA_READ) )!= FR_OK )
100 {
101 Debug("Open music flie error %d!:%s\n\r",Res,MusicPath);
102 break;
103 }
104 Debug("Playing...\n\r");
105 gPlayOperate=Play_Continue;
106 break;
107 case Play_Continue:
108 Res=f_read(&gMusicObj, MusicBuf, sizeof(MusicBuf), &ReadByte);
109 if(!Res && ReadByte)
110 {
111 pMusic=MusicBuf;
112
113 for(count=0;count<ReadByte;count+=32)
114 {
115 VS_XDCS_SET(0);
116 for(j=32;j>0;j--)//每次送32个数据
117 {
118 SPI1_Write_Byte(*pMusic++);
119 }
120 VS_XDCS_SET(1);
121
122 while(!VS_DREQ)
123 {
124
125 }
126 }
127
128 }
129 else
130 {
131 Debug("Read music file %s end!\n\r",MusicPath);
132
133 VS_XDCS_SET(0);
134 for(count=64;count;count--)
135 {
136 for(j=32;j>0;j--)//每次送32个数据
137 {
138 SPI1_Write_Byte(0);
139 }
140 while(!VS_DREQ);// OS_TaskDelayMs(5);
141 }
142 VS_XDCS_SET(1);
143
144 if((gCurrentSelection+1)<gMusicTotalNum)
145 {
146 PlayMusic(gCurrentSelection+1);
147 }
148 else
149 {
150 PlayMusic(0);
151 }
152 }
153 break;
154 case Play_Stop:
155 Debug("Stop playing %s\n\r",MusicPath);
156 if(f_close(&gMusicObj)!=FR_OK)
157 {
158 Debug("Close music file error!\n\r");
159 }
160 gPlayOperate=Play_Start;
161 break;
162 case Play_ChangeVol:
163 Debug("Set music output vol to %d\n\r",VolumeTable[gMusicVol]);
164 Mp3SetVolume(VolumeTable[gMusicVol],VolumeTable[gMusicVol]);
165 if(gCurrentSelection==-1)
166 {
167 DispPlayMsg("请选择文件!");
168 gPlayOperate=Play_Start;
169 }
170 else
171 {
172 DispPlayMsg("正在播放...");
173 gPlayOperate=Play_Continue;
174 }
175 break;
176 }
177 PageRunTaskLoopStop();
178 }
179 }
180
181 //在每次本页面变成系统当前页面时会调用的程序
182 static int SystemEventHandlerMusicPage(SYSTEM_EVENT_TYPE SysEvent ,int IntParam, void *pSysParam)
183 {
184 static u8 PageFristInit_Flag=0;
185 GUI_REGION DrawRegion;
186 int i;
187
188 switch(SysEvent)
189 {
190 case Sys_Page_Init:
191 case Sys_SubPage_Return:
192 if(!PageFristInit_Flag) //页面首次创建时需要做的事情
193 {
194 gMusicPageRunMutex=OS_SemaphoreCreateMutex();
195 OS_TaskCreate(MusicPageRun_Task , ( signed OS_CHAR * ) "MusicPageRun", OS_MINI_STACK_SIZE, NULL, MUSIC_TASK_PRIORITY, NULL );
196
197 gCurrentSelection=-1;
198 gPlayOperate=Play_Nothing;
199
200 gMusicVol=6;
201 Mp3Start(VolumeTable[gMusicVol],VolumeTable[gMusicVol]);
202 }
203 DrawRegion.x=DrawRegion.y=0;
204 DrawRegion.w=1;
205 DrawRegion.h=320;
206 Gui_FillImageArray_H((u8 *)gImage_MainBg,240,&DrawRegion);
207 DispTime();
208 break;
209 case Sys_TouchRegionSetFinish:
210 DispPlayName("");
211 DispPlayMsg("请选择文件!");
212
213 if(!PageFristInit_Flag) //页面首次创建时需要做的事情
214 {
215 PageFristInit_Flag=1;
216 ReadMusicDir(MUSIC_DIR_PATH);
217 for(i=0;i<gMusicTotalNum;i++)
218 {
219 Debug("%d:%s\n\r",i,gMusicSet[i]);
220 }
221 }
222
223 if(IntParam!=-1)
224 {
225 Debug("Play %s\n\r",gMusicSet[IntParam]);
226 PlayMusic(IntParam);
227 }
228 else
229 {
230 PlayMusic(gCurrentSelection);
231 }
232 break;
233 case Sys_Page_Clean:
234 break;
235 case Sys_Rtc_Min:
236 DispTime();
237 break;
238 }
239
240 return 0;
241 }
242
243 static int TouchEventHandlerMusicPage(int Key,TOUCH_EVENT_TYPE InEvent , TOUCH_INFO *pTouchInfo)
244 {
245 FILE_LIST_PAGE_SET FileListParam;
246
247 if(InEvent!=Touch_Key_Release) return 0;
248
249 switch(Key)
250 {
251 case MusicKey_ExtiKey0:
252 PrtScreen();
253 break;
254 case MusicKey_VolumeUp:
255 if(gMusicVol<VOL_STEP-1)
256 {
257 gMusicVol++;
258 gPlayOperate=Play_ChangeVol;
259 }
260 break;
261 case MusicKey_VolumeDown:
262 if(gMusicVol>0)
263 {
264 gMusicVol--;
265 gPlayOperate=Play_ChangeVol;
266 }
267 break;
268 case MusicKey_PreOne:
269 if(gCurrentSelection>0)
270 {
271 PlayMusic(gCurrentSelection-1);
272 }
273 else
274 {
275 PlayMusic(gMusicTotalNum-1);
276 }
277 break;
278 case MusicKey_NextOne:
279 if((gCurrentSelection+1)<gMusicTotalNum)
280 {
281 PlayMusic(gCurrentSelection+1);
282 }
283 else
284 {
285 PlayMusic(0);
286 }
287 break;
288 case MusicKey_Find:
289 FileListParam.pCallerPageRid="0.1";
290 FileListParam.pFileSet=gMusicSet;
291 FileListParam.FileTotalNum=gMusicTotalNum;
292 GotoPage("0.1.1",gCurrentSelection,&FileListParam);
293 break;
294 case MusicKey_Pause:
295 if(gCurrentSelection!=-1)
296 {
297 DispPlayMsg("暂停播放");
298 gPlayOperate=Play_Nothing;
299 }
300 break;
301 case MusicKey_Play:
302 if(gCurrentSelection==-1)
303 {
304 DispPlayMsg("请选择文件!");
305 gPlayOperate=Play_Start;
306 }
307 else
308 {
309 DispPlayMsg("正在播放...");
310 gPlayOperate=Play_Continue;
311 }
312 break;
313 case MusicKey_Return:
314 GotoPage("0",-1,NULL);
315 break;
316 }
317
318 return 0;
319 }
posted @ 2011-03-09 17:39  Mr.Shan  Views(500)  Comments(0)    收藏  举报