示例#1
0
BOOL CWaveRecord::SaveRecordData2File(UINT uFileChannel, LPCTSTR lpszFileName)
{
	CString strErr;
	CString strFileName = lpszFileName;
	
	if(strFileName.Right(4) == _T(".mp3"))
	{
		m_bCompressMp3 = TRUE;

		if(m_format.wBitsPerSample == 8)
		{
			MyMessageBox(_T("Can not record 8 bits mp3"), eERR_ERROR);
			goto end;
		}

		if(!LoadMp3Dll())
		{
			MyMessageBox(_T("Load mp3 dll failed"), eERR_ERROR);
			goto end;
		}
	
		if(!PreEncodeMp3(strFileName, uFileChannel))
		{
			MyMessageBox(_T("Prepare encoding mp3 failed"), eERR_ERROR);
			goto end;
		}
	}
	else
	{
		m_bCompressMp3 = FALSE;
		if(!CreateWaveFile(strFileName, uFileChannel))
		{
			MyMessageBox(_T("Create wave file failed"), eERR_ERROR);
			goto end;
		}
	}
	
	return TRUE;
	
end:
	Release();
	return FALSE;
}
示例#2
0
CSoundFile::CSoundFile(LPCTSTR pszFileName, WAVEFORMATEX* format)
{
	m_hFile = NULL;
	_tcscpy(m_szFileName, pszFileName);

	ZeroMemory(&m_MMCKInfoParent,sizeof(MMCKINFO));
	ZeroMemory(&m_MMCKInfoChild,sizeof(MMCKINFO));
	ZeroMemory(&m_MMCKInfoData,sizeof(MMCKINFO));
	
	if(format == NULL)
	{
		m_Mode = READ;
		OpenWaveFile();
	}
	else
	{
		m_Mode = WRITE;
		m_Format = *format;
		CreateWaveFile();
	}
	
	if(m_Mode == ERROR) Close();
}
示例#3
0
BOOL CWavFile::Combine(CString strPathL, CString strPathR, CString strPathObj)
{
	int nPackNo = 0;
	int nPackSize = 0;
	int nDataSize = 0;
	const int nLen = 65536;
	BYTE byDataL[nLen * 2] = {0};
	BYTE byDataR[nLen * 2] = {0};
	BYTE byDataC[nLen * 4] = {0};
	HMMIO hWaveFile = NULL;
	LPTSTR pszFilePath = strdup(strPathObj);

	if(!m_hWaveFileL || !m_hWaveFileR)
	{
		return FALSE;
	}

	// They can combine or not
	nDataSize = IsCanCombine(strPathL, strPathR);
	if(nDataSize == -1)
	{
		return FALSE;
	}

	// Create an object wave file
	m_formatL.nChannels = 2;
	m_formatL.nBlockAlign *= 2;
	m_formatL.nAvgBytesPerSec *= 2;
	if(!CreateWaveFile(hWaveFile, pszFilePath, m_formatL))
	{
		MyMessageBox(_T("Create object file failed: ") + strPathObj, eERR_ERROR);
		return FALSE;	
	}

	// Combine them one packet by one
	nPackSize = nLen;
	nPackNo = (nDataSize + nLen - 1) / nLen;
	for(int i = 0; i < nPackNo; i++)
	{
		// Case of the last packet
		if(i == (nPackNo - 1))	
		{
			nPackSize = nDataSize - i * nLen;
		}

		// Read data from left channel
		int nSizeL = nPackSize;
		if(ReadDataFromFile(m_hWaveFileL, byDataL, nSizeL))
		{
			if(nSizeL != nPackSize)
			{
				memset(byDataL + nSizeL, 0, nPackSize - nSizeL);
			}
		}
		else
		{
			MyMessageBox(_T("Read sound data from L file failed"), eERR_ERROR);
		}
		
		// Read data from right channel
		int nSizeR = nPackSize;
		if(ReadDataFromFile(m_hWaveFileR, byDataR, nSizeR))
		{
			if(nSizeR != nPackSize)
			{
				memset(byDataR + nSizeR, 0, nPackSize - nSizeR);
			}
		}
		else
		{
			MyMessageBox(_T("Read sound data from R file failed"), eERR_ERROR);
		}

		// Re-combine left channel data and right channel data
		int nCoef = m_formatL.wBitsPerSample / 8;
		for(int i = 0; i < (nPackSize / nCoef); i++)
		{
			if(nCoef == 1)
			{
				byDataC[2 * i + 0] = byDataL[i];
				byDataC[2 * i + 1] = byDataR[i];
			}
			else
			{
				byDataC[4 * i + 0] = byDataL[2 * i];
				byDataC[4 * i + 2] = byDataR[2 * i];
				byDataC[4 * i + 1] = byDataL[2 * i + 1];
				byDataC[4 * i + 3] = byDataR[2 * i + 1];
			}
		}

		// Write to object file
		if(mmioWrite(hWaveFile, (char*)byDataC, nPackSize * 2) != nPackSize * 2)
		{
			MyMessageBox(_T("Write object file failed"), eERR_ERROR);
			return FALSE;
		}
	}

	// Call mmioAscend function to mark the end of the chunk
	// And mmioAscend will corrects the chunk size
	mmioAscend(hWaveFile, &m_mmckinfoParent, 0);
	mmioAscend(hWaveFile, &m_mmckinfoSubChunk, 0);
	mmioClose(hWaveFile, 0);
	mmioClose(m_hWaveFileL, 0);
	mmioClose(m_hWaveFileR, 0);
	
	return TRUE;
}