//	BackgroundMusic.h - Class to play looped background music.
//  ----------------------------------------------------------------------------
//	This file is part of 'Die, you Stupid Hurdlers!', a game for Ludum Dare 14.
//	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 BACKGROUNDMUSIC_H_
#define BACKGROUNDMUSIC_H_

#include "Thread.h"

#include <string>
#include <vorbis/vorbisfile.h>

///	Class to play looped background music.
class BackgroundMusic : public Thread
{
  public:
	///	Constructor.
	BackgroundMusic();
	///	Destructor.
	~BackgroundMusic();

	///	Fills a buffer with audio.
	void audioBlock(float *buffer, int numSamples);

	///	Used to stream the audio off the hard disk.
	bool run();

	///	Returns the directory the sound files are in.
	static std::string getSoundsPath();
  private:
	///	Helper method to fill a buffer with audio.
	void fillBuffer(int buf);

	///	Some constants.
	enum
	{
		BufferSize = 11025,
		NumBuffers = 3
	};

	///	The sound file to play.
	FILE *file;
	///	The sound file to play.
	OggVorbis_File vorbisFile;
	///	Our read position within the file.
	unsigned int filePosition;
	///	The length of the file in samples.
	unsigned int length;

	///	Used to ramp the sound level up when the game's just been started.
	float level;

	///	Temporary buffers used to stream data from the disk.
	float left[NumBuffers][BufferSize];
	///	Temporary buffers used to stream data from the disk.
	float right[NumBuffers][BufferSize];
	///	Which buffer we're currently reading from.
	int readBuffer;
	///	Which buffers need re-loaded.
	bool clearedBuffers[NumBuffers];
	///	Our read position in the active buffer.
	int readPosition;

	///	Thread variable.
	bool timeToStop;
};

#endif
