-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.cpp
173 lines (155 loc) · 3.31 KB
/
sample.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
#include "sample.h"
#include <QMessageBox>
#include <fmod_errors.h>
#include <QDebug>
void Sample::init()
{
fadeOutTimer.setInterval(30);
fadeOutTimer.setSingleShot(false);
fadeOutTimer.stop();
connect(&fadeOutTimer, SIGNAL(timeout()), SLOT(onFadeOutTimer()));
needReloading = false;
playing = false;
sound = 0;
channel = 0;
muted = false;
reload();
}
Sample::Sample(const QString &name, const QString &file, LoopType looping, KeyboardKey kbdKey, FMOD::System *fmodSystem, int numChannel)
: keyboardKey(kbdKey)
{
system = fmodSystem;
sampleName = name;
sampleFileName = file;
loop = looping;
volume = 100;
panning = 0;
channelNumber = numChannel;
init();
}
Sample::Sample(const SampleInfo & info, FMOD::System * fmodSystem, int numChannel)
: keyboardKey(info.key)
{
system = fmodSystem;
sampleName = info.name;
sampleFileName = info.file;
loop = info.loopType;
volume = info.volume;
panning = info.panning;
channelNumber = numChannel;
init();
}
SampleInfo Sample::info() const
{
SampleInfo info;
info.name = sampleName;
info.file = sampleFileName;
info.key = keyboardKey;
info.loopType = loop;
info.volume = volume;
info.panning = panning;
return info;
}
bool Sample::resOk(FMOD_RESULT res)
{
if (res != FMOD_OK)
{
qDebug() << QString("FMOD returned error: %1 (%2)").arg(FMOD_ErrorString(res)).arg(res);
return false;
}
else
return true;
}
void Sample::reload()
{
FMOD_RESULT result = system->createSound(sampleFileName.toLatin1().data(), FMOD_SOFTWARE | ((loop != NoLoop) ? FMOD_LOOP_NORMAL : FMOD_LOOP_OFF), 0, &sound);
if (!resOk(result))
sound = 0;
needReloading = false;
}
void Sample::start()
{
if (fadeOutTimer.isActive())
{
fadeOutTimer.stop();
volume = oldVolume;
}
playing = resOk(system->playSound((FMOD_CHANNELINDEX)channelNumber, sound, true, &channel));
if (playing)
{
channel->setVolume(volume / 100.0);
channel->setPan(panning / 100.0);
channel->setMute(muted);
FMOD_RESULT result = channel->setPaused(false);
resOk(result);
emit started();
}
}
void Sample::stop()
{
if (playing)
{
oldVolume = volume;
fadeOutTimer.start();
playing = false;
}
}
void Sample::setMute(bool mute)
{
muted = mute;
if (channel)
channel->setMute(mute);
}
void Sample::setVolume(int newVolume)
{
volume = newVolume;
if (channel)
channel->setVolume(volume / 100.0);
emit changed();
}
void Sample::setPanning(int newPan)
{
panning = newPan;
if (channel)
channel->setPan(panning / 100.0);
emit changed();
}
void Sample::setLoopType(LoopType loopType)
{
loop = loopType;
if (sound)
sound->setMode(FMOD_SOFTWARE | ((loop != NoLoop) ? FMOD_LOOP_NORMAL : FMOD_LOOP_OFF));
emit changed();
}
void Sample::setKey(KeyboardKey newKey)
{
keyboardKey = newKey;
emit changed();
}
void Sample::setName(const QString & newName)
{
sampleName = newName;
emit changed();
}
void Sample::setFileName(const QString & newFile)
{
sampleFileName = newFile;
if (!playing)
reload();
else
needReloading = true;
emit changed();
}
void Sample::onFadeOutTimer()
{
setVolume(volume / 1.2);
if (!volume)
{
channel->stop();
fadeOutTimer.stop();
emit stopped();
volume = oldVolume;
if (needReloading)
reload();
}
}