1 | /** |
2 | * Copyright (c) 2006-2023 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 | |
21 | #include "ModPlugDecoder.h" |
22 | |
23 | #ifndef LOVE_NO_MODPLUG |
24 | |
25 | #include "common/Exception.h" |
26 | |
27 | namespace love |
28 | { |
29 | namespace sound |
30 | { |
31 | namespace lullaby |
32 | { |
33 | |
34 | ModPlugDecoder::ModPlugDecoder(Data *data, int bufferSize) |
35 | : Decoder(data, bufferSize) |
36 | , plug(0) |
37 | , duration(-2.0) |
38 | { |
39 | |
40 | // Set some ModPlug settings. |
41 | settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING | MODPLUG_ENABLE_NOISE_REDUCTION; |
42 | settings.mChannels = 2; |
43 | settings.mBits = 16; |
44 | settings.mFrequency = sampleRate; |
45 | settings.mResamplingMode = MODPLUG_RESAMPLE_LINEAR; |
46 | |
47 | // fill with modplug defaults (modplug _memsets_, so we could get |
48 | // garbage settings when the struct is only partially initialized) |
49 | // This does not exist yet on Windows. |
50 | |
51 | settings.mStereoSeparation = 128; |
52 | settings.mMaxMixChannels = 32; |
53 | settings.mReverbDepth = 0; |
54 | settings.mReverbDelay = 0; |
55 | settings.mBassAmount = 0; |
56 | settings.mBassRange = 0; |
57 | settings.mSurroundDepth = 0; |
58 | settings.mSurroundDelay = 0; |
59 | settings.mLoopCount = -1; |
60 | |
61 | ModPlug_SetSettings(&settings); |
62 | |
63 | // Load the module. |
64 | plug = ModPlug_Load(data->getData(), (int) data->getSize()); |
65 | |
66 | if (plug == 0) |
67 | throw love::Exception("Could not load file with ModPlug." ); |
68 | |
69 | // set master volume for delicate ears |
70 | ModPlug_SetMasterVolume(plug, 128); |
71 | } |
72 | |
73 | ModPlugDecoder::~ModPlugDecoder() |
74 | { |
75 | if (plug != 0) |
76 | ModPlug_Unload(plug); |
77 | } |
78 | |
79 | bool ModPlugDecoder::accepts(const std::string &ext) |
80 | { |
81 | static const std::string supported[] = |
82 | { |
83 | "699" , "abc" , "amf" , "ams" , "dbm" , "dmf" , |
84 | "dsm" , "far" , "it" , "j2b" , "mdl" , "med" , |
85 | "mid" , "mod" , "mt2" , "mtm" , "okt" , "pat" , |
86 | "psm" , "s3m" , "stm" , "ult" , "umx" , "xm" , |
87 | "" |
88 | }; |
89 | |
90 | for (int i = 0; !(supported[i].empty()); i++) |
91 | { |
92 | if (supported[i].compare(ext) == 0) |
93 | return true; |
94 | } |
95 | |
96 | return false; |
97 | } |
98 | |
99 | love::sound::Decoder *ModPlugDecoder::clone() |
100 | { |
101 | return new ModPlugDecoder(data.get(), bufferSize); |
102 | } |
103 | |
104 | int ModPlugDecoder::decode() |
105 | { |
106 | int r = ModPlug_Read(plug, buffer, bufferSize); |
107 | |
108 | if (r == 0) |
109 | eof = true; |
110 | |
111 | return r; |
112 | } |
113 | |
114 | bool ModPlugDecoder::seek(double s) |
115 | { |
116 | ModPlug_Seek(plug, (int)(s*1000.0)); |
117 | return true; |
118 | } |
119 | |
120 | bool ModPlugDecoder::rewind() |
121 | { |
122 | // Let's reload. |
123 | ModPlug_Unload(plug); |
124 | plug = ModPlug_Load(data->getData(), (int) data->getSize()); |
125 | ModPlug_SetMasterVolume(plug, 128); |
126 | eof = false; |
127 | return (plug != 0); |
128 | } |
129 | |
130 | bool ModPlugDecoder::isSeekable() |
131 | { |
132 | return true; |
133 | } |
134 | |
135 | int ModPlugDecoder::getChannelCount() const |
136 | { |
137 | return 2; |
138 | } |
139 | |
140 | int ModPlugDecoder::getBitDepth() const |
141 | { |
142 | return 16; |
143 | } |
144 | |
145 | double ModPlugDecoder::getDuration() |
146 | { |
147 | // Only calculate the duration if we haven't done so already. |
148 | if (duration == -2.0) |
149 | { |
150 | int lengthms = ModPlug_GetLength(plug); |
151 | |
152 | if (lengthms < 0) |
153 | duration = -1.0; |
154 | else |
155 | duration = (double) lengthms / 1000.0; |
156 | } |
157 | |
158 | return duration; |
159 | } |
160 | |
161 | } // lullaby |
162 | } // sound |
163 | } // love |
164 | |
165 | #endif // LOVE_NO_MODPLUG |
166 | |