forked from AttorneyOnline/AO2-Client
-
Notifications
You must be signed in to change notification settings - Fork 3
/
aobasshandle.cpp
97 lines (77 loc) · 2 KB
/
aobasshandle.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
#include "aobasshandle.hpp"
AOBassHandle::AOBassHandle(QObject *p_parent)
: QObject(p_parent)
{}
AOBassHandle::AOBassHandle(QString p_file, bool p_suicide, QObject *p_parent) noexcept(false)
: QObject(p_parent)
{
set_file(p_file, p_suicide);
}
AOBassHandle::~AOBassHandle()
{
if (m_handle && m_sync)
{
BASS_ChannelRemoveSync(m_handle, m_sync);
}
// nothing will go wrong if the handle isn't initialized, I promise...!
if (m_handle)
{
BASS_StreamFree(m_handle);
}
}
void AOBassHandle::suicide()
{
// safety first
disconnect();
// suicide note
emit body_discovery();
delete this;
}
QString AOBassHandle::get_file()
{
return m_file;
}
void AOBassHandle::set_file(QString p_file, bool p_suicide) noexcept(false)
{
if (m_handle)
return; // already created
m_file = p_file;
// create a handle based on the file
m_handle = BASS_StreamCreateFile(FALSE, m_file.utf16(), 0, 0, BASS_UNICODE|BASS_ASYNCFILE);
if (!m_handle)
throw AOException(QString("%1 could not be initialized to play").arg(p_file));
m_sync = BASS_ChannelSetSync(m_handle, BASS_SYNC_END, 0, &AOBassHandle::static_sync, this);
if (!m_sync)
{
// free stream since we can't sync
BASS_StreamFree(m_handle);
throw AOException(QString("could not set sync for %1").arg(m_file));
}
m_suicide = p_suicide;
if (m_suicide)
connect(this, &AOBassHandle::stopped, this, &AOBassHandle::suicide);
}
void AOBassHandle::set_volume(int p_volume)
{
BASS_ChannelSetAttribute(m_handle, BASS_ATTRIB_VOL, p_volume/100.0f);
}
void AOBassHandle::play()
{
BASS_ChannelPlay(m_handle, FALSE);
}
void AOBassHandle::stop()
{
BASS_ChannelStop(m_handle);
}
void CALLBACK AOBassHandle::static_sync(HSYNC handle, DWORD channel, DWORD data, void *user)
{
if (auto self = static_cast<AOBassHandle*>(user))
{
self->sync(data);
}
}
void AOBassHandle::sync(DWORD data)
{
Q_UNUSED(data)
emit stopped();
}