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

📄 soundcontext.cpp

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


using namespace io;
using namespace lang;


SoundContext::SoundContext() : 
	m_samples(0)
{
}

void SoundContext::playSound( const char* basename, int channel )
{
#ifndef SOUNDS_DISABLED
	ASSERT( channel >= 0 && channel < CHANNEL_COUNT );
	m_channels[channel].sample = findSample( basename );
	m_channels[channel].pos = 0;
#endif
}

void SoundContext::stopSound( const char* basename )
{
#ifndef SOUNDS_DISABLED
	for ( int i = 0 ; i < CHANNEL_COUNT ; ++i )
		if ( m_channels[i].sample && !m_channels[i].sample->name.compareTo(basename) )
			m_channels[i].sample = 0;
#endif
}

void SoundContext::addSample( const char* filename, int flags )
{
#ifndef SOUNDS_DISABLED
	Sample& sample = m_samplelist[m_samples++];

	WavFile wav( filename );
	if ( wav.format().bitsPerSample() != 8 || wav.format().channels() != 1 || wav.format().samplesPerSec() != 16000 )
		throwError( io::IOException( Format("Invalid wave format: {0}", filename) ) );

	sample.data.resize( wav.size() );
	wav.read( &sample.data[0], sample.data.size() );

	sample.name = PathName(filename).basename();
	sample.flags = flags;
#endif
}

SoundContext::Sample* SoundContext::findSample( const char* basename )
{
#ifndef SOUNDS_DISABLED
	for ( int i = 0 ; i < m_samples ; ++i )
		if ( !m_samplelist[i].name.compareTo(basename) )
			return &m_samplelist[i];
#endif
	return 0;
}

void SoundContext::mix( int16_t* out, int size )
{
#ifndef SOUNDS_DISABLED
	memset( out, 0, size*2 );

	for ( int k = 0 ; k < CHANNEL_COUNT ; ++k )
	{
		Channel& chn = m_channels[k];
		Sample* s = chn.sample;
		int written = 0;

		while ( s != 0 )
		{
			Array<int8_t>& data = s->data;

			int c = size;
			if ( c > data.size()-chn.pos )
				c = data.size()-chn.pos;

			int src = chn.pos;
			for ( int i = 0 ; i < c ; ++i )
			{
				int x = int(out[i]) + (int(data[src+i])<<6);
				if ( x > 32767 )
					x = 32767;
				else if ( x < -32767 )
					x = -32767;
				out[i] = (int16_t)x;
			}

			written += c;
			chn.pos += c;
			
			if ( (s->flags & SOUND_LOOP) != 0 && written < size )
				chn.pos = 0;
			else
				s = 0;
		}
	}
#endif
}

// End of File

⌨️ 快捷键说明

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