-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputDecoder.cpp
More file actions
331 lines (246 loc) · 8.47 KB
/
InputDecoder.cpp
File metadata and controls
331 lines (246 loc) · 8.47 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/** $VER: InputDecoder.cpp (2026.01.08) P. Stuer **/
#include "pch.h"
#include <sdk\input_impl.h>
#include <sdk\input_file_type.h>
#include <sdk\file_info_impl.h>
#include <sdk\tag_processor.h>
#include "Configuration.h"
#include "Resources.h"
#include "PMDDecoder.h"
#include "Preferences.h"
#pragma hdrstop
#pragma warning(disable: 4820) // x bytes padding added after last data member
/// <summary>
/// Implements an input decoder.
/// </summary>
class InputDecoder : public input_stubs
{
public:
InputDecoder() noexcept :
_File(), _FilePath(), _FileStats(),
_Decoder(),
_SynthesisRate(DefaultSynthesisRate),
_LoopNumber(),
_IsDynamicInfoSet(false)
{
}
InputDecoder(const InputDecoder &) = delete;
InputDecoder(InputDecoder &&) = delete;
InputDecoder& operator=(const InputDecoder &) = delete;
InputDecoder& operator=(InputDecoder &&) = delete;
~InputDecoder() noexcept
{
delete _Decoder;
}
public:
#pragma region input_impl
/// <summary>
/// Opens the specified file and parses it.
/// </summary>
void open(service_ptr_t<file> file, const char * filePath, t_input_open_reason reason, abort_callback & abortHandler)
{
if (reason == input_open_info_write)
throw exception_tagging_unsupported(); // Decoder does not support retagging.
delete _Decoder;
_Decoder = nullptr;
_File = file;
_FilePath = filePath;
input_open_file_helper(_File, filePath, reason, abortHandler);
{
_FileStats = _File->get_stats(abortHandler);
if ((_FileStats.m_size == 0) || (_FileStats.m_size > (t_size)(1 << 16)))
throw exception_io_unsupported_format("File too big");
}
{
pfc::array_t<t_uint8> Data;
Data.resize((size_t)_FileStats.m_size);
_File->read_object(&Data[0], (t_size) _FileStats.m_size, abortHandler);
{
const auto NativeFilePath = filesystem::g_get_native_path(filePath, abortHandler);
_Decoder = new pmd_decoder_t();
if (!_Decoder->Open(Data.get_ptr(), (size_t) _FileStats.m_size, (uint32_t) CfgSynthesisRate, NativeFilePath, CfgSamplesPath.get()))
throw exception_io_data("Invalid PMD file");
}
}
}
static bool g_is_our_content_type(const char * contentType)
{
return ::stricmp_utf8(contentType, "audio/pmd") == 0;
}
static bool g_is_our_path(const char *, const char * extension)
{
for (const auto & Extention : { "m", "m2", "mz" })
{
if (::stricmp_utf8(extension, Extention) == 0)
return true;
}
return false;
}
static GUID g_get_guid()
{
return GUID_COMPONENT;
}
static const char * g_get_name()
{
return STR_COMPONENT_NAME;
}
static GUID g_get_preferences_guid()
{
return GUID_PREFERENCES;
}
static bool g_is_low_merit()
{
return false;
}
#pragma endregion
#pragma region input_info_reader
unsigned get_subsong_count()
{
return 1;
}
t_uint32 get_subsong(unsigned subSongIndex)
{
return subSongIndex;
}
/// <summary>
/// Retrieves information about specified subsong.
/// </summary>
void get_info(t_uint32, file_info & fileInfo, abort_callback &)
{
double Length = _Decoder->GetLength() / 1'000.;
fileInfo.set_length(Length);
// General info tags
fileInfo.info_set("encoding", "Synthesized");
fileInfo.info_set("pcm_filename", _Decoder->GetPCMFileName());
fileInfo.info_set("pps_filename", _Decoder->GetPPSFileName());
fileInfo.info_set("ppz_filename_1", _Decoder->GetPPZFileName(1));
fileInfo.info_set("ppz_filename_2", _Decoder->GetPPZFileName(2));
fileInfo.info_set("pmd_file_version", pfc::format_int(_Decoder->GetFileVersion()));
double Loop = _Decoder->GetLoopLength() / 1'000.;
if (Loop > 0.)
fileInfo.info_set("loop_length", pfc::format_time_ex(Loop, 0));
// Meta data tags
fileInfo.meta_add("title", _Decoder->GetTitle());
fileInfo.meta_add("artist", _Decoder->GetArranger());
fileInfo.meta_add("composer", _Decoder->GetComposer());
fileInfo.meta_add("memo", _Decoder->GetMemo());
}
#pragma endregion
#pragma region input_info_reader_v2
t_filestats2 get_stats2(uint32_t stats, abort_callback & abortHandler)
{
return _File->get_stats2_(stats, abortHandler);
}
t_filestats get_file_stats(abort_callback &)
{
return _FileStats;
}
#pragma endregion
#pragma region input_info_writer
/// <summary>
/// Set the tags for the specified file.
/// </summary>
void retag_set_info(t_uint32, const file_info &, abort_callback &)
{
throw exception_tagging_unsupported();
}
void retag_commit(abort_callback &)
{
throw exception_tagging_unsupported();
}
void remove_tags(abort_callback &)
{
throw exception_tagging_unsupported();
}
#pragma endregion
#pragma region input_decoder
/// <summary>
/// Initializes the decoder before playing the specified subsong. Resets playback position to the beginning of specified subsong.
/// </summary>
void decode_initialize(unsigned, unsigned, abort_callback & abortHandler)
{
_File->reopen(abortHandler); // Equivalent to seek to zero, except it also works on nonseekable streams
_Decoder->Initialize();
_Decoder->SetMaxLoopNumber((uint32_t) CfgLoopCount);
_Decoder->SetFadeOutDuration((uint32_t) CfgFadeOutDuration);
_LoopNumber = 0;
_IsDynamicInfoSet = false;
}
/// <summary>
/// Reads/decodes one chunk of audio data.
/// </summary>
bool decode_run(audio_chunk & audioChunk, abort_callback & abortHandler)
{
abortHandler.check();
// Fill the audio chunk.
{
const size_t FramesToRender = _Decoder->GetBlockSize();
size_t FramesRendered = _Decoder->Render(audioChunk, FramesToRender);
if (FramesRendered == 0)
return false;
audioChunk.set_sample_count(FramesRendered);
}
return true;
}
/// <summary>
/// Seeks to the specified time offset.
/// </summary>
void decode_seek(double timeInSeconds, abort_callback & abortHandler)
{
abortHandler.check();
_Decoder->SetPosition((uint32_t)(timeInSeconds * 1'000.));
}
/// <summary>
/// Returns true if the input decoder supports seeking.
/// </summary>
bool decode_can_seek() noexcept
{
return true;
}
/// <summary>
/// Returns dynamic VBR bitrate etc...
/// </summary>
bool decode_get_dynamic_info(file_info & fileInfo, double &) noexcept
{
bool IsDynamicInfoUpdated = false;
if (!_IsDynamicInfoSet)
{
fileInfo.info_set_int("synthesis_rate", _SynthesisRate);
fileInfo.info_set_bitrate(((t_int64) _Decoder->GetBitsPerSample() * _Decoder->GetChannelCount() * _SynthesisRate + 500 /* rounding for bps to kbps*/) / 1'000 /* bps to kbps */);
_IsDynamicInfoSet = true;
IsDynamicInfoUpdated = true;
}
if (_LoopNumber != _Decoder->GetLoopNumber())
{
_LoopNumber = _Decoder->GetLoopNumber();
fileInfo.info_set_int("loop_number", _LoopNumber);
IsDynamicInfoUpdated = true;
}
return IsDynamicInfoUpdated;
}
/// <summary>
/// Deals with dynamic information such as track changes in live streams.
/// </summary>
bool decode_get_dynamic_info_track(file_info &, double &) noexcept
{
return false;
}
void decode_on_idle(abort_callback & abortHandler)
{
_File->on_idle(abortHandler);
}
#pragma endregion
private:
service_ptr_t<file> _File;
pfc::string _FilePath;
t_filestats _FileStats;
pmd_decoder_t * _Decoder;
uint32_t _SynthesisRate;
// Dynamic track info
uint32_t _LoopNumber;
bool _IsDynamicInfoSet;
};
#pragma warning(default: 4820) // x bytes padding added after last data member
// Declare the supported file types to make it show in "open file" dialog etc.
DECLARE_FILE_TYPE("PMD (Professional Music Driver) files", "*.m;*.m2;*.mz");
static input_factory_t<InputDecoder> _InputDecoderFactory;