This repository was archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAudio_DataManager.cpp
More file actions
191 lines (171 loc) · 8.82 KB
/
Audio_DataManager.cpp
File metadata and controls
191 lines (171 loc) · 8.82 KB
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
/*******************************************************************************
*******************************************************************************
* Copyright (c) 2009-2023 ectotropic (ectotropic@gmail.com, *
* https://github.com/ectotropic) *
* *
* This program is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation, either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************
******************************************************************************/
//--------------------------------------
//
#include "CommonHeaders.h"
#include "Audio_DataManager.h"
//--------------------------------------
//--------------------------------------
//
#include "Audio_DecibelData_Util.h"
#include "Audio_SampleData_Util.h"
//--------------------------------------
namespace Audio {
/**************************************************************************
* IAudioDataManager *
**************************************************************************/
void IAudioDataManager::Update(const Visualisation::RequestParams& request) {
m_UsingData = request.Want;
update_params params{};
update_hint_params hints{};
params.m_WantSpectrum = false;
params.m_WantWaveform = false;
if (m_UsingData & (vis_data_type::CombinedWaveform | vis_data_type::Waveform)) {
params.m_WantWaveform = true;
assert(request.Waveform.nSampleCountHint > 0);
m_fnWaveformTransform = request.Waveform.fnTransform;
m_Waveform.value_count(request.Waveform.nSampleCountHint);
m_Waveform.peak_decay_rate(request.Waveform.fPeakDecayRate,
request.Waveform.fPeakMininum);
} else {
m_Waveform.clear();
m_fnWaveformTransform = {};
}
if (m_UsingData & (vis_data_type::CombinedDecibel | vis_data_type::Decibel)) {
params.m_WantWaveform = true;
m_fnDecibelTransform = request.Decibel.fnTransform;
m_Decibel.peak_decay_rate(request.Decibel.fPeakDecayRate,
request.Decibel.fPeakMininum);
} else {
m_Decibel.clear();
m_fnDecibelTransform = {};
}
if (m_UsingData & (vis_data_type::CombinedSpectrum | vis_data_type::Spectrum)) {
hints.m_SpectrumSize = request.Spectrum.nSampleCountHint;
params.m_WantSpectrum = true;
assert(request.Spectrum.nSampleCountHint > 0);
m_fnSpectrumTransform = request.Spectrum.fnTransform;
m_Spectrum.value_count(request.Spectrum.nSampleCountHint);
m_Spectrum.peak_decay_rate(request.Spectrum.fPeakDecayRate,
request.Spectrum.fPeakMininum);
} else {
m_Spectrum.clear();
m_fnSpectrumTransform = {};
}
const auto elapsed{ m_UpdateTimer.GetElapsedSeconds() };
params.m_Offset = elapsed;
hints.m_Duration = elapsed;
OnUpdate(params, hints);
m_UpdateTimer.Start();
}
//--------------------------------------------------------------------------
void IAudioDataManager::SetWaveformData(const waveform_sample_type* samples,
size_type sampleCount,
size_type channelCount) {
if (m_UsingData & (vis_data_type::CombinedWaveform | vis_data_type::Waveform)) {
if (samples == nullptr || sampleCount == 0 || channelCount == 0) {
m_Waveform.zero();
return;
}
const auto cUpdated = Samples::update_sample_data(m_Waveform,
samples, sampleCount, channelCount,
m_fnWaveformTransform);
if ((cUpdated > 0) && (m_UsingData & vis_data_type::CombinedWaveform)) {
auto combined{ Samples::get_combined_samples(m_Waveform, cUpdated) };
m_Waveform.combined_update(std::move(combined));
}
}
if (m_UsingData & (vis_data_type::CombinedDecibel | vis_data_type::Decibel)) {
SetDecibelData(samples, sampleCount, channelCount);
}
}
//--------------------------------------------------------------------------
void IAudioDataManager::SetDecibelData(const dB_type* samples,
size_type sampleCount,
size_type channelCount) {
if (samples == nullptr || sampleCount == 0 || channelCount == 0) {
m_Decibel.zero();
return;
}
static constexpr const auto CombineChannelIndex{ static_cast<size_type>(-1) };
const auto dBChannelCount{ m_Decibel.channel_count() };
if (channelCount > 1) {
auto updateChannelCount{ std::min(dBChannelCount, channelCount) };
size_type ch{ 0 };
dB_type combined{ 0 };
while (ch < updateChannelCount) {
auto dB{ dB::dBFromWavedata(samples,
sampleCount,
channelCount) };
combined += dB;
if (m_fnDecibelTransform) { dB = m_fnDecibelTransform(ch, dB); }
m_Decibel.channel_update(ch, dB);
++ch; ++samples;
}
if (m_UsingData & vis_data_type::CombinedDecibel) {
combined /= static_cast<dB_type>(ch);
if (m_fnDecibelTransform) {
combined = m_fnDecibelTransform(CombineChannelIndex,
combined);
}
m_Decibel.combined_update(combined);
} else {
m_Decibel.combined_zero();
}
while (ch < dBChannelCount) { m_Decibel.channel_zero(ch++); }
} else {
constexpr const size_type stride{ 1 };
const auto dB{ dB::dBFromWavedata(samples, sampleCount, stride) };
for (size_type ch = 0; ch < dBChannelCount; ++ch) {
if (m_fnDecibelTransform) {
m_Decibel.channel_update(ch, m_fnDecibelTransform(ch, dB));
} else {
m_Decibel.channel_update(ch, dB);
}
}
if (m_UsingData & vis_data_type::CombinedDecibel) {
if (m_fnDecibelTransform) {
m_Decibel.combined_update(m_fnDecibelTransform(CombineChannelIndex, dB));
} else {
m_Decibel.combined_update(dB);
}
}
}
}
//--------------------------------------------------------------------------
void IAudioDataManager::SetSpectrumData(const spectrum_sample_type* samples,
size_type sampleCount,
size_type channelCount) {
if (m_UsingData & (vis_data_type::CombinedSpectrum | vis_data_type::Spectrum)) {
if (samples == nullptr || sampleCount == 0 || channelCount == 0) {
m_Spectrum.zero();
return;
}
const auto cUpdated = Samples::update_sample_data(m_Spectrum,
samples, sampleCount, channelCount,
m_fnSpectrumTransform);
if ((cUpdated > 0) && (m_UsingData & vis_data_type::CombinedSpectrum)) {
auto combined{ Samples::get_combined_samples(m_Spectrum, cUpdated) };
m_Spectrum.combined_update(std::move(combined));
}
}
}
} // namespace Audio