/***************************************************************************
 *   Copyright (C) 2004-2005 by                                            *
 *     Paolo Sacconier   <axa1981@tin.it>                                  *
 *     Francesco Tamagni <minchiahead@hacari.org>                          *
 *                                                                         *
 *   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 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU 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.             *
 ***************************************************************************/
#include "sound.h"

namespace gillo {

Sound::Sound()
: enabled(true), sched(44100)
{
	sched.setSafetyMargin ( 0.128f ) ;

	// load all the samples
	samples[BOING]		= new slSample (SND_BOING,		&sched); 
	samples[GOAL]		= new slSample (SND_GOAL,		&sched);
	samples[TIEGOAL]		= new slSample (SND_TIEGOAL,		&sched);
	samples[END]		= new slSample (SND_END,		&sched);
	samples[ENDTIE]		= new slSample (SND_ENDTIE,		&sched);
	samples[FIRE]		= new slSample (SND_FIRE,		&sched);
	samples[MENUSEL]	= new slSample (SND_MENU_SEL,	&sched);
	samples[MENUSEL]->adjustVolume(10.0);
	samples[PWRUP_CHARGE_INVERTER]	= new slSample (SND_PWRUP_CHARGE_INVERTER,	&sched);
	samples[PWRUP_CHARGE_INVERTER]->adjustVolume(10.0);
	samples[PWRUP_GRAVITY_ROTATOR]	= new slSample (SND_PWRUP_GRAVITY_ROTATOR,	&sched);
	samples[PWRUP_GRAVITY_ROTATOR]->adjustVolume(6.0);
	samples[PWRUP_BALL_INFLATE]	= new slSample (SND_PWRUP_BALL_INFLATE,		&sched);
	samples[PWRUP_BALL_DEFLATE]	= new slSample (SND_PWRUP_BALL_DEFLATE,	&sched);
	samples[PWRUP_BALL_ADD]	= new slSample (SND_PWRUP_BALL_ADD,	&sched);
	samples[PWRUP_NEW]	= new slSample (SND_PWRUP_NEW,	&sched);
	samples[POWERSHOOT]	= new slSample (SND_POWERSHOOT,	&sched);

	//slSample *s3 = new slSample ( "cuckoo.au", & sched ) ;
	//slSample *s4 = new slSample ( "wheeee.ub", & sched ) ;
	
	/* Mess about with some of the samples... */
	
	//s1 -> adjustVolume ( 10.0f  ) ;
	//s2 -> adjustVolume ( 0.5f  ) ;
	//s3 -> adjustVolume ( 0.2f  ) ;
	//s3 -> adjustVolume ( 0.3f  ) ;
	
	/* Play the engine sample continuously. */
	//sched.loopSample ( s ) ;
	//sched.loopSample ( s4 ) ;
}

Sound::~ Sound()
{
	printf("Sound(%p): destroyed\n",this);
}

void Sound::play(Effect e) {
	sched.playSample(samples[e]);
}
void Sound::music(Music m) {
	sched.stopMusic();
	switch (m) {
		case GAME1:
			sched.loopMusic(MOD_GAME1);
			break;
		case MENU:
			sched.loopMusic(MOD_MENU);
			break;
		case TRAINING:
			sched.loopMusic(MOD_TRAINING);
			break;
		default:
			//@fixme throw something
			break;
	}
}
void Sound::update(double t) {
	if (enabled)
		sched.update();
}
};
