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

📄 wavfile.cpp

📁 这个是symbian下的一个蛮庞大的3D游戏源代码!对于学习3D开发的人有很大的帮助!
💻 CPP
字号:
#include "WavFile.h"
#include <io/IOException.h>
#include <io/FileInputStream.h>
#include <lang/Array.h>
#include <string.h>
#include <stdint.h>
#include <config.h>


using namespace io;
using namespace lang;


WavFile::WavFile( const String& name ) :
	m_in( 0 ),
	m_format(0,0,0),
	m_begin(0),
	m_end(0),
	m_size(0)
{
	m_in = new FileInputStream( name.c_str() );
	InputStream& in = *m_in;

	// find RIFF chunk
	RIFFChunk ckRIFF;
	findChunk( &in, m_size, in.available(), "RIFF", &ckRIFF );
	int endRIFF = m_size + ckRIFF.size;

	// form WAVE
	char formWAVE[4];
	m_size += m_in->read( formWAVE, 4 );
	if ( memcmp(formWAVE,"WAVE",4) )
		throwError( IOException( Format("Failed to read WAVE form from {0}", name) ) );

	// find fmt chunk
	RIFFChunk ckfmt;
	findChunk( &in, m_size, endRIFF, "fmt ", &ckfmt );
	int endfmt = m_size + ckfmt.size;

	// read fmt data
	Array<uint8_t> fmtdata;
	fmtdata.resize( ckfmt.size );
	m_size += m_in->read( fmtdata.begin(), ckfmt.size );
	const WaveFormatHeader* wavefmt = reinterpret_cast<const WaveFormatHeader*>( fmtdata.begin() );
	m_format = SoundFormat( wavefmt->samplesPerSec, wavefmt->avgBytesPerSec / wavefmt->samplesPerSec / wavefmt->channels * 8, wavefmt->channels );

	// leave fmt chunk
	in.skip( endfmt-m_size );

	// find data chunk
	RIFFChunk ckdata;
	findChunk( &in, m_size, endRIFF, "data", &ckdata );
	m_begin = m_size;
	m_end = m_size + ckdata.size;
}

WavFile::~WavFile()
{
}

void WavFile::read( void* data, int bytes ) 
{
	assert( bytes <= size() );

	if ( bytes != m_in->read( data, bytes ) )
		throwError( Exception( Format("Failed to read WAV file {0} data", m_in->toString()) ) );
	m_size += bytes;

	assert( size() >= 0 );
}

int WavFile::size() const 
{
	return m_end - m_size;
}

const SoundFormat& WavFile::format() const 
{
	return m_format;
}

void WavFile::findChunk( InputStream* in, int& size, int end, const char* id, RIFFChunk* chunk )
{
	while ( size < end )
	{
		int bytes = in->read( chunk, sizeof(*chunk) );
		if ( bytes != sizeof(*chunk) )
			throwError( IOException( Format("Failed to read {0} chunk from {1}", id, in->toString()) ) );

		if ( !memcmp(id,chunk->id,4) )
			break;
		
		in->skip( chunk->size );
	}

	if ( memcmp(id,chunk->id,4) )
		throwError( IOException( Format("Failed to read {0} chunk from {1}", id, in->toString()) ) );
}

// End of File

⌨️ 快捷键说明

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