⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp3_player_06.c

📁 基于ATmega64单片机以及vs1003的SD卡方案的MP3播放源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
	vol_temp = 100-((DEFAULT_VOLUME & 0x00ff)*100/255);
	volume[0] = vol_temp/10 + 0x30;
	volume[1] = vol_temp%10 +0x30;    
	volume[2] = '\0'; 
/**********************************************/

	Timer1_Initial();//启动定时器,用于产生随机函数的种子	//initialize the timer
next://下一首歌的起始地方						//label for "goto"
	if(mode==RANDOM)//随机播放歌曲 				//if the mode is shuffle the songs
	{
		songs_cnt++;
		if(songs_cnt == totalsongs && totalsongs<1025)
		{
			ClearTrackInfo();
			songs_cnt = 0;
		}
		rand_val = TCNT1;
		Delay((random() && 0x00ff));
		rand_val <<= 16;
		rand_val += TCNT1;
		srandom(rand_val);
		if(totalsongs>1024)
		{
			songs = (uint16)(((random()/214749)*(uint32)totalsongs)/10000)+1;//随机产生歌曲序号	//create random song number
		}
		while(totalsongs<1025)
		{
			songs = (uint16)(((random()/214749)*(uint32)totalsongs)/10000)+1;//随机产生歌曲序号	//create random song number
			if(SetTrack(songs) == 0)break;
		}	
	}
	count=0;//清基数	//clear count
	flag=1;
	if(type != MID)VS1003B_Fill2048Zero();

	lrcDisFlag = 0;	/* clear the flag in case some lyric is not displayed of last songs */
	lrcCnt = 0;		/* clear the lrc count, maybe more lyric will display on one screen, for sometimes cpu do not have time to display the last one */
	//decodetime = -1;	
	lcdInit();	/* the connection of the 3310lcd is not stable, so we reinitialize it every time */
	VS1003B_SoftReset();//soft reset //in case of playing wma files//软件复位

#if FIX_DIRECTORY
	Search((unsigned char *)(MUSIC_PATH),&MusicInfo,&songs,&type);		//找到相应的文件		//find the file
#else
	Search(&lrcaddr,&MusicInfo,&songs,&type);		//找到相应的文件		//find the file
#endif

	/* here we use lrcaddr to get the record address in the eeprom which the file placed */

	for(j=0;j<MAX_LONG_NAME_SIZE/2;j++)			/* cut off the extension, only leave the name*/
	{
		if(((unsigned int*)LongNameBuffer)[j] == 0)
		{
			((unsigned int*)LongNameBuffer)[j-4] = 0;
			break;
		}
	}
/****************************************************/
	time[0] = '0';
	time[1] = '0';
	time[2] = ':';
	time[3] = '0';
	time[4] = '0';
	time[5] = '\0';
	decodetime = -1;	/* for display the lyric which is at time 00:00 */
/******************************************************/
	temp = songs;
	for(i=0;i<5;i++)
	{
		current[4-i] = temp%10;
		temp /= 10;
	}
	for(i=0;i<5;i++)
	{
		if(current[i])break;
	}
	for(j=0;j<5-i;j++)
	{
		current[j] = current[j+i] + 0x30;
	}
	current[j++] = '/';
	for(i=0;i<6;i++)
	{
		current[j+i] = total[i];
	}
	
/********************************************************/	
	lcdClrDisBuf();
	switch(type)
	{
		case MP3:LCD_print12_P(0,0,PSTR("mp3"));break;
		case WMA:LCD_print12_P(0,0,PSTR("wma"));break;
		case MID:LCD_print12_P(0,0,PSTR("mid"));break;
		case WAV:LCD_print12_P(0,0,PSTR("wav"));break;
		default:break;
	}
	Print_ICON_12(18,0,Playing);
	LCD_print12(30,0,current);
	switch(mode)
	{
		case REPET_ALL:Print_ICON_12(72,0,RepetAll);break;
		case REPET_ONE:Print_ICON_12(72,0,RepetOne);break;
		case RANDOM   :Print_ICON_12(72,0,Shuffle);break;
		default:break;
	}
