//	PortAudioDevice.h - Simple C++ wrapper for PortAudio.
//  ----------------------------------------------------------------------------
//	This file is part of 'GameTemplate', a game.
//	Copyright (C) 2009  Niall Moody
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 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 General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//	----------------------------------------------------------------------------

#ifndef PORTAUDIODEVICE_H_
#define PORTAUDIODEVICE_H_

#include "portaudio.h"

//------------------------------------------------------------------------------
///	Callback for PortAudioDevice.
/*!
	Subclass this to send and receive audio to/from the soundcard.
 */
class PortAudioCallback
{
  public:
	///	Destructor.
	virtual ~PortAudioCallback() {};

	///	Called to generate audio to send to the soundcard.
	virtual void getAudio(float *input, float *output, int numSamples) = 0;
};

//------------------------------------------------------------------------------
///	Simple C++ wrapper for PortAudio.
/*!
	For now it just uses the default device for the compiled platform (i.e.
	DirectSound on Windows, ALSA on Linux, CoreAudio on OSX).
 */
class PortAudioDevice
{
  public:
	///	Constructor.
	PortAudioDevice();
	///	Destructor.
	~PortAudioDevice();

	///	Sets the callback to call during audio processing.
	/*!
		It's your responsibility to delete the callback when you're done with
		it, not PortAudioDevice's.
	 */
	void setCallback(PortAudioCallback *val);

	///	Sets the number of desired inputs and outputs.
	void setNumChannels(int inputs, int outputs);
	///	Sets the desired samplerate.
	void setSamplerate(double rate);

	///	Starts audio processing.
	/*!
		\return True if successful, false if there's a problem.
	 */
	bool start();
	///	Stops audio processing.
	void stop();

	///	Returns the number of input channels.
	int getNumInputs() const {return numInputs;};
	///	Returns the number of output channels.
	int getNumOutputs() const {return numOutputs;};
	///	Returns the samplerate.
	double getSamplerate() const {return samplerate;};

	///	Our PortAudio callback.
	static int portAudioCallback(const void *input,
								 void *output,
								 unsigned long frameCount,
								 const PaStreamCallbackTimeInfo *timeInfo,
								 PaStreamCallbackFlags statusFlags,
								 void *userData);
  private:
	///	Our PortAudio stream.
	PaStream *stream;
	///	Our callback.
	PortAudioCallback *callback;

	///	The number of input channels.
	int numInputs;
	///	The number of output channels.
	int numOutputs;
	///	The samplerate.
	double samplerate;

	///	Whether we're currently running or not.
	bool isRunning;
};

#endif
