#include <iostream>
#include <miniaudio.h>
#include <Ogre.h>
#include <OgreResourceGroupManager.h>
#include "sound.h"
namespace Sound
{
static ma_decoder decoder;
static ma_device_config deviceConfig;
static ma_device device;
static ma_engine engine;
std::vector<ma_sound> psound_data;
std::map<Ogre::String, int> psounds;

void setup()
{
	int i;
	ma_result result;
	result = ma_engine_init(NULL, &engine);
	OgreAssert(result == MA_SUCCESS, "MiniAudio init failed");
	Ogre::FileInfoListPtr sounds =
		Ogre::ResourceGroupManager::getSingleton().findResourceFileInfo(
			"Audio", "*.wav");
	psound_data.resize(sounds->size());
	for (i = 0; i < sounds->size(); i++) {
		Ogre::FileInfo fi = sounds->at(i);
		Ogre::String path =
			fi.archive->getName() + "/" + fi.path + fi.filename;
		std::cout << "sound: " << path << "\n";
		std::cout << "sound: " << fi.basename << "\n";
		ma_sound_init_from_file(&engine, path.c_str(), 0, NULL, NULL,
					&psound_data[i]);
		psounds[fi.basename] = i;
	}
}
void ding()
{
	ma_sound &sound = psound_data[psounds["load.wav"]];
	if (ma_sound_is_playing(&sound) && ma_sound_at_end(&sound)) {
		ma_sound_stop(&sound);
		ma_sound_seek_to_pcm_frame(&sound, 0);
	} else if (ma_sound_is_playing(&sound))
		return;
	ma_sound_start(&sound);
}
}
/* nothing */