Skip to content

Commit 03bcb88

Browse files
committed
Unify sample rate in the internal audio engine
1 parent 427b155 commit 03bcb88

62 files changed

Lines changed: 244 additions & 181 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ set(HEADER_FILES
6666
domain/arpeggiator.hpp
6767
domain/column.hpp
6868
domain/column_settings.hpp
69-
domain/devices/device.hpp
7069
domain/devices/delay_effect.hpp
70+
domain/devices/device.hpp
7171
domain/devices/effect.hpp
7272
domain/devices/high_pass_filter_effect.hpp
7373
domain/devices/low_pass_filter_effect.hpp
@@ -219,6 +219,7 @@ set(SOURCE_FILES
219219
domain/song.cpp
220220
domain/track.cpp
221221
domain/devices/device.cpp
222+
domain/devices/effect.cpp
222223
domain/devices/delay_effect.cpp
223224
domain/devices/high_pass_filter_effect.cpp
224225
domain/devices/low_pass_filter_effect.cpp
@@ -229,6 +230,7 @@ set(SOURCE_FILES
229230
domain/devices/volume_effect.cpp
230231
domain/dsp/adsr_envelope.cpp
231232
domain/dsp/cascaded_svf.cpp
233+
domain/dsp/dsp_component.cpp
232234
domain/dsp/lfo.cpp
233235
domain/dsp/multi_engine.cpp
234236
domain/dsp/polyblep_oscillator.cpp

src/application/service/jack_service.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "jack_service.hpp"
1717

18+
#include "../../common/constants.hpp"
1819
#include "../../contrib/SimpleLogger/src/simple_logger.hpp"
1920
#include "../../infra/audio/audio_engine.hpp"
2021
#include "settings_service.hpp"
@@ -156,7 +157,7 @@ double JackService::bpm() const
156157

157158
uint32_t JackService::sampleRate() const
158159
{
159-
return m_client ? jack_get_sample_rate(m_client) : 48000;
160+
return m_client ? jack_get_sample_rate(m_client) : static_cast<uint32_t>(Constants::defaultSampleRate());
160161
}
161162

162163
jack_nframes_t JackService::currentFrame() const

src/application/service/sampler_controller.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "sampler_controller.hpp"
22
#include "../models/sampler/sampler_pad_model.hpp"
3+
#include "../../common/constants.hpp"
4+
#include "../../common/utils.hpp"
35
#include "../../common/waveform_generator.hpp"
46
#include "../../domain/devices/sampler_device.hpp"
57

@@ -43,7 +45,12 @@ void SamplerController::setSampler(SamplerDevice::SamplerDeviceS sampler)
4345

4446
uint32_t SamplerController::sampleRate() const
4547
{
46-
return m_sampler ? m_sampler->sampleRate() : 44100;
48+
return m_sampler ? m_sampler->sampleRate() : static_cast<uint32_t>(Constants::defaultSampleRate());
49+
}
50+
51+
float SamplerController::cutoffToHz(float cutoff) const
52+
{
53+
return Utils::Dsp::cutoffToHz(cutoff / 100.0f, static_cast<float>(sampleRate()));
4754
}
4855

4956
int SamplerController::selectedPad() const

src/application/service/sampler_controller.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class SamplerController : public QObject
4848
~SamplerController() override;
4949

5050
uint32_t sampleRate() const;
51+
Q_INVOKABLE float cutoffToHz(float cutoff) const;
5152

5253
SamplerPadModel * padModel() const;
5354
SamplerDevice::SamplerDeviceS sampler() const;

src/application/service/synth_controller.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// along with Noteahead. If not, see <http://www.gnu.org/licenses/>.
1515

1616
#include "synth_controller.hpp"
17+
#include "../../common/constants.hpp"
18+
#include "../../common/utils.hpp"
1719
#include "../../domain/devices/synth_device.hpp"
1820
#include "../../domain/devices/synth_presets.hpp"
1921

@@ -130,7 +132,12 @@ void SynthController::setMasterPan(int p) { if (m_synth) { m_synth->setMasterPan
130132
int SynthController::masterVolume() const { return m_synth ? static_cast<int>(std::round(m_synth->masterVolume() * 100.0f)) : 0; }
131133
void SynthController::setMasterVolume(int v) { if (m_synth) { m_synth->setMasterVolume(v / 100.0f); emit masterVolumeChanged(); } }
132134

133-
uint32_t SynthController::sampleRate() const { return m_synth ? m_synth->sampleRate() : 44100; }
135+
uint32_t SynthController::sampleRate() const { return m_synth ? m_synth->sampleRate() : static_cast<uint32_t>(Constants::defaultSampleRate()); }
136+
137+
float SynthController::cutoffToHz(float cutoff) const
138+
{
139+
return Utils::Dsp::cutoffToHz(cutoff / 100.0f, static_cast<float>(sampleRate()));
140+
}
134141

135142
QStringList SynthController::presetNames() const
136143
{

src/application/service/synth_controller.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class SynthController : public QObject
151151
int masterVolume() const; void setMasterVolume(int v);
152152

153153
uint32_t sampleRate() const;
154+
Q_INVOKABLE float cutoffToHz(float cutoff) const;
154155

155156
QStringList presetNames() const;
156157

src/common/constants.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ QString synthDeviceName()
8787
return "Notealogue";
8888
}
8989

90+
double defaultSampleRate()
91+
{
92+
return 48000.0;
93+
}
94+
9095
namespace NahdXml {
9196

9297
QString xmlKeyFileFormatVersion()

src/common/constants.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ int transposeMax();
4444
QString samplerDeviceName();
4545
QString synthDeviceName();
4646

47+
double defaultSampleRate();
48+
4749
namespace NahdXml {
4850

4951
QString xmlKeyFileFormatVersion();

src/common/utils.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,15 @@ std::optional<std::chrono::milliseconds> readMSecAttribute(QXmlStreamReader & re
150150
}
151151

152152
} // namespace Xml
153+
namespace Dsp {
154+
float cutoffToHz(float cutoff, float sampleRate)
155+
{
156+
const float maxFreq = std::min(20000.0f, sampleRate * 0.49f);
157+
if (cutoff <= 0.0f) return 0.0f;
158+
// Map 0..1 to 0..maxFreq using a formula that goes through 0.
159+
// We'll use f = maxFreq * (pow(1000.0, cutoff) - 1.0) / 999.0
160+
// This gives a nice logarithmic feel.
161+
return maxFreq * (std::pow(1000.0f, cutoff) - 1.0f) / 999.0f;
162+
}
163+
} // namespace Dsp
153164
} // namespace noteahead::Utils

src/common/utils.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ std::optional<size_t> readUIntAttribute(QXmlStreamReader & reader, QString name,
4545
std::optional<QString> readStringAttribute(QXmlStreamReader & reader, QString name, bool required = true);
4646
std::optional<std::chrono::milliseconds> readMSecAttribute(QXmlStreamReader & reader, QString name, bool required = true);
4747
} // namespace Xml
48+
namespace Dsp {
49+
float cutoffToHz(float cutoff, float sampleRate);
50+
} // namespace Dsp
4851
} // namespace noteahead::Utils
4952

5053
#endif // UTILS_HPP

0 commit comments

Comments
 (0)