forked from LMMS/lmms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAudioResampler.cpp
More file actions
80 lines (69 loc) · 2.37 KB
/
AudioResampler.cpp
File metadata and controls
80 lines (69 loc) · 2.37 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
/*
* AudioResampler.cpp
*
* Copyright (c) 2025 Sotonye Atemie <sakertooth@gmail.com>
*
* This file is part of LMMS - https://lmms.io
*
* This program 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 2 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include "AudioResampler.h"
#include <samplerate.h>
#include <stdexcept>
#include <string>
namespace lmms {
AudioResampler::AudioResampler(InterpolationMode interpolationMode)
: m_interpolationMode(interpolationMode)
, m_state(src_new(static_cast<int>(interpolationMode), DEFAULT_CHANNELS, &m_error))
{
if (!m_state)
{
const auto errorMessage = std::string{src_strerror(m_error)};
const auto fullMessage = std::string{"Failed to create an AudioResampler: "} + errorMessage;
throw std::runtime_error{fullMessage};
}
}
AudioResampler::~AudioResampler()
{
src_delete(m_state);
}
void AudioResampler::resample(SampleFrame* dst, size_t frames, double ratio, WriteCallback callback, void* callbackData)
{
m_data.data_out = &dst[0][0];
m_data.output_frames = static_cast<long>(frames);
m_data.src_ratio = ratio;
m_data.end_of_input = 0;
while (m_data.output_frames > 0)
{
if (m_data.input_frames == 0)
{
const auto numInputFrames = callback(m_writeBuffer.data(), m_writeBuffer.size(), callbackData);
m_data.data_in = &m_writeBuffer.data()[0][0];
m_data.input_frames = numInputFrames;
}
if (m_data.input_frames < 0 || src_process(m_state, &m_data))
{
std::fill_n(m_data.data_out, m_data.output_frames * DEFAULT_CHANNELS, 0.0f);
break;
}
m_data.data_in += m_data.input_frames_used * DEFAULT_CHANNELS;
m_data.input_frames -= m_data.input_frames_used;
m_data.data_out += m_data.output_frames_gen * DEFAULT_CHANNELS;
m_data.output_frames -= m_data.output_frames_gen;
}
}
} // namespace lmms