-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsilenceplugin.cpp
87 lines (73 loc) · 1.66 KB
/
silenceplugin.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
#include "silenceplugin.h"
#include <QSettings>
#include <qmath.h>
static float* alignFrame(int channels, float* begin, float* sample, bool end)
{
if(!sample)
{
return nullptr;
}
int offset = sample - begin;
if(end)
{
offset += channels;
}
return begin + (offset - offset % channels);
}
SilencePlugin *SilencePlugin::m_instance = nullptr;
SilencePlugin::SilencePlugin()
: Effect()
{
m_instance = this;
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
QSettings settings;
#else
QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
#endif
m_threshold = settings.value("Silence/threshold", -40).toDouble();
}
SilencePlugin::~SilencePlugin()
{
m_instance = nullptr;
}
void SilencePlugin::applyEffect(Buffer *b)
{
m_mutex.lock();
const float threshold = std::pow(10, m_threshold / 20.0f);
float* first = nullptr;
float* last = nullptr;
float* data = b->data;
for(size_t i = 0; i < b->samples; ++i)
{
if(data[i] > threshold || data[i] < -threshold)
{
if(!first)
{
first = &data[i];
}
last = &data[i];
}
}
first = alignFrame(channels(), data, first, false);
last = alignFrame(channels(), data, last, true);
m_size = last - first;
if(m_size < b->samples)
{
b->samples = b->size = m_size;
if(first)
{
memcpy(b->data, first, m_size * sizeof(float));
}
}
m_mutex.unlock();
}
void SilencePlugin::setThreshold(double value)
{
m_mutex.lock();
m_threshold = value;
m_mutex.unlock();
}
SilencePlugin* SilencePlugin::instance()
{
return m_instance;
}