Initial commit

This commit is contained in:
Brian Harris
2012-11-26 12:58:24 -06:00
parent a5214f79ef
commit 5016f605b8
1115 changed files with 587266 additions and 0 deletions

View File

@@ -0,0 +1,542 @@
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "../../idlib/precompiled.h"
#include "../snd_local.h"
#include "../../../doomclassic/doom/i_sound.h"
idCVar s_showLevelMeter( "s_showLevelMeter", "0", CVAR_BOOL|CVAR_ARCHIVE, "Show VU meter" );
idCVar s_meterTopTime( "s_meterTopTime", "1000", CVAR_INTEGER|CVAR_ARCHIVE, "How long (in milliseconds) peaks are displayed on the VU meter" );
idCVar s_meterPosition( "s_meterPosition", "100 100 20 200", CVAR_ARCHIVE, "VU meter location (x y w h)" );
idCVar s_device( "s_device", "-1", CVAR_INTEGER|CVAR_ARCHIVE, "Which audio device to use (listDevices to list, -1 for default)" );
idCVar s_showPerfData( "s_showPerfData", "0", CVAR_BOOL, "Show XAudio2 Performance data" );
extern idCVar s_volume_dB;
/*
========================
idSoundHardware_XAudio2::idSoundHardware_XAudio2
========================
*/
idSoundHardware_XAudio2::idSoundHardware_XAudio2() {
pXAudio2 = NULL;
pMasterVoice = NULL;
pSubmixVoice = NULL;
vuMeterRMS = NULL;
vuMeterPeak = NULL;
outputChannels = 0;
channelMask = 0;
voices.SetNum( 0 );
zombieVoices.SetNum( 0 );
freeVoices.SetNum( 0 );
lastResetTime = 0;
}
void listDevices_f( const idCmdArgs & args ) {
IXAudio2 * pXAudio2 = soundSystemLocal.hardware.GetIXAudio2();
if ( pXAudio2 == NULL ) {
idLib::Warning( "No xaudio object" );
return;
}
UINT32 deviceCount = 0;
if ( pXAudio2->GetDeviceCount( &deviceCount ) != S_OK || deviceCount == 0 ) {
idLib::Warning( "No audio devices found" );
return;
}
for ( unsigned int i = 0; i < deviceCount; i++ ) {
XAUDIO2_DEVICE_DETAILS deviceDetails;
if ( pXAudio2->GetDeviceDetails( i, &deviceDetails ) != S_OK ) {
continue;
}
idStaticList< const char *, 5 > roles;
if ( deviceDetails.Role & DefaultConsoleDevice ) {
roles.Append( "Console Device" );
}
if ( deviceDetails.Role & DefaultMultimediaDevice ) {
roles.Append( "Multimedia Device" );
}
if ( deviceDetails.Role & DefaultCommunicationsDevice ) {
roles.Append( "Communications Device" );
}
if ( deviceDetails.Role & DefaultGameDevice ) {
roles.Append( "Game Device" );
}
idStaticList< const char *, 11 > channelNames;
if ( deviceDetails.OutputFormat.dwChannelMask & SPEAKER_FRONT_LEFT ) {
channelNames.Append( "Front Left" );
}
if ( deviceDetails.OutputFormat.dwChannelMask & SPEAKER_FRONT_RIGHT ) {
channelNames.Append( "Front Right" );
}
if ( deviceDetails.OutputFormat.dwChannelMask & SPEAKER_FRONT_CENTER ) {
channelNames.Append( "Front Center" );
}
if ( deviceDetails.OutputFormat.dwChannelMask & SPEAKER_LOW_FREQUENCY ) {
channelNames.Append( "Low Frequency" );
}
if ( deviceDetails.OutputFormat.dwChannelMask & SPEAKER_BACK_LEFT ) {
channelNames.Append( "Back Left" );
}
if ( deviceDetails.OutputFormat.dwChannelMask & SPEAKER_BACK_RIGHT ) {
channelNames.Append( "Back Right" );
}
if ( deviceDetails.OutputFormat.dwChannelMask & SPEAKER_FRONT_LEFT_OF_CENTER ) {
channelNames.Append( "Front Left of Center" );
}
if ( deviceDetails.OutputFormat.dwChannelMask & SPEAKER_FRONT_RIGHT_OF_CENTER ) {
channelNames.Append( "Front Right of Center" );
}
if ( deviceDetails.OutputFormat.dwChannelMask & SPEAKER_BACK_CENTER ) {
channelNames.Append( "Back Center" );
}
if ( deviceDetails.OutputFormat.dwChannelMask & SPEAKER_SIDE_LEFT ) {
channelNames.Append( "Side Left" );
}
if ( deviceDetails.OutputFormat.dwChannelMask & SPEAKER_SIDE_RIGHT ) {
channelNames.Append( "Side Right" );
}
char mbcsDisplayName[ 256 ];
wcstombs( mbcsDisplayName, deviceDetails.DisplayName, sizeof( mbcsDisplayName ) );
idLib::Printf( "%3d: %s\n", i, mbcsDisplayName );
idLib::Printf( " %d channels, %d Hz\n", deviceDetails.OutputFormat.Format.nChannels, deviceDetails.OutputFormat.Format.nSamplesPerSec );
if ( channelNames.Num() != deviceDetails.OutputFormat.Format.nChannels ) {
idLib::Printf( S_COLOR_YELLOW "WARNING: " S_COLOR_RED "Mismatch between # of channels and channel mask\n" );
}
if ( channelNames.Num() == 1 ) {
idLib::Printf( " %s\n", channelNames[0] );
} else if ( channelNames.Num() == 2 ) {
idLib::Printf( " %s and %s\n", channelNames[0], channelNames[1] );
} else if ( channelNames.Num() > 2 ) {
idLib::Printf( " %s", channelNames[0] );
for ( int i = 1; i < channelNames.Num() - 1; i++ ) {
idLib::Printf( ", %s", channelNames[i] );
}
idLib::Printf( ", and %s\n", channelNames[channelNames.Num() - 1] );
}
if ( roles.Num() == 1 ) {
idLib::Printf( " Default %s\n", roles[0] );
} else if ( roles.Num() == 2 ) {
idLib::Printf( " Default %s and %s\n", roles[0], roles[1] );
} else if ( roles.Num() > 2 ) {
idLib::Printf( " Default %s", roles[0] );
for ( int i = 1; i < roles.Num() - 1; i++ ) {
idLib::Printf( ", %s", roles[i] );
}
idLib::Printf( ", and %s\n", roles[roles.Num() - 1] );
}
}
}
/*
========================
idSoundHardware_XAudio2::Init
========================
*/
void idSoundHardware_XAudio2::Init() {
cmdSystem->AddCommand( "listDevices", listDevices_f, 0, "Lists the connected sound devices", NULL );
DWORD xAudioCreateFlags = 0;
#ifdef _DEBUG
xAudioCreateFlags |= XAUDIO2_DEBUG_ENGINE;
#endif
XAUDIO2_PROCESSOR xAudioProcessor = XAUDIO2_DEFAULT_PROCESSOR;
if ( FAILED( XAudio2Create( &pXAudio2, xAudioCreateFlags, xAudioProcessor ) ) ) {
if ( xAudioCreateFlags & XAUDIO2_DEBUG_ENGINE ) {
// in case the debug engine isn't installed
xAudioCreateFlags &= ~XAUDIO2_DEBUG_ENGINE;
if ( FAILED( XAudio2Create( &pXAudio2, xAudioCreateFlags, xAudioProcessor ) ) ) {
idLib::FatalError( "Failed to create XAudio2 engine. Try installing the latest DirectX." );
return;
}
} else {
idLib::FatalError( "Failed to create XAudio2 engine. Try installing the latest DirectX." );
return;
}
}
#ifdef _DEBUG
XAUDIO2_DEBUG_CONFIGURATION debugConfiguration = { 0 };
debugConfiguration.TraceMask = XAUDIO2_LOG_WARNINGS;
debugConfiguration.BreakMask = XAUDIO2_LOG_ERRORS;
pXAudio2->SetDebugConfiguration( &debugConfiguration );
#endif
// Register the sound engine callback
pXAudio2->RegisterForCallbacks( &soundEngineCallback );
soundEngineCallback.hardware = this;
UINT32 deviceCount = 0;
if ( pXAudio2->GetDeviceCount( &deviceCount ) != S_OK || deviceCount == 0 ) {
idLib::Warning( "No audio devices found" );
pXAudio2->Release();
pXAudio2 = NULL;
return;
}
idCmdArgs args;
listDevices_f( args );
int preferredDevice = s_device.GetInteger();
if ( preferredDevice < 0 || preferredDevice >= (int)deviceCount ) {
int preferredChannels = 0;
for ( unsigned int i = 0; i < deviceCount; i++ ) {
XAUDIO2_DEVICE_DETAILS deviceDetails;
if ( pXAudio2->GetDeviceDetails( i, &deviceDetails ) != S_OK ) {
continue;
}
if ( deviceDetails.Role & DefaultGameDevice ) {
// if we find a device the user marked as their preferred 'game' device, then always use that
preferredDevice = i;
preferredChannels = deviceDetails.OutputFormat.Format.nChannels;
break;
}
if ( deviceDetails.OutputFormat.Format.nChannels > preferredChannels ) {
preferredDevice = i;
preferredChannels = deviceDetails.OutputFormat.Format.nChannels;
}
}
}
idLib::Printf( "Using device %d\n", preferredDevice );
XAUDIO2_DEVICE_DETAILS deviceDetails;
if ( pXAudio2->GetDeviceDetails( preferredDevice, &deviceDetails ) != S_OK ) {
// One way this could happen is if a device is removed between the loop and this line of code
// Highly unlikely but possible
idLib::Warning( "Failed to get device details" );
pXAudio2->Release();
pXAudio2 = NULL;
return;
}
DWORD outputSampleRate = 44100; // Max( (DWORD)XAUDIO2FX_REVERB_MIN_FRAMERATE, Min( (DWORD)XAUDIO2FX_REVERB_MAX_FRAMERATE, deviceDetails.OutputFormat.Format.nSamplesPerSec ) );
if ( FAILED( pXAudio2->CreateMasteringVoice( &pMasterVoice, XAUDIO2_DEFAULT_CHANNELS, outputSampleRate, 0, preferredDevice, NULL ) ) ) {
idLib::Warning( "Failed to create master voice" );
pXAudio2->Release();
pXAudio2 = NULL;
return;
}
pMasterVoice->SetVolume( DBtoLinear( s_volume_dB.GetFloat() ) );
outputChannels = deviceDetails.OutputFormat.Format.nChannels;
channelMask = deviceDetails.OutputFormat.dwChannelMask;
idSoundVoice::InitSurround( outputChannels, channelMask );
// ---------------------
// Initialize the Doom classic sound system.
// ---------------------
I_InitSoundHardware( outputChannels, channelMask );
// ---------------------
// Create VU Meter Effect
// ---------------------
IUnknown * vuMeter = NULL;
XAudio2CreateVolumeMeter( &vuMeter, 0 );
XAUDIO2_EFFECT_DESCRIPTOR descriptor;
descriptor.InitialState = true;
descriptor.OutputChannels = outputChannels;
descriptor.pEffect = vuMeter;
XAUDIO2_EFFECT_CHAIN chain;
chain.EffectCount = 1;
chain.pEffectDescriptors = &descriptor;
pMasterVoice->SetEffectChain( &chain );
vuMeter->Release();
// ---------------------
// Create VU Meter Graph
// ---------------------
vuMeterRMS = console->CreateGraph( outputChannels );
vuMeterPeak = console->CreateGraph( outputChannels );
vuMeterRMS->Enable( false );
vuMeterPeak->Enable( false );
memset( vuMeterPeakTimes, 0, sizeof( vuMeterPeakTimes ) );
vuMeterPeak->SetFillMode( idDebugGraph::GRAPH_LINE );
vuMeterPeak->SetBackgroundColor( idVec4( 0.0f, 0.0f, 0.0f, 0.0f ) );
vuMeterRMS->AddGridLine( 0.500f, idVec4( 0.5f, 0.5f, 0.5f, 1.0f ) );
vuMeterRMS->AddGridLine( 0.250f, idVec4( 0.5f, 0.5f, 0.5f, 1.0f ) );
vuMeterRMS->AddGridLine( 0.125f, idVec4( 0.5f, 0.5f, 0.5f, 1.0f ) );
const char * channelNames[] = { "L", "R", "C", "S", "Lb", "Rb", "Lf", "Rf", "Cb", "Ls", "Rs" };
for ( int i = 0, ci = 0; ci < sizeof( channelNames ) / sizeof( channelNames[0] ); ci++ ) {
if ( ( channelMask & BIT( ci ) ) == 0 ) {
continue;
}
vuMeterRMS->SetLabel( i, channelNames[ ci ] );
i++;
}
// ---------------------
// Create submix buffer
// ---------------------
if ( FAILED( pXAudio2->CreateSubmixVoice( &pSubmixVoice, 1, outputSampleRate, 0, 0, NULL, NULL ) ) ) {
idLib::FatalError( "Failed to create submix voice" );
}
// XAudio doesn't really impose a maximum number of voices
voices.SetNum( voices.Max() );
freeVoices.SetNum( voices.Max() );
zombieVoices.SetNum( 0 );
for ( int i = 0; i < voices.Num(); i++ ) {
freeVoices[i] = &voices[i];
}
}
/*
========================
idSoundHardware_XAudio2::Shutdown
========================
*/
void idSoundHardware_XAudio2::Shutdown() {
for ( int i = 0; i < voices.Num(); i++ ) {
voices[ i ].DestroyInternal();
}
voices.Clear();
freeVoices.Clear();
zombieVoices.Clear();
// ---------------------
// Shutdown the Doom classic sound system.
// ---------------------
I_ShutdownSoundHardware();
if ( pXAudio2 != NULL ) {
// Unregister the sound engine callback
pXAudio2->UnregisterForCallbacks( &soundEngineCallback );
}
if ( pSubmixVoice != NULL ) {
pSubmixVoice->DestroyVoice();
pSubmixVoice = NULL;
}
if ( pMasterVoice != NULL ) {
// release the vu meter effect
pMasterVoice->SetEffectChain( NULL );
pMasterVoice->DestroyVoice();
pMasterVoice = NULL;
}
if ( pXAudio2 != NULL ) {
XAUDIO2_PERFORMANCE_DATA perfData;
pXAudio2->GetPerformanceData( &perfData );
idLib::Printf( "Final pXAudio2 performanceData: Voices: %d/%d CPU: %.2f%% Mem: %dkb\n", perfData.ActiveSourceVoiceCount, perfData.TotalSourceVoiceCount, perfData.AudioCyclesSinceLastQuery / (float)perfData.TotalCyclesSinceLastQuery, perfData.MemoryUsageInBytes / 1024 );
pXAudio2->Release();
pXAudio2 = NULL;
}
if ( vuMeterRMS != NULL ) {
console->DestroyGraph( vuMeterRMS );
vuMeterRMS = NULL;
}
if ( vuMeterPeak != NULL ) {
console->DestroyGraph( vuMeterPeak );
vuMeterPeak = NULL;
}
}
/*
========================
idSoundHardware_XAudio2::AllocateVoice
========================
*/
idSoundVoice * idSoundHardware_XAudio2::AllocateVoice( const idSoundSample * leadinSample, const idSoundSample * loopingSample ) {
if ( leadinSample == NULL ) {
return NULL;
}
if ( loopingSample != NULL ) {
if ( ( leadinSample->format.basic.formatTag != loopingSample->format.basic.formatTag ) || ( leadinSample->format.basic.numChannels != loopingSample->format.basic.numChannels ) ) {
idLib::Warning( "Leadin/looping format mismatch: %s & %s", leadinSample->GetName(), loopingSample->GetName() );
loopingSample = NULL;
}
}
// Try to find a free voice that matches the format
// But fallback to the last free voice if none match the format
idSoundVoice * voice = NULL;
for ( int i = 0; i < freeVoices.Num(); i++ ) {
if ( freeVoices[i]->IsPlaying() ) {
continue;
}
voice = (idSoundVoice *)freeVoices[i];
if ( voice->CompatibleFormat( (idSoundSample_XAudio2*)leadinSample ) ) {
break;
}
}
if ( voice != NULL ) {
voice->Create( leadinSample, loopingSample );
freeVoices.Remove( voice );
return voice;
}
return NULL;
}
/*
========================
idSoundHardware_XAudio2::FreeVoice
========================
*/
void idSoundHardware_XAudio2::FreeVoice( idSoundVoice * voice ) {
voice->Stop();
// Stop() is asyncronous, so we won't flush bufferes until the
// voice on the zombie channel actually returns !IsPlaying()
zombieVoices.Append( voice );
}
/*
========================
idSoundHardware_XAudio2::Update
========================
*/
void idSoundHardware_XAudio2::Update() {
if ( pXAudio2 == NULL ) {
int nowTime = Sys_Milliseconds();
if ( lastResetTime + 1000 < nowTime ) {
lastResetTime = nowTime;
Init();
}
return;
}
if ( soundSystem->IsMuted() ) {
pMasterVoice->SetVolume( 0.0f, OPERATION_SET );
} else {
pMasterVoice->SetVolume( DBtoLinear( s_volume_dB.GetFloat() ), OPERATION_SET );
}
pXAudio2->CommitChanges( XAUDIO2_COMMIT_ALL );
// IXAudio2SourceVoice::Stop() has been called for every sound on the
// zombie list, but it is documented as asyncronous, so we have to wait
// until it actually reports that it is no longer playing.
for ( int i = 0; i < zombieVoices.Num(); i++ ) {
zombieVoices[i]->FlushSourceBuffers();
if ( !zombieVoices[i]->IsPlaying() ) {
freeVoices.Append( zombieVoices[i] );
zombieVoices.RemoveIndexFast( i );
i--;
} else {
static int playingZombies;
playingZombies++;
}
}
if ( s_showPerfData.GetBool() ) {
XAUDIO2_PERFORMANCE_DATA perfData;
pXAudio2->GetPerformanceData( &perfData );
idLib::Printf( "Voices: %d/%d CPU: %.2f%% Mem: %dkb\n", perfData.ActiveSourceVoiceCount, perfData.TotalSourceVoiceCount, perfData.AudioCyclesSinceLastQuery / (float)perfData.TotalCyclesSinceLastQuery, perfData.MemoryUsageInBytes / 1024 );
}
if ( vuMeterRMS == NULL ) {
// Init probably hasn't been called yet
return;
}
vuMeterRMS->Enable( s_showLevelMeter.GetBool() );
vuMeterPeak->Enable( s_showLevelMeter.GetBool() );
if ( !s_showLevelMeter.GetBool() ) {
pMasterVoice->DisableEffect( 0 );
return;
} else {
pMasterVoice->EnableEffect( 0 );
}
float peakLevels[ 8 ];
float rmsLevels[ 8 ];
XAUDIO2FX_VOLUMEMETER_LEVELS levels;
levels.ChannelCount = outputChannels;
levels.pPeakLevels = peakLevels;
levels.pRMSLevels = rmsLevels;
if ( levels.ChannelCount > 8 ) {
levels.ChannelCount = 8;
}
pMasterVoice->GetEffectParameters( 0, &levels, sizeof( levels ) );
int currentTime = Sys_Milliseconds();
for ( int i = 0; i < outputChannels; i++ ) {
if ( vuMeterPeakTimes[i] < currentTime ) {
vuMeterPeak->SetValue( i, vuMeterPeak->GetValue( i ) * 0.9f, colorRed );
}
}
float width = 20.0f;
float height = 200.0f;
float left = 100.0f;
float top = 100.0f;
sscanf( s_meterPosition.GetString(), "%f %f %f %f", &left, &top, &width, &height );
vuMeterRMS->SetPosition( left, top, width * levels.ChannelCount, height );
vuMeterPeak->SetPosition( left, top, width * levels.ChannelCount, height );
for ( uint32 i = 0; i < levels.ChannelCount; i++ ) {
vuMeterRMS->SetValue( i, rmsLevels[ i ], idVec4( 0.5f, 1.0f, 0.0f, 1.00f ) );
if ( peakLevels[ i ] >= vuMeterPeak->GetValue( i ) ) {
vuMeterPeak->SetValue( i, peakLevels[ i ], colorRed );
vuMeterPeakTimes[i] = currentTime + s_meterTopTime.GetInteger();
}
}
}
/*
================================================
idSoundEngineCallback
================================================
*/
/*
========================
idSoundEngineCallback::OnCriticalError
========================
*/
void idSoundEngineCallback::OnCriticalError( HRESULT Error ) {
soundSystemLocal.SetNeedsRestart();
}

