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

📄 thefile.cpp

📁 完整的MP3播放器源码
💻 CPP
字号:
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "TheFIle.h"
#include "Datatables.h"



//
//	defines (reflect the bytes in mirror)
//
#define ROTATE(dwdw) (((dwdw) << 24) | (((dwdw) << 8) & 0x00ff0000) | \
					 (((dwdw) >> 8) & 0x0000ff00) | ((dwdw) >> 24))

/////////////////////////////////////////////////////////////////////////////
// CTheFIle//Marius C
BOOL CTheFIle::Open(LPCSTR fileName)
{
    
	m_file = CreateFile(fileName,
                        GENERIC_READ,
                        0,
                        NULL,
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,(void*)0);
    if(INVALID_HANDLE_VALUE == m_file)
        return FALSE;
	Reset();
	m_nReades		= 0;
	m_nFrameLen     = 0;
	m_firstFrame	= 0;
    m_closeIt       = FALSE;
	
	DWORD     bytes   = 0,a,b,c,d;

	// read the prop of the file at this point Marius C
	GetFrame(&bytes,&a,&b,&c,&d); //read props

    SetFilePointer(m_file,0,0,FILE_BEGIN);
	Reset();
	m_filePtr = 0;
	    
    
	return TRUE;//(m_psHDR->eMode != 3);
}

//
//	//Marius C
//
BOOL		CTheFIle::ParseFileAttributes(int nReads)
{
    if (nReads == 0) 
	{
		if(m_psHDR->eFreq == 3)
        {
             return FALSE;
        }
        DWORD temp	= m_HdrSignature & 0xFFF80CC0;
		m_dwSinc	=  ROTATE(temp & 0xFFFFFF3F);
		m_bSingleChMode = ((temp & 0x000000C0) == 0x000000C0);
    } 

	m_psHDR->LayerNo    = 4 - m_psHDR->LayerNo;
    if (m_psHDR->eMode == JST)
       m_intStereoBound = (m_psHDR->modeExt << 2) + 4;
    else
        m_intStereoBound = 0;  // should never be used

    //
	//	comp subbands
	//
    if (m_psHDR->LayerNo == 1)
	{
       m_nSubBands = 32;
	}
    else
    {
        int chBitRate = m_psHDR->bitRateIdx;
        //
        // comp rate of bites / channel:
		//
        if (m_psHDR->eMode != MONO)
           if (chBitRate == 4)
				chBitRate = 1;
           else
	            chBitRate -= 4;

        if ((chBitRate == 1) || (chBitRate == 2))
	       if (m_psHDR->eFreq == E32000)
				m_nSubBands = 12;
			else
				m_nSubBands = 8;
	    else
	        if ((m_psHDR->eFreq == E48000) || ((chBitRate >= 3) && (chBitRate <= 5)))
				m_nSubBands = 27;
			else
				m_nSubBands = 30;
	}

    if(m_intStereoBound > m_nSubBands)
		m_intStereoBound = m_nSubBands;
   
    //
    // comp frame length
	//
    int frLen  = ComputeFrameSize();

	DWORD nBytes;
	if(!::ReadFile(m_file,m_pBuffer,frLen,&nBytes,0))
		return FALSE;

	if(nBytes < (DWORD)frLen)
		return FALSE;

	m_nFrameLen = frLen;

	//
	//	divide by 4 because we walk on the dword pointer
	//
	
	DWORD   dwT;
	DWORD*	pDw = (DWORD*)m_pBuffer + m_nFrameLen/4 + 1;

	while(pDw > m_pdwWalk)
	{
		pDw--;
		dwT = *pDw;
		*pDw = ROTATE(dwT);
	}
	
	
	m_nBitsOffset	= 0;
	//m_offsetBytes   = 0;
	m_bCanRead      =  TRUE;

    if (!m_psHDR->protect)
    {
        DWORD checksum =    GetBits(16);
        AddBits(m_HdrSignature, 16);
    }
    return TRUE;
}



//
//	MC
//
void CTheFIle::Close()
{
//    TRACE("CTheFIle::Close()\r\n");
    if(m_file > 0)
    {
        CloseHandle(m_file);
        m_closeIt = TRUE;
        m_file = 0;
    }
    
}


//
//	MC
//

DWORD CTheFIle::GetSlots()
{
	return m_nSlots;
}

