2 * Copyright (C) 2021 Alexander Schmidt
4 * This file is part of Seamulator.
6 * Seamulator is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Seamulator is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Seamulator. If not, see <http://www.gnu.org/licenses/>.
20 #include "synthesizer.h"
34 #include "watersurface.h"
36 using namespace std::placeholders;
38 Synthesizer::Synthesizer(ConstWaterSurfacePtr surface) :
39 m_surface{std::move(surface)},
40 m_startTime{std::chrono::system_clock::now()},
41 m_lastRuntime{getRuntime()}
43 stk::Stk::setSampleRate( 44100.0 );
44 stk::Stk::showWarnings( true );
46 m_dac = std::make_unique<stk::RtWvOut>(2);
48 //m_sine1.setFrequency( 441.0 );
49 //m_sine2.setFrequency( 520.0 );
51 //m_sine1.noteOn(40.0, 1.0);
52 // m_sine2.noteOn(74.0, 1.0);
54 //for (int i = 0; i < 10; ++i) {
55 // m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(i, 0), 440.0) );
58 m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(0, 0), 939.85) );
59 m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(0, 10), 704.09) );
60 m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(0, 20), 469.92) );
61 m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(0, 30), 352.04) );
62 m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(0, 40), 279.42) );
64 m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(10, 10), 1054.94) );
65 m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(20, 10), 704.09) );
66 m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(30, 10), 527.47) );
67 m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(40, 10), 418.65) );
68 m_dots.emplace_back( std::make_unique<Dot>(m_surface->at(50, 10), 313.64) );
70 m_audioThread = std::thread( [this]{audioLoop();} );
72 // std::cout << "NoteOn 0.0 1 64.000000 64.0" << std::endl;
73 // std::cout << std::flush;
75 // Figure out how many bytes in an StkFloat and setup the RtAudio stream.
76 // RtAudio::StreamParameters parameters;
77 // parameters.deviceId = m_dac.getDefaultOutputDevice();
78 // parameters.nChannels = 1;
79 // RtAudioFormat format = ( sizeof(stk::StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
80 // unsigned int bufferFrames = stk::RT_BUFFER_SIZE;
82 // // auto aTick = std::bind(&Synthesizer::audioTick, this, _1, _2, _3, _4, _5, _6);
83 // std::function<int (void*, void*, unsigned int, double, unsigned int, void*)> bla = [this](void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
84 // double streamTime, RtAudioStreamStatus status, void *dataPointer) -> int {
87 // m_dac.openStream( ¶meters, NULL, format, (unsigned int)stk::Stk::sampleRate(), &bufferFrames, bla,
90 // catch ( RtAudioError &error ) {
91 // // error.printMessage();
96 // m_sine.setFrequency(440.0);
98 // m_dac.startStream();
100 // catch ( RtAudioError &error ) {
101 // // error.printMessage();
107 Synthesizer::~Synthesizer()
110 m_audioThread.join();
113 void Synthesizer::tick()
115 const auto runtime = getRuntime();
116 const auto deltaT = runtime - m_lastRuntime;
118 for (auto& dot : m_dots) {
119 dot->advance(deltaT);
122 // const auto posBefore = m_pos;
123 // const auto posBefore2 = m_pos2;
125 // m_vel += -1.0 * ( m_pos - m_surface->at(0, 0).getHeight() ) * deltaT - deltaT * m_vel * 0.1;
126 // m_pos += m_vel * deltaT;
128 // //std::cout << m_vel << std::endl;
130 // std::cout << m_pos2 << std::endl;
132 // m_vel2 += -10.0 * ( m_pos2 - m_surface->at(10, 10).getHeight() ) * deltaT - deltaT * m_vel2 * 0.1;
133 // m_pos2 += m_vel2 * deltaT;
135 // m_velMax1 = std::max(m_velMax1, m_vel);
136 // m_velMax2 = std::max(m_velMax2, m_vel2);
138 // if (posBefore * m_pos < 0.0) {
139 // // std::cout << "ControlChange 0.0 2 7 " << 100 * std::fabs(m_vel) << std::endl;
141 // // std::cout << "NoteOn 0.0 2 75 64" << std::endl;
142 // // std::cout << std::flush;
143 // m_sine1.noteOn(m_pos >= 0 ? 440.0 : 329.628, m_vel / m_velMax1);
146 // if (posBefore2 * m_pos2 < 0.0) {
147 // // std::cout << "ControlChange 0.0 1 7 " << 100 * std::fabs(m_vel) << std::endl;
148 // // std::cout << "NoteOn 0.0 1 44 64" << std::endl;
149 // // std::cout << "NoteOff 0.1 1 44 64" << std::endl;
150 // // std::cout << std::flush;
151 // m_sine2.noteOn(m_pos2 >= 0 ? 440.0 : 329.628, m_vel2 / m_velMax2);
154 // // std::cout << std::fabs(m_vel) * 10000 << std::endl;
156 // // m_sine1.controlChange(128, std::fabs(m_vel) * 10000);
158 // // std::cout << "ControlChange 0.0 1 7 " << 100 * std::fabs(m_vel) << std::endl;
159 // // std::cout << "PitchChange 0.0 1 " << 42 + 0.1*m_pos << std::endl;
161 // // std::cout << "ControlChange 0.0 1 7 " << 64 + 100 * m_vel << std::endl;
162 // //std::cout << "AfterTouch 0.0 1 " << 64 + 10 * m_vel << std::endl;
163 // //std::cout << "PitchChange 0.0 1 " << 64 + 10*m_vel << std::endl;
165 // std::cout << std::flush;
169 m_lastRuntime = runtime;
172 void Synthesizer::audioLoop()
174 stk::PRCRev effect0(0.5);
176 stk::Chorus chorus{6000};
177 stk::StkFrames frames(88200, 2);
179 for (int i = 0; i < 88200; i++) {
182 for (std::size_t dotIdx = 0; dotIdx < m_dots.size(); ++dotIdx) {
183 frames(i, dotIdx % 2 == 0 ? 1 : 0) += m_dots[dotIdx]->tick();
185 //frames(i,1) = frames(i,0);
187 // frames = effect0.tick(frames);
188 // frames = effect.tick(frames);
189 frames = chorus.tick(frames);
195 double Synthesizer::getRuntime() const
197 auto timeNow = std::chrono::system_clock::now();
199 std::chrono::duration_cast<std::chrono::milliseconds>(timeNow - m_startTime);
200 return durationMs.count() / 1000.0;