//	PortAudioDevice.h - A PortAudio subclass of AudioDevice.
//  ----------------------------------------------------------------------------
//	This file is part of 'NiallsAVLib', base code for any kind of audiovisual
//	apps.
//	Copyright (C) 2012  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 "AudioDevice.h"

#include "portaudio.h"

///	A PortAudio subclass of AudioDevice.
class PortAudioDevice : public AudioDevice
{
  public:
	///	Constructor.
	PortAudioDevice();
	///	Destructor.
	~PortAudioDevice();

	///	Sets the callback to call during audio processing.
	void setCallback(AudioDeviceCallback *val);

	///	Sets the number of desired inputs and outputs.
	void setNumChannels(int inputs, int outputs);
	///	Sets the desired samplerate.
	void setSamplerate(double rate);
	///	Sets the audio API to use.
	/*!
		\param val Valid strings are:
		(Windows)
		DirectSound
		ASIO
		MME
		(Linux)
		ALSA
		JACK
		OSS
		(OSX)
		CoreAudio
	 */
	void setAPI(const std::wstring& val);

	///	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.
	AudioDeviceCallback *callback;

	///	The number of input channels.
	int numInputs;
	///	The number of output channels.
	int numOutputs;
	///	The samplerate.
	double samplerate;
	///	The API we're using.
	PaHostApiTypeId api;

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

#endif
