/************************************************************************************ This source file is part of the Theora Video Playback Library For latest info, see http://libtheoraplayer.sourceforge.net/ ************************************************************************************* Copyright (c) 2008-2010 Kresimir Spes (kreso@cateia.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License (LGPL) as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt. *************************************************************************************/ #include #include "OpenAL_AudioInterface.h" ALCdevice* gDevice=0; ALCcontext* gContext=0; short float2short(float f) { if (f > 1) f= 1; else if (f < -1) f=-1; return (short) (f*32767); } OpenAL_AudioInterface::OpenAL_AudioInterface(TheoraVideoClip* owner,int nChannels,int freq) : TheoraAudioInterface(owner,nChannels,freq), TheoraTimer() { mMaxBuffSize=freq*mNumChannels*2; mBuffSize=0; mNumProcessedSamples=0; mTimeOffset=0; mTempBuffer=new short[mMaxBuffSize]; alGenSources(1,&mSource); owner->setTimer(this); mNumPlayedSamples=0; } void OpenAL_AudioInterface::destroy() { // todo } OpenAL_AudioInterface::~OpenAL_AudioInterface() { // todo: delete buffers and source if (mTempBuffer) delete mTempBuffer; } void OpenAL_AudioInterface::insertData(float** data,int nSamples) { //Deactivated output //printf("got %d bytes, %d buffers queued\n",nSamples,(int)mBufferQueue.size()); for (int i=0;i 0) printf("%.2f\n",mTimeOffset); float duration=mClip->getDuration(); if (mTime > duration) mTime=duration; } void OpenAL_AudioInterface::pause() { alSourcePause(mSource); TheoraTimer::pause(); } void OpenAL_AudioInterface::play() { alSourcePlay(mSource); TheoraTimer::play(); } void OpenAL_AudioInterface::seek(float time) { OpenAL_Buffer buff; alSourceStop(mSource); while (!mBufferQueue.empty()) { buff=mBufferQueue.front(); mBufferQueue.pop(); alSourceUnqueueBuffers(mSource,1,&buff.id); alDeleteBuffers(1,&buff.id); } // int nProcessed; // alGetSourcei(mSource,AL_BUFFERS_PROCESSED,&nProcessed); // if (nProcessed != 0) // nProcessed=nProcessed; mBuffSize=0; mTimeOffset=0; mNumPlayedSamples=mNumProcessedSamples=(int) time*mFreq; } OpenAL_AudioInterfaceFactory::OpenAL_AudioInterfaceFactory() { // openal init is here used only to simplify samples for this plugin // if you want to use this interface in your own program, you'll // probably want to remove the openal init/destory lines gDevice = alcOpenDevice(""); if (alcGetError(gDevice) != ALC_NO_ERROR) goto Fail; gContext = alcCreateContext(gDevice, NULL); if (alcGetError(gDevice) != ALC_NO_ERROR) goto Fail; alcMakeContextCurrent(gContext); if (alcGetError(gDevice) != ALC_NO_ERROR) goto Fail; return; Fail: gDevice=NULL; gContext=NULL; } OpenAL_AudioInterfaceFactory::~OpenAL_AudioInterfaceFactory() { if (gDevice) { alcMakeContextCurrent(NULL); alcDestroyContext(gContext); alcCloseDevice(gDevice); } } OpenAL_AudioInterface* OpenAL_AudioInterfaceFactory::createInstance(TheoraVideoClip* owner,int nChannels,int freq) { return new OpenAL_AudioInterface(owner,nChannels,freq); }