/*


    s_null.c

    sound..


*/

#include <SDL.h>
#include "m_main.h"


long nosound;

cFile *sample=0;

long curpos = 0;


static void s_callback(void *userdata, Uint8 *stream, int len)
{
	if (!sample || len+curpos > sample->len) {
		memset(stream, 0, len);
		return;
	}

	memcpy(stream, &sample->data[curpos], len);

	curpos += len;
}


void    s_init(void)
{
	int i;
	SDL_AudioSpec desiredAudioSpec;    /* We fill in the desired SDL audio options here */

	sample = m_loadfile("sfx/sample.raw");

	/* Is enabled? */
	if (!sample) {
		/* Stop any sound access */
		printf("sample.raw not found\n");
		return;
	}

	for (i = 0; i < sample->len; i++) {
		sample->data[i] = ((int)sample->data[i]) * 3 / 4;
	}

	/* Set up SDL audio: */
	desiredAudioSpec.freq = 11025;
	desiredAudioSpec.format = AUDIO_S8;         /* 8-Bit signed */
	desiredAudioSpec.channels = 1;                  /* mono */
	desiredAudioSpec.callback = s_callback;
	desiredAudioSpec.userdata = NULL;
	desiredAudioSpec.samples = 1024;

	if (SDL_OpenAudio(&desiredAudioSpec, NULL))     /* Open audio device */
	{
		fprintf(stderr, "Can't use audio: %s\n", SDL_GetError());
		return;
	}
}


void    s_start(void) {
	SDL_PauseAudio(0);
}


void    s_play(int mist) {

}


void    s_shutdown(void) {
	SDL_PauseAudio(1);
	SDL_CloseAudio();
}
