Move decoder (de)initialization into their respective classes
[alure.git] / src / codec_modplug.cpp
blob01ebf3f384bf64bf4e8495e48c912d4e23ef8a64
1 /*
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
21 * IN THE SOFTWARE.
24 #include "config.h"
26 #include "main.h"
28 #include <string.h>
29 #include <assert.h>
31 #include <istream>
33 #include <modplug.h>
36 #ifdef _WIN32
37 #define MODPLUG_LIB "libmodplug.dll"
38 #elif defined(__APPLE__)
39 #define MODPLUG_LIB "libmodplug.1.dylib"
40 #else
41 #define MODPLUG_LIB "libmodplug.so.1"
42 #endif
44 static void *mod_handle;
45 #define MAKE_FUNC(x) static typeof(x)* p##x
46 MAKE_FUNC(ModPlug_Load);
47 MAKE_FUNC(ModPlug_Unload);
48 MAKE_FUNC(ModPlug_Read);
49 MAKE_FUNC(ModPlug_SeekOrder);
50 #undef MAKE_FUNC
53 struct modStream : public alureStream {
54 private:
55 ModPlugFile *modFile;
56 int lastOrder;
58 public:
59 static void Init()
61 mod_handle = OpenLib(MODPLUG_LIB);
62 if(!mod_handle) return;
64 LOAD_FUNC(mod_handle, ModPlug_Load);
65 LOAD_FUNC(mod_handle, ModPlug_Unload);
66 LOAD_FUNC(mod_handle, ModPlug_Read);
67 LOAD_FUNC(mod_handle, ModPlug_SeekOrder);
69 static void Deinit()
71 if(mod_handle)
72 CloseLib(mod_handle);
73 mod_handle = NULL;
76 virtual bool IsValid()
77 { return modFile != NULL; }
79 virtual bool GetFormat(ALenum *fmt, ALuint *frequency, ALuint *blockalign)
81 *fmt = AL_FORMAT_STEREO16;
82 *frequency = 44100;
83 *blockalign = 2 * sizeof(ALshort);
84 return true;
87 virtual ALuint GetData(ALubyte *data, ALuint bytes)
89 int ret = pModPlug_Read(modFile, data, bytes);
90 if(ret < 0) return 0;
91 return ret;
94 virtual bool Rewind()
95 { return SetOrder(lastOrder); }
97 virtual bool SetOrder(ALuint order)
99 std::vector<char> data(16384);
100 ALuint total = 0;
101 while(total < 2*1024*1024)
103 fstream->read(&data[total], data.size()-total);
104 if(fstream->gcount() == 0) break;
105 total += fstream->gcount();
106 data.resize(total*2);
108 data.resize(total);
110 ModPlugFile *newMod = pModPlug_Load(&data[0], data.size());
111 if(!newMod)
113 SetError("Could not reload data");
114 return false;
116 pModPlug_Unload(modFile);
117 modFile = newMod;
119 // There seems to be no way to tell if the seek succeeds
120 pModPlug_SeekOrder(modFile, order);
121 lastOrder = order;
123 return true;
126 modStream(std::istream *_fstream)
127 : alureStream(_fstream), modFile(NULL), lastOrder(0)
129 if(!mod_handle) return;
131 std::vector<char> data(16384);
132 ALuint total = 0;
133 while(total < 2*1024*1024)
135 fstream->read(&data[total], data.size()-total);
136 if(fstream->gcount() == 0) break;
137 total += fstream->gcount();
138 data.resize(total*2);
140 data.resize(total);
142 modFile = pModPlug_Load(&data[0], data.size());
145 virtual ~modStream()
147 if(modFile)
148 pModPlug_Unload(modFile);
149 modFile = NULL;
152 static DecoderDecl<modStream> modStream_decoder;