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

📄 main.c

📁 butterfly MP3源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
**	Resets the decoder and prepares it for a new stream of *	audio data. all setup params must be resent.**/void 	flush_decoder(char reset_type){	uint16 buf[2];		// reset VS1001 and set clock doubler	vs1001_reset(reset_type);	// send VS1001 settings	vs_1001_setvolume(pgm_read_byte(volume_val+gmusic_vol), pgm_read_byte(volume_val+gmusic_vol));	if (gbass_boost){		vs1001_read(0, 1, buf);		buf[1] |= SM_BASS;		buf[1] &= ~SM_MP12;		vs1001_write(0,1,buf);	}else{		vs1001_read(0, 1, buf);		buf[1] &= ~SM_BASS;		buf[1] &= ~SM_MP12;		vs1001_write(0,1,buf);	}}    /***	cue_file			*	@return		none	**	Gets file info for file pointed to by gFAT_entry*	and inits MP3 chip. Checks for MMC card as well.*  Reads ID3v2 tag for title and artist*	If successful then gFile_good is set**/void 	cue_file(void){	uint8 check;//	uint8 temp;   uint32 ID3index;	uint8 i;	uint16 j;	uint32 offset;	uint16 ID3_Bytes;	uint32 ID3_clusters;	uint16 ID3_sectors;	uint32 nextCluster=0;			// read file data and check gFAT_entry is aValid file entry.	check = FAT_readFile(gFAT_entry,FAT16_dir_first_cluster);	if (check) return;		if (FAT16_filetype == FILE_TYPE_DIR) return;/*	//skip or recurse Directories	if (FAT16_filetype == FILE_TYPE_DIR){		if (RECURSE_DIRECTORIES){			// scan a new directory....			// get lba from fat entry			gSector = FAT_scratch[13];			gSector <<= 8;			gSector += FAT_scratch[14];			gSector <<= 8;			gSector += FAT_scratch[15];			gSector <<= 8;			gSector += FAT_scratch[16];						// check for files and exit on no files(always 2 entries . and ..)			//if (open_Dir(gSector)<3) {			//	return;			//}						nextCluster = FAT_readFile(FAT16_entryMIN,FAT16_dir_first_cluster);					}else{			if (PLAY_NEXT_SONG){ // cue up next song and play it.				nextCluster = FAT_getNextSong(gFAT_entry,FAT16_dir_first_cluster);				if(nextCluster){// play next song					gFAT_entry = nextCluster;					gLCD_Refresh=TRUE;					gMode = MODE_PLAY;				}else if(PLAY_REPEAT_ALL){					gFAT_entry = FAT16_entryMIN;					gLCD_Refresh=TRUE;					gMode = MODE_PLAY;				}			}		}	}	*/	nextCluster=0;		// get first Cluster 	gSector = FAT_scratch[13];	gSector <<= 8;	gSector += FAT_scratch[14];	gSector <<= 8;	gSector += FAT_scratch[15];	gSector <<= 8;	gSector += FAT_scratch[16];	//get fileSize in whole sectors	gFileSectorSize = FAT_scratch[17];	gFileSectorSize <<= 8;	gFileSectorSize += FAT_scratch[18];	gFileSectorSize <<= 8;	gFileSectorSize += FAT_scratch[19];	cluster_pos = 0;	buff_pos = 0;	FAT_readCluster(gSector,0);		gFile_good = TRUE;	gFileSectorsPlayed = 0;		// Detect and Skip ID3 Info at start of file	// (we ignore tags at end of file)		// Version 1.x 	if (FAT_buffer[0] == 'T' && FAT_buffer[1] == 'A' && FAT_buffer[2]=='G') {		//PRINT("ID3 v1 TAG");EOL();		// jump to byte 128		buff_pos = 4; 	}else			// Version 2.x - now with very basic interpretation!	if (FAT_buffer[0] == 'I' && FAT_buffer[1] == 'D' 			  && FAT_buffer[2]=='3'){ 		//PRINT("ID3 v2.");		//UART_Printfu08(FAT_buffer[5]);		//UART_Printfu08(FAT_buffer[4]);		//PRINT(" TAG");      		/*		An ID3v2 tag can be detected with the following pattern:		$49 44 33 yy yy xx zz zz zz zz		Where yy is less than $FF, xx is the 'flags' byte and zz is less than $80.		z = ID3 tag length.		 */				offset = 0;		for(i=6;i<10;i++){			offset <<= 7; // only shift by 7 as MSB is unused.			offset += FAT_buffer[i];		}      		offset += 10; //include length of header		//UART_Printfu32(offset);PRINT("B");EOL();      // offset is now equal to the the length of the TAG      ID3index = 10; // skip header      switch (FAT_buffer[3])      {         case 2:            // TODO: Skip extended header            // Search for song / artist frame and replace long filename with them            while ((ID3index < offset) && (ID3index < 512))            {               if (FAT_buffer[ID3index] == 'T' &&                   FAT_buffer[ID3index+1] == 'T' &&                   FAT_buffer[ID3index+2] == '2')               {                  //PRINT(" SONG");                            // For some reason, all strings have a Null at the front of the                   // name - I think this indicates the encoding type - we'll assume                  // its always 00, string! (this is what the -1 is for)                  // other -1 is for the null on the end of the string                  for (i=0; (i<FAT_buffer[ID3index+5]-1) && (i<LFN_TITLE_LENGTH-1); i++)                  {                     FAT16_longfilename[LFN_TITLE_INDEX+i] = FAT_buffer[ID3index+7+i];                   }                  FAT16_longfilename[LFN_TITLE_INDEX+i] = '\0';                  // Recalculate string length / add a blank                  FAT16_LFNTitleLen = strLen(FAT16_longfilename+LFN_TITLE_INDEX);                  // Indicate TITLE found                  FAT16_longfilename[LFN_TYPE_INDEX] |= LFN_TYPE_ID3_TITLE;               }               else if (FAT_buffer[ID3index] == 'T' &&                        FAT_buffer[ID3index+1] == 'P' &&                        FAT_buffer[ID3index+2] == '1')               {                  //PRINT(" ARTIST");                  for (i=0; (i<FAT_buffer[ID3index+5]-1) && (i<LFN_ARTIST_LENGTH-1); i++)                  {                     FAT16_longfilename[LFN_ARTIST_INDEX+i] = FAT_buffer[ID3index+7+i];                   }                  FAT16_longfilename[LFN_ARTIST_INDEX+i] = '\0';                  // Recalculate string length / add a blank                  FAT16_LFNArtistLen = strLen(FAT16_longfilename+LFN_ARTIST_INDEX);                  // Indicate ARTIST found                  FAT16_longfilename[LFN_TYPE_INDEX] |= LFN_TYPE_ID3_ARTIST;               }                              // Skip to next ID3 'frame'               ID3index += FAT_buffer[ID3index+5] + 6;               //PRINT(" I:");               //UART_Printfu08(ID3index);            }            break;                     case 3:         case 4:            // TODO: Skip extended header            // Search for song / artist frame and replace long filename with them            while ((ID3index < offset) && (ID3index < 512))            {               if (FAT_buffer[ID3index] == 'T' &&                   FAT_buffer[ID3index+1] == 'I' &&                   FAT_buffer[ID3index+2] == 'T' &&                   FAT_buffer[ID3index+3] == '2')               {                  //PRINT(" SONG");                            // For some reason, all strings have a Null at the front of the                   // name - I think this indicates the encoding type - we'll assume                  // its always 00, string! (this is what the -1 is for)                  // other -1 is for the null on the end of the string                  for (i=0; (i<FAT_buffer[ID3index+7]-1) && (i<LFN_TITLE_LENGTH-1); i++)                  {                     FAT16_longfilename[LFN_TITLE_INDEX+i] = FAT_buffer[ID3index+11+i];                   }                  FAT16_longfilename[LFN_TITLE_INDEX+i] = '\0';                  // Recalculate string length / add a blank                  FAT16_LFNTitleLen = strLen(FAT16_longfilename+LFN_TITLE_INDEX);                  // Indicate TITLE found                  FAT16_longfilename[LFN_TYPE_INDEX] |= LFN_TYPE_ID3_TITLE;               }               else if (FAT_buffer[ID3index] == 'T' &&                        FAT_buffer[ID3index+1] == 'P' &&                        FAT_buffer[ID3index+2] == 'E' &&                        FAT_buffer[ID3index+3] == '1')               {                  //PRINT(" ARTIST");                  for (i=0; (i<FAT_buffer[ID3index+7]-1) && (i<LFN_ARTIST_LENGTH-1); i++)                  {                     FAT16_longfilename[LFN_ARTIST_INDEX+i] = FAT_buffer[ID3index+11+i];                   }                  FAT16_longfilename[LFN_ARTIST_INDEX+i] = '\0';                  // Recalculate string length / add a blank                  FAT16_LFNArtistLen = strLen(FAT16_longfilename+LFN_ARTIST_INDEX);                  // Indicate ARTIST found                  FAT16_longfilename[LFN_TYPE_INDEX] |= LFN_TYPE_ID3_ARTIST;               }                              // Skip to next ID3 'frame'               ID3index += FAT_buffer[ID3index+7] + 10;               //PRINT(" I:");               //UART_Printfu08(ID3index);            }            break;         default:            ; // Unknown version, do nothing      }         		// skip through to end of ID3 		ID3_clusters = (offset/512) / FAT16_sectors_per_cluster;		ID3_sectors = (offset/512) % FAT16_sectors_per_cluster;		ID3_Bytes = offset % 512;				//UART_Printfu32(ID3_clusters);EOL();		//UART_Printfu16(ID3_sectors);EOL();		//UART_Printfu16(ID3_Bytes);EOL();				// find first cluster of actual song		for (j=0;j<ID3_clusters;j++){						nextCluster = FAT_NextCluster(gSector);		//	UART_Printfu16(j);UART_SendByte(0x09);		//	UART_Printfu32(nextCluster);EOL();			if (nextCluster == 0xffffffff){				gFile_good = FALSE;				PRINT("EOF: ID3 Tag @ Clstr ");				UART_Printfu32(gSector);				EOL();				return;			}			gSector = nextCluster;		}				// Adjust variables for new position.		gFileSectorsPlayed = (offset / 512);		buff_pos = (ID3_Bytes/32);		cluster_pos = ID3_sectors;		/*		while(FAT_readCluster(gSector,cluster_pos)){			MMC_Reset();			}		*/					/*PRINT("ID3 Tag skipped. First ten bytes of MP3 stream are ... ");		for (i=0;i<10;i++){			UART_Printfu08(FAT_buffer[i+ID3_Bytes]);		}		EOL();		*/		j=ID3_Bytes % 32;				//UART_Printfu08(j); PRINT(" bytes offset");EOL();				if (j){//we need to play(skip;) some bytes			buff_pos++;		}	}else{ //dump  first 10 bytes of file		/*		for (i=0;i<10;i++){			UART_Printfu08(FAT_buffer[i]);		}		EOL();		 */	}		// send params to vs1001k	flush_decoder(SOFT_RESET); 			//PRINT("Fle Q'd");EOL();		//reset play timer...	gPlayTimeSeconds = 0;	return;}/***	Data Streaming*	@return none**	Take care of feeding data to mp3 chip and reading *	mmc in free time. AKA playing.**/   void 	streaming (void){//	uint16 c;	uint8	abort=0;//	uint16 tmp=0;//	uint16 state[2];	uint32 nextfile;		if (!gFile_good) return; // only run if we have a valid file.		if (buff_pos>15) { // we need to get a new sector		gFileSectorsPlayed++;		if (cluster_pos>FAT16_sectors_per_cluster-1){ // need new cluster			cluster_pos=0;			gSector = FAT_NextCluster(gSector);		}				if (gSector == 0xffffffff) { // finished song. on to next ?						// Stop all playing and reset everything			gFile_good = FALSE;			gMode = MODE_STOP;			MMC_Reset();			flush_decoder(SOFT_RESET);						if (PLAY_NEXT_SONG){ // cue up next song and play it.				nextfile = FAT_getNextSong(gFAT_entry,FAT16_dir_first_cluster);				if(nextfile){// play next song					gFAT_entry = nextfile;					gLCD_Refresh=TRUE;					cue_file();					gMode = MODE_PLAY;				}else if(PLAY_REPEAT_ALL){					gFAT_entry = FAT16_entryMIN;					gLCD_Refresh=TRUE;					cue_file();					gMode = MODE_PLAY;				}			}			return;		}				// read sector from MMC up to 255 times		while (FAT_readCluster(gSector,cluster_pos)&& (--abort)){			MMC_Reset(); // reset if read failed.		}

⌨️ 快捷键说明

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