-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv2mhelper.cpp
96 lines (78 loc) · 1.75 KB
/
v2mhelper.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
#include "v2mhelper.h"
#include "archivereader.h"
#include <libv2m/v2mconv.h>
#include <libv2m/sounddef.h>
static bool v2mInitialized = false;
int loadAndConvert(unsigned char *module, qint64 size, uint8_t **conv, int *convlen)
{
if(!v2mInitialized)
{
sdInit();
v2mInitialized = true;
}
const int ver = CheckV2MVersion(module, size);
if(ver < 0)
{
return -1;
}
ConvertV2M(module, size, conv, convlen);
return 0;
}
V2MHelper::V2MHelper(const QString &path)
: m_path(path)
{
}
V2MHelper::~V2MHelper()
{
deinit();
}
void V2MHelper::deinit()
{
if(m_tune)
{
free(m_tune);
}
if(m_input)
{
m_input->Close();
delete m_input;
}
}
bool V2MHelper::initialize()
{
QFile file(m_path);
if(!file.open(QIODevice::ReadOnly))
{
qWarning("V2MHelper: open file failed");
return false;
}
const QByteArray &buffer = ArchiveReader::isSupported(m_path) ? ArchiveReader::unpack(m_path) : file.readAll();
file.close();
if(buffer.isEmpty())
{
qWarning("V2MHelper: input buffer is empty");
return false;
}
int convlen;
if(loadAndConvert((unsigned char *)buffer.constData(), buffer.length(), &m_tune, &convlen) < 0)
{
qWarning("V2MHelper: load_and_convert error");
return false;
}
m_input = new V2MPlayer;
m_input->Init();
m_input->Open(m_tune);
m_input->Play();
return true;
}
qint64 V2MHelper::read(unsigned char *data, qint64 maxSize)
{
if(!m_input->IsPlaying())
{
return 0;
}
const int sampleSize = (depth() >> 3) * channels();
const int samples = maxSize / sampleSize;
m_input->Render((float*)data, samples);
return maxSize;
}