View File

@@ -0,0 +1,112 @@
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#ifndef __XA_SOUNDHARDWARE_H__
#define __XA_SOUNDHARDWARE_H__
class idSoundSample_XAudio2;
class idSoundVoice_XAudio2;
/*
================================================
idSoundEngineCallback
================================================
*/
class idSoundEngineCallback : public IXAudio2EngineCallback {
public:
idSoundHardware_XAudio2 * hardware;
private:
// Called by XAudio2 just before an audio processing pass begins.
STDMETHOD_( void, OnProcessingPassStart ) ( THIS ) {}
// Called just after an audio processing pass ends.
STDMETHOD_( void, OnProcessingPassEnd ) ( THIS ) {}
// Called in the event of a critical system error which requires XAudio2
// to be closed down and restarted. The error code is given in Error.
STDMETHOD_( void, OnCriticalError ) ( THIS_ HRESULT Error );
};
/*
================================================
idSoundHardware_XAudio2
================================================
*/
class idSoundHardware_XAudio2 {
public:
idSoundHardware_XAudio2();
void Init();
void Shutdown();
void Update();
idSoundVoice * AllocateVoice( const idSoundSample * leadinSample, const idSoundSample * loopingSample );
void FreeVoice( idSoundVoice * voice );
// video playback needs this
IXAudio2 * GetIXAudio2() const { return pXAudio2; };
int GetNumZombieVoices() const { return zombieVoices.Num(); }
int GetNumFreeVoices() const { return freeVoices.Num(); }
protected:
friend class idSoundSample_XAudio2;
friend class idSoundVoice_XAudio2;
private:
IXAudio2 * pXAudio2;
IXAudio2MasteringVoice * pMasterVoice;
IXAudio2SubmixVoice * pSubmixVoice;
idSoundEngineCallback soundEngineCallback;
int lastResetTime;
int outputChannels;
int channelMask;
idDebugGraph * vuMeterRMS;
idDebugGraph * vuMeterPeak;
int vuMeterPeakTimes[ 8 ];
// Can't stop and start a voice on the same frame, so we have to double this to handle the worst case scenario of stopping all voices and starting a full new set
idStaticList<idSoundVoice_XAudio2, MAX_HARDWARE_VOICES * 2 > voices;
idStaticList<idSoundVoice_XAudio2 *, MAX_HARDWARE_VOICES * 2 > zombieVoices;
idStaticList<idSoundVoice_XAudio2 *, MAX_HARDWARE_VOICES * 2 > freeVoices;
};
/*
================================================
idSoundHardware
================================================
*/
class idSoundHardware : public idSoundHardware_XAudio2 {
};
#endif

View File

@@ -0,0 +1,487 @@
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "../../idlib/precompiled.h"
#include "../snd_local.h"
extern idCVar s_useCompression;
extern idCVar s_noSound;
#define GPU_CONVERT_CPU_TO_CPU_CACHED_READONLY_ADDRESS( x ) x
const uint32 SOUND_MAGIC_IDMSA = 0x6D7A7274;
extern idCVar sys_lang;
/*
========================
AllocBuffer
========================
*/
static void * AllocBuffer( int size, const char * name ) {
return Mem_Alloc( size, TAG_AUDIO );
}
/*
========================
FreeBuffer
========================
*/
static void FreeBuffer( void * p ) {
return Mem_Free( p );
}
/*
========================
idSoundSample_XAudio2::idSoundSample_XAudio2
========================
*/
idSoundSample_XAudio2::idSoundSample_XAudio2() {
timestamp = FILE_NOT_FOUND_TIMESTAMP;
loaded = false;
neverPurge = false;
levelLoadReferenced = false;
memset( &format, 0, sizeof( format ) );
totalBufferSize = 0;
playBegin = 0;
playLength = 0;
lastPlayedTime = 0;
}
/*
========================
idSoundSample_XAudio2::~idSoundSample_XAudio2
========================
*/
idSoundSample_XAudio2::~idSoundSample_XAudio2() {
FreeData();
}
/*
========================
idSoundSample_XAudio2::WriteGeneratedSample
========================
*/
void idSoundSample_XAudio2::WriteGeneratedSample( idFile *fileOut ) {
fileOut->WriteBig( SOUND_MAGIC_IDMSA );
fileOut->WriteBig( timestamp );
fileOut->WriteBig( loaded );
fileOut->WriteBig( playBegin );
fileOut->WriteBig( playLength );
idWaveFile::WriteWaveFormatDirect( format, fileOut );
fileOut->WriteBig( ( int )amplitude.Num() );
fileOut->Write( amplitude.Ptr(), amplitude.Num() );
fileOut->WriteBig( totalBufferSize );
fileOut->WriteBig( ( int )buffers.Num() );
for ( int i = 0; i < buffers.Num(); i++ ) {
fileOut->WriteBig( buffers[ i ].numSamples );
fileOut->WriteBig( buffers[ i ].bufferSize );
fileOut->Write( buffers[ i ].buffer, buffers[ i ].bufferSize );
};
}
/*
========================
idSoundSample_XAudio2::WriteAllSamples
========================
*/
void idSoundSample_XAudio2::WriteAllSamples( const idStr &sampleName ) {
idSoundSample_XAudio2 * samplePC = new idSoundSample_XAudio2();
{
idStrStatic< MAX_OSPATH > inName = sampleName;
inName.Append( ".msadpcm" );
idStrStatic< MAX_OSPATH > inName2 = sampleName;
inName2.Append( ".wav" );
idStrStatic< MAX_OSPATH > outName = "generated/";
outName.Append( sampleName );
outName.Append( ".idwav" );
if ( samplePC->LoadWav( inName ) || samplePC->LoadWav( inName2 ) ) {
idFile *fileOut = fileSystem->OpenFileWrite( outName, "fs_basepath" );
samplePC->WriteGeneratedSample( fileOut );
delete fileOut;
}
}
delete samplePC;
}
/*
========================
idSoundSample_XAudio2::LoadGeneratedSound
========================
*/
bool idSoundSample_XAudio2::LoadGeneratedSample( const idStr &filename ) {
idFileLocal fileIn( fileSystem->OpenFileReadMemory( filename ) );
if ( fileIn != NULL ) {
uint32 magic;
fileIn->ReadBig( magic );
fileIn->ReadBig( timestamp );
fileIn->ReadBig( loaded );
fileIn->ReadBig( playBegin );
fileIn->ReadBig( playLength );
idWaveFile::ReadWaveFormatDirect( format, fileIn );
int num;
fileIn->ReadBig( num );
amplitude.Clear();
amplitude.SetNum( num );
fileIn->Read( amplitude.Ptr(), amplitude.Num() );
fileIn->ReadBig( totalBufferSize );
fileIn->ReadBig( num );
buffers.SetNum( num );
for ( int i = 0; i < num; i++ ) {
fileIn->ReadBig( buffers[ i ].numSamples );
fileIn->ReadBig( buffers[ i ].bufferSize );
buffers[ i ].buffer = AllocBuffer( buffers[ i ].bufferSize, GetName() );
fileIn->Read( buffers[ i ].buffer, buffers[ i ].bufferSize );
buffers[ i ].buffer = GPU_CONVERT_CPU_TO_CPU_CACHED_READONLY_ADDRESS( buffers[ i ].buffer );
}
return true;
}
return false;
}
/*
========================
idSoundSample_XAudio2::Load
========================
*/
void idSoundSample_XAudio2::LoadResource() {
FreeData();
if ( idStr::Icmpn( GetName(), "_default", 8 ) == 0 ) {
MakeDefault();
return;
}
if ( s_noSound.GetBool() ) {
MakeDefault();
return;
}
loaded = false;
for ( int i = 0; i < 2; i++ ) {
idStrStatic< MAX_OSPATH > sampleName = GetName();
if ( ( i == 0 ) && !sampleName.Replace( "/vo/", va( "/vo/%s/", sys_lang.GetString() ) ) ) {
i++;
}
idStrStatic< MAX_OSPATH > generatedName = "generated/";
generatedName.Append( sampleName );
{
if ( s_useCompression.GetBool() ) {
sampleName.Append( ".msadpcm" );
} else {
sampleName.Append( ".wav" );
}
generatedName.Append( ".idwav" );
}
loaded = LoadGeneratedSample( generatedName ) || LoadWav( sampleName );
if ( !loaded && s_useCompression.GetBool() ) {
sampleName.SetFileExtension( "wav" );
loaded = LoadWav( sampleName );
}
if ( loaded ) {
if ( cvarSystem->GetCVarBool( "fs_buildresources" ) ) {
fileSystem->AddSamplePreload( GetName() );
WriteAllSamples( GetName() );
if ( sampleName.Find( "/vo/" ) >= 0 ) {
for ( int i = 0; i < Sys_NumLangs(); i++ ) {
const char * lang = Sys_Lang( i );
if ( idStr::Icmp( lang, ID_LANG_ENGLISH ) == 0 ) {
continue;
}
idStrStatic< MAX_OSPATH > locName = GetName();
locName.Replace( "/vo/", va( "/vo/%s/", Sys_Lang( i ) ) );
WriteAllSamples( locName );
}
}
}
return;
}
}
if ( !loaded ) {
// make it default if everything else fails
MakeDefault();
}
return;
}
/*
========================
idSoundSample_XAudio2::LoadWav
========================
*/
bool idSoundSample_XAudio2::LoadWav( const idStr & filename ) {
// load the wave
idWaveFile wave;
if ( !wave.Open( filename ) ) {
return false;
}
idStrStatic< MAX_OSPATH > sampleName = filename;
sampleName.SetFileExtension( "amp" );
LoadAmplitude( sampleName );
const char * formatError = wave.ReadWaveFormat( format );
if ( formatError != NULL ) {
idLib::Warning( "LoadWav( %s ) : %s", filename.c_str(), formatError );
MakeDefault();
return false;
}
timestamp = wave.Timestamp();
totalBufferSize = wave.SeekToChunk( 'data' );
if ( format.basic.formatTag == idWaveFile::FORMAT_PCM || format.basic.formatTag == idWaveFile::FORMAT_EXTENSIBLE ) {
if ( format.basic.bitsPerSample != 16 ) {
idLib::Warning( "LoadWav( %s ) : %s", filename.c_str(), "Not a 16 bit PCM wav file" );
MakeDefault();
return false;
}
playBegin = 0;
playLength = ( totalBufferSize ) / format.basic.blockSize;
buffers.SetNum( 1 );
buffers[0].bufferSize = totalBufferSize;
buffers[0].numSamples = playLength;
buffers[0].buffer = AllocBuffer( totalBufferSize, GetName() );
wave.Read( buffers[0].buffer, totalBufferSize );
if ( format.basic.bitsPerSample == 16 ) {
idSwap::LittleArray( (short *)buffers[0].buffer, totalBufferSize / sizeof( short ) );
}
buffers[0].buffer = GPU_CONVERT_CPU_TO_CPU_CACHED_READONLY_ADDRESS( buffers[0].buffer );
} else if ( format.basic.formatTag == idWaveFile::FORMAT_ADPCM ) {
playBegin = 0;
playLength = ( ( totalBufferSize / format.basic.blockSize ) * format.extra.adpcm.samplesPerBlock );
buffers.SetNum( 1 );
buffers[0].bufferSize = totalBufferSize;
buffers[0].numSamples = playLength;
buffers[0].buffer = AllocBuffer( totalBufferSize, GetName() );
wave.Read( buffers[0].buffer, totalBufferSize );
buffers[0].buffer = GPU_CONVERT_CPU_TO_CPU_CACHED_READONLY_ADDRESS( buffers[0].buffer );
} else if ( format.basic.formatTag == idWaveFile::FORMAT_XMA2 ) {
if ( format.extra.xma2.blockCount == 0 ) {
idLib::Warning( "LoadWav( %s ) : %s", filename.c_str(), "No data blocks in file" );
MakeDefault();
return false;
}
int bytesPerBlock = format.extra.xma2.bytesPerBlock;
assert( format.extra.xma2.blockCount == ALIGN( totalBufferSize, bytesPerBlock ) / bytesPerBlock );
assert( format.extra.xma2.blockCount * bytesPerBlock >= totalBufferSize );
assert( format.extra.xma2.blockCount * bytesPerBlock < totalBufferSize + bytesPerBlock );
buffers.SetNum( format.extra.xma2.blockCount );
for ( int i = 0; i < buffers.Num(); i++ ) {
if ( i == buffers.Num() - 1 ) {
buffers[i].bufferSize = totalBufferSize - ( i * bytesPerBlock );
} else {
buffers[i].bufferSize = bytesPerBlock;
}
buffers[i].buffer = AllocBuffer( buffers[i].bufferSize, GetName() );
wave.Read( buffers[i].buffer, buffers[i].bufferSize );
buffers[i].buffer = GPU_CONVERT_CPU_TO_CPU_CACHED_READONLY_ADDRESS( buffers[i].buffer );
}
int seekTableSize = wave.SeekToChunk( 'seek' );
if ( seekTableSize != 4 * buffers.Num() ) {
idLib::Warning( "LoadWav( %s ) : %s", filename.c_str(), "Wrong number of entries in seek table" );
MakeDefault();
return false;
}
for ( int i = 0; i < buffers.Num(); i++ ) {
wave.Read( &buffers[i].numSamples, sizeof( buffers[i].numSamples ) );
idSwap::Big( buffers[i].numSamples );
}
playBegin = format.extra.xma2.loopBegin;
playLength = format.extra.xma2.loopLength;
if ( buffers[buffers.Num()-1].numSamples < playBegin + playLength ) {
// This shouldn't happen, but it's not fatal if it does
playLength = buffers[buffers.Num()-1].numSamples - playBegin;
} else {
// Discard samples beyond playLength
for ( int i = 0; i < buffers.Num(); i++ ) {
if ( buffers[i].numSamples > playBegin + playLength ) {
buffers[i].numSamples = playBegin + playLength;
// Ideally, the following loop should always have 0 iterations because playBegin + playLength ends in the last block already
// But there is no guarantee for that, so to be safe, discard all buffers beyond this one
for ( int j = i + 1; j < buffers.Num(); j++ ) {
FreeBuffer( buffers[j].buffer );
}
buffers.SetNum( i + 1 );
break;
}
}
}
} else {
idLib::Warning( "LoadWav( %s ) : Unsupported wave format %d", filename.c_str(), format.basic.formatTag );
MakeDefault();
return false;
}
wave.Close();
if ( format.basic.formatTag == idWaveFile::FORMAT_EXTENSIBLE ) {
// HACK: XAudio2 doesn't really support FORMAT_EXTENSIBLE so we convert it to a basic format after extracting the channel mask
format.basic.formatTag = format.extra.extensible.subFormat.data1;
}
// sanity check...
assert( buffers[buffers.Num()-1].numSamples == playBegin + playLength );
return true;
}
/*
========================
idSoundSample_XAudio2::MakeDefault
========================
*/
void idSoundSample_XAudio2::MakeDefault() {
FreeData();
static const int DEFAULT_NUM_SAMPLES = 256;
timestamp = FILE_NOT_FOUND_TIMESTAMP;
loaded = true;
memset( &format, 0, sizeof( format ) );
format.basic.formatTag = idWaveFile::FORMAT_PCM;
format.basic.numChannels = 1;
format.basic.bitsPerSample = 16;
format.basic.samplesPerSec = XAUDIO2_MIN_SAMPLE_RATE;
format.basic.blockSize = format.basic.numChannels * format.basic.bitsPerSample / 8;
format.basic.avgBytesPerSec = format.basic.samplesPerSec * format.basic.blockSize;
assert( format.basic.blockSize == 2 );
totalBufferSize = DEFAULT_NUM_SAMPLES * 2;
short * defaultBuffer = (short *)AllocBuffer( totalBufferSize, GetName() );
for ( int i = 0; i < DEFAULT_NUM_SAMPLES; i += 2 ) {
defaultBuffer[i + 0] = SHRT_MIN;
defaultBuffer[i + 1] = SHRT_MAX;
}
buffers.SetNum( 1 );
buffers[0].buffer = defaultBuffer;
buffers[0].bufferSize = totalBufferSize;
buffers[0].numSamples = DEFAULT_NUM_SAMPLES;
buffers[0].buffer = GPU_CONVERT_CPU_TO_CPU_CACHED_READONLY_ADDRESS( buffers[0].buffer );
playBegin = 0;
playLength = DEFAULT_NUM_SAMPLES;
}
/*
========================
idSoundSample_XAudio2::FreeData
Called before deleting the object and at the start of LoadResource()
========================
*/
void idSoundSample_XAudio2::FreeData() {
if ( buffers.Num() > 0 ) {
soundSystemLocal.StopVoicesWithSample( (idSoundSample *)this );
for ( int i = 0; i < buffers.Num(); i++ ) {
FreeBuffer( buffers[i].buffer );
}
buffers.Clear();
}
amplitude.Clear();
timestamp = FILE_NOT_FOUND_TIMESTAMP;
memset( &format, 0, sizeof( format ) );
loaded = false;
totalBufferSize = 0;
playBegin = 0;
playLength = 0;
}
/*
========================
idSoundSample_XAudio2::LoadAmplitude
========================
*/
bool idSoundSample_XAudio2::LoadAmplitude( const idStr & name ) {
amplitude.Clear();
idFileLocal f( fileSystem->OpenFileRead( name ) );
if ( f == NULL ) {
return false;
}
amplitude.SetNum( f->Length() );
f->Read( amplitude.Ptr(), amplitude.Num() );
return true;
}
/*
========================
idSoundSample_XAudio2::GetAmplitude
========================
*/
float idSoundSample_XAudio2::GetAmplitude( int timeMS ) const {
if ( timeMS < 0 || timeMS > LengthInMsec() ) {
return 0.0f;
}
if ( IsDefault() ) {
return 1.0f;
}
int index = timeMS * 60 / 1000;
if ( index < 0 || index >= amplitude.Num() ) {
return 0.0f;
}
return (float)amplitude[index] / 255.0f;
}

View File

@@ -0,0 +1,129 @@
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#ifndef __XA2_SOUNDSAMPLE_H__
#define __XA2_SOUNDSAMPLE_H__
/*
================================================
idSoundSample_XAudio2
================================================
*/
class idSampleInfo;
class idSoundSample_XAudio2 {
public:
idSoundSample_XAudio2();
// Loads and initializes the resource based on the name.
virtual void LoadResource();
void SetName( const char * n ) { name = n; }
const char * GetName() const { return name; }
ID_TIME_T GetTimestamp() const { return timestamp; }
// turns it into a beep
void MakeDefault();
// frees all data
void FreeData();
int LengthInMsec() const { return SamplesToMsec( NumSamples(), SampleRate() ); }
int SampleRate() const { return format.basic.samplesPerSec; }
int NumSamples() const { return playLength; }
int NumChannels() const { return format.basic.numChannels; }
int BufferSize() const { return totalBufferSize; }
bool IsCompressed() const { return ( format.basic.formatTag != idWaveFile::FORMAT_PCM ); }
bool IsDefault() const { return timestamp == FILE_NOT_FOUND_TIMESTAMP; }
bool IsLoaded() const { return loaded; }
void SetNeverPurge() { neverPurge = true; }
bool GetNeverPurge() const { return neverPurge; }
void SetLevelLoadReferenced() { levelLoadReferenced = true; }
void ResetLevelLoadReferenced() { levelLoadReferenced = false; }
bool GetLevelLoadReferenced() const { return levelLoadReferenced; }
int GetLastPlayedTime() const { return lastPlayedTime; }
void SetLastPlayedTime( int t ) { lastPlayedTime = t; }
float GetAmplitude( int timeMS ) const;
protected:
friend class idSoundHardware_XAudio2;
friend class idSoundVoice_XAudio2;
~idSoundSample_XAudio2();
bool LoadWav( const idStr & name );
bool LoadAmplitude( const idStr & name );
void WriteAllSamples( const idStr &sampleName );
bool LoadGeneratedSample( const idStr &name );
void WriteGeneratedSample( idFile *fileOut );
struct sampleBuffer_t {
void * buffer;
int bufferSize;
int numSamples;
};
idStr name;
ID_TIME_T timestamp;
bool loaded;
bool neverPurge;
bool levelLoadReferenced;
bool usesMapHeap;
uint32 lastPlayedTime;
int totalBufferSize; // total size of all the buffers
idList<sampleBuffer_t, TAG_AUDIO> buffers;
int playBegin;
int playLength;
idWaveFile::waveFmt_t format;
idList<byte, TAG_AMPLITUDE> amplitude;
};
/*
================================================
idSoundSample
This reverse-inheritance purportedly makes working on
multiple platforms easier.
================================================
*/
class idSoundSample : public idSoundSample_XAudio2 {
public:
};
#endif

View File

@@ -0,0 +1,492 @@
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "../../idlib/precompiled.h"
#include "../snd_local.h"
idCVar s_skipHardwareSets( "s_skipHardwareSets", "0", CVAR_BOOL, "Do all calculation, but skip XA2 calls" );
idCVar s_debugHardware( "s_debugHardware", "0", CVAR_BOOL, "Print a message any time a hardware voice changes" );
// The whole system runs at this sample rate
static int SYSTEM_SAMPLE_RATE = 44100;
static float ONE_OVER_SYSTEM_SAMPLE_RATE = 1.0f / SYSTEM_SAMPLE_RATE;
/*
========================
idStreamingVoiceContext
========================
*/
class idStreamingVoiceContext : public IXAudio2VoiceCallback {
public:
STDMETHOD_(void, OnVoiceProcessingPassStart)( UINT32 BytesRequired ) {}
STDMETHOD_(void, OnVoiceProcessingPassEnd)() {}
STDMETHOD_(void, OnStreamEnd)() {}
STDMETHOD_(void, OnBufferStart)( void * pContext ) {
idSoundSystemLocal::bufferContext_t * bufferContext = (idSoundSystemLocal::bufferContext_t *) pContext;
bufferContext->voice->OnBufferStart( bufferContext->sample, bufferContext->bufferNumber );
}
STDMETHOD_(void, OnLoopEnd)( void * ) {}
STDMETHOD_(void, OnVoiceError)( void *, HRESULT hr ) { idLib::Warning( "OnVoiceError( %d )", hr ); }
STDMETHOD_(void, OnBufferEnd)( void* pContext ) {
idSoundSystemLocal::bufferContext_t * bufferContext = (idSoundSystemLocal::bufferContext_t *) pContext;
soundSystemLocal.ReleaseStreamBufferContext( bufferContext );
}
} streamContext;
/*
========================
idSoundVoice_XAudio2::idSoundVoice_XAudio2
========================
*/
idSoundVoice_XAudio2::idSoundVoice_XAudio2()
: pSourceVoice( NULL ),
leadinSample( NULL ),
loopingSample( NULL ),
formatTag( 0 ),
numChannels( 0 ),
sampleRate( 0 ),
paused( true ),
hasVUMeter( false ) {
}
/*
========================
idSoundVoice_XAudio2::~idSoundVoice_XAudio2
========================
*/
idSoundVoice_XAudio2::~idSoundVoice_XAudio2() {
DestroyInternal();
}
/*
========================
idSoundVoice_XAudio2::CompatibleFormat
========================
*/
bool idSoundVoice_XAudio2::CompatibleFormat( idSoundSample_XAudio2 * s ) {
if ( pSourceVoice == NULL ) {
// If this voice has never been allocated, then it's compatible with everything
return true;
}
return false;
}
/*
========================
idSoundVoice_XAudio2::Create
========================
*/
void idSoundVoice_XAudio2::Create( const idSoundSample * leadinSample_, const idSoundSample * loopingSample_ ) {
if ( IsPlaying() ) {
// This should never hit
Stop();
return;
}
leadinSample = (idSoundSample_XAudio2 *)leadinSample_;
loopingSample = (idSoundSample_XAudio2 *)loopingSample_;
if ( pSourceVoice != NULL && CompatibleFormat( leadinSample ) ) {
sampleRate = leadinSample->format.basic.samplesPerSec;
} else {
DestroyInternal();
formatTag = leadinSample->format.basic.formatTag;
numChannels = leadinSample->format.basic.numChannels;
sampleRate = leadinSample->format.basic.samplesPerSec;
soundSystemLocal.hardware.pXAudio2->CreateSourceVoice( &pSourceVoice, (const WAVEFORMATEX *)&leadinSample->format, XAUDIO2_VOICE_USEFILTER, 4.0f, &streamContext );
if ( pSourceVoice == NULL ) {
// If this hits, then we are most likely passing an invalid sample format, which should have been caught by the loader (and the sample defaulted)
return;
}
if ( s_debugHardware.GetBool() ) {
if ( loopingSample == NULL || loopingSample == leadinSample ) {
idLib::Printf( "%dms: %p created for %s\n", Sys_Milliseconds(), pSourceVoice, leadinSample ? leadinSample->GetName() : "<null>" );
} else {
idLib::Printf( "%dms: %p created for %s and %s\n", Sys_Milliseconds(), pSourceVoice, leadinSample ? leadinSample->GetName() : "<null>", loopingSample ? loopingSample->GetName() : "<null>" );
}
}
}
sourceVoiceRate = sampleRate;
pSourceVoice->SetSourceSampleRate( sampleRate );
pSourceVoice->SetVolume( 0.0f );
}
/*
========================
idSoundVoice_XAudio2::DestroyInternal
========================
*/
void idSoundVoice_XAudio2::DestroyInternal() {
if ( pSourceVoice != NULL ) {
if ( s_debugHardware.GetBool() ) {
idLib::Printf( "%dms: %p destroyed\n", Sys_Milliseconds(), pSourceVoice );
}
pSourceVoice->DestroyVoice();
pSourceVoice = NULL;
hasVUMeter = false;
}
}
/*
========================
idSoundVoice_XAudio2::Start
========================
*/
void idSoundVoice_XAudio2::Start( int offsetMS, int ssFlags ) {
if ( s_debugHardware.GetBool() ) {
idLib::Printf( "%dms: %p starting %s @ %dms\n", Sys_Milliseconds(), pSourceVoice, leadinSample ? leadinSample->GetName() : "<null>", offsetMS );
}
if ( !leadinSample ) {
return;
}
if ( !pSourceVoice ) {
return;
}
if ( leadinSample->IsDefault() ) {
idLib::Warning( "Starting defaulted sound sample %s", leadinSample->GetName() );
}
bool flicker = ( ssFlags & SSF_NO_FLICKER ) == 0;
if ( flicker != hasVUMeter ) {
hasVUMeter = flicker;
if ( flicker ) {
IUnknown * vuMeter = NULL;
if ( XAudio2CreateVolumeMeter( &vuMeter, 0 ) == S_OK ) {
XAUDIO2_EFFECT_DESCRIPTOR descriptor;
descriptor.InitialState = true;
descriptor.OutputChannels = leadinSample->NumChannels();
descriptor.pEffect = vuMeter;
XAUDIO2_EFFECT_CHAIN chain;
chain.EffectCount = 1;
chain.pEffectDescriptors = &descriptor;
pSourceVoice->SetEffectChain( &chain );
vuMeter->Release();
}
} else {
pSourceVoice->SetEffectChain( NULL );
}
}
assert( offsetMS >= 0 );
int offsetSamples = MsecToSamples( offsetMS, leadinSample->SampleRate() );
if ( loopingSample == NULL && offsetSamples >= leadinSample->playLength ) {
return;
}
RestartAt( offsetSamples );
Update();
UnPause();
}
/*
========================
idSoundVoice_XAudio2::RestartAt
========================
*/
int idSoundVoice_XAudio2::RestartAt( int offsetSamples ) {
offsetSamples &= ~127;
idSoundSample_XAudio2 * sample = leadinSample;
if ( offsetSamples >= leadinSample->playLength ) {
if ( loopingSample != NULL ) {
offsetSamples %= loopingSample->playLength;
sample = loopingSample;
} else {
return 0;
}
}
int previousNumSamples = 0;
for ( int i = 0; i < sample->buffers.Num(); i++ ) {
if ( sample->buffers[i].numSamples > sample->playBegin + offsetSamples ) {
return SubmitBuffer( sample, i, sample->playBegin + offsetSamples - previousNumSamples );
}
previousNumSamples = sample->buffers[i].numSamples;
}
return 0;
}
/*
========================
idSoundVoice_XAudio2::SubmitBuffer
========================
*/
int idSoundVoice_XAudio2::SubmitBuffer( idSoundSample_XAudio2 * sample, int bufferNumber, int offset ) {
if ( sample == NULL || ( bufferNumber < 0 ) || ( bufferNumber >= sample->buffers.Num() ) ) {
return 0;
}
idSoundSystemLocal::bufferContext_t * bufferContext = soundSystemLocal.ObtainStreamBufferContext();
if ( bufferContext == NULL ) {
idLib::Warning( "No free buffer contexts!" );
return 0;
}
bufferContext->voice = this;
bufferContext->sample = sample;
bufferContext->bufferNumber = bufferNumber;
XAUDIO2_BUFFER buffer = { 0 };
if ( offset > 0 ) {
int previousNumSamples = 0;
if ( bufferNumber > 0 ) {
previousNumSamples = sample->buffers[bufferNumber-1].numSamples;
}
buffer.PlayBegin = offset;
buffer.PlayLength = sample->buffers[bufferNumber].numSamples - previousNumSamples - offset;
}
buffer.AudioBytes = sample->buffers[bufferNumber].bufferSize;
buffer.pAudioData = (BYTE *)sample->buffers[bufferNumber].buffer;
buffer.pContext = bufferContext;
if ( ( loopingSample == NULL ) && ( bufferNumber == sample->buffers.Num() - 1 ) ) {
buffer.Flags = XAUDIO2_END_OF_STREAM;
}
pSourceVoice->SubmitSourceBuffer( &buffer );
return buffer.AudioBytes;
}
/*
========================
idSoundVoice_XAudio2::Update
========================
*/
bool idSoundVoice_XAudio2::Update() {
if ( pSourceVoice == NULL || leadinSample == NULL ) {
return false;
}
XAUDIO2_VOICE_STATE state;
pSourceVoice->GetState( &state );
const int srcChannels = leadinSample->NumChannels();
float pLevelMatrix[ MAX_CHANNELS_PER_VOICE * MAX_CHANNELS_PER_VOICE ] = { 0 };
CalculateSurround( srcChannels, pLevelMatrix, 1.0f );
if ( s_skipHardwareSets.GetBool() ) {
return true;
}
pSourceVoice->SetOutputMatrix( soundSystemLocal.hardware.pMasterVoice, srcChannels, dstChannels, pLevelMatrix, OPERATION_SET );
assert( idMath::Fabs( gain ) <= XAUDIO2_MAX_VOLUME_LEVEL );
pSourceVoice->SetVolume( gain, OPERATION_SET );
SetSampleRate( sampleRate, OPERATION_SET );
// we don't do this any longer because we pause and unpause explicitly when the soundworld is paused or unpaused
// UnPause();
return true;
}
/*
========================
idSoundVoice_XAudio2::IsPlaying
========================
*/
bool idSoundVoice_XAudio2::IsPlaying() {
if ( pSourceVoice == NULL ) {
return false;
}
XAUDIO2_VOICE_STATE state;
pSourceVoice->GetState( &state );
return ( state.BuffersQueued != 0 );
}
/*
========================
idSoundVoice_XAudio2::FlushSourceBuffers
========================
*/
void idSoundVoice_XAudio2::FlushSourceBuffers() {
if ( pSourceVoice != NULL ) {
pSourceVoice->FlushSourceBuffers();
}
}
/*
========================
idSoundVoice_XAudio2::Pause
========================
*/
void idSoundVoice_XAudio2::Pause() {
if ( !pSourceVoice || paused ) {
return;
}
if ( s_debugHardware.GetBool() ) {
idLib::Printf( "%dms: %p pausing %s\n", Sys_Milliseconds(), pSourceVoice, leadinSample ? leadinSample->GetName() : "<null>" );
}
pSourceVoice->Stop( 0, OPERATION_SET );
paused = true;
}
/*
========================
idSoundVoice_XAudio2::UnPause
========================
*/
void idSoundVoice_XAudio2::UnPause() {
if ( !pSourceVoice || !paused ) {
return;
}
if ( s_debugHardware.GetBool() ) {
idLib::Printf( "%dms: %p unpausing %s\n", Sys_Milliseconds(), pSourceVoice, leadinSample ? leadinSample->GetName() : "<null>" );
}
pSourceVoice->Start( 0, OPERATION_SET );
paused = false;
}
/*
========================
idSoundVoice_XAudio2::Stop
========================
*/
void idSoundVoice_XAudio2::Stop() {
if ( !pSourceVoice ) {
return;
}
if ( !paused ) {
if ( s_debugHardware.GetBool() ) {
idLib::Printf( "%dms: %p stopping %s\n", Sys_Milliseconds(), pSourceVoice, leadinSample ? leadinSample->GetName() : "<null>" );
}
pSourceVoice->Stop( 0, OPERATION_SET );
paused = true;
}
}
/*
========================
idSoundVoice_XAudio2::GetAmplitude
========================
*/
float idSoundVoice_XAudio2::GetAmplitude() {
if ( !hasVUMeter ) {
return 1.0f;
}
float peakLevels[ MAX_CHANNELS_PER_VOICE ];
float rmsLevels[ MAX_CHANNELS_PER_VOICE ];
XAUDIO2FX_VOLUMEMETER_LEVELS levels;
levels.ChannelCount = leadinSample->NumChannels();
levels.pPeakLevels = peakLevels;
levels.pRMSLevels = rmsLevels;
if ( levels.ChannelCount > MAX_CHANNELS_PER_VOICE ) {
levels.ChannelCount = MAX_CHANNELS_PER_VOICE;
}
if ( pSourceVoice->GetEffectParameters( 0, &levels, sizeof( levels ) ) != S_OK ) {
return 0.0f;
}
if ( levels.ChannelCount == 1 ) {
return rmsLevels[0];
}
float rms = 0.0f;
for ( uint32 i = 0; i < levels.ChannelCount; i++ ) {
rms += rmsLevels[i];
}
return rms / (float)levels.ChannelCount;
}
/*
========================
idSoundVoice_XAudio2::ResetSampleRate
========================
*/
void idSoundVoice_XAudio2::SetSampleRate( uint32 newSampleRate, uint32 operationSet ){
if ( pSourceVoice == NULL || leadinSample == NULL ) {
return;
}
sampleRate = newSampleRate;
XAUDIO2_FILTER_PARAMETERS filter;
filter.Type = LowPassFilter;
filter.OneOverQ = 1.0f; // [0.0f, XAUDIO2_MAX_FILTER_ONEOVERQ]
float cutoffFrequency = 1000.0f / Max( 0.01f, occlusion );
if ( cutoffFrequency * 6.0f >= (float)sampleRate ) {
filter.Frequency = XAUDIO2_MAX_FILTER_FREQUENCY;
} else {
filter.Frequency = 2.0f * idMath::Sin( idMath::PI * cutoffFrequency / (float)sampleRate );
}
assert( filter.Frequency >= 0.0f && filter.Frequency <= XAUDIO2_MAX_FILTER_FREQUENCY );
filter.Frequency = idMath::ClampFloat( 0.0f, XAUDIO2_MAX_FILTER_FREQUENCY, filter.Frequency );
pSourceVoice->SetFilterParameters( &filter, operationSet );
float freqRatio = pitch * (float)sampleRate / (float)sourceVoiceRate;
assert( freqRatio >= XAUDIO2_MIN_FREQ_RATIO && freqRatio <= XAUDIO2_MAX_FREQ_RATIO );
freqRatio = idMath::ClampFloat( XAUDIO2_MIN_FREQ_RATIO, XAUDIO2_MAX_FREQ_RATIO, freqRatio );
// if the value specified for maxFreqRatio is too high for the specified format, the call to CreateSourceVoice will fail
if ( numChannels == 1 ) {
assert( freqRatio * (float)SYSTEM_SAMPLE_RATE <= XAUDIO2_MAX_RATIO_TIMES_RATE_XMA_MONO );
} else {
assert( freqRatio * (float)SYSTEM_SAMPLE_RATE <= XAUDIO2_MAX_RATIO_TIMES_RATE_XMA_MULTICHANNEL );
}
pSourceVoice->SetFrequencyRatio( freqRatio, operationSet );
}
/*
========================
idSoundVoice_XAudio2::OnBufferStart
========================
*/
void idSoundVoice_XAudio2::OnBufferStart( idSoundSample_XAudio2 * sample, int bufferNumber ) {
SetSampleRate( sample->SampleRate(), XAUDIO2_COMMIT_NOW );
idSoundSample_XAudio2 * nextSample = sample;
int nextBuffer = bufferNumber + 1;
if ( nextBuffer == sample->buffers.Num() ) {
if ( sample == leadinSample ) {
if ( loopingSample == NULL ) {
return;
}
nextSample = loopingSample;
}
nextBuffer = 0;
}
SubmitBuffer( nextSample, nextBuffer, 0 );
}

View File

@@ -0,0 +1,114 @@
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#ifndef __XA2_SOUNDVOICE_H__
#define __XA2_SOUNDVOICE_H__
static const int MAX_QUEUED_BUFFERS = 3;
/*
================================================
idSoundVoice_XAudio2
================================================
*/
class idSoundVoice_XAudio2 : public idSoundVoice_Base {
public:
idSoundVoice_XAudio2();
~idSoundVoice_XAudio2();
void Create( const idSoundSample * leadinSample, const idSoundSample * loopingSample );
// Start playing at a particular point in the buffer. Does an Update() too
void Start( int offsetMS, int ssFlags );
// Stop playing.
void Stop();
// Stop consuming buffers
void Pause();
// Start consuming buffers again
void UnPause();
// Sends new position/volume/pitch information to the hardware
bool Update();
// returns the RMS levels of the most recently processed block of audio, SSF_FLICKER must have been passed to Start
float GetAmplitude();
// returns true if we can re-use this voice
bool CompatibleFormat( idSoundSample_XAudio2 * s );
uint32 GetSampleRate() const { return sampleRate; }
// callback function
void OnBufferStart( idSoundSample_XAudio2 * sample, int bufferNumber );
private:
friend class idSoundHardware_XAudio2;
// Returns true when all the buffers are finished processing
bool IsPlaying();
// Called after the voice has been stopped
void FlushSourceBuffers();
// Destroy the internal hardware resource
void DestroyInternal();
// Helper function used by the initial start as well as for looping a streamed buffer
int RestartAt( int offsetSamples );
// Helper function to submit a buffer
int SubmitBuffer( idSoundSample_XAudio2 * sample, int bufferNumber, int offset );
// Adjust the voice frequency based on the new sample rate for the buffer
void SetSampleRate( uint32 newSampleRate, uint32 operationSet );
IXAudio2SourceVoice * pSourceVoice;
idSoundSample_XAudio2 * leadinSample;
idSoundSample_XAudio2 * loopingSample;
// These are the fields from the sample format that matter to us for voice reuse
uint16 formatTag;
uint16 numChannels;
uint32 sourceVoiceRate;
uint32 sampleRate;
bool hasVUMeter;
bool paused;
};
/*
================================================
idSoundVoice
================================================
*/
class idSoundVoice : public idSoundVoice_XAudio2 {
};
#endif