-
Notifications
You must be signed in to change notification settings - Fork 243
/
WaveformGenerator.cpp
291 lines (228 loc) · 8.2 KB
/
WaveformGenerator.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//------------------------------------------------------------------------------
//
// Copyright 2013-2021 BBC Research and Development
//
// Author: Chris Needham
//
// This file is part of Audio Waveform Image Generator.
//
// Audio Waveform Image Generator is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// Audio Waveform Image Generator 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 General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// Audio Waveform Image Generator. If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------
#include "WaveformGenerator.h"
#include "Error.h"
#include "Log.h"
#include "WaveformBuffer.h"
#include <boost/format.hpp>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>
#include <stdexcept>
//------------------------------------------------------------------------------
ScaleFactor::~ScaleFactor()
{
}
//------------------------------------------------------------------------------
SamplesPerPixelScaleFactor::SamplesPerPixelScaleFactor(int samples_per_pixel) :
samples_per_pixel_(samples_per_pixel)
{
}
//------------------------------------------------------------------------------
int SamplesPerPixelScaleFactor::getSamplesPerPixel(int /* sample_rate */) const
{
return samples_per_pixel_;
}
//------------------------------------------------------------------------------
// Calculates samples_per_pixel such that the time range start_time to end_time
// fits the specified image width.
DurationScaleFactor::DurationScaleFactor(
double start_time,
double end_time,
int width_pixels) :
start_time_(start_time),
end_time_(end_time),
width_pixels_(width_pixels)
{
if (end_time < start_time) {
throwError("Invalid end time, must be greater than %1%", start_time);
}
if (width_pixels < 1) {
throwError("Invalid image width: minimum 1");
}
}
//------------------------------------------------------------------------------
int DurationScaleFactor::getSamplesPerPixel(const int sample_rate) const
{
const double seconds = end_time_ - start_time_;
const long long width_samples = static_cast<long long>(seconds * sample_rate);
const int samples_per_pixel = static_cast<int>(width_samples / width_pixels_);
return samples_per_pixel;
}
//------------------------------------------------------------------------------
PixelsPerSecondScaleFactor::PixelsPerSecondScaleFactor(int pixels_per_second) :
pixels_per_second_(pixels_per_second)
{
if (pixels_per_second_ <= 0) {
throwError("Invalid pixels per second: must be greater than zero");
}
}
//------------------------------------------------------------------------------
int PixelsPerSecondScaleFactor::getSamplesPerPixel(int sample_rate) const
{
return sample_rate / pixels_per_second_;
}
//------------------------------------------------------------------------------
const int MAX_SAMPLE = std::numeric_limits<short>::max();
const int MIN_SAMPLE = std::numeric_limits<short>::min();
//------------------------------------------------------------------------------
WaveformGenerator::WaveformGenerator(
WaveformBuffer& buffer,
bool split_channels,
const ScaleFactor& scale_factor) :
buffer_(buffer),
scale_factor_(scale_factor),
split_channels_(split_channels),
channels_(0),
output_channels_(0),
samples_per_pixel_(0),
count_(0)
{
}
//------------------------------------------------------------------------------
bool WaveformGenerator::init(
const int sample_rate,
const int channels,
const long /* frame_count */,
const int /* buffer_size */)
{
if (channels < 1 || channels > WaveformBuffer::MAX_CHANNELS) {
log(Error) << "Cannot generate waveform data from audio file with "
<< channels << " channels\n";
return false;
}
channels_ = channels;
samples_per_pixel_ = scale_factor_.getSamplesPerPixel(sample_rate);
if (samples_per_pixel_ < 2) {
log(Error) << "Invalid zoom: minimum 2\n";
return false;
}
output_channels_ = split_channels_ ? channels : 1;
buffer_.setSamplesPerPixel(samples_per_pixel_);
buffer_.setSampleRate(sample_rate);
buffer_.setChannels(output_channels_);
log(Info) << "Generating waveform data...\n"
<< "Samples per pixel: " << samples_per_pixel_ << '\n'
<< "Input channels: " << channels_ << '\n'
<< "Output channels: " << output_channels_ << '\n';
min_.resize(output_channels_, MAX_SAMPLE);
max_.resize(output_channels_, MIN_SAMPLE);
reset();
return true;
}
//------------------------------------------------------------------------------
bool WaveformGenerator::shouldContinue() const
{
return true;
}
//------------------------------------------------------------------------------
int WaveformGenerator::getSamplesPerPixel() const
{
return samples_per_pixel_;
}
//------------------------------------------------------------------------------
void WaveformGenerator::reset()
{
for (int channel = 0; channel < output_channels_; ++channel) {
min_[channel] = MAX_SAMPLE;
max_[channel] = MIN_SAMPLE;
}
count_ = 0;
}
//------------------------------------------------------------------------------
void WaveformGenerator::done()
{
if (count_ > 0) {
for (int channel = 0; channel < output_channels_; ++channel) {
buffer_.appendSamples(
static_cast<short>(min_[channel]),
static_cast<short>(max_[channel])
);
}
reset();
}
log(Info) << "Generated " << buffer_.getSize() << " points\n";
}
//------------------------------------------------------------------------------
// See BlockFile::CalcSummary in Audacity
bool WaveformGenerator::process(
const short* input_buffer,
const int input_frame_count)
{
for (int i = 0; i < input_frame_count; ++i) {
const int index = i * channels_;
if (output_channels_ == 1) {
// Sum samples from each input channel to make a single (mono) waveform
int sample = 0;
for (int channel = 0; channel < channels_; ++channel) {
sample += input_buffer[index + channel];
}
sample /= channels_;
// Avoid numeric overflow when converting to short
if (sample > MAX_SAMPLE) {
sample = MAX_SAMPLE;
}
else if (sample < MIN_SAMPLE) {
sample = MIN_SAMPLE;
}
if (sample < min_[0]) {
min_[0] = sample;
}
if (sample > max_[0]) {
max_[0] = sample;
}
}
else {
for (int channel = 0; channel < channels_; ++channel) {
int sample = input_buffer[index + channel];
// Avoid numeric overflow when converting to short
if (sample > MAX_SAMPLE) {
sample = MAX_SAMPLE;
}
else if (sample < MIN_SAMPLE) {
sample = MIN_SAMPLE;
}
if (sample < min_[channel]) {
min_[channel] = sample;
}
if (sample > max_[channel]) {
max_[channel] = sample;
}
}
}
if (++count_ == samples_per_pixel_) {
for (int channel = 0; channel < output_channels_; ++channel) {
buffer_.appendSamples(
static_cast<short>(min_[channel]),
static_cast<short>(max_[channel])
);
}
reset();
}
}
return true;
}
//------------------------------------------------------------------------------