/*****************************************************/
	//LCD_print12(30,36,time);
	//LCD_print12(72,36,volume);
	//Print_ICON_12(60,36,Speaker);
	//Print_ICON_12(0,12,Music);	
/******************************************************/
	if(LongNameFlag)
	{
		if(HanziEnable)	/* if HanziEnable, convert the long name from unicode to GBK */
		{
			buffer = malloc(MAX_LONG_NAME_SIZE);
			if(buffer == 0)return 1;
			i = 0;
			j=0;
			while(1)
			{
				temp = ((unsigned int *)LongNameBuffer)[i++];

				/*  you may confused about following code, because at previous we set "." to 0 for lry search*/
				if(temp == 0 )
				{
					buffer[j++] = '.';
					while(1)	/*copy the extention */
					{
						temp = ((unsigned int *)LongNameBuffer)[i++];
						if(temp == 0)break;	/* end */
						buffer[j++] = (uint8)temp;
					}
					break;
				}
				else if (temp<0x80)
				{
					buffer[j++]=(uint8)temp;
				}
				else if(temp<0x4e00)
				{
					*((unsigned int*)&(buffer[j])) = "?";
					j+=2;
				}
				else if(temp<0x9fa6)
				{
					if(Unicode_to_GBK((unsigned char *)&temp))return 1;
					*((unsigned int*)&(buffer[j])) = temp;
					j+=2;                                
				}
				else
				{
					*((unsigned int*)&(buffer[j])) = "?";
					j+=2;
				}
			}
			buffer[j] = 0;
			LCD_print12(12,12,buffer);
			//LCD_print_unicode12(12,12,(unsigned int *)LongNameBuffer);
		}
		else
		{
			LCD_print12_P(0,12,PSTR("-- --By bozai\nFirmware lost!"));
		}
	}
	else 	/* short name */
	{
		for(j=0;j<8;j++)
		{
			if(MusicInfo.deName[j]==0x20)break;
			LongNameBuffer[j]=MusicInfo.deName[j];
		}
		//LongNameBuffer[j++]='\0';
		LongNameBuffer[j++]='.';
		count = j+3;
		for(count=0;count<3;count++)LongNameBuffer[j+count]=MusicInfo.deExtension[count];
		LongNameBuffer[j+count]='\0';
		if(HanziEnable)LCD_print12(12,12,LongNameBuffer);
		else
		{
			LCD_print12_P(0,12,PSTR("    -By bozai\nFirmware lost!"));
		}
	} 
/*************************************************************/
	LCD_print12(30,36,time);
	LCD_print12(72,36,volume);
	Print_ICON_12(60,36,Speaker);
	Print_ICON_12(0,12,Music);	
	lcdUpdateDisplay();
/**************************************************************/

	printf_P(PSTR("\r\nNow playing "));
	printf_P(PSTR("%s :"),current);
	if(LongNameFlag && HanziEnable)
	{
		printf_P(PSTR("%s"),buffer);
		free(buffer);
	}
	else
	{
		for(j=0;j<8;j++)
		{
			if(MusicInfo.deName[j]==0x20)break;
			USART_putchar(MusicInfo.deName[j]);
		}
        
		USART_putchar('.');
		for(j=0;j<3;j++)
		{
			//if(MusicInfo.deName[j]==0x20)break;
			USART_putchar(MusicInfo.deExtension[j]);
		}
	}

/**************************************************************/
#if FIX_DIRECTORY
	LrcProc(LongNameBuffer);
#else
	LrcProc(LongNameBuffer,lrcaddr);			/* search lyc file */
											/* lrcaddr indicate the record address of eeprom */
											/* first search the current folder then the default */
#endif											
	LrcStruct_pp = LrcStructHead.header;

