示例#1
0
文件: sound.cpp 项目: DuMuT6p/Epiar
/**\brief Plays the sound at a specified coordinate from origin.
 */
bool Sound::Play( Coordinate offset ){
	if ( this->sound == NULL )
		return false;

	// Distance fading
	double dist = this->fadefactor * offset.GetMagnitude();
	if ( dist > 255 )
		return false;			// Sound is out of range
	Uint8 sounddist = static_cast<Uint8>( dist );

	// Left-Right panning
	float panx = this->panfactor * static_cast<float>(offset.GetX())+127.f;
	Uint8 soundpan = 127;
	if ( panx < 0 )
		soundpan = 0;
	else if ( panx > 254 )
		soundpan = 254;
	else
		soundpan = static_cast<Uint8>( panx );

	int freechan = Audio::Instance().GetFreeChannel();
	if( Mix_SetDistance( freechan, sounddist ) == 0 )
		LogMsg(ERR,"Set distance %d failed on channel %d.", sounddist, freechan );
	//else
	//	LogMsg(INFO,"Distance set to %d on channel %d.", sounddist, freechan );

	/**\bug SDL_mixer bug possibly: Need to check whether SDL_mixer is getting
	 * Left/Right speaker switched around.
	 */
	if( Mix_SetPanning( freechan, 254 - soundpan, soundpan ) == 0 )
		LogMsg(ERR,"Set panning %d failed on channel %d.", soundpan - 127, freechan );
	//else
	//	LogMsg(INFO,"Panning set to %d on channel %d.", soundpan - 127, freechan );

	Mix_Volume( freechan, this->volume );
	this->channel = Audio::Instance().PlayChannel( freechan, this->sound, 0 );

	if ( channel == -1 )
		return false;
	
	return true;
}