Skip to content

Commit

Permalink
quantum refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pd3v committed Dec 26, 2023
1 parent e4e5d65 commit 6eb5cb0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
**Most recent upgrades**

. New meaning to `/x` command. Phrase duration spreads out "x" times.
. New meaning to `/` command. Eg. `/8`, phrase duration will be 8 times longer.

. Improve sync

Expand Down
33 changes: 16 additions & 17 deletions line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,15 @@ phraseT replicate(phraseT _phrase,uint8_t times) {
return nTimesPhrase;
}

// const uint16_t barToMs(const double _bpm, const uint16_t barDur) {
const uint64_t barToMs(const double _bpm, const uint64_t barDur) {
return DEFAULT_BPM/bpm*barDur;
const uint64_t barToMs(const double _bpm, const uint64_t _barDur) {
return DEFAULT_BPM / _bpm * _barDur;
}

uint64_t barDur = barToMs(DEFAULT_BPM,REF_BAR_DUR);
uint64_t barDur = barToMs(DEFAULT_BPM, REF_BAR_DUR);

void bpmLink(double _bpm) {
bpm = _bpm;
barDur = barToMs(_bpm,quantum*REF_QUANTUM*REF_BAR_DUR);
barDur = barToMs(_bpm, REF_BAR_DUR);
}

std::string prompt = PROMPT;
Expand Down Expand Up @@ -507,6 +506,10 @@ inline long long subPhraseDur(uint64_t& _barDur, phraseT& _phrase, int64_t& _phr
return _barDur / _phrase.size();
}

inline float iterDur() {
return ITER_DUR - (phrase.size() >= 4 ? (log2(phrase.size()) - 2) * 240 : 0);
}

bool parsePhrase(std::string& _phrase) {
phraseT tempPhrase{};
phraseStr = _phrase;
Expand Down Expand Up @@ -552,15 +555,12 @@ int main(int argc, char **argv) {
state.link.enable(!state.link.isEnabled());
state.link.setTempoCallback(bpmLink);

barDur = barToMs(DEFAULT_BPM,REF_BAR_DUR);

noteMessage.push_back(0);
noteMessage.push_back(0);
noteMessage.push_back(0);

auto sequencer = async(std::launch::async, [&](){
double toNextBar = 0;
unsigned long partial = 0;
phraseT _phrase{};
uint8_t _ch = ch;
uint8_t _ccCh = ccCh;
Expand Down Expand Up @@ -601,15 +601,14 @@ int main(int argc, char **argv) {
if (phase >= toNextBar) {
for (auto& subPhrase : _phrase) {
phraseStartTime = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now()).time_since_epoch();

for (auto& subsubPhrase : subPhrase) {
for (auto& notes : subsubPhrase) {
noteMessage[0] = 144+_ch;
noteMessage[1] = notes.first;
noteMessage[2] = ((notes.first == REST_VAL) || muted) ? 0 : notes.second;
midiOut.sendMessage(&noteMessage);
}
std::this_thread::sleep_for(std::chrono::microseconds(static_cast<long long>(subPhraseDur(_barDur, _phrase, phraseElapsedTime) / subPhrase.size() - (ITER_DUR - ((log2(phrase.size()) - 2) * 240)))));
std::this_thread::sleep_for(std::chrono::microseconds(static_cast<long long>(subPhraseDur(_barDur, _phrase, phraseElapsedTime) / subPhrase.size() - iterDur())));

for (auto& notes : subsubPhrase) {
noteMessage[0] = 128+_ch;
Expand All @@ -633,7 +632,7 @@ int main(int argc, char **argv) {
noteMessage[2] = ccValues.first;
midiOut.sendMessage(&noteMessage);
}
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<unsigned long>(subPhraseDur(_barDur, _phrase, phraseElapsedTime) / subPhrase.size() - (ITER_DUR - ((log2(phrase.size()) - 2) * 240)))));
std::this_thread::sleep_for(std::chrono::microseconds(static_cast<long long>(subPhraseDur(_barDur, _phrase, phraseElapsedTime) / subPhrase.size() - iterDur())));
}
phraseEndTime = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now()).time_since_epoch();
phraseElapsedTime = static_cast<long long>(phraseEndTime.count()) - static_cast<long long>(phraseStartTime.count());
Expand All @@ -647,7 +646,7 @@ int main(int argc, char **argv) {
noteMessage[2] = ccValues.first;
midiOut.sendMessage(&noteMessage);
}
std::this_thread::sleep_for(std::chrono::milliseconds(CTRL_RATE));
std::this_thread::sleep_for(std::chrono::microseconds(CTRL_RATE));
}
}
}
Expand Down Expand Up @@ -696,6 +695,7 @@ int main(int argc, char **argv) {
try {
bpm = static_cast<double>(std::abs(std::stoi(opt.substr(3,opt.size()-1))));
engine.setTempo(bpm);
barDur = barToMs(bpm, REF_BAR_DUR);
} catch (...) {
std::cerr << "Invalid bpm." << std::endl;
}
Expand All @@ -704,9 +704,8 @@ int main(int argc, char **argv) {
} else if (opt.substr(0,1) == "/") {
if (opt.length() > strlen("/"))
try {
// quantum = static_cast<double>(std::stof(opt.substr(1,opt.size()-1)));
quantum = static_cast<double>(std::stof(opt.substr(1,opt.size()-1))) * phrase.size();
barDur = barToMs(bpm,quantum*REF_QUANTUM*REF_BAR_DUR);
quantum = static_cast<double>(std::stof(opt.substr(1,opt.size()-1)));
barDur = barToMs(bpm, quantum * REF_BAR_DUR);
} catch (...) {
std::cerr << "Invalid phrase duration." << std::endl;
}
Expand Down Expand Up @@ -764,9 +763,9 @@ int main(int argc, char **argv) {
std::cout << "[" << (int)i++ << "] " << _p << std::endl;
});
} else if (opt == "i") {
sync= true;
sync = true;
} else if (opt == "o") {
sync= false;
sync = false;
} else if (opt.substr(0,2) == "mi") {
if (opt.length() > strlen("mi"))
try {
Expand Down
2 changes: 1 addition & 1 deletion lineparser.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package.cpath = 'externals/?/lib?.dylib;expernals/?/?.so;' .. package.cpath
package.cpath = 'externals/?/lib?.dylib;externals/?/lib?.so;' .. package.cpath

local lpeg = require 'lpeg'
local write = io.write
Expand Down

0 comments on commit 6eb5cb0

Please sign in to comment.