summaryrefslogtreecommitdiff
blob: e207a4a354bf1f4a553700f3275e3476317c9b29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
commit b96981f5f23b4269108afb465f29a23abbd32ae2
Author: hasufell <hasufell@gentoo.org>
Date:   Thu Sep 5 12:31:19 2013 +0200

    Fixing wavpack sound loading. Based on Gentoo Bugzilla
    
    From: Azamat H. Hackimov <azamat.hackimov@gmail.com>
    
    https://bugs.gentoo.org/show_bug.cgi?id=363395

diff --git a/src/engine/client/sound.cpp b/src/engine/client/sound.cpp
index e32cac9..2a4c653 100644
--- a/src/engine/client/sound.cpp
+++ b/src/engine/client/sound.cpp
@@ -51,6 +51,55 @@ struct CVoice
 	int m_X, m_Y;
 } ;
 
+#ifdef WAVPACK_H
+static int32_t ReadBytes(void *pFile, void *pBuffer, int32_t Size)
+{
+	return (int32_t)io_read((IOHANDLE)pFile, pBuffer, Size);
+}
+static uint32_t GetPos(void *pFile)
+{
+	return (uint32_t)io_tell((IOHANDLE)pFile);
+}
+static int SetPosAbs(void *pFile, uint32_t Offset)
+{
+	return io_seek((IOHANDLE)pFile, Offset, IOSEEK_START);
+}
+static int SetPosRel(void *pFile, int32_t Offset, int Mode)
+{
+	switch(Mode)
+	{
+	case SEEK_SET:
+		Mode = IOSEEK_START;
+		break;
+	case SEEK_CUR:
+		Mode = IOSEEK_CUR;
+		break;
+	case SEEK_END:
+		Mode = IOSEEK_END;
+	}
+	return io_seek((IOHANDLE)pFile, Offset, Mode);
+}
+
+//TODO: Fix if 'real' functionality is needed by the wavpack header
+static int PushBackByte(void *pFile, int Char)
+{
+	return io_seek((IOHANDLE)pFile, -1, IOSEEK_CUR);
+}
+static uint32_t GetLength(void *pFile)
+{
+	return (uint32_t)io_length((IOHANDLE)pFile);
+}
+// Essentially assuming this to always be true, should fix if this isn't the case
+static int CanSeek(void *pFile)
+{
+	return pFile != NULL;
+}
+static WavpackStreamReader CWavpackReader  = {
+    ReadBytes, GetPos, SetPosAbs, SetPosRel, PushBackByte, GetLength, CanSeek, 0
+};
+#endif
+
+
 static CSample m_aSamples[NUM_SAMPLES] = { {0} };
 static CVoice m_aVoices[NUM_VOICES] = { {0} };
 static CChannel m_aChannels[NUM_CHANNELS] = { {255, 0} };
@@ -351,14 +400,12 @@ int CSound::LoadWV(const char *pFilename)
 	if(!m_pStorage)
 		return -1;
 
-	#ifndef WAVPACK_H
 	ms_File = m_pStorage->OpenFile(pFilename, IOFLAG_READ, IStorage::TYPE_ALL);
 	if(!ms_File)
 	{
 		dbg_msg("sound/wv", "failed to open file. filename='%s'", pFilename);
 		return -1;
 	}
-	#endif
 
 	SampleID = AllocID();
 	if(SampleID < 0)
@@ -368,7 +415,7 @@ int CSound::LoadWV(const char *pFilename)
 	#ifndef WAVPACK_H
 	pContext = WavpackOpenFileInput(ReadData, aError);
 	#else
-	pContext = WavpackOpenFileInput(pFilename, aError, 0, 0);
+	pContext = WavpackOpenFileInputEx(&CWavpackReader, ms_File, 0, aError, 0, 0);
 	#endif
 	if (pContext)
 	{
@@ -422,13 +469,11 @@ int CSound::LoadWV(const char *pFilename)
 	}
 	else
 	{
-		dbg_msg("sound/wv", "failed to open %s: %s", pFilename, aError);
+		dbg_msg("sound/wv", "failed to open '%s': %s", pFilename, aError);
 	}
 
-	#ifndef WAVPACK_H
 	io_close(ms_File);
 	ms_File = NULL;
-	#endif
 
 	if(g_Config.m_Debug)
 		dbg_msg("sound/wv", "loaded %s", pFilename);