about summary refs log tree commit diff
path: root/src/engine/client/snd.cpp
diff options
context:
space:
mode:
authorJakob Fries <jakob.fries@gmail.com>2007-08-09 14:55:11 +0000
committerJakob Fries <jakob.fries@gmail.com>2007-08-09 14:55:11 +0000
commitbf3efbc586bb3a99e963f8ee3a8a65d376f86187 (patch)
treec260ad73d5671344285241b3542718d1ed5716ad /src/engine/client/snd.cpp
parenta6d4e6ab53e1b494a25f015b8d82699035c268e0 (diff)
downloadzcatch-bf3efbc586bb3a99e963f8ee3a8a65d376f86187.tar.gz
zcatch-bf3efbc586bb3a99e963f8ee3a8a65d376f86187.zip
sounds are now WavPack instead of Wav
Diffstat (limited to 'src/engine/client/snd.cpp')
-rw-r--r--src/engine/client/snd.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/engine/client/snd.cpp b/src/engine/client/snd.cpp
index 2b01fc84..69cca0f5 100644
--- a/src/engine/client/snd.cpp
+++ b/src/engine/client/snd.cpp
@@ -4,6 +4,10 @@
 
 #include <engine/interface.h>
 
+extern "C" {
+#include "../../wavpack/wavpack.h"
+}
+
 using namespace baselib;
 
 static const int NUM_FRAMES_STOP = 512;
@@ -244,6 +248,87 @@ static int snd_alloc_sound()
 	return id;
 }
 
+static FILE *file = NULL;
+
+static int read_data(void *buffer, int size)
+{
+	return fread(buffer, 1, size, file);	
+}
+
+int snd_load_wv(const char *filename)
+{
+	sound_data snd;
+	int id = -1;
+
+	char error[100];
+
+	file = fopen(filename, "r");
+
+	WavpackContext *context = WavpackOpenFileInput(read_data, error);
+	if (context)
+	{
+		int samples = WavpackGetNumSamples(context);
+		int bitspersample = WavpackGetBitsPerSample(context);
+		int bytespersample = WavpackGetBytesPerSample(context);
+		unsigned int samplerate = WavpackGetSampleRate(context);
+		int channels = WavpackGetNumChannels(context);
+
+		snd.channels = channels;
+		snd.rate = samplerate;
+
+		if(snd.channels > 2)
+		{
+			dbg_msg("sound/wv", "file is not mono or stereo. filename='%s'", filename);
+			return -1;
+		}
+
+		if(snd.rate != 44100)
+		{
+			dbg_msg("sound/wv", "file is %d Hz, not 44100 Hz. filename='%s'", snd.rate, filename);
+			return -1;
+		}
+		
+		if(bitspersample != 16)
+		{
+			dbg_msg("sound/wv", "bps is %d, not 16, filname='%s'", bitspersample, filename);
+			return -1;
+		}
+
+		int *data = (int *)mem_alloc(4*samples*channels, 1);
+		WavpackUnpackSamples(context, data, samples); // TODO: check return value
+		int *src = data;
+		
+		snd.data = (short *)mem_alloc(2*samples*channels, 1);
+		short *dst = snd.data;
+
+		for (int i = 0; i < samples*channels; i++)
+			*dst++ = (short)*src++;
+
+		mem_free(data);
+
+		snd.num_samples = samples;
+		snd.sustain_start = -1;
+		snd.sustain_end = -1;
+		snd.last_played = 0;
+		id = snd_alloc_sound();
+		sounds[id].sound = snd;
+	}
+	else
+	{
+		dbg_msg("sound/wv", "failed to open %s: %s", filename, error);
+	}
+
+	fclose(file);
+	file = NULL;
+
+	if(id >= 0)
+		dbg_msg("sound/wv", "loaded %s", filename);
+	else
+		dbg_msg("sound/wv", "failed to load %s", filename);
+
+	return id;
+}
+
 int snd_load_wav(const char *filename)
 {
 	sound_data snd;