/**************************************************************/

	p     = MusicInfo.deStartCluster+(((unsigned long)MusicInfo.deHighClust)<<16);//读文件首簇	//the first cluster of the file
		
	totalsect = MusicInfo.deFileSize/512; //计算扇区数			//calculate the total sectors
	leftbytes = MusicInfo.deFileSize%512; //计算剩余的字节数	//calculate the left bytes	
	i=0;
	sector=0;

	if(lrc && HanziEnable)		/* clear the previous time area */
	{
		if(LrcStruct_pp->time/1000 == 0 && LrcStruct_pp != 0)/* see if it has lyric at time 00:00 */
		{
			
			decodetime = -1;	/* for display the lyric which is at time 00:00 */
		}
		else
		{
			decodetime = 0;		/* otherwise, it will not flush screen when 00:00, then you can see the filename and other info for 1 second */
		}
		LCD_print12_P(36,36,PSTR("    "));
		lcdClrDisBuf(); 
	}
    
	while(1)
	{
		keylen=0;
		for(;i<SectorsPerClust;i++)		//一个簇	//a cluster
		{
			buffer=malloc(512);
			FAT_LoadPartCluster(p,i,buffer);//读一个扇区	//read a sector
			count=0;
			while(count<512)
			{
				if(flag==0){if(keylen){Delay(100);keylen--;}}
				else if(type == MID){if(keylen){Delay(100);keylen--;}}

				if(VS1003B_NeedData() && flag)	//根据需要送数据  //send data  honoring DREQ
				{
					VS1003B_Write32B(&buffer[count]);
					count += 32;
					if(keylen)keylen--;			//用于键处理			//for key processing
					if(sector == totalsect && count >= leftbytes)		//如果文件已结束		//if this is the end of the file
					{
						if(type == MID)//waiting the midi file was decoded
							VS1003B_Fill2048Zero();
						i=SectorsPerClust;
						break;
					}//文件结束			//file ended
					if(count == 511){break;}//512字节送完跳出		//break if a sector was sent
				}
				else if(VS1003B_ReadDecodeTime() != decodetime)
				{
					decodetime = VS1003B_ReadDecodeTime();
					min = decodetime/60;
					sec = decodetime%60;
					if(lrc)
					{ 
						if(min>99);
						else if(HanziEnable)
						{
							time[0] = min/10 + 0x30;
							time[1] = min%10 + 0x30;
							time[3] = sec/10 + 0x30;
							time[4] = sec%10 + 0x30;
							LCD_print12_fast(54,36,time);
						} 
						else
						{
							time[0] = min/10 + 0x30;
							time[1] = min%10 + 0x30;
							time[3] = sec/10 + 0x30;
							time[4] = sec%10 + 0x30;
							LCD_print12_fast(30,36,time);
						}
						while(1)
						{
							if(LrcStruct_pp->time/1000 <= decodetime && LrcStruct_pp != 0)
							{
								lrcaddr = LrcStruct_pp->eeaddr;
								while(1)
								{
									if(lrcaddr<MAXLRCDATSIZE)temp = lrcdatbuf[lrcaddr++];
									else temp = eeprom_read_byte(lrcaddr++ - MAXLRCDATSIZE);

									if(temp == 0)
									{
										break;
									}
									if(lrcCnt<MAX_LRC_DISP-1)lrcdata[lrcCnt++] = temp;
								}
								lrcdata[lrcCnt] = '\n';
								LrcStruct_pp = LrcStruct_pp->next;
							}
							else
							{
								if(lrcCnt && HanziEnable)
								{
									lcdClrDisBuf(); 
									lrcdata[lrcCnt] = '\0';
									printf_P(PSTR("\r\n[%s] %s"),time,lrcdata);
									//putsLrc(time,lrcdata);	/* faster string put */

									lrcDisFlag = 1;	/* we do not display the lyric now, it will display at next time */
													/* then it will not take many time to process the lyric          */
													/* otherwise, when playing 320Kbps file, glitch may occurs       */
									
									//LCD_print12_fast(0,0,lrcdata);
									//LCD_print12_fast(54,36,time);
								}
								else if(lrcCnt)
								{
									lrcdata[lrcCnt] = '\0';
									printf_P(PSTR("\r\n[%s] %s"),time,lrcdata);
									LCD_print12_fast(30,36,time);
								}
								break;
							}
						}
						lcdUpdateDisplay();
					}
					else
					{
						if(min>99);
						else
						{
							time[0] = min/10 + 0x30;
							time[1] = min%10 + 0x30;
							time[3] = sec/10 + 0x30;
							time[4] = sec%10 + 0x30;
							LCD_print12(30,36,time);
							lcdUpdateDisplay();
						} 
					}
				}
				else if(lrcDisFlag)	/* display the lyric   */
				{					/* the lowest priority */
					lrcDisFlag = 0;
					lrcCnt = 0;
					LCD_print12_fast(0,0,lrcdata);
					LCD_print12_fast(54,36,time);
					lcdUpdateDisplay();
				} 
/**********************************************************************************************************/ 
/* the following code if for uart control  */
				else if(UCSR0A & (1<<RXC0))
				{
					temp = UDR0;
					switch(temp)
					{
						case 'x':
						case 'X':
						case 0x41:	/* volume up */
									if(vol_temp == 99);
									else  
									{
										vol_temp++;
										vol = (uint8)(255-(((uint16)vol_temp)*255)/100);
										vol =(vol<<8) + vol;
										VS1003B_SetVolume(vol);
										volume[0] = vol_temp/10 + 0x30;
										volume[1] = vol_temp%10 +0x30;
										printf_P(PSTR("\r\nVolume: %s"),volume);
										if(lrc == 0 || HanziEnable == 0)
										{
											LCD_print12(72,36,volume);
											lcdUpdateDisplay();
										}
										else
										{
											Print_ICON_12(30,36,Speaker);
											LCD_print12(42,36,volume);
											lcdUpdateDisplay();
										}
									}
									break;
								
						case 'z':
						case 'Z':	 
						case 0x42:	/* volume down */
									if(vol_temp == 0);
									else 
									{
										vol_temp--;
										vol = (uint8)(255-(((uint16)vol_temp)*255)/100);
										vol = (vol<<8) +vol;
										VS1003B_SetVolume(vol);
										volume[0] = vol_temp/10 + 0x30;
										volume[1] = vol_temp%10 +0x30; 
										printf_P(PSTR("\r\nVolume: %s"),volume);
										if(lrc == 0 || HanziEnable == 0)
										{
											LCD_print12(72,36,volume);
											lcdUpdateDisplay();
										}
										else
										{
											Print_ICON_12(30,36,Speaker);
											LCD_print12(42,36,volume);
											lcdUpdateDisplay();
										}
									}
									break; 

						case '.':
						case '>':
						case 0x43:	/* next songs */
									songs++;
									if(songs > totalsongs)songs=1;
									free(buffer);
									goto next;
									break;
						
						case ',':
						case '<':
						case 0x44:	/*  previous songs */
									if(songs == 1)songs=totalsongs;
									else songs--;
									free(buffer);
									goto next;
									break;
						case 0x0d:
						case 0x0a:
						case 0x20:	/* play/pause */
									if(flag)
									{
										flag=0;
										printf_P(PSTR("\r\nPause"));
										if(lrc == 0 || HanziEnable == 0)
										{
											Print_ICON_12(18,0,Pause);
											lcdUpdateDisplay();
										}
									}
									else
									{
										flag=1;
										printf_P(PSTR("\r\nPlaying"));
										if(lrc == 0 || HanziEnable == 0)
										{
											Print_ICON_12(18,0,Playing);
											lcdUpdateDisplay();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -