Skip to content

Commit

Permalink
- Added volume slider
Browse files Browse the repository at this point in the history
- Fixed spectrum not working on Emscripten (FIXME: chebwin NAN on ems)
- Fixed mouseup not registered when outside window on web
  • Loading branch information
clo-yunhee committed Jan 14, 2023
1 parent b8e2e01 commit e1d83e9
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/app/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,6 @@ bool Application::initGLFW() {
glfwMakeContextCurrent(m_window);

#ifdef __EMSCRIPTEN__
// Register mouseup event on the window object to register if a slider is released
// when the mouse is outside of the window.
emscripten_set_mouseup_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, true,
emsMouseCallback);
#else
// Register content scale events.
glfwSetWindowContentScaleCallback(m_window, glfwScaleCallback);
Expand Down Expand Up @@ -223,6 +219,11 @@ bool Application::setupImGui() {
// Register HTML5 events on Emscripten.
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, true,
emsUiCallback);

// Register mouseup event on the window object to register if a slider is released
// when the mouse is outside of the window.
emscripten_set_mouseup_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, true,
emsMouseCallback);
#endif

return true;
Expand Down
4 changes: 4 additions & 0 deletions src/app/GeneratorSpectrum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ void GeneratorSpectrum::constructWindow() {
m_window.resize(m_nfft);
m_values.resize(m_nfft);

#ifdef __EMSCRIPTEN__
m_window = windows::blackmanHarris<Scalar>(m_nfft, true);
#else
m_window = windows::chebwin<Scalar>(m_nfft, 320, true);
#endif
}

void GeneratorSpectrum::constructFrequencyArray() {
Expand Down
2 changes: 1 addition & 1 deletion src/app/GlottalFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void GlottalFlow::updateSamples() {
m_flow[i] = gauss_kronrod<float, 15>::integrate(
[this](double t) -> double { return m_model->evaluate(t); }, 0,
m_times[i], 8, 1e-6);
#elif defined(USING_DOUBLE_FLOAT) m_flow[i] =
#elif defined(USING_DOUBLE_FLOAT)
gauss_kronrod<double, 31>::integrate(
[this](double t) -> double { return m_model->evaluate(t); }, 0,
m_times[i], 15, 1e-6);
Expand Down
17 changes: 14 additions & 3 deletions src/app/SourceModelApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ SourceModelApp::SourceModelApp(const int initialWidth, const int initialHeight)
m_showAdvancedSourceParams(false),
m_doBypassFilter(false),
m_doNormalizeFlowPlot(true),
m_spectrumFrequencyScale(FrequencyScale_Mel) {
m_spectrumFrequencyScale(FrequencyScale_Mel),
m_logVolume(90),
m_linVolume(0.6561) {
ImPlot::CreateContext();

m_audioOutput.setBufferCallback([this](std::vector<Scalar>& out) {
Expand All @@ -41,14 +43,15 @@ SourceModelApp::SourceModelApp(const int initialWidth, const int initialHeight)
} else {
m_formantGenerator.fillBuffer(out);
}
for (auto& x : out) x *= m_linVolume;
return true;
});

m_glottalFlow.setSampleCount(1024);
m_glottalFlow.setModelType(GlottalFlowModel_LF); // Default model to LF.

m_sourceSpectrum.setResponseTime(0.025);
m_formantSpectrum.setResponseTime(0.025);
m_sourceSpectrum.setResponseTime(0.00125);
m_formantSpectrum.setResponseTime(0.00125);

m_sourceSpectrum.setTransformSize(4096);
m_formantSpectrum.setTransformSize(4096);
Expand Down Expand Up @@ -118,6 +121,14 @@ void SourceModelApp::renderMenuBar() {

ImGui::Separator();

ImGui::SetNextItemWidth(5 * em());
if (ImGui::SliderFloatOrDouble("##Volume", &m_logVolume, 0.0_f, 100.0_f, "%.0f %%")) {
// Using x^4 as an approximation.
m_linVolume = std::pow(m_logVolume / 100.0_f, 4.0_f);
}

ImGui::Separator();

// No point in showing audio settings on Web
if (ImGui::BeginMenu("Audio settings")) {
#ifdef USING_RTAUDIO
Expand Down
2 changes: 2 additions & 0 deletions src/app/SourceModelApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class SourceModelApp : public Application {
bool m_doBypassFilter;
bool m_doNormalizeFlowPlot;
FrequencyScale m_spectrumFrequencyScale;
Scalar m_logVolume;
Scalar m_linVolume;
};

#endif // SOURCEMODEL__SOURCEMODELAPP_H
5 changes: 3 additions & 2 deletions src/app/math/windows.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SOURCEMODEL__MATH_WINDOWS_H
#define SOURCEMODEL__MATH_WINDOWS_H

#include <array>
#include <boost/math/constants/constants.hpp>
#include <boost/math/special_functions/acosh.hpp>
#include <boost/math/special_functions/bessel.hpp>
Expand Down Expand Up @@ -66,7 +67,7 @@ class linspace {
// Sum-of-cosine templates
template <typename T, auto& coefs>
inline T cosineSum(T x) {
constexpr size_t n = std::tuple_size<decltype(coefs)>::value;
constexpr size_t n(coefs.size());
T v(coefs[0]);
int s = -1;
for (size_t i = 1; i < n; ++i) {
Expand Down Expand Up @@ -267,7 +268,7 @@ std::vector<T> chebwin(int M, const T at, const bool sym = true) {

// Compute the parameter beta
const T order = M - 1;
const T beta = std::cosh(1.0 / order * acosh(std::pow(10.0, std::abs(at) / 20.0)));
const T beta = std::cosh(T(1) / order * acosh(std::pow(T(10), std::abs(at) / T(20))));
std::vector<T> x(M);
for (int i = 0; i < M; ++i) {
x[i] = beta * cos_pi(T(i) / T(M));
Expand Down

0 comments on commit e1d83e9

Please sign in to comment.