4e43ab8
diff -up /dev/null ClanLib-0.6.5/Sources/Sound/Sound/ClanSound/alsa.cpp
4e43ab8
--- /dev/null	2008-03-02 13:00:58.162258534 +0100
4e43ab8
+++ ClanLib-0.6.5/Sources/Sound/Sound/ClanSound/alsa.cpp	2008-03-02 13:32:17.000000000 +0100
4e43ab8
@@ -0,0 +1,167 @@
4e43ab8
+/*
4e43ab8
+**  ClanLib SDK
4e43ab8
+**  Copyright (c) 1997-2008 The ClanLib Team
4e43ab8
+**
4e43ab8
+**  This software is provided 'as-is', without any express or implied
4e43ab8
+**  warranty.  In no event will the authors be held liable for any damages
4e43ab8
+**  arising from the use of this software.
4e43ab8
+**
4e43ab8
+**  Permission is granted to anyone to use this software for any purpose,
4e43ab8
+**  including commercial applications, and to alter it and redistribute it
4e43ab8
+**  freely, subject to the following restrictions:
4e43ab8
+**
4e43ab8
+**  1. The origin of this software must not be misrepresented; you must not
4e43ab8
+**     claim that you wrote the original software. If you use this software
4e43ab8
+**     in a product, an acknowledgment in the product documentation would be
4e43ab8
+**     appreciated but is not required.
4e43ab8
+**  2. Altered source versions must be plainly marked as such, and must not be
4e43ab8
+**     misrepresented as being the original software.
4e43ab8
+**  3. This notice may not be removed or altered from any source distribution.
4e43ab8
+**
4e43ab8
+**  Note: Some of the libraries ClanLib may link to may have additional
4e43ab8
+**  requirements or restrictions.
4e43ab8
+**
4e43ab8
+**  File Author(s):
4e43ab8
+**
4e43ab8
+**    Magnus Norddahl
4e43ab8
+**    Hans de Goede
4e43ab8
+**    (if your name is missing here, please add it)
4e43ab8
+*/
4e43ab8
+
4e43ab8
+#include "alsa.h"
4e43ab8
+#include "API/Core/System/error.h"
4e43ab8
+#include "API/Core/System/cl_assert.h"
4e43ab8
+#include "API/Core/System/system.h"
4e43ab8
+#include <iostream>
4e43ab8
+
4e43ab8
+#ifdef USE_CLANSOUND
4e43ab8
+#ifdef __linux__
4e43ab8
+
4e43ab8
+/////////////////////////////////////////////////////////////////////////////
4e43ab8
+// CL_SoundOutput_alsa construction:
4e43ab8
+
4e43ab8
+CL_SoundOutput_alsa::CL_SoundOutput_alsa() :
4e43ab8
+	frames_in_buffer(2048), frames_in_period(512)
4e43ab8
+{
4e43ab8
+	int rc;
4e43ab8
+	snd_pcm_hw_params_t *hwparams;
4e43ab8
+	unsigned int mixing_frequency = 22050;
4e43ab8
+	
4e43ab8
+	rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
4e43ab8
+	if (rc < 0)
4e43ab8
+	{
4e43ab8
+		std::cout <<  "ClanSound: Warning, Couldn't open sound device, disabling sound\n";
4e43ab8
+		handle = NULL;
4e43ab8
+		return;
4e43ab8
+	}
4e43ab8
+
4e43ab8
+	snd_pcm_hw_params_alloca(&hwparams);
4e43ab8
+	snd_pcm_hw_params_any(handle, hwparams);
4e43ab8
+	snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
4e43ab8
+	snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_S16);
4e43ab8
+	snd_pcm_hw_params_set_channels(handle, hwparams, 2);
4e43ab8
+	snd_pcm_hw_params_set_rate_near(handle, hwparams, &mixing_frequency, 0);
4e43ab8
+	snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &frames_in_buffer);
4e43ab8
+	frames_in_period = frames_in_buffer / 4;
4e43ab8
+	snd_pcm_hw_params_set_period_size_near(handle, hwparams, &frames_in_period, 0);
4e43ab8
+	
4e43ab8
+	rc = snd_pcm_hw_params(handle, hwparams);
4e43ab8
+	if (rc < 0)
4e43ab8
+	{
4e43ab8
+		std::cout << "ClanSound: Warning, Couldn't initialize sound device, disabling sound\n";
4e43ab8
+		snd_pcm_close(handle);
4e43ab8
+		handle = NULL;
4e43ab8
+		return;
4e43ab8
+	}
4e43ab8
+	
4e43ab8
+	snd_pcm_hw_params_get_period_size(hwparams, &frames_in_period, 0);
4e43ab8
+	snd_pcm_hw_params_get_rate(hwparams, &mixing_frequency, 0);
4e43ab8
+
4e43ab8
+	float percent_wrong = mixing_frequency / (float) 22050;
4e43ab8
+	if (percent_wrong < 0.90 || percent_wrong > 1.10)
4e43ab8
+	{
4e43ab8
+		snd_pcm_close(handle);
4e43ab8
+		/* Taken from oss driver, maybe make this a warning too? */
4e43ab8
+		throw CL_Error("ClanSound: Mixing rate (22.05 kHz) not supported by soundcard.");
4e43ab8
+	}
4e43ab8
+}
4e43ab8
+
4e43ab8
+CL_SoundOutput_alsa::~CL_SoundOutput_alsa()
4e43ab8
+{
4e43ab8
+	if (handle) {
4e43ab8
+		snd_pcm_close(handle);
4e43ab8
+		handle = NULL;
4e43ab8
+	}
4e43ab8
+}
4e43ab8
+
4e43ab8
+/////////////////////////////////////////////////////////////////////////////
4e43ab8
+// CL_SoundOutput_alsa operations:
4e43ab8
+
4e43ab8
+void CL_SoundOutput_alsa::silence()
4e43ab8
+{
4e43ab8
+	/* Note: not supported by all hardware! */
4e43ab8
+	if (handle)
4e43ab8
+		snd_pcm_pause(handle, 1);
4e43ab8
+}
4e43ab8
+
4e43ab8
+bool CL_SoundOutput_alsa::is_full()
4e43ab8
+{
4e43ab8
+	int rc;
4e43ab8
+	snd_pcm_sframes_t delay;
4e43ab8
+	
4e43ab8
+	if (handle == NULL) return false;
4e43ab8
+	
4e43ab8
+	rc = snd_pcm_delay(handle, &delay);
4e43ab8
+	if (rc < 0) {
4e43ab8
+		std::cout << "ClanSound: Warning, snd_pcm_delay() failed!?\n";
4e43ab8
+		return false;
4e43ab8
+	}
4e43ab8
+
4e43ab8
+	/* See if there is more then one period free in the buffer */
4e43ab8
+	return delay > (snd_pcm_sframes_t)(frames_in_buffer - frames_in_period);
4e43ab8
+}
4e43ab8
+
4e43ab8
+int CL_SoundOutput_alsa::get_frag_size()
4e43ab8
+{
4e43ab8
+	return frames_in_period;
4e43ab8
+}
4e43ab8
+
4e43ab8
+void CL_SoundOutput_alsa::write_fragment(short *data)
4e43ab8
+{
4e43ab8
+	snd_pcm_sframes_t rc;
4e43ab8
+
4e43ab8
+	if (handle == NULL) return;
4e43ab8
+
4e43ab8
+	switch(snd_pcm_state(handle)) {
4e43ab8
+		case SND_PCM_STATE_XRUN:
4e43ab8
+		case SND_PCM_STATE_SUSPENDED:
4e43ab8
+			snd_pcm_prepare(handle);
4e43ab8
+			break;
4e43ab8
+		case SND_PCM_STATE_PAUSED:
4e43ab8
+			snd_pcm_pause(handle, 0);
4e43ab8
+			break;
4e43ab8
+		default:
4e43ab8
+			break;
4e43ab8
+	}
4e43ab8
+
4e43ab8
+	rc = snd_pcm_writei(handle, data, frames_in_period);
4e43ab8
+	if (rc < 0)
4e43ab8
+		std::cout << "ClanSound: Warning, snd_pcm_writei() failed!\n";
4e43ab8
+}
4e43ab8
+
4e43ab8
+void CL_SoundOutput_alsa::wait()
4e43ab8
+{
4e43ab8
+	if(handle == NULL)
4e43ab8
+	{
4e43ab8
+		CL_System::sleep(100);
4e43ab8
+		return;
4e43ab8
+	}
4e43ab8
+	/* wait upto 1 second */
4e43ab8
+	snd_pcm_wait(handle, 1000);
4e43ab8
+}
4e43ab8
+
4e43ab8
+/////////////////////////////////////////////////////////////////////////////
4e43ab8
+// CL_SoundOutput_alsa implementation:
4e43ab8
+
4e43ab8
+#endif
4e43ab8
+#endif
4e43ab8
diff -up ClanLib-0.6.5/Sources/Sound/Sound/ClanSound/oss.h.alsa ClanLib-0.6.5/Sources/Sound/Sound/ClanSound/oss.h
4e43ab8
--- ClanLib-0.6.5/Sources/Sound/Sound/ClanSound/oss.h.alsa	2001-09-08 21:24:21.000000000 +0200
4e43ab8
+++ ClanLib-0.6.5/Sources/Sound/Sound/ClanSound/oss.h	2008-03-02 13:18:19.000000000 +0100
4e43ab8
@@ -10,6 +10,8 @@
4e43ab8
 #ifndef header_oss
4e43ab8
 #define header_oss
4e43ab8
 
4e43ab8
+#include "alsa.h"
4e43ab8
+
4e43ab8
 class CL_CSOutput
4e43ab8
 {
4e43ab8
 public:
4e43ab8
@@ -35,6 +37,9 @@ public:
4e43ab8
 private:
4e43ab8
 	int dev_dsp_fd;
4e43ab8
 	int frag_size;
4e43ab8
+#ifdef __linux__
4e43ab8
+	CL_SoundOutput_alsa *alsa;
4e43ab8
+#endif
4e43ab8
 };
4e43ab8
 
4e43ab8
 #endif
4e43ab8
diff -up ClanLib-0.6.5/Sources/Sound/Sound/ClanSound/oss.cpp.alsa ClanLib-0.6.5/Sources/Sound/Sound/ClanSound/oss.cpp
4e43ab8
--- ClanLib-0.6.5/Sources/Sound/Sound/ClanSound/oss.cpp.alsa	2002-07-02 22:56:33.000000000 +0200
4e43ab8
+++ ClanLib-0.6.5/Sources/Sound/Sound/ClanSound/oss.cpp	2008-03-02 13:18:19.000000000 +0100
4e43ab8
@@ -37,6 +37,14 @@ bool has_sound = true;
4e43ab8
 
4e43ab8
 CL_CSOutput::CL_CSOutput()
4e43ab8
 {
4e43ab8
+#ifdef __linux__
4e43ab8
+	alsa = new CL_SoundOutput_alsa();
4e43ab8
+	if (!alsa->handle) {
4e43ab8
+		delete alsa;
4e43ab8
+		alsa = NULL;
4e43ab8
+	} else
4e43ab8
+		return;
4e43ab8
+#endif	
4e43ab8
 	dev_dsp_fd = open(DSP_DEVICE, O_WRONLY|O_NONBLOCK);
4e43ab8
 	if (dev_dsp_fd == -1)
4e43ab8
 	{
4e43ab8
@@ -99,6 +107,13 @@ CL_CSOutput::CL_CSOutput()
4e43ab8
 
4e43ab8
 CL_CSOutput::~CL_CSOutput()
4e43ab8
 {
4e43ab8
+#ifdef __linux__
4e43ab8
+	if (alsa) {
4e43ab8
+		delete alsa;
4e43ab8
+		alsa = NULL;
4e43ab8
+		return;
4e43ab8
+	}
4e43ab8
+#endif
4e43ab8
 	if (dev_dsp_fd != -1) close(dev_dsp_fd);
4e43ab8
 }
4e43ab8
 
4e43ab8
@@ -106,12 +121,22 @@ void CL_CSOutput::silence()
4e43ab8
 // Called when we have no samples to play - and wants to tell the soundcard
4e43ab8
 // about this possible event.
4e43ab8
 {
4e43ab8
+#ifdef __linux__
4e43ab8
+	if (alsa) {
4e43ab8
+		alsa->silence();
4e43ab8
+		return;
4e43ab8
+	}
4e43ab8
+#endif
4e43ab8
 	ioctl(dev_dsp_fd, SNDCTL_DSP_POST, 0);
4e43ab8
 }
4e43ab8
 
4e43ab8
 bool CL_CSOutput::is_full()
4e43ab8
 // Returns true if all fragments are filled with data.
4e43ab8
 {
4e43ab8
+#ifdef __linux__
4e43ab8
+	if (alsa)
4e43ab8
+		return alsa->is_full();
4e43ab8
+#endif
4e43ab8
 	if (!has_sound) return false;
4e43ab8
 	audio_buf_info info;
4e43ab8
 	int err = ioctl(dev_dsp_fd, SNDCTL_DSP_GETOSPACE, &info;;
4e43ab8
@@ -128,17 +153,33 @@ bool CL_CSOutput::is_full()
4e43ab8
 int CL_CSOutput::get_frag_size()
4e43ab8
 // Returns the buffer size used by device (returned as num [stereo] samples).
4e43ab8
 {
4e43ab8
+#ifdef __linux__
4e43ab8
+	if (alsa)
4e43ab8
+		return alsa->get_frag_size();
4e43ab8
+#endif
4e43ab8
 	return frag_size/4;
4e43ab8
 }
4e43ab8
 
4e43ab8
 void CL_CSOutput::write_fragment(short *data)
4e43ab8
 // Writes a fragment to the soundcard.
4e43ab8
 {
4e43ab8
+#ifdef __linux__
4e43ab8
+	if (alsa) {
4e43ab8
+		alsa->write_fragment(data);
4e43ab8
+		return;
4e43ab8
+	}
4e43ab8
+#endif
4e43ab8
 	write(dev_dsp_fd, data, frag_size);
4e43ab8
 }
4e43ab8
 
4e43ab8
 void CL_CSOutput::wait()
4e43ab8
 {
4e43ab8
+#ifdef __linux__
4e43ab8
+	if (alsa) {
4e43ab8
+		alsa->wait();
4e43ab8
+		return;
4e43ab8
+	}
4e43ab8
+#endif
4e43ab8
 	if(!has_sound)
4e43ab8
 	{
4e43ab8
 		CL_System::sleep(100);
4e43ab8
diff -up /dev/null ClanLib-0.6.5/Sources/Sound/Sound/ClanSound/alsa.h
4e43ab8
--- /dev/null	2008-03-02 13:00:58.162258534 +0100
4e43ab8
+++ ClanLib-0.6.5/Sources/Sound/Sound/ClanSound/alsa.h	2008-03-02 13:23:03.000000000 +0100
4e43ab8
@@ -0,0 +1,75 @@
4e43ab8
+/*
4e43ab8
+**  ClanLib SDK
4e43ab8
+**  Copyright (c) 1997-2008 The ClanLib Team
4e43ab8
+**
4e43ab8
+**  This software is provided 'as-is', without any express or implied
4e43ab8
+**  warranty.  In no event will the authors be held liable for any damages
4e43ab8
+**  arising from the use of this software.
4e43ab8
+**
4e43ab8
+**  Permission is granted to anyone to use this software for any purpose,
4e43ab8
+**  including commercial applications, and to alter it and redistribute it
4e43ab8
+**  freely, subject to the following restrictions:
4e43ab8
+**
4e43ab8
+**  1. The origin of this software must not be misrepresented; you must not
4e43ab8
+**     claim that you wrote the original software. If you use this software
4e43ab8
+**     in a product, an acknowledgment in the product documentation would be
4e43ab8
+**     appreciated but is not required.
4e43ab8
+**  2. Altered source versions must be plainly marked as such, and must not be
4e43ab8
+**     misrepresented as being the original software.
4e43ab8
+**  3. This notice may not be removed or altered from any source distribution.
4e43ab8
+**
4e43ab8
+**  Note: Some of the libraries ClanLib may link to may have additional
4e43ab8
+**  requirements or restrictions.
4e43ab8
+**
4e43ab8
+**  File Author(s):
4e43ab8
+**
4e43ab8
+**    Magnus Norddahl
4e43ab8
+**    Hans de Goede
4e43ab8
+**    (if your name is missing here, please add it)
4e43ab8
+*/
4e43ab8
+
4e43ab8
+#ifndef header_soundoutput_alsa
4e43ab8
+#define header_soundoutput_alsa
4e43ab8
+
4e43ab8
+#ifdef __linux__
4e43ab8
+
4e43ab8
+#include <alsa/asoundlib.h> 
4e43ab8
+
4e43ab8
+class CL_SoundOutput_alsa
4e43ab8
+{
4e43ab8
+//! Construction:
4e43ab8
+public:
4e43ab8
+	CL_SoundOutput_alsa();
4e43ab8
+	
4e43ab8
+	~CL_SoundOutput_alsa();
4e43ab8
+
4e43ab8
+//! Attributes:
4e43ab8
+public:
4e43ab8
+	snd_pcm_t *handle;
4e43ab8
+	snd_pcm_uframes_t frames_in_buffer;
4e43ab8
+	snd_pcm_uframes_t frames_in_period;
4e43ab8
+
4e43ab8
+//! Operations:
4e43ab8
+public:
4e43ab8
+	//: Called when we have no samples to play - and wants to tell the soundcard
4e43ab8
+	//: about this possible event.
4e43ab8
+	void silence();
4e43ab8
+
4e43ab8
+	//: Returns true if all fragments are filled with data.
4e43ab8
+	bool is_full();
4e43ab8
+
4e43ab8
+	//: Returns the buffer size used by device (returned as num [stereo] samples).
4e43ab8
+	int get_frag_size();
4e43ab8
+
4e43ab8
+	//: Writes a fragment to the soundcard.
4e43ab8
+	void write_fragment(short *data);
4e43ab8
+
4e43ab8
+	//: Waits until output source isn't full anymore.
4e43ab8
+	void wait();
4e43ab8
+
4e43ab8
+//! Implementation:
4e43ab8
+private:
4e43ab8
+};
4e43ab8
+
4e43ab8
+#endif
4e43ab8
+#endif
4e43ab8
diff -up ClanLib-0.6.5/Setup/Unix/Makefile.sound.in.alsa ClanLib-0.6.5/Setup/Unix/Makefile.sound.in
4e43ab8
--- ClanLib-0.6.5/Setup/Unix/Makefile.sound.in.alsa	2008-03-02 13:18:19.000000000 +0100
4e43ab8
+++ ClanLib-0.6.5/Setup/Unix/Makefile.sound.in	2008-03-02 13:30:35.000000000 +0100
4e43ab8
@@ -42,6 +42,7 @@ OBJF_SOUND_CLANSOUND = \
4e43ab8
 	Libs/Intermediate/cardplayback_clan.o \
4e43ab8
 	Libs/Intermediate/mixer.o \
4e43ab8
 	Libs/Intermediate/oss.o \
4e43ab8
+	Libs/Intermediate/alsa.o \
4e43ab8
 	Libs/Intermediate/cdaudio_linux.o
4e43ab8
 
4e43ab8
 OBJF_SOUND_INTEL_ASM = \
4e43ab8
@@ -51,8 +52,8 @@ OBJF_SOUND_ALL = $(OBJF_SOUND_GENERIC) $
4e43ab8
 
4e43ab8
 Libs/libclanSound.so: Libs/libclanCore.so $(OBJF_SOUND_ALL)
4e43ab8
 	@echo "Building Libs/libclanSound.so"
4e43ab8
-	@echo $(LINK_COMMAND) -Wl,-soname=libclanSound.so.$(D_VERSION_MAJOR) -o Libs/libclanSound.so.$(D_VERSION_MINOR) $(OBJF_SOUND_ALL) -L Libs -lclanCore
4e43ab8
-	@$(LINK_COMMAND) -Wl,-soname=libclanSound.so.$(D_VERSION_MAJOR) -o Libs/libclanSound.so.$(D_VERSION_MINOR) $(OBJF_SOUND_ALL) -L Libs -lclanCore
4e43ab8
+	@echo $(LINK_COMMAND) -Wl,-soname=libclanSound.so.$(D_VERSION_MAJOR) -o Libs/libclanSound.so.$(D_VERSION_MINOR) $(OBJF_SOUND_ALL) -L Libs -lclanCore -lasound
4e43ab8
+	@$(LINK_COMMAND) -Wl,-soname=libclanSound.so.$(D_VERSION_MAJOR) -o Libs/libclanSound.so.$(D_VERSION_MINOR) $(OBJF_SOUND_ALL) -L Libs -lclanCore -lasound
4e43ab8
 	@ln -s -f libclanSound.so.$(D_VERSION_MINOR) Libs/libclanSound.so.$(D_VERSION_MAJOR)
4e43ab8
 	@ln -s -f libclanSound.so.$(D_VERSION_MAJOR) Libs/libclanSound.so
4e43ab8