Click here to Skip to main content
15,896,359 members
Articles / Desktop Programming / MFC

MPEG Audio Frame Header

Rate me:
Please Sign up or sign in to vote.
4.89/5 (95 votes)
12 Apr 2007LGPL317 min read 902.1K   20.8K   245  
An article about the MPEG audio frame header.
#include "StdAfx.h"
#include ".\musicmatchtag.h"

#include <math.h> // for floor

// information from documentation "musicmatch.txt" in id3lib's doc folder

CMusicMatchTag* CMusicMatchTag::FindTag(CMPAStream* pStream, bool bAppended, DWORD dwBegin, DWORD dwEnd)
{
	// check for footer at the end of file
	DWORD dwOffset = dwEnd - 48;
	BYTE* pBuffer = pStream->ReadBytes(32, dwOffset, true);
	if (memcmp("Brava Software Inc.             ", pBuffer, 32)==0)
		return new CMusicMatchTag(pStream, dwOffset);
	return NULL;
}


CMusicMatchTag::CMusicMatchTag(CMPAStream* pStream, DWORD dwOffset) :
	CTag(pStream, _T("MM"), true, dwOffset)
{
	// get version info (from footer)
	char szVersion[5];
	memcpy(szVersion, (char*)pStream->ReadBytes(4, dwOffset, false, true), 4);
	szVersion[4] = '\0';

	m_fVersion = (float)atof(szVersion);
	
	// data offsets in the 20 bytes before footer
	dwOffset -= 52;
	DWORD dwImgExtOffset = pStream->ReadLEValue(4, dwOffset);
	dwOffset += 4;
	//DWORD dwImgOffset = pStream->ReadLEValue(4, dwOffset);

	// next 4 bytes unused
	dwOffset += 4;
	DWORD dwVersionOffset = pStream->ReadLEValue(4, dwOffset);

	dwOffset += 4;
//	DWORD dwMetadataOffset = pStream->ReadLEValue(4, dwOffset);

	DWORD dwMetadataSize = 0;
	BYTE* pBuffer;

	if (m_fVersion > 3.00)
	{
		// three possible lengths for metadata section: 7936, 8004, and 8132 bytes
		DWORD dwPossSizes[3] = {7936, 8004, 8132};
		DWORD dwSyncOffset;
		
		for (int nIndex = 0; nIndex < 3; nIndex++)
		{
			// check for sync in version section
			dwSyncOffset = dwOffset - 20 - dwPossSizes[nIndex] - 256;
			pBuffer = pStream->ReadBytes(8, dwSyncOffset, false, true);

			if (memcmp("18273645", pBuffer, 8) == 0)
			{
				dwMetadataSize = dwPossSizes[nIndex];
				break;
			}
		}
	}
	else
		dwMetadataSize = 7868;	// fixed size up to version 3

	if (dwMetadataSize == 0)	// could not find correct metadata size
		throw NULL;

	// total size = footer + data offsets + metadata + version + img (incl. unused)
	m_dwSize = 48 + 20 + dwMetadataSize + 256 + (dwVersionOffset - dwImgExtOffset);

	// is header there?
	dwOffset -= (m_dwSize - 48) + 256;
	pBuffer = pStream->ReadBytes(8, dwOffset, false);
	if (memcmp("18273645", pBuffer, 8) == 0)
	{
		m_dwSize += 256;	// header exists
		m_dwOffset = dwOffset;
	}
	else
		m_dwOffset = dwOffset + 256;
}

CMusicMatchTag::~CMusicMatchTag(void)
{
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Web Developer
Germany Germany
Author of the shareware WinCD.

Comments and Discussions