DWORD CTheFIle::GetModeExtension()
{
	return m_psHDR->modeExt;
}


//
//	MC
//
DWORD CTheFIle::GetFreqIndex()
{
	return m_psHDR->eFreq;
}

//
//	MC
//
DWORD CTheFIle::GetVersion()
{
	return m_psHDR->eVersion;

	
}

//
//	MC
//
DWORD CTheFIle::GetLayer()
{
	return m_psHDR->LayerNo;

	
}


//
//	MC
//
DWORD CTheFIle::GetMode()
{
	return m_psHDR->eMode;

	
}


//
//	MC
//
DWORD CTheFIle::GetFrequency()
{
	return MP3File_Cppfrequencies[m_psHDR->eVersion][m_psHDR->eFreq];

	
}

//
//	MC
//
DWORD CTheFIle::GetSubBands()
{
	return m_nSubBands;

	
}

//
//	MC
//
DWORD CTheFIle::GetIntensityStereoBounds()
{
	return m_intStereoBound;

	
}


//
//	MC
//
DWORD CTheFIle::GetTimePerFrame()
{
	return (DWORD)MP3File_MsPerFrame[m_psHDR->LayerNo-1][m_psHDR->eFreq];

	
}


//
//	MC
//
DWORD CTheFIle::GetMilisecPerFrame()
{
	return (DWORD)MP3File_MsPerFrame[m_psHDR->LayerNo-1][m_psHDR->eFreq];

	
}

//
//	MC
//
DWORD CTheFIle::GetDuration()
{
    
	return (DWORD)GetMaxFrames() * 
            (DWORD)MP3File_MsPerFrame[m_psHDR->LayerNo-1][m_psHDR->eFreq];

	
}

//
//	Marius C
//
BYTE*	CTheFIle::GetFrame(DWORD* pnBytes,DWORD* version,DWORD* mode,DWORD* modeEx,DWORD* frqIdx)
{
	
	DWORD*	pdwPointer	PURE;
	DWORD	nOfset		PURE;
	DWORD	checkVal	PURE;
    BOOL    bRetVal	    PURE; 
	DWORD	localDw		PURE;
	DWORD	nBytes		PURE;

	// TODO: Add your implementation code here
	if(TRUE == m_closeIt )
    {
		CloseHandle(m_file);
        return 0;
    }
    
	// search for the signature
	do {
		
		if(!::ReadFile(m_file,&localDw,sizeof(DWORD),&nBytes,0))
			break;
		if(nBytes < 4)
			break;

		if(m_nReades == 0 )			
		{
			if(((localDw) & 0x0000F0FF)== 0x0000F0FF)
			{
                bRetVal = TRUE;
				break;
			}
		}
		else
		{
			checkVal = (((localDw) & 0x000CF8FF) == m_dwSinc);
			BOOL bTemp = (((localDw) & 0xC0000000) == 0xC0000000);
			if(checkVal && (bTemp == m_bSingleChMode))
			{
                bRetVal = TRUE;
				break;
			}
		}
	}while(::SetFilePointer(m_file, -3, NULL, FILE_CURRENT));

    if(!bRetVal)
        return 0;
         
    m_frmStartOffset = nOfset;
    m_HdrSignature  = ROTATE(localDw);
    //	update the pointer

    if(pnBytes)
    {
        if (!m_psHDR->protect)
        {
	        AddBits(m_HdrSignature, 16);
        }

        //	extract from the header information for next frame
        if(!ParseFileAttributes(m_nReades))
		{
            return 0;
		}
        m_nReades++; //VARIANT
        *pnBytes  = m_nSlots;
    }

    *version = m_psHDR->eVersion;
    *mode    = m_psHDR->eMode;
    *modeEx  = m_psHDR->modeExt;
    *frqIdx  = m_psHDR->eFreq;
	

	return m_pBuffer;
}

