#include "wou.h"

int sound_init() {

	// get volumes from config file and set them
	int svolume, mvolume;
	svolume = get_config_int("sound", "svolume", 255);
	mvolume = get_config_int("sound", "mvolume", 170);
	set_volume(svolume, mvolume);

	// load sound and music
	log_println("* Loading sound datafiles..");
	if ((musics = data_load("data/music.dat"))==NULL) return -1;
	//if ((sounds = data_load("data/sound.dat"))==NULL) return -1;

	// start the sound device
	log_print("* Starting sound device: ");
	if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, "")<0) {
		allegro_message("Error initialising sound\n%s\n", allegro_error);
		log_println("ERROR! Unable to initialize sound!!");
		return -1;
	}
	log_println("OK");
	log_println("- Sound volume: %i", svolume);
	log_println("- Music volume: %i", mvolume);
}

inline void sound_sample_play(int wav, int vol, int pan, int loop) {
	play_sample((SAMPLE*)sounds[wav].dat, vol, pan, 1000, loop);
}

void sound_midi_play(int music, int loop) {
	play_midi((MIDI*)musics[music].dat, loop);
}

void sound_midi_stop() {
	stop_midi();
}

void sound_destroy() {
	unload_datafile(musics);
	unload_datafile(sounds);
}
