]> git.treefish.org Git - seamulator.git/blobdiff - src/synthesizer.cpp
Play note on velocity sign change
[seamulator.git] / src / synthesizer.cpp
index a4148ced1431aa8210cf9c4f50ceb23b35749def..fb920ae465de765185cc049b5aeb811038db8787 100644 (file)
 #include <PRCRev.h>
 #include <NRev.h>
 
+#include <Eigen/Dense>
+
 #include "watersurface.h"
 
-using namespace std::placeholders;
+using namespace Eigen;
 
 Synthesizer::Synthesizer(ConstWaterSurfacePtr surface) :
     m_surface{std::move(surface)},
     m_startTime{std::chrono::system_clock::now()},
     m_lastRuntime{getRuntime()}
 {
-    stk::Stk::setSampleRate( 44100.0 );
+    stk::Stk::setSampleRate( 48000.0 );
     stk::Stk::showWarnings( true );
 
     m_dac = std::make_unique<stk::RtWvOut>(2);
@@ -55,17 +57,28 @@ Synthesizer::Synthesizer(ConstWaterSurfacePtr surface) :
 //        m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(i, 0), 440.0) );
         //}
 
-    m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(10, 50), 939.85) );
-    m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(20, 40), 704.09) );
-    m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(30, 30), 469.92) );
-    m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(40, 20), 352.04) );
-    m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(50, 10), 279.42) );
+    srand (time(NULL));
+    
+    const std::vector<double> 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) {
+        const std::array<int, 2> pos =
+            { rand() % m_surface->size(), rand() %  m_surface->size() };
+        m_posDots.emplace_back(
+            PosDot{pos, std::make_unique<Dot>(m_surface->at(pos[0], pos[1]), frequency)} );
+    }
+
+    // m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(10, 50), 939.85) );
+    // m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(20, 40), 704.09) );
+    // m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(30, 30), 469.92) );
+    // m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(40, 20), 352.04) );
+    // m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(50, 10), 279.42) );
 
-    m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(50, 10), 1054.94) );
-    m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(40, 20), 704.09) );
-    m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(30, 30), 527.47) );
-    m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(20, 40), 418.65) );
-    m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(10, 50), 313.64) );
+    // m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(50, 10), 1054.94) );
+    // m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(40, 20), 704.09) );
+    // m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(30, 30), 527.47) );
+    // m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(20, 40), 418.65) );
+    // m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(10, 50), 313.64) );
 
     m_audioThread = std::thread( [this]{audioLoop();} );
 
@@ -115,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;
@@ -177,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<double> dotDist;
+
+    for (std::size_t dotIdx = 0; dotIdx < m_posDots.size(); ++dotIdx) {
+        auto& posDot = m_posDots[dotIdx];
+        const Vector2d pos( static_cast<double>(posDot.pos[0]) / m_surface->size(),
+                            static_cast<double>(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);
         }