2 * ALURE OpenAL utility library
3 * Copyright (c) 2009-2010 by Chris Robinson.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 #include <libmodplug/modplug.h>
37 static void *mod_handle;
38 #define MAKE_FUNC(x) static typeof(x)* p##x
39 MAKE_FUNC(ModPlug_Load);
40 MAKE_FUNC(ModPlug_Unload);
41 MAKE_FUNC(ModPlug_Read);
42 MAKE_FUNC(ModPlug_SeekOrder);
45 #define ModPlug_Load pModPlug_Load
46 #define ModPlug_Unload pModPlug_Unload
47 #define ModPlug_Read pModPlug_Read
48 #define ModPlug_SeekOrder pModPlug_SeekOrder
54 struct modStream : public alureStream {
64 #define MODPLUG_LIB "libmodplug.dll"
65 #elif defined(__APPLE__)
66 #define MODPLUG_LIB "libmodplug.1.dylib"
68 #define MODPLUG_LIB "libmodplug.so.1"
70 mod_handle = OpenLib(MODPLUG_LIB);
71 if(!mod_handle) return;
73 LOAD_FUNC(mod_handle, ModPlug_Load);
74 LOAD_FUNC(mod_handle, ModPlug_Unload);
75 LOAD_FUNC(mod_handle, ModPlug_Read);
76 LOAD_FUNC(mod_handle, ModPlug_SeekOrder);
85 static void Init() { }
86 static void Deinit() { }
89 virtual bool IsValid()
90 { return modFile != NULL; }
92 virtual bool GetFormat(ALenum *fmt, ALuint *frequency, ALuint *blockalign)
94 *fmt = AL_FORMAT_STEREO16;
96 *blockalign = 2 * sizeof(ALshort);
100 virtual ALuint GetData(ALubyte *data, ALuint bytes)
102 int ret = ModPlug_Read(modFile, data, bytes);
103 if(ret < 0) return 0;
107 virtual bool Rewind()
108 { return SetOrder(lastOrder); }
110 virtual bool SetOrder(ALuint order)
112 std::vector<char> data(16384);
116 fstream->read(&data[total], data.size()-total);
117 if(fstream->gcount() == 0) break;
118 total += fstream->gcount();
119 data.resize(total*2);
123 ModPlugFile *newMod = ModPlug_Load(&data[0], data.size());
126 SetError("Could not reload data");
129 ModPlug_Unload(modFile);
132 // There seems to be no way to tell if the seek succeeds
133 ModPlug_SeekOrder(modFile, order);
139 modStream(std::istream *_fstream)
140 : alureStream(_fstream), modFile(NULL), lastOrder(0)
142 if(!mod_handle) return;
144 std::vector<char> data(1024);
147 fstream->read(&data[total], data.size()-total);
148 total += fstream->gcount();
149 if(total < 32) return;
151 if(memcmp(&data[0], "Extended Module: ", 17) == 0 || /* XM */
152 (data[28] == 0x1A && data[29] == 0x10) || /* S3M */
153 memcmp(&data[0], "IMPM", 4) == 0) /* IT */
157 data.resize(total*2);
158 fstream->read(&data[total], data.size()-total);
159 if(fstream->gcount() == 0) break;
160 total += fstream->gcount();
164 modFile = ModPlug_Load(&data[0], data.size());
171 ModPlug_Unload(modFile);
175 // Priority = -1, because mod loading can find false-positives
176 static DecoderDecl<modStream,-1> modStream_decoder;
177 Decoder &alure_init_modplug(void)
178 { return modStream_decoder; }