Source

gb_emulator / gb_emulator / include / gb_emulator / gb_sound_sdl.h

Full commit
/*  Copyright © 2011 Chris Spencer <spencercw@gmail.com>

    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 GB_SOUND_SDL_H_706545C0_1824_11E1_A650_0002A5D5C51B
#define GB_SOUND_SDL_H_706545C0_1824_11E1_A650_0002A5D5C51B

#include <stdint.h>

#include <boost/circular_buffer.hpp>

#include <gb_emulator/gb_sound.hpp>

//! SDL sound driver.
class GbSoundSdl: public GbSound
{
public:
	//! Constructor; opens the sound device.
	GbSoundSdl(Gb &gb);

	//! Indicates whether the audio driver is available.
	/**
	 * The implementation for this class always returns \c true.
	 * \return \c true.
	 */
	bool isAvailable()
	{
		return true;
	}

	//! Performs initialisation.
	/**
	 * The implementation for this class does nothing.
	 */
	void initialise()
	{
	}

	//! Generates a single sample.
	double poll();

	//! Sets the callback function to be called at the given frequency based on the audio clock.
	void setTimerCallback(double frequency, boost::function<void ()> callback);

private:
	// Buffer of samples ready to be played
	boost::circular_buffer<int16_t> soundBuf_;

	// Sound timer callback
	double callbackSampleRate_;
	double callbackCountdown_;
	boost::function<void ()> timerCallback_;

	// Sound callback which copies samples from soundBuf_ into the device buffer
	static void soundCallback(void *context, uint8_t *stream, int len);
	void doSoundCallback(uint8_t *stream, int len);
};

#endif