Skip to content

Commit

Permalink
Merge pull request Chysn#51 from qiemem/fix-transpose
Browse files Browse the repository at this point in the history
Fix transpose in quantizer
  • Loading branch information
benirose committed Feb 9, 2023
2 parents fe2c9d6 + 3a1f29f commit a78742e
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions software/o_c_REV/braids_quantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@

namespace braids {

const int32_t NEIGHBOR_WEIGHT = 10; // out of 16
constexpr int32_t CUR_WEIGHT = 16 - NEIGHBOR_WEIGHT;

void SortScale(Scale &scale) {
std::sort(scale.notes, scale.notes + scale.num_notes);
}
Expand Down Expand Up @@ -64,6 +67,7 @@ int32_t Quantizer::Process(int32_t pitch, int32_t root, int32_t transpose) {
// We're still in the voronoi cell for the active codeword.
pitch = codeword_;
} else {
requantize_ = false;
int16_t octave = pitch / span_ - (pitch < 0 ? 1 : 0);
int16_t rel_pitch = pitch - span_ * octave;

Expand All @@ -77,26 +81,37 @@ int32_t Quantizer::Process(int32_t pitch, int32_t root, int32_t transpose) {
}
}

if (abs(pitch - (octave + 1) * span_ + notes_[0]) < best_distance) {
if (abs(pitch - (octave + 1) * span_ - notes_[0]) < best_distance) {
octave++;
q = 0;
} else if (abs(pitch - (octave - 1) * span_ + notes_[num_notes_ - 1]) < best_distance) {
} else if (abs(pitch - (octave - 1) * span_ - notes_[num_notes_ - 1]) <= best_distance) {
octave--;
q = num_notes_ - 1;
}

q += transpose;
octave += q / num_notes_;
q %= num_notes_;
if (q < 0) {
q += num_notes_;
octave--;
}

note_number_ = octave * num_notes_ + q;
codeword_ = notes_[q] + octave * span_;
previous_boundary_ = q == 0
? notes_[num_notes_ - 1] + (octave - 1) * span_
: notes_[q - 1] + octave * span_;

previous_boundary_ = (9 * previous_boundary_ + 7 * codeword_) >> 4;
previous_boundary_ =
(NEIGHBOR_WEIGHT * previous_boundary_ + CUR_WEIGHT * codeword_) >> 4;
next_boundary_ = q == num_notes_ - 1
? notes_[0] + (octave + 1) * span_
: notes_[q + 1] + octave * span_;
next_boundary_ = (9 * next_boundary_ + 7 * codeword_) >> 4;
next_boundary_ =
(NEIGHBOR_WEIGHT * next_boundary_ + CUR_WEIGHT * codeword_) >> 4;

transpose_ = transpose;
pitch = codeword_;
}
pitch += root;
Expand Down

0 comments on commit a78742e

Please sign in to comment.