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

📄 snd_wave_source.cpp

📁 hl2 source code. Do not use it illegal.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include "riff.h"
#include "snd_wave_source.h"
#include "snd_wave_mixer_private.h"
#include "snd_audio_source.h"
#include <mmsystem.h>		// wave format
#include <mmreg.h>			// adpcm format
#include "hlfaceposer.h"
#include "FileSystem.h"
#include "utlbuffer.h"
#include "phonemeconverter.h"

//-----------------------------------------------------------------------------
// Purpose: Implements the RIFF i/o interface on stdio
//-----------------------------------------------------------------------------
class StdIOReadBinary : public IFileReadBinary
{
public:
	int open( const char *pFileName )
	{
		return (int)filesystem->Open( pFileName, "rb" );
	}

	int read( void *pOutput, int size, int file )
	{
		if ( !file )
			return 0;

		return filesystem->Read( pOutput, size, (FileHandle_t)file );
	}

	void seek( int file, int pos )
	{
		if ( !file )
			return;

		filesystem->Seek( (FileHandle_t)file, pos, FILESYSTEM_SEEK_HEAD );
	}

	unsigned int tell( int file )
	{
		if ( !file )
			return 0;

		return filesystem->Tell( (FileHandle_t)file );
	}

	unsigned int size( int file )
	{
		if ( !file )
			return 0;

		return filesystem->Size( (FileHandle_t)file );
	}

	void close( int file )
	{
		if ( !file )
			return;

		filesystem->Close( (FileHandle_t)file );
	}
};

static StdIOReadBinary io;

#define RIFF_WAVE			MAKEID('W','A','V','E')
#define WAVE_FMT			MAKEID('f','m','t',' ')
#define WAVE_DATA			MAKEID('d','a','t','a')
#define WAVE_FACT			MAKEID('f','a','c','t')
#define WAVE_CUE			MAKEID('c','u','e',' ')

void ChunkError( unsigned int id )
{
}

//-----------------------------------------------------------------------------
// Purpose: Init to empty wave
//-----------------------------------------------------------------------------
CAudioSourceWave::CAudioSourceWave( void )
{
	m_format = 0;
	m_pHeader = NULL;
	// no looping
	m_loopStart = -1;
	m_sampleSize = 1;
	m_sampleCount = 0;
}


CAudioSourceWave::~CAudioSourceWave( void )
{
	// for non-standard waves, we store a copy of the header in RAM
	delete[] m_pHeader;
	//	m_pWords points into m_pWordBuffer, no need to delete
}

