1 /**
2 * Copyright (c) 2006-2011 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty.  In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 *    claim that you wrote the original software. If you use this software
14 *    in a product, an acknowledgment in the product documentation would be
15 *    appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 *    misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20 #include <common/config.h>
21 #include "ModPlugDecoder.h"
22 
23 #include <common/Exception.h>
24 
25 namespace love
26 {
27 namespace sound
28 {
29 namespace lullaby
30 {
ModPlugDecoder(Data * data,const std::string & ext,int bufferSize)31 	ModPlugDecoder::ModPlugDecoder(Data * data, const std::string & ext, int bufferSize)
32 		: Decoder(data, ext, bufferSize), plug(0)
33 	{
34 
35 		// Set some ModPlug settings.
36 		settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING | MODPLUG_ENABLE_NOISE_REDUCTION;
37 		settings.mChannels = 2;
38 		settings.mBits = 16;
39 		settings.mFrequency = sampleRate;
40 		settings.mResamplingMode = MODPLUG_RESAMPLE_LINEAR;
41 
42 		// fill with modplug defaults (modplug _memsets_, so we could get
43 		// garbage settings when the struct is only partially initialized)
44 		// This does not exist yet on Windows.
45 		settings.mStereoSeparation = 128;
46 		settings.mMaxMixChannels = 32;
47 		settings.mReverbDepth = 0;
48 		settings.mReverbDelay = 0;
49 		settings.mBassAmount = 0;
50 		settings.mBassRange = 0;
51 		settings.mSurroundDepth = 0;
52 		settings.mSurroundDelay = 0;
53 		settings.mLoopCount = 0;
54 
55 		ModPlug_SetSettings(&settings);
56 
57 		// Load the module.
58 		plug = ModPlug_Load(data->getData(), data->getSize());
59 
60 		if(plug == 0)
61 			throw love::Exception("Could not load file with ModPlug.");
62 
63 		// set master volume for delicate ears
64 		ModPlug_SetMasterVolume(plug, 128);
65 	}
66 
~ModPlugDecoder()67 	ModPlugDecoder::~ModPlugDecoder()
68 	{
69 		if(plug != 0)
70 			ModPlug_Unload(plug);
71 	}
72 
accepts(const std::string & ext)73 	bool ModPlugDecoder::accepts(const std::string & ext)
74 	{
75 		static const std::string supported[] = {
76 			"699", "abc", "amf", "ams", "dbm", "dmf",
77 			"dsm", "far", "it", "j2b", "mdl", "med",
78 			"mid", "mod", "mt2", "mtm", "okt", "pat",
79 			"psm", "s3m", "stm", "ult", "umx", "wav",
80 			"xm", ""
81 		};
82 
83 		for(int i = 0; !(supported[i].empty()); i++)
84 		{
85 			if(supported[i].compare(ext) == 0)
86 				return true;
87 		}
88 
89 		return false;
90 	}
91 
clone()92 	love::sound::Decoder * ModPlugDecoder::clone()
93 	{
94 		return new ModPlugDecoder(data, ext, bufferSize);
95 	}
96 
decode()97 	int ModPlugDecoder::decode()
98 	{
99 		int r =  ModPlug_Read(plug, buffer, bufferSize);
100 
101 		if(r == 0)
102 			eof = true;
103 
104 		return r;
105 	}
106 
seek(float s)107 	bool ModPlugDecoder::seek(float s)
108 	{
109 		ModPlug_Seek(plug, (int)(s*1000.0f));
110 		return true;
111 	}
112 
rewind()113 	bool ModPlugDecoder::rewind()
114 	{
115 		// Let's reload.
116 		ModPlug_Unload(plug);
117 		plug = ModPlug_Load(data->getData(), data->getSize());
118 		ModPlug_SetMasterVolume(plug, 128);
119 		eof = false;
120 		return (plug != 0);
121 	}
122 
isSeekable()123 	bool ModPlugDecoder::isSeekable()
124 	{
125 		return true;
126 	}
127 
getChannels() const128 	int ModPlugDecoder::getChannels() const
129 	{
130 		return 2;
131 	}
132 
getBits() const133 	int ModPlugDecoder::getBits() const
134 	{
135 		return 16;
136 	}
137 
138 } // lullaby
139 } // sound
140 } // love
141