1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*
* VstPresets.cpp
* --------------
* Purpose: Plugin preset / bank handling
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#ifndef NO_PLUGINS
#include "../soundlib/Sndfile.h"
#include "../soundlib/plugins/PlugInterface.h"
#ifdef MPT_WITH_VST
#include "Vstplug.h"
#endif // MPT_WITH_VST
#include "VstPresets.h"
#include "../common/FileReader.h"
#include <ostream>
#include "mpt/io/base.hpp"
#include "mpt/io/io.hpp"
#include "mpt/io/io_stdstream.hpp"
OPENMPT_NAMESPACE_BEGIN
// This part of the header is identical for both presets and banks.
struct ChunkHeader
{
char chunkMagic[4]; // 'CcnK'
int32be byteSize; // Size of this chunk, excluding magic + byteSize
char fxMagic[4]; // 'FxBk' (regular) or 'FBCh' (opaque chunk)
int32be version; // Format version (1 or 2)
int32be fxID; // Plugin unique ID
int32be fxVersion; // Plugin version
};
MPT_BINARY_STRUCT(ChunkHeader, 24)
VSTPresets::ErrorCode VSTPresets::LoadFile(FileReader &file, IMixPlugin &plugin)
{
const bool firstChunk = file.GetPosition() == 0;
ChunkHeader header;
if(!file.ReadStruct(header) || memcmp(header.chunkMagic, "CcnK", 4))
{
return invalidFile;
}
if(header.fxID != plugin.GetUID())
{
return wrongPlugin;
}
#ifdef MPT_WITH_VST
CVstPlugin *vstPlug = dynamic_cast<CVstPlugin *>(&plugin);
#endif // MPT_WITH_VST
if(!memcmp(header.fxMagic, "FxCk", 4) || !memcmp(header.fxMagic, "FPCh", 4))
{
// Program
PlugParamIndex numParams = file.ReadUint32BE();
#ifdef MPT_WITH_VST
if(vstPlug != nullptr)
{
Vst::VstPatchChunkInfo info;
info.version = 1;
info.pluginUniqueID = header.fxID;
info.pluginVersion = header.fxVersion;
info.numElements = numParams;
MemsetZero(info.reserved);
vstPlug->Dispatch(Vst::effBeginLoadProgram, 0, 0, &info, 0.0f);
}
#endif // MPT_WITH_VST
plugin.BeginSetProgram();
std::string prgName;
file.ReadString<mpt::String::maybeNullTerminated>(prgName, 28);
plugin.SetCurrentProgramName(mpt::ToCString(mpt::Charset::Locale, prgName));
if(!memcmp(header.fxMagic, "FxCk", 4))
{
if(plugin.GetNumParameters() != numParams)
{
return wrongParameters;
}
for(PlugParamIndex p = 0; p < numParams; p++)
{
const auto value = file.ReadFloatBE();
plugin.SetParameter(p, std::isfinite(value) ? value : 0.0f);
}
} else
{
uint32 chunkSize = file.ReadUint32BE();
// Some nasty plugins (e.g. SmartElectronix Ambience) write to our memory block.
// Directly writing to a memory-mapped file block results in a crash...
std::byte *chunkData = new (std::nothrow) std::byte[chunkSize];
if(chunkData)
{
file.ReadRaw(mpt::span(chunkData, chunkSize));
plugin.SetChunk(mpt::as_span(chunkData, chunkSize), false);
delete[] chunkData;
} else
{
return outOfMemory;
}
}
plugin.EndSetProgram();
} else if((!memcmp(header.fxMagic, "FxBk", 4) || !memcmp(header.fxMagic, "FBCh", 4)) && firstChunk)
{
// Bank - only read if it's the first chunk in the file, not if it's a sub chunk.
uint32 numProgs = file.ReadUint32BE();
uint32 currentProgram = file.ReadUint32BE();
file.Skip(124);
#ifdef MPT_WITH_VST
if(vstPlug != nullptr)
{
Vst::VstPatchChunkInfo info;
info.version = 1;
info.pluginUniqueID = header.fxID;
info.pluginVersion = header.fxVersion;
info.numElements = numProgs;
MemsetZero(info.reserved);
vstPlug->Dispatch(Vst::effBeginLoadBank, 0, 0, &info, 0.0f);
}
#endif // MPT_WITH_VST
if(!memcmp(header.fxMagic, "FxBk", 4))
{
int32 oldCurrentProgram = plugin.GetCurrentProgram();
for(uint32 p = 0; p < numProgs; p++)
{
plugin.BeginSetProgram(p);
ErrorCode retVal = LoadFile(file, plugin);
if(retVal != noError)
{
return retVal;
}
plugin.EndSetProgram();
}
plugin.SetCurrentProgram(oldCurrentProgram);
} else
{
uint32 chunkSize = file.ReadUint32BE();
// Some nasty plugins (e.g. SmartElectronix Ambience) write to our memory block.
// Directly writing to a memory-mapped file block results in a crash...
std::byte *chunkData = new (std::nothrow) std::byte[chunkSize];
if(chunkData)
{
file.ReadRaw(mpt::span(chunkData, chunkSize));
plugin.SetChunk(mpt::as_span(chunkData, chunkSize), true);
delete[] chunkData;
} else
{
return outOfMemory;
}
}
if(header.version >= 2)
{
plugin.SetCurrentProgram(currentProgram);
}
}
return noError;
}
bool VSTPresets::SaveFile(std::ostream &f, IMixPlugin &plugin, bool bank)
{
if(!bank)
{
SaveProgram(f, plugin);
} else
{
bool writeChunk = plugin.ProgramsAreChunks();
ChunkHeader header;
memcpy(header.chunkMagic, "CcnK", 4);
header.byteSize = 0; // will be corrected later
header.version = 2;
header.fxID = plugin.GetUID();
header.fxVersion = plugin.GetVersion();
// Write unfinished header... We need to update the size once we're done writing.
mpt::IO::Write(f, header);
uint32 numProgs = std::max(plugin.GetNumPrograms(), int32(1)), curProg = plugin.GetCurrentProgram();
mpt::IO::WriteIntBE(f, numProgs);
mpt::IO::WriteIntBE(f, curProg);
uint8 reserved[124];
MemsetZero(reserved);
mpt::IO::WriteRaw(f, reserved, sizeof(reserved));
if(writeChunk)
{
auto chunk = plugin.GetChunk(true);
uint32 chunkSize = mpt::saturate_cast<uint32>(chunk.size());
if(chunkSize)
{
mpt::IO::WriteIntBE(f, chunkSize);
mpt::IO::WriteRaw(f, chunk.data(), chunkSize);
} else
{
// The plugin returned no chunk! Gracefully go back and save parameters instead...
writeChunk = false;
}
}
if(!writeChunk)
{
for(uint32 p = 0; p < numProgs; p++)
{
plugin.SetCurrentProgram(p);
SaveProgram(f, plugin);
}
plugin.SetCurrentProgram(curProg);
}
// Now we know the correct chunk size.
std::streamoff end = f.tellp();
header.byteSize = static_cast<int32>(end - 8);
memcpy(header.fxMagic, writeChunk ? "FBCh" : "FxBk", 4);
mpt::IO::SeekBegin(f);
mpt::IO::Write(f, header);
}
return true;
}
void VSTPresets::SaveProgram(std::ostream &f, IMixPlugin &plugin)
{
bool writeChunk = plugin.ProgramsAreChunks();
ChunkHeader header;
memcpy(header.chunkMagic, "CcnK", 4);
header.byteSize = 0; // will be corrected later
header.version = 1;
header.fxID = plugin.GetUID();
header.fxVersion = plugin.GetVersion();
// Write unfinished header... We need to update the size once we're done writing.
mpt::IO::Offset start = mpt::IO::TellWrite(f);
mpt::IO::Write(f, header);
const uint32 numParams = plugin.GetNumParameters();
mpt::IO::WriteIntBE(f, numParams);
char name[28];
mpt::String::WriteBuf(mpt::String::maybeNullTerminated, name) = mpt::ToCharset(mpt::Charset::Locale, plugin.GetCurrentProgramName());
mpt::IO::WriteRaw(f, name, 28);
if(writeChunk)
{
auto chunk = plugin.GetChunk(false);
uint32 chunkSize = mpt::saturate_cast<uint32>(chunk.size());
if(chunkSize)
{
mpt::IO::WriteIntBE(f, chunkSize);
mpt::IO::WriteRaw(f, chunk.data(), chunkSize);
} else
{
// The plugin returned no chunk! Gracefully go back and save parameters instead...
writeChunk = false;
}
}
if(!writeChunk)
{
plugin.BeginGetProgram();
for(uint32 p = 0; p < numParams; p++)
{
mpt::IO::Write(f, IEEE754binary32BE(plugin.GetParameter(p)));
}
plugin.EndGetProgram();
}
// Now we know the correct chunk size.
mpt::IO::Offset end = mpt::IO::TellWrite(f);
header.byteSize = static_cast<int32>(end - start - 8);
memcpy(header.fxMagic, writeChunk ? "FPCh" : "FxCk", 4);
mpt::IO::SeekAbsolute(f, start);
mpt::IO::Write(f, header);
mpt::IO::SeekAbsolute(f, end);
}
// Translate error code to string. Returns nullptr if there was no error.
const char *VSTPresets::GetErrorMessage(ErrorCode code)
{
switch(code)
{
case VSTPresets::invalidFile:
return "This does not appear to be a valid preset file.";
case VSTPresets::wrongPlugin:
return "This file appears to be for a different plugin.";
case VSTPresets::wrongParameters:
return "The number of parameters in this file is incompatible with the current plugin.";
case VSTPresets::outOfMemory:
return "Not enough memory to load preset data.";
}
return nullptr;
}
#endif // NO_PLUGINS
OPENMPT_NAMESPACE_END
|