//Marius C
DWORD CTheFIle::GetBits(int nBits)
{

	DWORD	dwRetVal	PURE;										
	DWORD	dwValue  = *m_pdwWalk;						// extract the pointed value
    DWORD	nSumBits = m_nBitsOffset + nBits;			// update the bits index
	DWORD	dwMask   = 0xFFFFFFFF >> (32 - nBits );		// build the mask
    
    if (nSumBits <= 32)
    {
        dwRetVal = (dwValue >> (32 - nSumBits)) & dwMask;
        if ((m_nBitsOffset += nBits) == 32)
        {
            m_nBitsOffset = 0;
            m_pdwWalk++; 
			//m_pbyWalk+=4;
        }
    }
    else
	{	
		dwRetVal = MAKELONG(HIWORD(*(m_pdwWalk+1)),LOWORD(*(m_pdwWalk)));
		//dwRetVal = MAKELONG(LOWORD(*(m_pdwWalk)),HIWORD(*(m_pdwWalk+1)));
		m_pdwWalk++;
		//m_pbyWalk+=4;

		m_nBitsOffset	= nSumBits - 32;
		dwRetVal >>= 48 - nSumBits; 
		dwRetVal &= dwMask;
	}
   
    return  dwRetVal;
	
}

//Marius C
DWORD CTheFIle::Goto( DWORD newPos)
{
	if(GetLength() < newPos)
		return 0;
	
    long newCalcPos = (newPos-=BF_SIZE);

    if(newCalcPos < 0)
    {
        newCalcPos = 0;
        m_nReades  = 0;    
    }
    else
    {
        m_nReades  = 1;    
    }
    
	int moduloFrame = newPos % m_nFrameLen;
	if(moduloFrame)
		newCalcPos = newPos -(moduloFrame+8);
	if(0 == newCalcPos)
	newCalcPos = 0;
	// we can get wrong to the same signature so at leat pre fit the ptr MCO
	m_filePtr = newCalcPos;
    SetFilePointer(m_file,newCalcPos, 0,FILE_BEGIN);

    return newCalcPos;
}

void CTheFIle::GetFrames(DWORD* minFrm, DWORD *maxFrrm)
{
	*maxFrrm  = (DWORD)GetMaxFrames();
    *minFrm   = (DWORD)GetMinFrames();
}

DWORD CTheFIle::GetLength()
{
    DWORD dwl = 0;
    return GetFileSize(m_file,&dwl);
}

DWORD CTheFIle::GetFrameLength()
{
    return m_nFrameLen;
}

//Marius C
DWORD CTheFIle::GetRealNrOfFrames()
{

	SetFilePointer(m_file,0,0,FILE_BEGIN);
	m_filePtr = 0;
    
    DWORD	dwfr = 0,a,b,c,d;
	DWORD		bytes=1;
    while(bytes)
    {
        bytes = 0;
        GetFrame(&bytes,&a,&b,&c,&d);
        dwfr++;
    }
	m_maby = FALSE;
    Goto(0);
	return dwfr;
}


DWORD CTheFIle::GetSpeed()
{
    return MP3File_Cpp[(int)m_psHDR->eVersion][(m_psHDR->LayerNo-1)][m_psHDR->bitRateIdx];
}

int			CTheFIle::ComputeFrameSize()
{
	int nLocalVar = -1;

	nLocalVar = (144  * MP3File_Cpp[(int)m_psHDR->eVersion][(m_psHDR->LayerNo-1)][m_psHDR->bitRateIdx])/
					    MP3File_Cppfrequencies[(int)m_psHDR->eVersion][(int)m_psHDR->eFreq];
	
	
	if (MPEG2_LSF == m_psHDR->eVersion)
         nLocalVar >>= 1;
	if(m_psHDR->padding)
		 nLocalVar++;
	if(3 == m_psHDR->LayerNo)
	{
		int temp1;
		int temp2 = (m_psHDR->protect ? 0 : 2);

		if(m_psHDR->eVersion == MPEG1)
		{
			temp1 = (m_psHDR->eMode == MONO ? 17: 32 ) - temp2;
			m_nSlots = nLocalVar - temp1 - 4;
		}
		else
		{
			temp1 = (m_psHDR->eMode == MONO ? 9 : 17 )- temp2;
			m_nSlots = nLocalVar - temp2 - 4;
		}
	}
	else
	{
		m_nSlots = 0;
	}
	nLocalVar-=4;	// get the header bexause we skip it
	return nLocalVar;

}

//Marius C
void CTheFIle::Reset()
{
	//m_offsetBytes	= 0;
    m_frmStartOffset = 0;
  	m_nBitsOffset   = 0;
	m_bytesRead     = 0;
	m_pdwWalk       = (DWORD*)m_pBuffer;
	//m_pbyWalk		= m_pBuffer;	
 	m_bCanRead      = TRUE;
	
}

⌨️ 快捷键说明

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