1 /**
2 * Copyright (c) 2006-2012 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 
46 		// Some settings not supported by some older versions
47 #ifndef LOVE_OLD_MODPLUG
48 		settings.mStereoSeparation = 128;
49 		settings.mMaxMixChannels = 32;
50 #endif
51 		settings.mReverbDepth = 0;
52 		settings.mReverbDelay = 0;
53 		settings.mBassAmount = 0;
54 		settings.mBassRange = 0;
55 		settings.mSurroundDepth = 0;
56 		settings.mSurroundDelay = 0;
57 		settings.mLoopCount = 0;
58 
59 		ModPlug_SetSettings(&settings);
60 
61 		// Load the module.
62 		plug = ModPlug_Load(data->getData(), data->getSize());
63 
64 		if (plug == 0)
65 			throw love::Exception("Could not load file with ModPlug.");
66 
67 		// set master volume for delicate ears
68 		ModPlug_SetMasterVolume(plug, 128);
69 	}
70 
~ModPlugDecoder()71 	ModPlugDecoder::~ModPlugDecoder()
72 	{
73 		if (plug != 0)
74 			ModPlug_Unload(plug);
75 	}
76 
accepts(const std::string & ext)77 	bool ModPlugDecoder::accepts(const std::string & ext)
78 	{
79 		static const std::string supported[] = {
80 			"699", "abc", "amf", "ams", "dbm", "dmf",
81 			"dsm", "far", "it", "j2b", "mdl", "med",
82 			"mid", "mod", "mt2", "mtm", "okt", "pat",
83 			"psm", "s3m", "stm", "ult", "umx", "wav",
84 			"xm", ""
85 		};
86 
87 		for (int i = 0; !(supported[i].empty()); i++)
88 		{
89 			if (supported[i].compare(ext) == 0)
90 				return true;
91 		}
92 
93 		return false;
94 	}
95 
clone()96 	love::sound::Decoder * ModPlugDecoder::clone()
97 	{
98 		return new ModPlugDecoder(data, ext, bufferSize);
99 	}
100 
decode()101 	int ModPlugDecoder::decode()
102 	{
103 		int r =  ModPlug_Read(plug, buffer, bufferSize);
104 
105 		if (r == 0)
106 			eof = true;
107 
108 		return r;
109 	}
110 
seek(float s)111 	bool ModPlugDecoder::seek(float s)
112 	{
113 		ModPlug_Seek(plug, (int)(s*1000.0f));
114 		return true;
115 	}
116 
rewind()117 	bool ModPlugDecoder::rewind()
118 	{
119 		// Let's reload.
120 		ModPlug_Unload(plug);
121 		plug = ModPlug_Load(data->getData(), data->getSize());
122 		ModPlug_SetMasterVolume(plug, 128);
123 		eof = false;
124 		return (plug != 0);
125 	}
126 
isSeekable()127 	bool ModPlugDecoder::isSeekable()
128 	{
129 		return true;
130 	}
131 
getChannels() const132 	int ModPlugDecoder::getChannels() const
133 	{
134 		return 2;
135 	}
136 
getBits() const137 	int ModPlugDecoder::getBits() const
138 	{
139 		return 16;
140 	}
141 
142 } // lullaby
143 } // sound
144 } // love
145