Code Search for Developers
 
 
  

sound.cpp from NeoEngineNG at Krugle


Show sound.cpp syntax highlighted

/***************************************************************************
                     sound.cpp  -  Sound classes and codecs
                             -------------------
    begin                : Wed Mar 17 2004
    copyright            : (C) 2004 by Reality Rift Studios
    email                : mattias@realityrift.com
 ***************************************************************************

 The contents of this file are subject to the Mozilla Public License Version
 1.1 (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.mozilla.org/MPL/

 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 the NeoEngine, NeoDevALSA, sound.cpp

 The Initial Developer of the Original Code is Mattias Jansson.
 Portions created by Mattias Jansson are Copyright (C) 2004
 Reality Rift Studios. All Rights Reserved.

 ***************************************************************************/

#include "sound.h"

#include <neoengine/file.h>
#include <neoengine/filemanager.h>
#include <neoengine/logstream.h>

#include <alsa/asoundlib.h>
#include <errno.h>

using namespace NeoEngine;
using namespace std;


namespace NeoALSA
{


NeoEngine::Sound *AudioDevice::LoadSound( const std::string &rstrFilename )
{
	vector< SoundCodec* >::iterator ppkCodec    = m_vpkCodecs.begin();
	vector< SoundCodec* >::iterator ppkCodecEnd = m_vpkCodecs.end();

	for( ; ppkCodec != ppkCodecEnd; ++ppkCodec )
	{
		string strFilename = rstrFilename + ".";

		const vector<string> &rvstrExtensions = (*ppkCodec)->GetExtensions();

		vector<string>::const_iterator pstrExt    = rvstrExtensions.begin();
		vector<string>::const_iterator pstrExtEnd = rvstrExtensions.end();

		for( ; pstrExt != pstrExtEnd; ++pstrExt )
		{
			File        *pkFile   = m_pkFileManager->GetByName( strFilename + (*pstrExt) );
			SoundStream *pkStream = 0;

			if( pkFile && pkFile->Open() && (*ppkCodec)->IsType( pkFile ) && ( pkStream = (*ppkCodec)->GetStream( pkFile ) ) )
				return new Sound( this, rstrFilename, pkStream );
			else
				delete pkFile;
		}
	}

	neolog << LogLevel( WARNING ) << "*** unable to load sound [" << rstrFilename << "]" << endl;

	return 0;
}



Sound::Sound( AudioDevice *pkDevice, const std::string &rstrName, SoundStream *pkStream ) :
	NeoEngine::Sound( rstrName, pkStream ),
	m_pkDevice( pkDevice ),
	m_pkSound( 0 ),
	m_pkThread( 0 ),
	m_bPaused( false )
{
}


Sound::~Sound()
{
	delete m_pkThread;
}


void Sound::Play( bool bLoop )
{
	m_bLoop   = bLoop;
	
	if( m_pkThread )
	{
		if( m_pkThread->m_bTerminated )
			delete m_pkThread, m_pkThread = 0;
		else
			return;
	}

	m_pkThread = new Thread( this, 0 );
}


void *Sound::ThreadEntry( Thread *pkThread, void *pData )
{
	pkThread->m_bStarted = true;

	int err;

	snd_pcm_hw_params_t *pkHWParams;
	
	m_pkSound = 0;

	if( ( err = snd_pcm_open( &m_pkSound, "default", SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 )
	{
		neolog << LogLevel( ERROR ) << "*** unable to play sound: unable to open sound device: " << snd_strerror( err ) << endl;
		return 0;
	}

	if( ( err = snd_pcm_hw_params_malloc( &pkHWParams ) ) < 0 )
	{
		neolog << LogLevel( ERROR ) << "unable to play sound: failed to allocate hardware parameter structure: " << snd_strerror( err ) << endl;
		return 0;
	}
				
	if( ( err = snd_pcm_hw_params_any( m_pkSound, pkHWParams ) ) < 0 )
	{
		neolog << LogLevel( ERROR ) << "unable to play sound: failed to initialize hardware parameter structure: " << snd_strerror( err ) << endl;
		return 0;
	}
	
	if( ( err = snd_pcm_hw_params_set_access( m_pkSound, pkHWParams, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
	{
		neolog << LogLevel( ERROR ) << "unable to play sound: failed to set acces type: " << snd_strerror( err ) << endl;
		return 0;
	}
	
	unsigned int uiRate = m_pkStream->GetSamplesPerSecond();
	unsigned int uiBytesPerSample = m_pkStream->GetBitsPerSample() / 8;
	unsigned int uiFrameSize = uiBytesPerSample * m_pkStream->GetNumChannels();
	
	if( ( err = snd_pcm_hw_params_set_format( m_pkSound, pkHWParams, SND_PCM_FORMAT_S16_LE ) < 0 ) )
	{
		neolog << LogLevel( ERROR ) << "unable to play sound: failed to set sample format: " << snd_strerror( err ) << endl;
		return 0;
	}

	if( ( err = snd_pcm_hw_params_set_rate_near( m_pkSound, pkHWParams, &uiRate, 0 ) ) < 0 )
	{
		neolog << LogLevel( ERROR ) << "unable to play sound: failed to set sample rate: " << snd_strerror( err ) << endl;
		return 0;
	}
	
	if( ( err = snd_pcm_hw_params_set_channels( m_pkSound, pkHWParams, m_pkStream->GetNumChannels() ) ) < 0 )
	{
		neolog << LogLevel( ERROR ) << "unable to play sound: failed to set number of channels [" << m_pkStream->GetNumChannels() << "]: " << snd_strerror( err ) << endl;
		return 0;
	}

	if( ( err = snd_pcm_hw_params_set_periods( m_pkSound, pkHWParams, 2, 0 ) ) < 0 )
	{
		neolog << LogLevel( ERROR ) << "unable to play sound: failed to set number of periods: " << snd_strerror( err ) << endl;
		return 0;
	}
	
	snd_pcm_uframes_t uiHWBufferSize = uiBytesPerSample * uiRate * m_pkStream->GetNumChannels();
	
	if( ( err = snd_pcm_hw_params_set_buffer_size_near( m_pkSound, pkHWParams, &uiHWBufferSize ) ) < 0 )
	{
		neolog << LogLevel( ERROR ) << "unable to play sound: failed to set buffer size: " << snd_strerror( err ) << endl;
		return 0;
    }
	
	if( ( err = snd_pcm_hw_params( m_pkSound, pkHWParams ) ) < 0 )
	{
		neolog << LogLevel( ERROR ) << "unable to play sound: failed to set parameters: " << snd_strerror( err ) << endl;
		return 0;
	}

	if( pkHWParams )
		snd_pcm_hw_params_free( pkHWParams );	
	
	if( ( err = snd_pcm_prepare( m_pkSound ) ) < 0 )
	{
		neolog << LogLevel( ERROR ) << "unable to play sound: failed to prepare device: " << snd_strerror( err ) << endl;
		return 0;
	}

	if( !m_bPaused )
		m_pkStream->Reset();
		
	m_bPaused = false;
	
	unsigned int   uiBufferSize = uiHWBufferSize * uiFrameSize;
	unsigned char *pucBuf       = new unsigned char[ uiBufferSize ];
	unsigned int   uiBytes      = m_pkStream->GetSize() - m_pkStream->GetPosition();
	
	neolog << LogLevel( DEBUG ) << "starting sound playing [" << GetName() << "]" << endl;
	
	while( uiBytes && !pkThread->m_bTerminate )
	{
		unsigned int uiDecoded = m_pkStream->Decode( pucBuf, uiBufferSize );
		unsigned int uiBuffer  = uiDecoded;
		
		while( uiBuffer )
		{
			unsigned int uiWrite = uiBuffer / uiFrameSize;
		
			if( ( err = snd_pcm_writei( m_pkSound, pucBuf, uiWrite ) ) < 0 )
			{
				neolog << LogLevel( WARNING ) << "*** Sound buffer underrun: " << snd_strerror( err ) << endl;
			
				if( ( err = snd_pcm_prepare( m_pkSound ) ) < 0 )
				{
					neolog << LogLevel( ERROR ) << "unable to play sound: failed to prepare device after underrun: " << snd_strerror( err ) << endl;
					uiBytes = 0;
					break;
				}
			}
			else
			{
				uiWrite = (unsigned int)err * uiFrameSize;
			
				if( uiBuffer < uiWrite )
					uiBuffer = 0;
				else
					uiBuffer -= uiWrite;
			}
		}
		
		if( err < 0 )
			break;
		
		uiBytes -= uiDecoded;
		
		if( !uiBytes && m_bLoop )
		{
			m_pkStream->Reset();
			uiBytes = m_pkStream->GetSize();
		}
	}
	
	neolog << LogLevel( DEBUG ) << "finished sound playing [" << GetName() << "]" << endl;

	if( m_pkSound )	
		snd_pcm_close( m_pkSound );
	
	delete [] pucBuf;
	
	pkThread->m_bTerminated = true;
	
	return 0;
}


void Sound::Stop()
{
	if( m_pkThread )
		delete m_pkThread, m_pkThread = 0;
		
	m_pkStream->Reset();
	
	m_bPaused = false;
}


void Sound::Pause()
{
	if( m_pkThread )
		delete m_pkThread, m_pkThread = 0;
		
	m_bPaused = true;
}


};






See more files for this project here

NeoEngineNG

NeoenEngine NG (Next Generation) is the evolution of neoengine one,it\'s a different development from NeoEngine2, it\'s a direct inherits from NeoEngine one.\n

Project homepage: http://sourceforge.net/projects/neoengineng
Programming language(s): C,C++
License: other

  Makefile.am
  SConscript
  device.cpp
  device.h
  glue.cpp
  link.h
  sound.cpp
  sound.h