📄 snd_wave_mixer.cpp
字号:
//=========== (C) Copyright 1999 Valve, L.L.C. All rights reserved. ===========
//
// The copyright to the contents herein is the property of Valve, L.L.C.
// The contents may be used and/or copied only with the written permission of
// Valve, L.L.C., or in accordance with the terms and conditions stipulated in
// the agreement/contract under which the contents have been supplied.
//
// Purpose:
//
// $Workfile: $
// $Date: $
//
//-----------------------------------------------------------------------------
// $Log: $
//
// $NoKeywords: $
//=============================================================================
#include <stdio.h>
#include <windows.h>
#include "snd_audio_source.h"
#include "snd_wave_source.h"
#include "snd_wave_mixer_private.h"
#include "snd_wave_mixer_adpcm.h"
#include "ifaceposersound.h"
#include "viewersettings.h"
#include "AudioWaveOutput.h"
typedef struct channel_s
{
int leftvol;
int rightvol;
int rleftvol;
int rrightvol;
float pitch;
} channel_t;
//-----------------------------------------------------------------------------
// These mixers provide an abstraction layer between the audio device and
// mixing/decoding code. They allow data to be decoded and mixed using
// optimized, format sensitive code by calling back into the device that
// controls them.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Purpose: maps mixing to 8-bit mono mixer
//-----------------------------------------------------------------------------
class CAudioMixerWave8Mono : public CAudioMixerWave
{
public:
CAudioMixerWave8Mono( CWaveData *data ) : CAudioMixerWave( data ) {}
virtual void Mix( IAudioDevice *pDevice, channel_t *pChannel, void *pData, int outputOffset, int inputOffset, fixedint fracRate, int outCount, int timecompress, bool forward = true )
{
pDevice->Mix8Mono( pChannel, (char *)pData, outputOffset, inputOffset, fracRate, outCount, timecompress, forward );
}
};
//-----------------------------------------------------------------------------
// Purpose: maps mixing to 8-bit stereo mixer
//-----------------------------------------------------------------------------
class CAudioMixerWave8Stereo : public CAudioMixerWave
{
public:
CAudioMixerWave8Stereo( CWaveData *data ) : CAudioMixerWave( data ) {}
virtual void Mix( IAudioDevice *pDevice, channel_t *pChannel, void *pData, int outputOffset, int inputOffset, fixedint fracRate, int outCount, int timecompress, bool forward = true )
{
pDevice->Mix8Stereo( pChannel, (char *)pData, outputOffset, inputOffset, fracRate, outCount, timecompress, forward );
}
};
//-----------------------------------------------------------------------------
// Purpose: maps mixing to 16-bit mono mixer
//-----------------------------------------------------------------------------
class CAudioMixerWave16Mono : public CAudioMixerWave
{
public:
CAudioMixerWave16Mono( CWaveData *data ) : CAudioMixerWave( data ) {}
virtual void Mix( IAudioDevice *pDevice, channel_t *pChannel, void *pData, int outputOffset, int inputOffset, fixedint fracRate, int outCount, int timecompress, bool forward = true )
{
pDevice->Mix16Mono( pChannel, (short *)pData, outputOffset, inputOffset, fracRate, outCount, timecompress, forward );
}
};
//-----------------------------------------------------------------------------
// Purpose: maps mixing to 16-bit stereo mixer
//-----------------------------------------------------------------------------
class CAudioMixerWave16Stereo : public CAudioMixerWave
{
public:
CAudioMixerWave16Stereo( CWaveData *data ) : CAudioMixerWave( data ) {}
virtual void Mix( IAudioDevice *pDevice, channel_t *pChannel, void *pData, int outputOffset, int inputOffset, fixedint fracRate, int outCount, int timecompress, bool forward = true )
{
pDevice->Mix16Stereo( pChannel, (short *)pData, outputOffset, inputOffset, fracRate, outCount, timecompress, forward );
}
};
#define WAVE_FORMAT_PCM 1
#define WAVE_FORMAT_ADPCM 2
//-----------------------------------------------------------------------------
// Purpose: Create an approprite mixer type given the data format
// Input : *data - data access abstraction
// format - pcm or adpcm (1 or 2 -- RIFF format)
// channels - number of audio channels (1 = mono, 2 = stereo)
// bits - bits per sample
// Output : CAudioMixer * abstract mixer type that maps mixing to appropriate code
//-----------------------------------------------------------------------------
CAudioMixer *CreateWaveMixer( CWaveData *data, int format, int channels, int bits )
{
if ( format == WAVE_FORMAT_PCM )
{
if ( channels > 1 )
{
if ( bits == 8 )
return new CAudioMixerWave8Stereo( data );
else
return new CAudioMixerWave16Stereo( data );
}
else
{
if ( bits == 8 )
return new CAudioMixerWave8Mono( data );
else
return new CAudioMixerWave16Mono( data );
}
}
else if ( format == WAVE_FORMAT_ADPCM )
{
return CreateADPCMMixer( data );
}
return NULL;
}
#include "hlfaceposer.h"
//-----------------------------------------------------------------------------
// Purpose: Init the base WAVE mixer.
// Input : *data - data access object
//-----------------------------------------------------------------------------
CAudioMixerWave::CAudioMixerWave( CWaveData *data ) : m_pData(data)
{
m_loop = 0;
m_sample = 0;
m_absoluteSample = 0;
m_fracOffset = 0;
m_bActive = false;
m_nModelIndex = -1;
m_bForward = true;
m_bAutoDelete = true;
}
//-----------------------------------------------------------------------------
// Purpose: Frees the data access object (we own it after construction)
//-----------------------------------------------------------------------------
CAudioMixerWave::~CAudioMixerWave( void )
{
delete m_pData;
}
//-----------------------------------------------------------------------------
// Purpose: Decode and read the data
// by default we just pass the request on to the data access object
// other mixers may need to buffer or decode the data for some reason
//
// Input : **pData - dest pointer
// sampleCount - number of samples needed
// Output : number of samples available in this batch
//-----------------------------------------------------------------------------
int CAudioMixerWave::GetOutputData( void **pData, int samplePosition, int sampleCount, bool forward /*= true*/ )
{
if ( samplePosition != m_sample )
{
// Seek
m_sample = samplePosition;
m_absoluteSample = samplePosition;
}
return m_pData->ReadSourceData( pData, m_sample, sampleCount, forward );
}
//-----------------------------------------------------------------------------
// Purpose: calls through the wavedata to get the audio source
// Output : CAudioSource
//-----------------------------------------------------------------------------
CAudioSource *CAudioMixerWave::GetSource( void )
{
if ( m_pData )
return &m_pData->Source();
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Gets the current sample location in playback
// Output : int (samples from start of wave)
//-----------------------------------------------------------------------------
int CAudioMixerWave::GetSamplePosition( void )
{
return m_sample;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : position -
//-----------------------------------------------------------------------------
bool CAudioMixerWave::SetSamplePosition( int position )
{
position = max( 0, position );
m_sample = position;
m_absoluteSample = position;
m_startpos = m_sample;
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : position -
//-----------------------------------------------------------------------------
void CAudioMixerWave::SetLoopPosition( int position )
{
m_loop = position;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : int
//-----------------------------------------------------------------------------
int CAudioMixerWave::GetStartPosition( void )
{
return m_startpos;
}
bool CAudioMixerWave::GetActive( void )
{
return m_bActive;
}
void CAudioMixerWave::SetActive( bool active )
{
m_bActive = active;
}
void CAudioMixerWave::SetModelIndex( int index )
{
m_nModelIndex = index;
}
int CAudioMixerWave::GetModelIndex( void ) const
{
return m_nModelIndex;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -