Creating a VGUI Soundscape Maker
This tutorial will show you how to make a vgui soundscape maker panel that allows you to: modify, create, and play soundscapes IN-GAME!!
Requirements
- A mod running on Source SDK 2013
- Knowledge of C++
The main vgui panel
Before you start the tutorial, you need to either: copy and paste all of these following files into your mods src/game/client/soundscape_maker directory (or download the files below and follow the instructions)
- SoundscapeMaker.cpp
- SoundscapeMaker.h
- SoundscapeMakerClipboardDialog.cpp
- SoundscapeMakerClipboardDialog.h
- SoundscapeMakerDataPanel.cpp
- SoundscapeMakerDataPanel.h
- SoundscapeMakerDebugDialog.cpp
- SoundscapeMakerDebugDialog.h
- SoundscapeMakerFindSoundscapeDialog.cpp
- SoundscapeMakerFindSoundscapeDialog.h
- SoundscapeMakerManifestEditorDialog.cpp
- SoundscapeMakerManifestEditorDialog.h
- SoundscapeMakerNameDialog.cpp
- SoundscapeMakerNameDialog.h
- SoundscapeMakerPositionEditorDialog.cpp
- SoundscapeMakerPositionEditorDialog.h
- SoundscapeMakerPreloadedSoundscapesDialog.cpp
- SoundscapeMakerPreloadedSoundscapesDialog.h
- SoundscapeMakerTextEditorDialog.cpp
- SoundscapeMakerTextEditorDialog.h
You will also need to copy and paste all of these following files into your mods src/game/client/vgui_base directory
Now create 2 folders in your client project inside of visual studio. Name these: Soundscape Maker and Vgui Base.
Inside of the Soundscape Maker folder you are going to want to add all the files you created inside the soundscape_maker directory.
And inside of the Vgui Base folder you are going to want to add all the files you created inside the vgui_base directory.
If you cant be bothered with all the copying, you can optionally download the files from here: ["Source Code.rar"] and follow the instructions.
Changing External Files
Once that is done you are almost all done. The next thing you're going to want to do is, go to public\tier1\KeyValues.h and find:
private:
KeyValues( KeyValues& ); // prevent copy constructor being used
// prevent delete being called except through deleteThis()
~KeyValues();
(should be around line 258) and right after the ~KeyValues(); and just before the KeyValues* CreateKeyUsingKnownLastChild( const char *keyName, KeyValues *pLastChild ); function add
public:
Now go to tier1\KeyValues.cpp and find: void KeyValues::RecursiveLoadFromBuffer( char const *resourceName, CUtlBuffer &buf ) (around line 2251).
Inside this function, find:
if ( !*name ) // empty token, maybe "" or EOF
{
g_KeyValuesErrorStack.ReportError("RecursiveLoadFromBuffer: got empty keyname" );
break;
}
if ( *name == '}' && !wasQuoted ) // top level closed, stop reading
break;
// Always create the key; note that this could potentially
// cause some duplication, but that's what we want sometimes
KeyValues *dat = CreateKeyUsingKnownLastChild( name, pLastChild );
errorKey.Reset( dat->GetNameSymbol() );
// get the value
const char * value = ReadToken( buf, wasQuoted, wasConditional );
if ( wasConditional && value )
{
bAccepted = !m_bEvaluateConditionals || EvaluateConditional( value );
// get the real value
value = ReadToken( buf, wasQuoted, wasConditional );
}
if ( !value )
{
g_KeyValuesErrorStack.ReportError("RecursiveLoadFromBuffer: got NULL key" );
break;
}
(around line 2278) and change that to be:
bool WasEmpty = false;
if ( !*name ) // empty token, maybe "" or EOF
{
//g_KeyValuesErrorStack.ReportError("RecursiveLoadFromBuffer: got empty keyname" );
WasEmpty = true;
}
if ( *name == '}' && !wasQuoted ) // top level closed, stop reading
break;
KeyValues* dat = nullptr;
if (!WasEmpty)
{
// Always create the key; note that this could potentially
// cause some duplication, but that's what we want sometimes
dat = CreateKeyUsingKnownLastChild(name, pLastChild);
errorKey.Reset(dat->GetNameSymbol());
}
// get the value
const char * value = ReadToken( buf, wasQuoted, wasConditional );
if ( wasConditional && value )
{
bAccepted = !m_bEvaluateConditionals || EvaluateConditional( value );
// get the real value
value = ReadToken( buf, wasQuoted, wasConditional );
}
if ( !value || WasEmpty)
{
if (!value) { g_KeyValuesErrorStack.ReportError("RecursiveLoadFromBuffer: got NULL key"); break; }
else continue;
}
Now go to public\vgui_controls\TextEntry.h and find:
virtual void GetTextRange( char *buf, int from, int numchars ); // copy a portion of the text to the buffer and add zero-termination
private:
CUtlVector<wchar_t> m_TextStream; // the text in the text window is stored in this buffer
(should be around line 318) and make the private be public:
Now go to vgui2\vgui_controls\TextEntry.cpp and find:
if (!iswdigit(ch) && ((char)ch != '.'))
Inside of void TextEntry::InsertChar(wchar_t ch) (should be around line 2825) and replace that with:
if (!iswdigit(ch) && (m_bAllowNonAsciiCharacters || ((char)ch != '.') && ((char)ch != ',') && ((char)ch != ' ')))
Now go to public\vgui_controls\ListPanel.h and find:
private:
// Cleans up allocations associated with a particular item
void CleanupItem( FastSortListPanelItem *data );
(should be around line 265) and make the private be public:
Now go to vgui2\vgui_controls\ListPanel.cpp and find:
if ( bScrollToItem )
{
// scroll to last item
m_vbar->SetValue(displayRow);
}
(should be around line 890) and replace that with:
if ( bScrollToItem )
{
// scroll to last item
m_vbar->SetRange(0, m_VisibleItems.Count());
m_vbar->SetValue(displayRow);
}
Changing The Soundscape System
Go to client/c_soundscape.cpp and copy:
#include "c_soundscape.h"
//soundscape maker
#include "fmtstr.h"
#include "soundscape_maker/SoundscapeMaker.h"
#include "soundscape_maker/SoundscapeMakerDebugDialog.h"
to be under
#include "tier0/icommandline.h"
Then in the same file, find and move these structs and classes:
struct loopingsound_t {...}
struct randomsound_t {...}
struct subsoundscapeparams_t {...}
class C_SoundscapeSystem : public CBaseGameSystemPerFrame {...}
from c_soundscape.cpp to under:
#ifdef _WIN32
#pragma once
#endif
for inside of the c_soundscape.h file.
Now, in the loopingsound_t struct you moved before. Replace
const char *pWaveName; // name of the wave file
With:
char pWaveName[512]; // name of the wave file
Now, look for
void ProcessDSPVolume(KeyValues* pKey, subsoundscapeparams_t& params);
private:
bool IsBeingRestored() const
in the .h file you just added the class to. and if you find it then make the private be public.
Now under the class definition (under)
static ConVar* m_pDSPVolumeVar;
static ConVar* m_pSoundMixerVar;
};
add:
extern C_SoundscapeSystem g_SoundscapeSystem;
Now find:
// "playlooping"
void ProcessPlayLooping( KeyValues *pPlayLooping, const subsoundscapeparams_t ¶ms );
// "playrandom"
void ProcessPlayRandom( KeyValues *pPlayRandom, const subsoundscapeparams_t ¶ms );
And make those functions be:
// "playlooping"
void ProcessPlayLooping(KeyValues* pPlayLooping, const subsoundscapeparams_t& params, const char* SoundscapeName);
// "playrandom"
void ProcessPlayRandom(KeyValues* pPlayRandom, const subsoundscapeparams_t& params, const char* SoundscapeName);
Finally. Replace these function definitions:
int AddLoopingAmbient(const char* pSoundName, float volume, int pitch);
...
int AddLoopingSound(const char* pSoundName, bool isAmbient, float volume,
soundlevel_t soundLevel, int pitch, const Vector& position);
int AddRandomSound(const randomsound_t& sound);
void PlayRandomSound(randomsound_t& sound);
with:
int AddLoopingAmbient(const char* pSoundName, float volume, int pitch, const char* FromSoundscape);
...
int AddLoopingSound(const char* pSoundName, bool isAmbient, float volume, soundlevel_t soundLevel, int pitch, const Vector& position, const char* FromSoundscape);
int AddRandomSound(const randomsound_t& sound, const char* FromSoundscape);
void PlayRandomSound(randomsound_t& sound, int index);
Now back in c_soundscape.cpp. Go to the function bool C_SoundscapeSystem::Init() (around line 150). At the very end of the function find: manifest->deleteThis();. Just below that add:
//check the soundscape editor
if (g_SoundscapeEditorInterface->GetIsPlaying())
g_SoundscapeEditorInterface->RestartSoundscape();
Now go to the function void C_SoundscapeSystem::Update( float frametime ) (around line 323). And at the very end of the function add:
//Update the debug panel/stuff
if (g_SoundscapeEditorInterface->_Debug_ShouldSendItems())
g_SoundscapeEditorInterface->Debug_UpdatePanel();
Now find the function void C_SoundscapeSystem::StartNewSoundscape( KeyValues *pSoundscape ) (around line 374) and replace:
int i;
// Reset the system
// fade out the current loops
for ( i = m_loopingSounds.Count()-1; i >= 0; --i )
{
m_loopingSounds[i].volumeTarget = 0;
if ( !pSoundscape )
{
// if we're cancelling the soundscape, stop the sound immediately
m_loopingSounds[i].volumeCurrent = 0;
}
}
With:
//check for the soundscape editor global enabled bool
if (g_SoundscapeEditorInterface->GetIsPlaying())
{
//out a message
ConDMsg("Cant play soundscape: '%s' as the soundscape editor panel is in use!\n", pSoundscape ? pSoundscape->GetName() : "(null)");
return;
}
//check for debug. If so then log an event
if (g_SoundscapeEditorInterface->_Debug_ShouldSendItems() && pSoundscape)
{
//log event
g_SoundscapeEditorInterface->Debug_AddLogEvent(CFmtStr("\n\n================================================\n%.2f - Starting new soundscape: %s\n================================================\n\n", gpGlobals->curtime, pSoundscape->GetName()));
}
//clear the soundscape debug panel
g_SoundscapeEditorInterface->Debug_ClearEvents();
int i;
// Reset the system
// fade out the current loops
for ( i = m_loopingSounds.Count()-1; i >= 0; --i )
{
m_loopingSounds[i].volumeTarget = 0;
if ( !pSoundscape )
{
// if we're cancelling the soundscape, stop the sound (almost) immediately
m_loopingSounds[i].volumeCurrent = 0.01f;
}
}
And at the very end of the function add
//tell the debug panel
if (g_SoundscapeEditorInterface->_Debug_ShouldSendItems())
{
//write a newline to seperate the 'adding' items from the 'playing' items
g_SoundscapeEditorInterface->Debug_AddLogEvent(g_SoundscapeEditorInterface->m_DebugColor_AddItem, CFmtStr("%.2f - Finished Adding Items!\n\n", gpGlobals->curtime));
}
Now find the function: void C_SoundscapeSystem::StartSubSoundscape( KeyValues *pSoundscape, subsoundscapeparams_t ¶ms ) (around line 421). At the very start of the function and just above
// Parse/process all of the commands
KeyValues *pKey = pSoundscape->GetFirstSubKey();
add:
if (!pSoundscape)
return;
//always add Default_Mix if Soundmixer isnt being overriden
if (!pSoundscape->FindKey("Soundmixer") && params.recurseLevel == 0)
pSoundscape->SetString("Soundmixer", "Default_Mix");
Now inside the same function. replace
else if ( !Q_strcasecmp( pKey->GetName(), "playlooping" ) )
{
ProcessPlayLooping( pKey, params );
}
else if ( !Q_strcasecmp( pKey->GetName(), "playrandom" ) )
{
ProcessPlayRandom( pKey, params );
}
With
else if ( !Q_strcasecmp( pKey->GetName(), "playlooping" ) )
{
ProcessPlayLooping( pKey, params, pSoundscape->GetName());
}
else if ( !Q_strcasecmp( pKey->GetName(), "playrandom" ) )
{
ProcessPlayRandom( pKey, params, pSoundscape->GetName() );
}
Now find the function void C_SoundscapeSystem::ProcessPlayLooping( KeyValues *pAmbient, const subsoundscapeparams_t ¶ms ) And replace the function decleration with:
void C_SoundscapeSystem::ProcessPlayLooping( KeyValues *pAmbient, const subsoundscapeparams_t ¶ms, const char* SoundscapeName )
At the very end of the same function. Replace:
if ( positionIndex < 0 )
{
AddLoopingAmbient( pSoundName, volume, pitch );
}
else
{
if ( positionIndex > 31 || !(m_params.localBits & (1<<positionIndex) ) )
{
// suppress sounds if the position isn't available
//DevMsg( 1, "Bad position %d\n", positionIndex );
return;
}
AddLoopingSound( pSoundName, false, volume, soundlevel, pitch, m_params.localSound[positionIndex] );
}
With
if ( positionIndex < 0 )
{
AddLoopingAmbient( pSoundName, volume, pitch, SoundscapeName);
}
else
{
if ( positionIndex > 31 || !(m_params.localBits & (1<<positionIndex) ) )
{
// suppress sounds if the position isn't available
//DevMsg( 1, "Bad position %d\n", positionIndex );
if (g_SoundscapeEditorInterface->_Debug_ShouldSendItems())
g_SoundscapeEditorInterface->Debug_AddLogEvent(g_SoundscapeEditorInterface->m_DebugColor_Warning, CFmtStr("%.2f - Warning: Failed to get position '%d' for '%s' soundscape '%s'\n", gpGlobals->curtime, positionIndex, V_GetFileName(pSoundName), SoundscapeName));
//just play it as an ambient
AddLoopingAmbient(pSoundName, volume, pitch, SoundscapeName);
return;
}
AddLoopingSound( pSoundName, false, volume, soundlevel, pitch, m_params.localSound[positionIndex], SoundscapeName);
}
Now in the same file again, Find void C_SoundscapeSystem::ProcessPlayRandom( KeyValues *pPlayRandom, const subsoundscapeparams_t ¶ms ) And replace that with
void C_SoundscapeSystem::ProcessPlayRandom( KeyValues *pPlayRandom, const subsoundscapeparams_t ¶ms, const char* SoundscapeNames)
At the very end of the same function. Replace:
if ( positionIndex < 0 && !randomPosition )
{
sound.isAmbient = true;
AddRandomSound( sound );
}
else
{
sound.isAmbient = false;
if ( randomPosition )
{
sound.isRandom = true;
}
else
{
if ( positionIndex > 31 || !(m_params.localBits & (1<<positionIndex) ) )
{
// suppress sounds if the position isn't available
//DevMsg( 1, "Bad position %d\n", positionIndex );
return;
}
sound.position = m_params.localSound[positionIndex];
}
AddRandomSound( sound );
}
With
if ( positionIndex < 0 && !randomPosition )
{
sound.isAmbient = true;
AddRandomSound( sound, SoundscapeNames);
}
else
{
sound.isAmbient = false;
if ( randomPosition )
{
sound.isRandom = true;
}
else
{
if ( positionIndex > 31 || !(m_params.localBits & (1<<positionIndex) ) )
{
// suppress sounds if the position isn't available
//DevMsg( 1, "Bad position %d\n", positionIndex );
if (g_SoundscapeEditorInterface->_Debug_ShouldSendItems())
g_SoundscapeEditorInterface->Debug_AddLogEvent(g_SoundscapeEditorInterface->m_DebugColor_Warning, CFmtStr("%.2f - Warning: Failed to get position '%d' for playrandom soundscape '%s'\n", gpGlobals->curtime, positionIndex, SoundscapeNames));
//just play as an ambient
sound.isAmbient = true;
AddRandomSound(sound, SoundscapeNames);
return;
}
sound.position = m_params.localSound[positionIndex];
}
AddRandomSound( sound, SoundscapeNames);
}
Now find the function void C_SoundscapeSystem::ProcessPlaySoundscape( KeyValues *pPlaySoundscape, subsoundscapeparams_t ¶msIn ) (around line 817) and at the end of the function. Replace
KeyValues *pSoundscapeKeys = SoundscapeByIndex( FindSoundscapeByName( pSoundscapeName ) );
if ( pSoundscapeKeys )
{
StartSubSoundscape( pSoundscapeKeys, subParams );
}
else
{
DevMsg( 1, "Trying to play unknown soundscape %s\n", pSoundscapeName );
}
With
KeyValues* pSoundscapeKeys = SoundscapeByIndex(FindSoundscapeByName(pSoundscapeName));
if (pSoundscapeKeys)
{
//check for the debug editor
if (g_SoundscapeEditorInterface->_Debug_ShouldSendItems())
{
//log the event
g_SoundscapeEditorInterface->Debug_AddLogEvent(g_SoundscapeEditorInterface->m_DebugColor_AddItem, CFmtStr("%.2f - Adding new sub-soundscape (%s)\n", gpGlobals->curtime, pSoundscapeName));
}
StartSubSoundscape(pSoundscapeKeys, subParams);
}
else
{
//check for the debug editor
if (g_SoundscapeEditorInterface->_Debug_ShouldSendItems())
{
//log the event
g_SoundscapeEditorInterface->Debug_AddLogEvent(g_SoundscapeEditorInterface->m_DebugColor_Error, CFmtStr("%.2f - ERROR: Trying to start unknown sub-soundscape (%s)\n", gpGlobals->curtime, pSoundscapeName));
}
DevMsg(1, "Trying to play unknown soundscape %s\n", pSoundscapeName);
}
Now find the function:
// special kind of looping sound with no spatialization
int C_SoundscapeSystem::AddLoopingAmbient( const char *pSoundName, float volume, int pitch )
{
return AddLoopingSound( pSoundName, true, volume, SNDLVL_NORM, pitch, vec3_origin );
}
(around line 888) and replace that with:
// special kind of looping sound with no spatialization
int C_SoundscapeSystem::AddLoopingAmbient( const char *pSoundName, float volume, int pitch, const char* FromSoundscape)
{
return AddLoopingSound( pSoundName, true, volume, SNDLVL_NORM, pitch, vec3_origin, FromSoundscape);
}
Just below that. Replace the entirety of the int C_SoundscapeSystem::AddLoopingSound( const char *pSoundName, bool isAmbient, float volume, soundlevel_t soundlevel, int pitch, const Vector &position ) function with:
int C_SoundscapeSystem::AddLoopingSound(const char* pSoundName, bool isAmbient, float volume, soundlevel_t soundlevel, int pitch, const Vector& position, const char* FromSoundscape)
{
//get the fixed slashes version
char pSoundNameCompare[512];
Q_strncpy(pSoundNameCompare, pSoundName, sizeof(pSoundNameCompare));
Q_FixSlashes(pSoundNameCompare);
loopingsound_t* pSoundSlot = NULL;
int soundSlot = m_loopingSounds.Count() - 1;
bool bForceSoundUpdate = false;
while (soundSlot >= 0)
{
loopingsound_t& sound = m_loopingSounds[soundSlot];
//get the wave name with fixed slashes
char WavNameCompare[512];
Q_strncpy(WavNameCompare, sound.pWaveName, sizeof(WavNameCompare));
Q_FixSlashes(WavNameCompare);
// NOTE: Will always restart/crossfade positional sounds
if (sound.id != m_loopingSoundId &&
!Q_strcasecmp(pSoundNameCompare, WavNameCompare))
{
//check the pitch
if (sound.pitch != pitch)
{
StopLoopingSound(sound);
// Start the new sound immediately.
if (isAmbient)
{
enginesound->EmitAmbientSound(pSoundName, sound.volumeCurrent, pitch);
}
else
{
CLocalPlayerFilter filter;
EmitSound_t ep;
ep.m_nChannel = CHAN_STATIC;
ep.m_pSoundName = pSoundName;
ep.m_flVolume = sound.volumeCurrent;
ep.m_SoundLevel = soundlevel;
ep.m_nPitch = pitch;
ep.m_pOrigin = &position;
C_BaseEntity::EmitSound(filter, SOUND_FROM_WORLD, ep);
sound.volumeCurrent = sound.volumeCurrent;
}
pSoundSlot = &sound;
break;
}
// Ambient sounds can reuse the slots.
if (isAmbient == true &&
sound.isAmbient == true)
{
// reuse this sound
pSoundSlot = &sound;
break;
}
// Positional sounds can reuse the slots if the positions are the same.
else if (isAmbient == sound.isAmbient)
{
if (VectorsAreEqual(position, sound.position, 0.1f) && soundlevel == sound.soundlevel)
{
// reuse this sound
pSoundSlot = &sound;
break;
}
else
{
// If it's trying to fade out one positional sound and fade in another, then it gets screwy
// because it'll be sending alternating commands to the sound engine, referencing the same sound
// (SOUND_FROM_WORLD, CHAN_STATIC, pSoundName). One of the alternating commands will be as
// it fades the sound out, and one will be fading the sound in.
// Because this will occasionally cause the sound to vanish entirely, we stop the old sound immediately.
StopLoopingSound(sound);
pSoundSlot = &sound;
// make a note to update the sound immediately. Otherwise, if its volume happens to be
// the same as the old sound's volume, it will never update at all.
bForceSoundUpdate = true;
break;
}
}
}
soundSlot--;
}
if (soundSlot < 0)
{
// can't find the sound in the list, make a new one
soundSlot = m_loopingSounds.AddToTail();
if (isAmbient)
{
// start at 0 and fade in
enginesound->EmitAmbientSound(pSoundName, 0, pitch);
m_loopingSounds[soundSlot].volumeCurrent = 0.0;
}
else
{
// non-ambients at 0 volume are culled, so start at 0.05
CLocalPlayerFilter filter;
EmitSound_t ep;
ep.m_nChannel = CHAN_STATIC;
ep.m_pSoundName = pSoundName;
ep.m_flVolume = 0.05;
ep.m_SoundLevel = soundlevel;
ep.m_nPitch = pitch;
ep.m_pOrigin = &position;
C_BaseEntity::EmitSound(filter, SOUND_FROM_WORLD, ep);
m_loopingSounds[soundSlot].volumeCurrent = 0.05;
}
}
// fill out the slot
loopingsound_t& sound = m_loopingSounds[soundSlot];
Q_strncpy(sound.pWaveName, pSoundName, sizeof(sound.pWaveName));
sound.volumeTarget = volume;
sound.pitch = pitch;
sound.id = m_loopingSoundId;
sound.isAmbient = isAmbient;
sound.position = position;
sound.soundlevel = soundlevel;
if (bForceSoundUpdate)
{
UpdateLoopingSound(sound);
}
//create the debug event
if (g_SoundscapeEditorInterface->_Debug_ShouldSendItems())
{
SoundscapeDebugEvent_t DebugEvent;
memset(&DebugEvent, 0, sizeof(DebugEvent));
Q_strncpy(DebugEvent.name, CFmtStr("playlooping (%s)", V_GetFileName(sound.pWaveName)), sizeof(DebugEvent.name));
Q_strncpy(DebugEvent.FromSoundscape, FromSoundscape, sizeof(DebugEvent.FromSoundscape));
Q_strncpy(DebugEvent.LoopingData.SoundName, sound.pWaveName, sizeof(DebugEvent.LoopingData.SoundName));
DebugEvent.Pitch = sound.pitch;
DebugEvent.Position = sound.position;
DebugEvent.Volume = sound.volumeTarget;
DebugEvent.EventType = SoundscapeDebugEventType_e::TYPE_PLAYLOOPING;
//send to the debug panel
g_SoundscapeEditorInterface->Debug_AddEvent(DebugEvent);
//log the event
g_SoundscapeEditorInterface->Debug_AddLogEvent(g_SoundscapeEditorInterface->m_DebugColor_PlayItem, CFmtStr("%.2f - Adding new looping sound %s Volume = %.2f, Pitch = %d, Position = {%.2f %.2f %.2f}\n", gpGlobals->curtime, V_GetFileName(sound.pWaveName), sound.volumeTarget, sound.pitch, sound.position.x, sound.position.y, sound.position.z));
}
return soundSlot;
}
Next find the function: int C_SoundscapeSystem::AddRandomSound( const randomsound_t &sound ) (around line 1030) and replace the entire function with:
// add a recurring random sound event
int C_SoundscapeSystem::AddRandomSound( const randomsound_t &sound, const char* FromSoundscape )
{
int index = m_randomSounds.AddToTail( sound );
m_randomSounds[index].nextPlayTime = gpGlobals->curtime + 0.5 * RandomInterval( sound.time );
//create the debug event
if (g_SoundscapeEditorInterface->_Debug_ShouldSendItems())
{
SoundscapeDebugEvent_t DebugEvent;
memset(&DebugEvent, 0, sizeof(DebugEvent));
Q_strncpy(DebugEvent.name, CFmtStr("playrandom (first = %s)", V_GetFileName(sound.pWaves->GetString())), sizeof(DebugEvent.name));
Q_strncpy(DebugEvent.FromSoundscape, FromSoundscape, sizeof(DebugEvent.FromSoundscape));
DebugEvent.RandomWaveData.NextPlayTime = m_randomSounds[index].nextPlayTime;
DebugEvent.RandomWaveData.Waves = m_randomSounds[index].pWaves;
DebugEvent.EventType = SoundscapeDebugEventType_e::TYPE_PLAYRANDOM;
//send to the debug panel
g_SoundscapeEditorInterface->Debug_AddEvent(DebugEvent);
//log the event
g_SoundscapeEditorInterface->Debug_AddLogEvent(g_SoundscapeEditorInterface->m_DebugColor_AddItem, CFmtStr("%.2f - Adding new random sound set (number of sounds = %d for sound slot %d)\n", gpGlobals->curtime, m_randomSounds[index].waveCount, index));
}
return index;
}
Next. Find the function: void C_SoundscapeSystem::PlayRandomSound( randomsound_t &sound ) (around line 1504) and replace the entire function with
// play a random sound randomly from this parameterization table
void C_SoundscapeSystem::PlayRandomSound( randomsound_t &sound, int index )
{
Assert( sound.waveCount > 0 );
int waveId = random->RandomInt( 0, sound.waveCount-1 );
KeyValues *pWaves = sound.pWaves;
while ( waveId > 0 && pWaves )
{
pWaves = pWaves->GetNextKey();
waveId--;
}
if ( !pWaves )
return;
const char *pWaveName = pWaves->GetString();
if ( !pWaveName )
return;
int pitch = (int)RandomInterval(sound.pitch);
float volume = sound.masterVolume * RandomInterval(sound.volume);
if (sound.isRandom)
{
sound.position = GenerateRandomSoundPosition();
}
if ( sound.isAmbient )
{
enginesound->EmitAmbientSound( pWaveName, sound.masterVolume * RandomInterval( sound.volume ), (int)RandomInterval( sound.pitch ) );
}
else
{
CLocalPlayerFilter filter;
EmitSound_t ep;
ep.m_nChannel = CHAN_STATIC;
ep.m_pSoundName = pWaveName;
ep.m_pSoundName = pWaveName;
ep.m_flVolume = volume;
ep.m_SoundLevel = (soundlevel_t)(int)RandomInterval(sound.soundlevel);
ep.m_nPitch = pitch;
if (sound.isRandom)
{
sound.position = GenerateRandomSoundPosition();
}
ep.m_pOrigin = &sound.position;
C_BaseEntity::EmitSound( filter, SOUND_FROM_WORLD, ep );
}
//tell the debug panel
if (g_SoundscapeEditorInterface->_Debug_ShouldSendItems())
{
//update
g_SoundscapeEditorInterface->Debug_UpdateRandomSound(index, pWaveName, sound.position, enginesound->GetSoundDuration(pWaveName), volume, pitch, sound.nextPlayTime);
//log
g_SoundscapeEditorInterface->Debug_AddLogEvent(g_SoundscapeEditorInterface->m_DebugColor_PlayItem, CFmtStr("%.2f - Playing random sound %s, Volume = %.2f, Pitch = %d (Next random sound at %.2f)\n", gpGlobals->curtime, V_GetFileName(pWaveName), volume, pitch, sound.nextPlayTime));
}
}
Finally. Inside the function: void C_SoundscapeSystem::UpdateRandomSounds( float gameTime ) replace:
// UNDONE: add this in to fix range?
// float dt = m_randomSounds[i].nextPlayTime - gameTime;
PlayRandomSound( m_randomSounds[i] );
// now schedule the next occurrance
// UNDONE: add support for "play once" sounds? FastRemove() here.
m_randomSounds[i].nextPlayTime = gameTime + RandomInterval( m_randomSounds[i].time );
with
// now schedule the next occurrance
// UNDONE: add support for "play once" sounds? FastRemove() here.
m_randomSounds[i].nextPlayTime = gameTime + RandomInterval(m_randomSounds[i].time);
// UNDONE: add this in to fix range?
// float dt = m_randomSounds[i].nextPlayTime - gameTime;
PlayRandomSound( m_randomSounds[i], i );
Adding The Position Editor
First off, You are going to want to head over too game/client/in_main.cpp. At the very top of the file and under #include <vgui/ISurface.h> you are going to want to add:
//soundscape maker
#include "soundscape_maker/SoundscapeMaker.h"
Now find the function int CInput::KeyEvent( int down, ButtonCode_t code, const char *pszCurrentBinding ) (should be around line 595). At the very top of the function add:
//check for the soundscape editor
if (down && g_SoundscapeEditorInterface->PositionEditor_IsEnabled())
{
if (g_SoundscapeEditorInterface->PositionEditor_HandleKeyPress(code))
return 0;
}
Now go to game/client/c_baseplayer.h and find the function decleration virtual void PreThink( void ); (around line 249) and above it add:
virtual void ClientThink( void ) override;
Now go to game/client/c_baseplayer.cpp and just below #include "haptics/ihaptics.h" (around the top of the file) add:
//soundscape maker
#include "fmtstr.h"
#include "debugoverlay_shared.h"
#include "soundscape_maker/SoundscapeMaker.h"
Finally. just above the function void C_BasePlayer::PreThink( void ) (around line 1985) add this:
// Called when the client thinks
void C_BasePlayer::ClientThink()
{
//check if we are in the soundscape editor
if (g_SoundscapeEditorInterface->PositionEditor_IsEnabled())
{
//get the position
Vector position = g_SoundscapeEditorInterface->PositionEditor_GetPositionFromPlayer();
Vector eyePosition = EyePosition();
eyePosition.z -= 8;
//finally draw the debug overlays
NDebugOverlay::Line(eyePosition, position, 0, 255, 100, false, NDEBUG_PERSIST_TILL_NEXT_SERVER);
NDebugOverlay::Axis(position, vec3_angle, 16, false, NDEBUG_PERSIST_TILL_NEXT_SERVER);
NDebugOverlay::Text(position, CFmtStr("Position: %d", g_SoundscapeEditorInterface->PositionEditor_GetCurrentSelectedPosition()), false, NDEBUG_PERSIST_TILL_NEXT_SERVER);
}
//draw each position
if (g_SoundscapeEditorInterface->PositionEditor_IsEnabled() || g_SoundscapeEditorInterface->PositionEditor_IsInDebug())
{
for (int i = 0; i < MAXIMUM_SOUNDSCAPE_POSITIONS; i++)
{
//do NOT draw the current working position if we are currently in the editor
if (g_SoundscapeEditorInterface->PositionEditor_IsEnabled() && i == g_SoundscapeEditorInterface->PositionEditor_GetCurrentSelectedPosition())
continue;
//get the position
SoundscapeEditorPosition_t& position = g_SoundscapeEditorInterface->PositionEditor_GetPosition(i);
if (!position.in_use)
continue;
//get the position
Vector vec = position.position;
//draw all the items
NDebugOverlay::Axis(vec, vec3_angle, 16, false, NDEBUG_PERSIST_TILL_NEXT_SERVER);
//get the distance from the player and move the text up
float offset = MAX(1.0f, GetPlayerDistanceFromPoint(vec) * 0.02f);
vec.z += offset;
//draw
NDebugOverlay::Text(vec, CFmtStr("Origin: %f %f %f", vec.x, vec.y, vec.z), false, NDEBUG_PERSIST_TILL_NEXT_SERVER);
//move vec down 1 then draw
vec.z -= offset * 1.0f;
NDebugOverlay::Text(vec, CFmtStr("Position: %d", i), false, NDEBUG_PERSIST_TILL_NEXT_SERVER);
}
}
}
The Resource Files
Before you start the making soundscapes. You must either manually create or download and place the resource files into your mods resource/vgui base directory (not the source code directory but your actual mod directory)
- SoundscapeEditorData.res
- SoundscapeEditorDataPanel.res
- SoundscapeEditorPanel.res
- SoundscapeEditorPanelClipboardDialog.res
- SoundscapeEditorPanelDebugDialog.res
- SoundscapeEditorPanelFindSoundscapesDialog.res
- SoundscapeEditorPanelManifestEditorDialog.res
- SoundscapeEditorPanelPositionDialog.res
- SoundscapeEditorPanelPositionListData.res
- SoundscapeEditorPanelPreloadedSoundscapesDialog.res
- SoundscapeEditorPanelSetNameDialog.res
- SoundscapeEditorPanelTextEditorDialog.res
- SoundscapeSelectorPanel.res
- SoundSelectorPanel.res
Or download them here: ["Resource Files.rar"] and place them inside your mods resource directory.
Final Message
Now the panel should be all done. to open the panel in game use the modbase_soundscape_panel command and the panel should show.
For information on how to use the vgui panel head over to: