-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorganyahelper.cpp
207 lines (167 loc) · 4.53 KB
/
organyahelper.cpp
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
#include "organyahelper.h"
#include <liborganya/organya/organya.h>
#include <liborganya/pxtone/pxtnService.h>
#define INPUT_BUFFER_SIZE 1024
class OrgFileReader : public AbstractReader
{
public:
OrgFileReader() = default;
virtual ~OrgFileReader();
virtual bool load(const QString &path) override final;
virtual void seek(qint64 time) override final;
virtual qint64 totalTime() const override final;
virtual qint64 read(unsigned char *data, qint64 maxSize) override final;
private:
org_decoder_t *m_input = nullptr;
};
OrgFileReader::~OrgFileReader()
{
if(m_input)
{
org_decoder_destroy(m_input);
}
}
bool OrgFileReader::load(const QString &path)
{
m_input = org_decoder_create(qPrintable(path), 1);
if(!m_input)
{
qWarning("OrgFileReader: org_decoder_create error");
return false;
}
return true;
}
void OrgFileReader::seek(qint64 time)
{
org_decoder_seek_sample(m_input, time * sampleRate() / 1000);
}
qint64 OrgFileReader::totalTime() const
{
return org_decoder_get_total_samples(m_input) / sampleRate() * 1000;
}
qint64 OrgFileReader::read(unsigned char *data, qint64 maxSize)
{
const unsigned int sample = maxSize / channels() / sizeof(int16_t);
return org_decode_samples(m_input, (int16_t*)data, sample) * channels() * sizeof(int16_t);
}
class PxFileReader : public AbstractReader
{
public:
PxFileReader() = default;
virtual ~PxFileReader();
virtual bool load(const QString &path) override final;
virtual void seek(qint64 time) override final;
virtual qint64 totalTime() const override final;
virtual qint64 read(unsigned char *data, qint64 maxSize) override final;
private:
pxtnService *m_pxs = nullptr;
pxtnDescriptor *m_pxd = nullptr;
};
PxFileReader::~PxFileReader()
{
if(m_pxs)
{
m_pxs->clear();
delete m_pxs;
delete m_pxd;
}
}
bool PxFileReader::load(const QString &path)
{
QFile file(path);
if(!file.open(QIODevice::ReadOnly))
{
qWarning("PxFileReader: open file failed");
return false;
}
m_pxs = new pxtnService;
m_pxd = new pxtnDescriptor;
if(m_pxs->init() != pxtnOK)
{
qWarning("PxFileReader: pxtnService init error");
return false;
}
if(!m_pxs->set_destination_quality(2, sampleRate()))
{
qWarning("PxFileReader: set_destination_quality error");
return false;
}
const QByteArray &buffer = file.readAll();
file.close();
if(!m_pxd->set_memory_r((void*)buffer.constData(), buffer.length()))
{
qWarning("PxFileReader: set_memory_r error");
return false;
}
if(m_pxs->read(m_pxd) != pxtnOK)
{
qWarning("PxFileReader: pxtnService read error");
return false;
}
if(m_pxs->tones_ready() != pxtnOK)
{
qWarning("PxFileReader: pxtnService tones_ready error");
m_pxs->evels->Release();
return false;
}
pxtnVOMITPREPARATION prep = {0};
// prep.flags |= pxtnVOMITPREPFLAG_loop; // don't loop
prep.start_pos_float = 0;
prep.master_volume = 1.0f; //(volume / 100.0f);
if(!m_pxs->moo_preparation(&prep))
{
qWarning("PxFileReader: moo_preparation error");
return false;
}
return true;
}
void PxFileReader::seek(qint64 time)
{
pxtnVOMITPREPARATION prep = {0};
prep.start_pos_sample = sampleRate() * time / 1000;
prep.master_volume = 1.0f; //(volume / 100.0f);
m_pxs->moo_preparation(&prep);
}
qint64 PxFileReader::totalTime() const
{
return m_pxs->moo_get_total_sample() / sampleRate() * 1000;
}
qint64 PxFileReader::read(unsigned char *data, qint64)
{
if(m_pxs->moo_is_end_vomit() || !m_pxs->moo_is_valid_data())
{
return 0;
}
const unsigned int sample = pxtnBITPERSAMPLE / 8 * channels() * INPUT_BUFFER_SIZE;
return m_pxs->Moo((void*)data, sample) ? sample : 0;
}
OrganyaHelper::OrganyaHelper(const QString &path)
: m_path(path)
{
}
OrganyaHelper::~OrganyaHelper()
{
deinit();
}
void OrganyaHelper::deinit()
{
delete m_input;
}
bool OrganyaHelper::initialize()
{
const QString &suffix = m_path.toLower();
if(suffix.endsWith(".org")) m_input = new OrgFileReader;
else if(suffix.endsWith(".pttune") || suffix.endsWith(".ptcop")) m_input = new PxFileReader;
if(!m_input)
{
qWarning("OrganyaHelper: load file suffix error");
return false;
}
if(!m_input->load(m_path))
{
qWarning("OrganyaHelper: unable to open file");
return false;
}
seek(0);
return true;
}