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

📄 chsound.cpp

📁 Windows上的MUD客户端程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*----------------------------------------------------------------------------
                        _                              _ _       
        /\             | |                            | (_)      
       /  \   _ __   __| |_ __ ___  _ __ ___   ___  __| |_  __ _ 
      / /\ \ | '_ \ / _` | '__/ _ \| '_ ` _ \ / _ \/ _` | |/ _` |
     / ____ \| | | | (_| | | | (_) | | | | | |  __/ (_| | | (_| |
    /_/    \_\_| |_|\__,_|_|  \___/|_| |_| |_|\___|\__,_|_|\__,_|

    The contents of this file are subject to the Andromedia Public
	License Version 1.0 (the "License"); you may not use this file
	except in compliance with the License. You may obtain a copy of
	the License at http://www.andromedia.com/APL/

    Software distributed under the License is distributed on an
	"AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
	implied. See the License for the specific language governing
	rights and limitations under the License.

    The Original Code is Pueblo client code, released November 4, 1998.

    The Initial Developer of the Original Code is Andromedia Incorporated.
	Portions created by Andromedia are Copyright (C) 1998 Andromedia
	Incorporated.  All Rights Reserved.

	Andromedia Incorporated                         415.365.6700
	818 Mission Street - 2nd Floor                  415.365.6701 fax
	San Francisco, CA 94103

    Contributor(s):
	--------------------------------------------------------------------------
	   Chaco team:  Dan Greening, Glenn Crocker, Jim Doubek,
	                Coyote Lussier, Pritham Shetty.

					Wrote and designed original codebase.

------------------------------------------------------------------------------

	Defines the ChSound module for the Pueblo system.  This module is
	used to play MIDI music.

----------------------------------------------------------------------------*/

// $Header: /home/cvs/chaco/modules/client/msw/ChSound/ChSound.cpp,v 1.54 1996/12/04 07:44:05 pritham Exp $

#include "headers.h"
#if !defined(CH_PUEBLO_PLUGIN )
#include "resource.h"
#else
#include "vwrres.h"
#endif

#include "ChMPlay.h"

#ifdef CH_UNIX
#include <ChDispat.h>
#include <ChMsgTyp.h>
#include <ChReg.h>
#include "../../unix/ChTxtMud/UnixRes.h"
#endif // CH_UNIX

#include <fstream.h>

#include <ChMsgTyp.h>
#include <ChExcept.h>
#include <ChMenu.h>
#include <ChHtmWnd.h>
#include <ChCore.h>
#include <ChHtpCon.h>

#include <ChSound.h>
#include <ChWorld.h>

#include "ChSoundInfo.h"
#include "ChSoundStream.h"
#include "ChTNT.h"
#include "ChSpeechPrefs.h"
#include "ChSpeechStatus.h"


/*----------------------------------------------------------------------------
	Constants
----------------------------------------------------------------------------*/

#define TNT_PUEBLOSOUND_PARAM	"speech_port"

											/* Note that the following
												strings should be entirely
												in lower case */
#define ATTR_XCH_SOUND			"xch_sound"
#define ATTR_XCH_ALERT			"xch_alert"
#define ATTR_XCH_SPEECH			"xch_speech"
#define ATTR_XCH_DEVICE			"xch_device"
#define ATTR_XCH_VOLUME			"xch_volume"
#define ATTR_EVENT				"event"
#define ATTR_OPTIONS			"options"
#define ATTR_REMOTE_OPTIONS		"remote_options"
#define ATTR_ACTION				"action"
#define ATTR_MD5				"md5"
#define ATTR_HOST				"host"
#define ATTR_ID					"id"

#define SND_CMD_PLAY			"play"
#define SND_CMD_LOOP			"loop"
#define SND_CMD_STOP			"stop"
#define SND_CMD_STOPLOOP		"stoploop"

#if defined( CH_USE_VOXWARE )

#define SPEECH_CMD_CALL			"call"
#define SPEECH_CMD_CLOSE		"close"

#endif	// defined( CH_USE_VOXWARE )

#define MAX_VOLUME_SETTING		100

#define SOUND_HTML_DELIM		" ,;\t"


/*----------------------------------------------------------------------------
	Type definitions
----------------------------------------------------------------------------*/

typedef enum { invalid, xch_sound, xch_volume, xch_alert, href,
				xch_speech } CommandType;

											/* The following structure
												associates a pane option
												name with a flag bit */
typedef struct
{
	char*		pstrName;
	chflag32	flOption;

} OptionsNameType;


/*----------------------------------------------------------------------------
	Handler declarations
----------------------------------------------------------------------------*/

CH_DECLARE_MESSAGE_HANDLER( defSoundHandler )
CH_DECLARE_MESSAGE_HANDLER( soundInitHandler )
CH_DECLARE_MESSAGE_HANDLER( soundLoadCompleteHandler )
CH_DECLARE_MESSAGE_HANDLER( soundShowModuleHandler )
#if !defined(CH_PUEBLO_PLUGIN )
CH_DECLARE_MESSAGE_HANDLER( soundGetPageCountHandler )
CH_DECLARE_MESSAGE_HANDLER( soundGetPagesHandler )
CH_DECLARE_MESSAGE_HANDLER( soundGetPageDataHandler )
CH_DECLARE_MESSAGE_HANDLER( soundReleasePagesHandler )
#endif
CH_DECLARE_MESSAGE_HANDLER( soundCommandHandler )
CH_DECLARE_MESSAGE_HANDLER( soundInlineHandler )
CH_DECLARE_MESSAGE_HANDLER( soundAlertHandler )
CH_DECLARE_MESSAGE_HANDLER( soundMediaPlayHandler )
CH_DECLARE_MESSAGE_HANDLER( soundMediaStopHandler )
CH_DECLARE_MESSAGE_HANDLER( soundConnectedHandler )

static ChMsgHandlerDesc	soundHandlers[] =
					{	{CH_MSG_INIT, soundInitHandler},
						{CH_MSG_LOAD_COMPLETE, soundLoadCompleteHandler},
						{CH_MSG_SHOW_MODULE, soundShowModuleHandler},
#if !defined(CH_PUEBLO_PLUGIN )
						{CH_MSG_GET_PAGE_COUNT, soundGetPageCountHandler},
						{CH_MSG_GET_PAGES, soundGetPagesHandler},
						{CH_MSG_GET_PAGE_DATA, soundGetPageDataHandler},
						{CH_MSG_RELEASE_PAGES, soundReleasePagesHandler},
#endif
						{CH_MSG_CMD, soundCommandHandler},
						{CH_MSG_INLINE, soundInlineHandler},
						{CH_MSG_SOUND_ALERT, soundAlertHandler},
						{CH_MSG_MEDIA_PLAY, soundMediaPlayHandler},
						{CH_MSG_MEDIA_STOP, soundMediaStopHandler},
						{CH_MSG_CONNECTED, soundConnectedHandler} };


/*----------------------------------------------------------------------------
	Global variables
----------------------------------------------------------------------------*/

const OptionsNameType	soundEventsList[] =
									{	{ "complete", soundEvtComplete } };


const OptionsNameType	soundOptionsList[] =
									{	{ "queue", soundOptQueue } };
const OptionsNameType	speechOptionsList[] =
									{	{ "status", speechOptStatus },
										{ "sounds", speechOptSounds },
										{ "query", speechOptQuery } };



/*----------------------------------------------------------------------------
	ChSoundQueue public methods
----------------------------------------------------------------------------*/

void ChSoundQueue::AddItem( DeviceType deviceType, const ChSoundInfo& info )
{
	ChList<ChSoundInfo>*	pDevQueue = GetDeviceQueue( deviceType );

	pDevQueue->AddTail( info );
}


bool ChSoundQueue::GetNextItem( DeviceType deviceType, ChSoundInfo& info )
{
	ChList<ChSoundInfo>*	pDevQueue = GetDeviceQueue( deviceType );
	bool					boolFound;

	if (boolFound = !pDevQueue->IsEmpty())
	{
		info = pDevQueue->RemoveHead();
	}

	return boolFound;
}


/*----------------------------------------------------------------------------
	ChSoundMainInfo public methods
----------------------------------------------------------------------------*/

ChSoundMainInfo::ChSoundMainInfo( const ChModuleID& idModule, ChCore* pCore,
										const string& strLoadParam ) :
					ChMainInfo(  idModule,  pCore),
					m_reg( SOUND_PREFS_GROUP ),
					m_idWorldModule( 0 ),
					m_soundDispatcher( pCore, idModule, defSoundHandler ),
					m_strInitialCommand( strLoadParam ),
					m_suMidiVolume( 100 ),
					m_suWaveVolume( 100 ),
					m_boolShown( false ),
					m_boolMenus( false ),
					m_boolHooksInstalled( false ),
					m_boolMidiDeviceInUse( false ),
					m_boolWaveDeviceInUse( false ),
					m_midiPlayer( devMidi, &m_boolMidiDeviceInUse ),
					m_wavePlayer( devWave, &m_boolWaveDeviceInUse ),
					#if defined( CH_USE_VOXWARE )
					m_speechPlayer( devSpeech, &m_boolWaveDeviceInUse ),
					m_suSpeechVolume( 100 ),
					m_pSpeechStatus( 0 ),
					#endif	// defined( CH_USE_VOXWARE )
					m_lastAlertTime(),
					m_alertTime( &m_reg )
{
	chint32		lVolume;
	string		strDefAlertSound;
											// Set the sound stream

	m_pSoundStream = new ChSoundStreamManager( this ); 
	ASSERT( m_pSoundStream );

	RegisterDispatchers();

	m_midiPlayer.SetMainInfo( this );
	m_wavePlayer.SetMainInfo( this );

	#if defined( CH_USE_VOXWARE )
	{
		m_speechPlayer.SetMainInfo( this );
		m_pSpeechStatus = new ChSpeechStatus( this );
		m_pTNT = new ChTNT( m_pSpeechStatus );
	}
	#endif	// defined( CH_USE_VOXWARE )

	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;

	GetMidiPlayer()->SetVolume( m_suMaxMidi );
	GetWavePlayer()->SetVolume( m_suMaxWave );

	#if defined( CH_USE_VOXWARE )
	{
		m_reg.Read( SOUND_PREFS_SPEECH_VOLUME, lVolume, VOLUME_MAX_RANGE );
		m_suMaxSpeech = (chuint16)lVolume;

		GetSpeechPlayer()->SetVolume( m_suMaxSpeech );
	}
	#endif	// defined( CH_USE_VOXWARE )
											/* Pick a reasonable default alert
												sound */

	strDefAlertSound = GetSysSoundFilesPath() + "Tada.wav";
	m_reg.Read( SOUND_PREFS_ALERT_SOUND, m_strAlertSoundPath,
				strDefAlertSound );
											// Read whether we're disabled

	m_reg.ReadBool( SOUND_PREFS_DISABLED, m_boolDisableSound,
					SOUND_PREFS_DISABLED_DEF );
											// See if we have any sound devices

	m_boolSoundDeviceFound = (midiOutGetNumDevs() || waveOutGetNumDevs());

											// Get initial volume settings
	UpdateVolume();

	#if defined( CH_USE_VOXWARE )
	{
		UpdateSpeechVolume();
	}
	#endif	// defined( CH_USE_VOXWARE )
}


ChSoundMainInfo::~ChSoundMainInfo()
{
											// Stop any playing sounds
	GetMidiPlayer()->Stop();
	GetWavePlayer()->Stop();

	#if defined( CH_USE_VOXWARE )
	{
		GetSpeechPlayer()->Stop();
		
		delete m_pTNT;
		m_pTNT = 0;
	}
	#endif	// defined( CH_USE_VOXWARE )

	if (m_pSoundStream)
	{
		delete m_pSoundStream;
		m_pSoundStream = 0;
	}

	DestroyMenus();
											// Let go of the world module
	UnloadWorldModule();
}


void ChSoundMainInfo::Initialize()
{
	CreateMenus();							// Create & Install menus

											// Load the ChWorld module
	LoadWorldModule();
}


void ChSoundMainInfo::ShowModule( bool boolShow )
{
	if (boolShow && !IsShown())
	{
		InstallMenus();
		InstallHooks();

#if defined( CH_USE_VOXWARE )
		m_pSpeechStatus->Show( boolShow );
		SetShown( boolShow );
#endif
	}
	else if (!boolShow && IsShown())
	{
											// Stop any playing music
		GetMidiPlayer()->Stop();

		UninstallMenus();
		InstallHooks( false );

#if defined( CH_USE_VOXWARE )
		m_pSpeechStatus->Show( boolShow );
		SetShown( boolShow );
#endif
	}
}


bool ChSoundMainInfo::DoCommand( const string& strArgs, bool boolInline )
{
	bool		boolProcessed;
	CommandType	command = invalid;
	string		strCommand;
	string		strDevice;

	if (ChHtmlWnd::GetHTMLAttribute( strArgs, ATTR_XCH_SOUND, strCommand ))
	{
		command = xch_sound;
		strCommand.MakeLower();
	}
	else if (ChHtmlWnd::GetHTMLAttribute( strArgs, ATTR_XCH_VOLUME, strCommand ))
	{
		command = xch_volume;
	}
	else if (ChHtmlWnd::GetHTMLAttribute( strArgs, ATTR_XCH_ALERT, strCommand ))
	{
		command = xch_alert;
	}

	#if defined( CH_USE_VOXWARE )

	else if (ChHtmlWnd::GetHTMLAttribute( strArgs, ATTR_XCH_SPEECH, strCommand ))
	{
		command = xch_speech;
		strCommand.MakeLower();
	}

	#endif	// defined( CH_USE_VOXWARE )
											/* Device is pretty common, so look
												for it here */

	if (ChHtmlWnd::GetHTMLAttribute( strArgs, ATTR_XCH_DEVICE, strDevice ))
	{
		strDevice.MakeLower();
	}

	switch( command )
	{
		case xch_sound:
		{
			string		strURL;
			string		strVolume;
			bool		boolVolume;
			chuint16	suVolume;
			string		strEvents;
			chflag32	flEvents = 0;
			string		strOptions;
			chflag32	flOptions = 0;
			string		strAction;
			string		strMD5;

			ChHtmlWnd::GetHTMLHref( strArgs, boolInline, strURL );
			if (boolVolume = ChHtmlWnd::GetHTMLAttribute( strArgs,
															ATTR_XCH_VOLUME,
															strVolume ))
			{
				boolVolume = GetVolume( strVolume, suVolume );
			}

			if (ChHtmlWnd::GetHTMLAttribute( strArgs, ATTR_OPTIONS, strOptions ))
			{
				flOptions = ParseOptions( strOptions );
			}

			if (ChHtmlWnd::GetHTMLAttribute( strArgs, ATTR_EVENT, strEvents ))
			{
				if (flEvents = ParseEvents( strEvents ))
				{
					if (ChHtmlWnd::GetHTMLAttribute( strArgs, ATTR_MD5,
														strMD5 ))
					{
						if (!ChHtmlWnd::GetHTMLAttribute( strArgs, ATTR_ACTION,
															strAction ))
						{
											// No action, so ignore MD5 attrib
							strMD5 = "";
						}
					}
				}
			}

			boolProcessed = DoSoundCommand( strCommand, strURL, strDevice,
											boolVolume, suVolume, flOptions,
											flEvents, strAction, strMD5 );
			break;
		}

		case xch_volume:
		{
			string		strURL;
			chuint16	suVolume;
			DeviceType	device = ParseDevice( strDevice );

			if (GetVolume( strCommand, suVolume ))
			{
				boolProcessed = DoVolumeCommand( device, suVolume );
			}
			else
			{
				boolProcessed = false;
			}
			break;
		}

		case xch_alert:
		{
			boolProcessed = DoAlertCommand();
			break;
		}

		#if defined( CH_USE_VOXWARE )

		case xch_speech:
		{
			string		strHost;
			chuint16	suPort;
			string		strCallId;
			chflag32	flOptions = 0;
			chflag32	flRemoteOptions = 0;
			int			iIndex;
			string		strOptions;
			string		strRemoteOptions;

			ChHtmlWnd::GetHTMLAttribute( strArgs, ATTR_ID, strCallId );
			ChHtmlWnd::GetHTMLAttribute( strArgs, ATTR_HOST, strHost );
			iIndex = strHost.Find( ':' );
			if (-1 == iIndex)
			{
				suPort = 0;
			}
			else
			{
				const char*	pstrPort;

				pstrPort = ((const char*)strHost) + iIndex + 1;
				suPort = atoi( pstrPort );

				strHost = strHost.Left( iIndex );
			}

			if (ChHtmlWnd::GetHTMLAttribute( strArgs, ATTR_OPTIONS,
												strOptions ))
			{
				flOptions = ParseSpeechOptions( strOptions );
			}

			if (ChHtmlWnd::GetHTMLAttribute( strArgs, ATTR_REMOTE_OPTIONS,
												strRemoteOptions ))
			{
				flRemoteOptions = ParseSpeechOptions( strRemoteOptions );
			}

			boolProcessed = DoSpeechCommand( strCommand, strHost,
											suPort, strCallId, flOptions,
											flRemoteOptions );
			break;
		}

		#endif	// defined( CH_USE_VOXWARE )

		default:
		{
			boolProcessed = false;
			break;
		}
	}

	return boolProcessed;
}


void ChSoundMainInfo::Play( string strURL, bool boolLooping )
{
	if (UseSound())
	{
		ChSoundInfo*	pSoundInfo;
		pSoundInfo = new ChSoundInfo( boolLooping );

		GetCore()->GetURL( strURL, 0, m_pSoundStream,  (chparam)pSoundInfo );
	}
}


void ChSoundMainInfo::StopAll()
{
	GetMidiPlayer()->Stop();
}


/*----------------------------------------------------------------------------

	FUNCTION	||	ChSoundMainInfo::DoAlertCommand

	RETURNS		||  true if the command was processed.

------------------------------------------------------------------------------

	This method will perform a sound command.

----------------------------------------------------------------------------*/

bool ChSoundMainInfo::DoAlertCommand()
{
	if (UseSound())
	{
		ChTime		timeNow( ChTime::GetCurrentTime() );
		ChTimeSpan	span;

		span = timeNow - m_lastAlertTime;

		if (span.GetTotalSeconds() > m_alertTime.GetAlertSeconds())
		{
			m_lastAlertTime = timeNow;
											/* Make sure we're not getting
												alerts too frequently */
			if (!GetWavePlayer()->IsPlaying())
			{
				if (!m_strAlertSoundPath.IsEmpty())
				{
					ChSoundInfo		info( false, true, GetAlertVolume() );

					info.SetFilename( m_strAlertSoundPath );

					GetWavePlayer()->Play( &info );
				}
			}
		}
	}

	return true;
}


void ChSoundMainInfo::SendWorldCommand( const string& strMD5,
										const string& strAction,
										const string& strParams )
{
	ChSendWorldCmdMsg*	pMsg;

⌨️ 快捷键说明

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