From: Alexander Schmidt Date: Sat, 27 Feb 2021 09:47:43 +0000 (+0100) Subject: Distribute dots in space X-Git-Url: http://git.treefish.org/~alex/seamulator.git/commitdiff_plain/18258cd17adcc9e97a58926a9882d0e23683c59d Distribute dots in space --- diff --git a/include/dot.h b/include/dot.h index 28792ff..4e086bd 100644 --- a/include/dot.h +++ b/include/dot.h @@ -1,3 +1,5 @@ +#pragma once + #include #include "Plucked.h" diff --git a/include/posdot.h b/include/posdot.h new file mode 100644 index 0000000..611c387 --- /dev/null +++ b/include/posdot.h @@ -0,0 +1,8 @@ +#pragma once + +#include "dot.h" + +struct PosDot { + std::array pos; + std::unique_ptr dot; +}; diff --git a/include/synthesizer.h b/include/synthesizer.h index 5d38053..9dc3b61 100644 --- a/include/synthesizer.h +++ b/include/synthesizer.h @@ -31,6 +31,7 @@ #include "Plucked.h" #include "dot.h" +#include "posdot.h" class Synthesizer { @@ -64,7 +65,7 @@ private: void audioLoop(); - std::vector> m_dots; + std::vector m_posDots; stk::Plucked m_sine1; stk::Plucked m_sine2; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 75990a0..41c3754 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,6 +9,7 @@ find_package(STK REQUIRED) find_package(Boost 1.40 COMPONENTS program_options REQUIRED) find_package(ALSA REQUIRED) find_package(Threads REQUIRED) +find_package(Eigen3 3.3 REQUIRED NO_MODULE) include_directories(${seamulator_SOURCE_DIR}/include ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} @@ -21,7 +22,7 @@ add_executable(seamulator seamulator.cpp target_link_libraries(seamulator ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${FFTW_LIBRARIES} ${Boost_LIBRARIES} - ${STK_LIBRARY} ${ALSA_LIBRARIES} ${}) + ${STK_LIBRARY} ${ALSA_LIBRARIES} Eigen3::Eigen) target_link_libraries(seamulator PRIVATE Threads::Threads) diff --git a/src/synthesizer.cpp b/src/synthesizer.cpp index 5943b06..8352e7a 100644 --- a/src/synthesizer.cpp +++ b/src/synthesizer.cpp @@ -31,9 +31,11 @@ #include #include +#include + #include "watersurface.h" -using namespace std::placeholders; +using namespace Eigen; Synthesizer::Synthesizer(ConstWaterSurfacePtr surface) : m_surface{std::move(surface)}, @@ -60,9 +62,12 @@ Synthesizer::Synthesizer(ConstWaterSurfacePtr surface) : const std::vector frequencies = {939.85, 704.09, 469.92, 352.04, 279.42, 1054.94, 704.09, 527.47, 418.65, 313.64}; for (const auto& frequency : frequencies) { - m_dots.emplace_back( std::make_unique(m_surface->at(rand() % m_surface->size(), rand() % m_surface->size()), frequency) ); + const std::array pos = + { rand() % m_surface->size(), rand() % m_surface->size() }; + m_posDots.emplace_back( + PosDot{pos, std::make_unique(m_surface->at(pos[0], pos[1]), frequency)} ); } - + // m_dots.emplace_back( std::make_unique(m_surface->at(10, 50), 939.85) ); // m_dots.emplace_back( std::make_unique(m_surface->at(20, 40), 704.09) ); // m_dots.emplace_back( std::make_unique(m_surface->at(30, 30), 469.92) ); @@ -123,8 +128,8 @@ void Synthesizer::tick() const auto runtime = getRuntime(); const auto deltaT = runtime - m_lastRuntime; - for (auto& dot : m_dots) { - dot->advance(deltaT); + for (auto& posDot : m_posDots) { + posDot.dot->advance(deltaT); } // const auto posBefore = m_pos; @@ -185,12 +190,30 @@ void Synthesizer::audioLoop() stk::Echo echo3{7000}; stk::Chorus chorus{6000}; stk::StkFrames frames(88200, 2); + + + const Vector2d leftPos{0.0, -1.0}; + const Vector2d rightPos{1.0, -1.0}; + + std::vector dotDist; + + for (std::size_t dotIdx = 0; dotIdx < m_posDots.size(); ++dotIdx) { + auto& posDot = m_posDots[dotIdx]; + const Vector2d pos( static_cast(posDot.pos[0]) / m_surface->size(), + static_cast(posDot.pos[1]) / m_surface->size() ); + dotDist.emplace_back( (leftPos - pos).squaredNorm() ); + dotDist.emplace_back( (rightPos - pos).squaredNorm() ); + } + while (!m_stop) { for (int i = 0; i < 88200; i++) { frames(i, 0) *= 0; frames(i, 1) *= 0; - for (std::size_t dotIdx = 0; dotIdx < m_dots.size(); ++dotIdx) { - frames(i, dotIdx % 2 == 0 ? 1 : 0) += m_dots[dotIdx]->tick(); + for (std::size_t dotIdx = 0; dotIdx < m_posDots.size(); ++dotIdx) { + const auto tick = m_posDots[dotIdx].dot->tick(); + frames(i, 0) += tick / dotDist[dotIdx*2]; + frames(i, 1) += tick / dotDist[dotIdx*2+1]; + // frames(i, 1) += (1.0 - normPosX) * tick; } //frames(i,1) = frames(i,0); }