📄 chsound.cpp
字号:
pMsg = new ChSendWorldCmdMsg( PUEBLO_SOUND_CMD, strMD5, strAction,
strParams );
ASSERT( pMsg );
GetCore()->DispatchMsg( GetWorldID(), *pMsg );
delete pMsg;
}
void ChSoundMainInfo::OnPlayComplete( DeviceType deviceType )
{
ASSERT( devAll != deviceType );
/* Play the next sound in the
queue */
PlayFromQueue( deviceType );
}
/*----------------------------------------------------------------------------
ChSoundMainInfo protected methods
----------------------------------------------------------------------------*/
void ChSoundMainInfo::SetWorldID( const ChModuleID& idModule )
{
m_idWorldModule = idModule;
}
void ChSoundMainInfo::HandleSoundFile( const string& strFile, int iMimeType,
ChSoundInfo* pSoundInfo )
{
bool boolVolume;
chuint16 suVolume;
bool boolPlaying;
ASSERT( pSoundInfo );
boolVolume = pSoundInfo->IsVolumeValid();
suVolume = pSoundInfo->GetVolume();
pSoundInfo->SetFilename( strFile );
switch( iMimeType )
{
case ChHTTPConn::typeMidi:
{
if (boolVolume)
{ /* Adjust the volume for this
device */
suVolume = GetScaledVolume( suVolume, GetMaxMidi() );
pSoundInfo->SetVolume( suVolume );
}
/* Set the device into the sound
info object */
pSoundInfo->SetDeviceType( devMidi );
boolPlaying = m_boolMidiDeviceInUse;
break;
}
case ChHTTPConn::typeWave:
{
if (boolVolume)
{ /* Adjust the volume for this
device */
suVolume = GetScaledVolume( suVolume, GetMaxWave() );
pSoundInfo->SetVolume( suVolume );
}
/* Set the device into the sound
info object */
pSoundInfo->SetDeviceType( devWave );
boolPlaying = m_boolWaveDeviceInUse;
break;
}
#if defined( CH_USE_VOXWARE )
case ChHTTPConn::typeVox:
{
if (boolVolume)
{ /* Adjust the volume for this
device */
suVolume = GetScaledVolume( suVolume, GetMaxSpeech() );
pSoundInfo->SetVolume( suVolume );
}
/* Set the device into the sound
info object */
pSoundInfo->SetDeviceType( devSpeech );
boolPlaying = m_boolWaveDeviceInUse;
break;
}
#endif // defined( CH_USE_VOXWARE )
default:
{
break;
}
}
if (boolPlaying && (pSoundInfo->GetOptions() & soundOptQueue))
{
/* Add the item to the correct
device queue */
GetDeviceQueue()->AddItem( pSoundInfo->GetDeviceType(), *pSoundInfo );
}
else
{ // Play the sound immediately
DoPlay( pSoundInfo );
}
}
void ChSoundMainInfo::PlayFromQueue( DeviceType queueDevType )
{
ChSoundInfo info;
ASSERT( devAll != queueDevType );
if (!GetDeviceQueue()->IsEmpty( queueDevType ))
{
GetDeviceQueue()->GetNextItem( queueDevType, info );
ASSERT( devAll != info.GetDeviceType() );
DoPlay( &info );
}
}
bool ChSoundMainInfo::DoPlay( const ChSoundInfo* pInfo )
{
bool boolSuccess;
// Stop any playing sounds
if (devMidi == pInfo->GetDeviceType())
{
GetMidiPlayer()->Stop();
}
else
{
GetWavePlayer()->Stop();
#if defined( CH_USE_VOXWARE )
{
GetSpeechPlayer()->Stop();
}
#endif // defined( CH_USE_VOXWARE )
}
// Play the requested sound
switch( pInfo->GetDeviceType() )
{
case devMidi:
{
boolSuccess = GetMidiPlayer()->Play( pInfo );
break;
}
case devWave:
{
boolSuccess = GetWavePlayer()->Play( pInfo );
break;
}
#if defined( CH_USE_VOXWARE )
case devSpeech:
{
boolSuccess = GetSpeechPlayer()->Play( pInfo );
break;
}
#endif // defined( CH_USE_VOXWARE )
default:
{
break;
}
}
return boolSuccess;
}
/*----------------------------------------------------------------------------
FUNCTION || ChSoundMainInfo::InstallHooks
boolInstall || 'true' to install the hooks, and false to uninstall them.
------------------------------------------------------------------------------
This method will install or uninstall the hooks into other modules.
----------------------------------------------------------------------------*/
void ChSoundMainInfo::InstallHooks( bool boolInstall )
{
if (boolInstall != m_boolHooksInstalled)
{ // We're changing state
if (boolInstall)
{
InstallHook( CH_MSG_CMD, GetModuleID() );
InstallHook( CH_MSG_INLINE, GetModuleID() );
m_boolHooksInstalled = true;
}
else
{
UninstallHook( CH_MSG_CMD, GetModuleID() );
UninstallHook( CH_MSG_INLINE, GetModuleID() );
m_boolHooksInstalled = false;
}
}
}
/*----------------------------------------------------------------------------
FUNCTION || ChSoundMainInfo::DoSoundCommand
strCommand || Sound command to perform.
strURL || URL parameter to sound command. (May be empty.)
strDevice || Indicates the affected device. May be empty, or one
of the following values:
midi
wave
boolVolume || true if the volume is valid.
suVolume || Volume setting.
RETURNS || true if the command was processed.
------------------------------------------------------------------------------
This method will perform a sound command.
----------------------------------------------------------------------------*/
bool ChSoundMainInfo::DoSoundCommand( const string& strCommand,
const string& strURL,
const string& strDevice,
bool boolVolume, chuint16 suVolume,
chflag32 flOptions, chflag32 flEvents,
const string& strAction,
const string& strMD5 )
{
bool boolProcessed = true;
DeviceType device = ParseDevice( strDevice );
int iCommand = ParseSoundCmd( strCommand );
switch( iCommand )
{
case sndPlay:
case sndLoop:
{
if (UseSound())
{
if (!strURL.IsEmpty())
{
ChSoundInfo* pSoundInfo;
bool boolSuccess;
pSoundInfo = new ChSoundInfo( (sndLoop == iCommand),
boolVolume, suVolume,
flOptions, flEvents,
strAction, strMD5 );
boolSuccess = GetCore()->GetURL( strURL, 0, m_pSoundStream,
(chparam)pSoundInfo );
boolProcessed = true;
}
else
{
TRACE( "Sound played (or looped) without an 'href' field.\n" );
}
}
break;
}
case sndStop:
{
if ((devAll == device) || (devMidi == device))
{
GetMidiPlayer()->Stop();
}
if ((devAll == device) || (devWave == device))
{
GetWavePlayer()->Stop();
}
break;
}
case sndStopLoop:
{
if ((devAll == device) || (devMidi == device))
{
GetMidiPlayer()->StopLoop();
}
if ((devAll == device) || (devWave == device))
{
GetWavePlayer()->StopLoop();
}
break;
}
default:
{
boolProcessed = false;
break;
}
}
return boolProcessed;
}
bool ChSoundMainInfo::DoVolumeCommand( DeviceType device,
chuint16 suVolume )
{
if (UseSound())
{ // Set the volume
if ((devAll == device) || (devMidi == device))
{
// Save the unscaled volume
m_suMidiVolume = suVolume;
suVolume = GetScaledVolume( m_suMidiVolume, GetMaxMidi() );
GetMidiPlayer()->SetVolume( suVolume );
}
if ((devAll == device) || (devWave == device))
{
// Save the unscaled volume
m_suWaveVolume = suVolume;
suVolume = GetScaledVolume( m_suWaveVolume, GetMaxWave() );
GetWavePlayer()->SetVolume( suVolume );
}
#if defined( CH_USE_VOXWARE )
{
if ((devAll == device) || (devSpeech == device))
{
// Save the unscaled volume
m_suSpeechVolume = suVolume;
suVolume = GetScaledVolume( m_suSpeechVolume, GetMaxSpeech() );
GetSpeechPlayer()->SetVolume( suVolume );
}
}
#endif // defined( CH_USE_VOXWARE )
}
return true;
}
bool ChSoundMainInfo::GetVolume( const string& strVolume,
chuint16& suVolume )
{
bool boolTranslated;
if (!strVolume.IsEmpty() && isdigit( strVolume[0] ))
{
int iValue = atoi( strVolume );
// Range check
if (iValue <= 0)
{
iValue = 0;
}
else if (iValue > MAX_VOLUME_SETTING)
{
iValue = MAX_VOLUME_SETTING;
}
boolTranslated = true;
suVolume = (chuint16)iValue;
}
else
{
boolTranslated = false;
}
return boolTranslated;
}
void ChSoundMainInfo::UpdateVolume()
{
chint32 lVolume;
chuint16 suVolume;
m_reg.Read( SOUND_PREFS_MUSIC_VOLUME, lVolume, VOLUME_MAX_RANGE );
m_suMaxMidi = (chuint16)lVolume;
m_reg.Read( SOUND_PREFS_EFFECTS_VOLUME, lVolume, VOLUME_MAX_RANGE );
m_suMaxWave = (chuint16)lVolume;
m_reg.Read( SOUND_PREFS_ALERT_VOLUME, lVolume, VOLUME_MAX_RANGE );
m_suAlertVolume = (chuint16)lVolume;
// Now rescale the volume
suVolume = GetScaledVolume( m_suMidiVolume, GetMaxMidi() );
GetMidiPlayer()->SetVolume( suVolume );
suVolume = GetScaledVolume( m_suWaveVolume, GetMaxWave() );
GetWavePlayer()->SetVolume( suVolume );
}
#if defined( CH_USE_VOXWARE )
void ChSoundMainInfo::UpdateSpeechVolume()
{
chint32 lVolume;
chuint16 suVolume;
m_reg.Read( SOUND_PREFS_SPEECH_VOLUME, lVolume, VOLUME_MAX_RANGE );
m_suSpeechVolume = (chuint16)lVolume;
// Now rescale the volume
suVolume = GetScaledVolume( m_suWaveVolume, GetMaxSpeech() );
GetSpeechPlayer()->SetVolume( suVolume );
}
#endif // defined( CH_USE_VOXWARE )
#if defined( CH_USE_VOXWARE )
bool ChSoundMainInfo::DoSpeechCommand( const string& strCommand,
const string& strHost,
chuint16 suPort,
const string& strCallId,
chflag32 flOptions,
chflag32 flRemoteOptions )
{
int iCommand = ParseSpeechCmd( strCommand );
bool boolProcessed = true;
switch( iCommand )
{
case speechCall:
{
if (strHost.IsEmpty())
{
boolProcessed = false;
}
else
{
m_pTNT->MakeCall( strCallId, strHost, suPort, flOptions,
flRemoteOptions );
}
break;
}
case speechClose:
{
m_pTNT->Hangup( strCallId );
break;
}
default:
{
boolProcessed = false;
break;
}
}
return boolProcessed;
}
#endif // defined( CH_USE_VOXWARE )
/*----------------------------------------------------------------------------
ChSoundMainInfo private methods
----------------------------------------------------------------------------*/
void ChSoundMainInfo::RegisterDispatchers()
{
chint16 sHandlerCount = sizeof( soundHandlers ) /
sizeof( ChMsgHandlerDesc );
m_soundDispatcher.AddHandler( soundHandlers, sHandlerCount );
}
void ChSoundMainInfo::CreateMenus()
{
m_boolMenus = true;
}
void ChSoundMainInfo::InstallMenus()
{
}
void ChSoundMainInfo::UninstallMenus()
{
}
void ChSoundMainInfo::DestroyMenus()
{
m_boolMenus = false;
}
void ChSoundMainInfo::LoadWorldModule()
{
LoadClientModule( CH_MODULE_WORLD, CH_MODULE_WORLD_BASE,
GetModuleID() );
}
void ChSoundMainInfo::UnloadWorldModule()
{
if (m_idWorldModule)
{
UnloadClientModule( m_idWorldModule );
m_idWorldModule = 0;
}
}
int ChSoundMainInfo::ParseSoundCmd( const string& strCommand )
{
string strCmd( strCommand );
strCmd.MakeLower();
if (strCmd == SND_CMD_PLAY)
{
return sndPlay;
}
else if (strCmd == SND_CMD_LOOP)
{
return sndLoop;
}
else if (strCmd == SND_CMD_STOP)
{
return sndStop;
}
else if (strCmd == SND_CMD_STOPLOOP)
{
return sndStopLoop;
}
return 0;
}
#if defined( CH_USE_VOXWARE )
int ChSoundMainInfo::ParseSpeechCmd( const string& strCommand )
{
string strCmd( strCommand );
strCmd.MakeLower();
if (strCmd == SPEECH_CMD_CALL)
{
return speechCall;
}
else if (strCmd == SPEECH_CMD_CLOSE)
{
return speechClose;
}
return 0;
}
#endif // defined( CH_USE_VOXWARE )
DeviceType ChSoundMainInfo::ParseDevice( const string& strDevice )
{
string strDev( strDevice );
strDev.MakeLower();
if (strDev == "wave")
{
return devWave;
}
else if (strDev == "midi")
{
return devMidi;
}
#if defined( CH_USE_VOXWARE )
else if (strDev == "speech")
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -