#include "sfl.h"
#include "sflSound_internal.h"
#include "sflSoundMP3.h"
#include "sflSoundWav.h"
#include <stdio.h>
#include <string.h>
#include <pspthreadman.h>

sflSound *sflSoundOpen(const char *filename)
{
	char *ext = strrchr(filename, '.');
	if (ext)
		++ext;

	sflSound *sound = 0;
	if (string_compare(ext, "WAV") == 0) {
		sound = sflSoundWavOpen(filename);
	} else if (string_compare(ext, "MP3") == 0) {
		sound = sflSoundMP3Open(filename);
	} else {
		DEBUG_PRINTF("Unrecognized music format %s\n", filename);
	}

	if (!sound)
		DEBUG_PRINTF("can't open %s\n", filename);
	
	return sound;
}

size_t sflSoundRead(sflSound *self, void *outbuf, size_t n)
{
	return ((size_t (*)(sflSound *self, void *outbuf, size_t n))self->vptr[SFL_SOUND_READ])(self, outbuf, n);
}

int sflSoundSeek(sflSound *self, int offset, int whence)
{
	return ((int (*)(sflSound *self, int offset, int whence))self->vptr[SFL_SOUND_SEEK])(self, offset, whence);
}

long sflSoundTell(sflSound *self)
{
	return ((int (*)(sflSound *self))self->vptr[SFL_SOUND_TELL])(self);
}

int sflSoundClose(sflSound *self)
{
	return ((int (*)(sflSound *self))self->vptr[SFL_SOUND_CLOSE])(self);
}

sflSound *sflSoundClone(sflSound *self)
{
	return ((sflSound *(*)(sflSound *self))self->vptr[SFL_SOUND_CLONE])(self);
}