//-----------------------------------------------------------------------------
// Purpose: Init the wave data.
// Input  : *pHeaderBuffer - the RIFF fmt chunk
//			headerSize - size of that chunk
//-----------------------------------------------------------------------------
void CAudioSourceWave::Init( const char *pHeaderBuffer, int headerSize )
{
	const WAVEFORMATEX *pHeader = (const WAVEFORMATEX *)pHeaderBuffer;

	// copy the relevant header data
	m_format = pHeader->wFormatTag;
	m_bits = pHeader->wBitsPerSample;
	m_rate = pHeader->nSamplesPerSec;
	m_channels = pHeader->nChannels;

	m_sampleSize = (m_bits * m_channels) / 8;
	
	// this can never be zero -- other functions divide by this. 
	// This should never happen, but avoid crashing
	if ( m_sampleSize <= 0 )
		m_sampleSize = 1;

	// For non-standard waves (like ADPCM) store the header, it has some useful data
	if ( m_format != WAVE_FORMAT_PCM )
	{
		m_pHeader = new char[headerSize];
		memcpy( m_pHeader, pHeader, headerSize );
		if ( m_format == WAVE_FORMAT_ADPCM )
		{
			// treat ADPCM sources as a file of bytes.  They are decoded by the mixer
			m_sampleSize = 1;
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
// Output : float
//-----------------------------------------------------------------------------
float CAudioSourceWave::TrueSampleSize( void )
{
	if ( m_format == WAVE_FORMAT_ADPCM )
	{
		return 0.5f;
	}
	return (float)m_sampleSize;
}

//-----------------------------------------------------------------------------
// Purpose: Total number of samples in this source
// Output : int
//-----------------------------------------------------------------------------
int CAudioSourceWave::SampleCount( void ) 
{
	if ( m_format == WAVE_FORMAT_ADPCM )
	{
		ADPCMWAVEFORMAT *pFormat = (ADPCMWAVEFORMAT *)m_pHeader;
		int blockSize = ((pFormat->wSamplesPerBlock - 2) * pFormat->wfx.nChannels ) / 2;
		blockSize += 7 * pFormat->wfx.nChannels;

		int blockCount = m_sampleCount / blockSize;
		int blockRem = m_sampleCount % blockSize;
		
		// total samples in complete blocks
		int sampleCount = blockCount * pFormat->wSamplesPerBlock;

		// add remaining in a short block
		if ( blockRem )
		{
			sampleCount += pFormat->wSamplesPerBlock - (((blockSize - blockRem) * 2) / m_channels);
		}
		return sampleCount;
	}
	return m_sampleCount; 
}

//-----------------------------------------------------------------------------
// Purpose: Do any sample conversion
//			For 8 bit PCM, convert to signed because the mixing routine assumes this
// Input  : *pData - pointer to sample data
//			sampleCount - number of samples
//-----------------------------------------------------------------------------
void CAudioSourceWave::ConvertSamples( char *pData, int sampleCount )
{
	if ( m_format == WAVE_FORMAT_PCM )
	{
		if ( m_bits == 8 )
		{
			for ( int i = 0; i < sampleCount; i++ )
			{
				for ( int j = 0; j < m_channels; j++ )
				{
					*pData = (unsigned char)((int)((unsigned)*pData) - 128);
					pData++;
				}
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : &walk - 
//-----------------------------------------------------------------------------
void CAudioSourceWave::ParseSentence( IterateRIFF &walk )
{
	CUtlBuffer buf( 0, 0, true );

	buf.EnsureCapacity( walk.ChunkSize() );
	walk.ChunkRead( buf.Base() );
	buf.SeekPut( CUtlBuffer::SEEK_HEAD, walk.ChunkSize() );

	m_Sentence.InitFromDataChunk( buf.Base(), buf.TellPut() );
}

//-----------------------------------------------------------------------------
// Purpose: Parse base chunks
// Input  : &walk - riff file to parse
//		  : chunkName - name of the chunk to parse
//-----------------------------------------------------------------------------
// UNDONE: Move parsing loop here and drop each chunk into a virtual function
//			instead of this being virtual.
void CAudioSourceWave::ParseChunk( IterateRIFF &walk, int chunkName )
{
	switch( chunkName )
	{
		case WAVE_CUE:
			{
				m_loopStart = ParseCueChunk( walk );
			}
			break;
		case WAVE_VALVEDATA:
			{
				ParseSentence( walk );
			}
			break;
			// unknown/don't care
		default:
			{
				ChunkError( walk.ChunkName() );
			}
			break;
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
// Output : CSentence
//-----------------------------------------------------------------------------
CSentence *CAudioSourceWave::GetSentence( void )
{
	return &m_Sentence;
}

//-----------------------------------------------------------------------------
// Purpose: Bastardized construction routine.  This is just to avoid complex
//			constructor functions so code can be shared more easily by sub-classes
// Input  : *pFormatBuffer - RIFF header
//			formatSize - header size
//			&walk - RIFF file
//-----------------------------------------------------------------------------
void CAudioSourceWave::Setup( const char *pFormatBuffer, int formatSize, IterateRIFF &walk )
{
	Init( pFormatBuffer, formatSize );

	while ( walk.ChunkAvailable() )
	{
		ParseChunk( walk, walk.ChunkName() );
		walk.ChunkNext();
	}
}

//-----------------------------------------------------------------------------
// Purpose: Wave file that is completely in memory
//			UNDONE: Implement Lock/Unlock and caching
//-----------------------------------------------------------------------------
class CAudioSourceMemWave : public CAudioSourceWave
{
public:
	CAudioSourceMemWave( void );
	~CAudioSourceMemWave( void );

	// Create an instance (mixer) of this audio source
	virtual CAudioMixer		*CreateMixer( void );

	virtual void			ParseChunk( IterateRIFF &walk, int chunkName );
	void					ParseDataChunk( IterateRIFF &walk );

	virtual int				GetOutputData( void **pData, int samplePosition, int sampleCount, bool forward = true );
	virtual float			GetRunningLength( void ) { return CAudioSourceWave::GetRunningLength(); };

private:

⌨️ 快捷键说明

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