/*
 * Copyright (C) 1997-2001 Id Software, Inc.
 *
 * 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.
 *
 */

/* snd_arts.c */
/* arts support for quake2 */

#include <artsc.h>

#include "../client/client.h"
#include "../client/snd_loc.h"
#define snd_buf (si->dma->samples * 2)

static arts_stream_t stream;
static int	snd_inited;
static byte    *buffer;

cvar_t        *s_khz;

struct sndinfo *si;

qboolean
SNDDMA_Init(struct sndinfo *s)
{
	int		errorcode;
	int		frag_spec;

	if (snd_inited)
		return 1;

	errorcode = arts_init();

	if (errorcode)
		si->Com_Printf("aRts: %s\n", arts_error_text(errorcode));

	si->dma->samplebits = (int)si->sndbits->value;
	
	si->dma->speed = (int)si->s_khz->value;
	
	if (si->dma->speed == 48)
		si->dma->speed = 48000;
	else if (si->dma->speed == 44)
		si->dma->speed = 44100;
	else if (si->dma->speed == 22)
		si->dma->speed = 22050;
	else
		si->dma->speed = 11025;


	si->dma->channels = (int)si->sndchannels->value;
	
	if (si->dma->speed == 48000)
		si->dma->samples = (4096 * si->dma->channels);
	else if (si->dma->speed == 44100)
		si->dma->samples = (2048 * si->dma->channels);
	else if (si->dma->speed == 22050)
		si->dma->samples = (1024 * si->dma->channels);
	else
		si->dma->samples = (512 * si->dma->channels);

	buffer = malloc(snd_buf);
	memset(buffer, 0, snd_buf);

	for (frag_spec = 0; (0x01 << frag_spec) < snd_buf; ++frag_spec);
	frag_spec |= 0x00020000;
	stream = arts_play_stream(si->dma->speed, si->dma->samplebits, si->dma->channels, "Q2Stream");
	arts_stream_set(stream, ARTS_P_PACKET_SETTINGS, frag_spec);
	arts_stream_set(stream, ARTS_P_BLOCKING, 0);

	si->dma->samplepos = 0;
	si->dma->submission_chunk = 1;
	si->dma->buffer = buffer;

	si->Com_Printf("\nInitializing aRts Sound System\n");

	snd_inited = 1;

	return 1;
}

int
SNDDMA_GetDMAPos(void)
{
	if (snd_inited)
		return si->dma->samplepos;
	else
		si->Com_Printf("Sound not inizialized\n");
	return 0;
}

void
SNDDMA_Shutdown(void)
{
	if (!snd_inited) {
		si->Com_Printf("Sound not inizialized\n");
		return;
	}
	arts_close_stream(stream);
	arts_free();
	snd_inited = 0;
	free(si->dma->buffer);
}

void
SNDDMA_BeginPainting(void)
{
}

void
SNDDMA_Submit(void)
{
	int		written;

	if (!snd_inited)
		return;

	written = arts_write(stream, si->dma->buffer, snd_buf);
	si->dma->samplepos += (written / (si->dma->samplebits / 8));
}
