DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
ChordDetector.h
1// DSPark - Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi - MIT License
3
4#pragma once
5
71#include "../Core/AudioBuffer.h"
72#include "../Core/AudioSpec.h"
73#include "../Core/DspMath.h"
74#include "../Core/SimdOps.h"
75#include "HarmonyConstants.h"
76
77#include <algorithm>
78#include <array>
79#include <atomic>
80#include <cmath>
81#include <cstddef>
82#include <cstdint>
83#include <cstring>
84#include <span>
85#include <vector>
86
87namespace dspark {
88
104template <FloatType T>
106{
107public:
109 enum class ChordType : std::uint8_t
110 {
113 };
114
116 struct Result
117 {
118 int rootPitchClass = -1;
120 float confidence = 0.0f;
121 };
122
123 // -- Lifecycle ---------------------------------------------------------------
124
186 bool prepare(const AudioSpec& spec, int windowSize = 0)
187 {
188 // Conservative no-op on invalid specs (NaN rate included): a hot
189 // detector keeps its previous configuration instead of going deaf.
190 if (!spec.isValid() || !std::isfinite(spec.sampleRate)) return false;
191 const double highestNote = noteHz(kFirstMidi + kNumNotes - 1);
192 if (!(spec.sampleRate > highestNote * 2.0)) return false;
193 sampleRate_ = spec.sampleRate;
194 if (windowSize <= 0)
195 {
196 // Automatic: hold the analysis TIME SPAN constant across sample
197 // rates (the span 4096 samples cover at 48 kHz). The Hann main
198 // lobe spans ~4/timeSpan Hz, so a constant span pins the
199 // frequency resolution -- and with it the reliable register --
200 // instead of letting it degrade as fs rises. Rounding up to a
201 // power of two only narrows the lobe further.
202 const double target = sampleRate_ * (4096.0 / 48000.0);
203 int n = 1024;
204 while (n < 16384 && static_cast<double>(n) < target) n *= 2;
205 windowSize_ = n;
206 }
207 else
208 {
209 windowSize_ = std::clamp(windowSize, 1024, 16384);
210 }
211 hopSize_ = windowSize_ / 2;
212
213 designBank();
214
215 prepared_.store(true, std::memory_order_release);
216 reset();
217 return true;
218 }
219
227 void reset() noexcept
228 {
229 std::fill(input_.begin(), input_.end(), T(0));
230 inputFill_ = 0;
231 for (auto& s : stages_)
232 {
233 std::fill(s.even.begin(), s.even.end(), T(0));
234 std::fill(s.odd.begin(), s.odd.end(), T(0));
235 }
236 for (auto& o : octaves_) std::fill(o.history.begin(), o.history.end(), T(0));
237 chromaFrame_.fill(T(0));
238 frameCount_ = 0;
239 packed_.store(pack(Result {}), std::memory_order_relaxed);
240 }
241
249 void setConfidenceThreshold(float threshold) noexcept
250 {
251 if (!std::isfinite(threshold)) return;
252 threshold_.store(std::clamp(threshold, 0.0f, 1.0f), std::memory_order_relaxed);
253 }
254
256 [[nodiscard]] float getConfidenceThreshold() const noexcept
257 {
258 return threshold_.load(std::memory_order_relaxed);
259 }
260
268 [[nodiscard]] int getWindowSize() const noexcept { return windowSize_; }
269
278 [[nodiscard]] int getHopSize() const noexcept { return hopSize_; }
279
288 [[nodiscard]] int getAnalysisDelaySamples() const noexcept { return analysisDelay_; }
289
290 // -- Shared chroma front end (stream-owner thread) ---------------------------
291
302 [[nodiscard]] std::uint64_t getFrameCount() const noexcept { return frameCount_; }
303
326 [[nodiscard]] const std::array<T, 12>& getChroma() const noexcept { return chromaFrame_; }
327
328 // -- Processing -------------------------------------------------------------------
329
332 {
333 if (!prepared_.load(std::memory_order_acquire)) return;
334 const int nCh = buffer.getNumChannels();
335 const int nS = buffer.getNumSamples();
336 if (nCh <= 0) return;
337
338 const T invCh = T(1) / static_cast<T>(nCh);
339 int done = 0;
340 while (done < nS)
341 {
342 const int take = std::min(nS - done, hopSize_ - inputFill_);
343 T* dst = input_.data() + inputFill_;
344 for (int i = 0; i < take; ++i)
345 {
346 T m = T(0);
347 for (int ch = 0; ch < nCh; ++ch)
348 m += buffer.getChannel(ch)[done + i] * invCh;
349 dst[i] = m;
350 }
351 done += take;
352 advance(take);
353 }
354 }
355
357 void pushSamples(std::span<const T> samples) noexcept
358 {
359 if (!prepared_.load(std::memory_order_acquire)) return;
360 size_t done = 0;
361 while (done < samples.size())
362 {
363 const size_t take = std::min(samples.size() - done,
364 static_cast<size_t>(hopSize_ - inputFill_));
365 std::memcpy(input_.data() + inputFill_, samples.data() + done, take * sizeof(T));
366 done += take;
367 advance(static_cast<int>(take));
368 }
369 }
370
371 // -- Readout (lock-free, any thread) ------------------------------------------------
372
374 [[nodiscard]] Result getChord() const noexcept
375 {
376 return unpack(packed_.load(std::memory_order_relaxed));
377 }
378
386 static int getChordName(const Result& result, char* dest, int size) noexcept
387 {
388 if (size <= 0) return 0;
389 if (result.rootPitchClass < 0 || result.type == ChordType::None)
390 {
391 dest[0] = '\0';
392 return 0;
393 }
394 static constexpr const char* kSuffix[11] = {
395 "", "", "m", "dim", "aug", "sus2", "sus4", "7", "maj7", "m7", "m7b5"
396 };
397 int len = 0;
398 for (const char c : harmony::noteName(result.rootPitchClass, result.rootPitchClass))
399 if (len < size - 1) dest[len++] = c;
400 for (const char* p = kSuffix[static_cast<int>(result.type)]; *p && len < size - 1; ++p)
401 dest[len++] = *p;
402 dest[len] = '\0';
403 return len;
404 }
405
406private:
407 static constexpr int kFirstMidi = 36;
408 static constexpr int kNumNotes = 48;
409 static constexpr int kNumOctaves = kNumNotes / 12;
410 static constexpr int kNumTemplates = 10;
411
415 static constexpr double kMarginBins = 8.0;
419 static constexpr double kRateSeparation = 2.5;
421 static constexpr double kStopbandDb = 80.0;
423 static constexpr int kMinOctaveWindow = 32;
425 static constexpr int kMaxLevels = 9;
426
427 struct Template
428 {
429 ChordType type;
430 harmony::NoteSet mask;
431 int count;
432 };
433
434 static constexpr std::array<Template, kNumTemplates> kTemplates { {
435 { ChordType::Major, 0b000010010001, 3 }, // 0 4 7
436 { ChordType::Minor, 0b000010001001, 3 }, // 0 3 7
437 { ChordType::Diminished, 0b000001001001, 3 }, // 0 3 6
438 { ChordType::Augmented, 0b000100010001, 3 }, // 0 4 8
439 { ChordType::Sus2, 0b000010000101, 3 }, // 0 2 7
440 { ChordType::Sus4, 0b000010100001, 3 }, // 0 5 7
441 { ChordType::Dominant7, 0b010010010001, 4 }, // 0 4 7 10
442 { ChordType::Major7, 0b100010010001, 4 }, // 0 4 7 11
443 { ChordType::Minor7, 0b010010001001, 4 }, // 0 3 7 10
444 { ChordType::HalfDim7, 0b010001001001, 4 }, // 0 3 6 10
445 } };
446
457 struct Stage
458 {
459 int pairs = 1;
460 T centre = T(0.5);
461 std::vector<T> taps;
462 std::vector<T> kernel;
463 std::vector<T> even, odd;
464 [[nodiscard]] int historyHalf() const noexcept { return 2 * pairs - 1; }
465 [[nodiscard]] int delay() const noexcept { return 2 * pairs - 1; }
466 };
467
469 struct Octave
470 {
471 int level = 0;
472 int length = 0;
473 int offset = 0;
474 std::vector<T> history;
475 std::vector<T> rows;
476 };
477
478 [[nodiscard]] static double noteHz(int midi) noexcept
479 {
480 return 440.0 * std::exp2((midi - 69) / 12.0);
481 }
482
483 [[nodiscard]] static double besselI0(double x) noexcept
484 {
485 double sum = 1.0, term = 1.0;
486 for (int k = 1; k < 64; ++k)
487 {
488 term *= (0.5 * x) / static_cast<double>(k);
489 sum += term * term;
490 if (term * term < 1e-17 * sum) break;
491 }
492 return sum;
493 }
494
497 static void designStage(Stage& s, double passHz, double rate)
498 {
499 const double width = std::max(0.02, (0.5 * rate - 2.0 * passHz) / rate);
500 const double beta = 0.1102 * (kStopbandDb - 8.7);
501 const double length = (kStopbandDb - 8.0) / (2.285 * twoPi<double> * width) + 1.0;
502 const int k = std::clamp(static_cast<int>(std::ceil((length - 3.0) / 4.0)), 0, 64);
503 s.pairs = k + 1;
504 const double half = static_cast<double>(2 * k + 1); // (L - 1) / 2
505 std::vector<double> h(static_cast<size_t>(s.pairs));
506 double sum = 0.5;
507 for (int j = 0; j < s.pairs; ++j)
508 {
509 const int n = 2 * j + 1;
510 const double r = static_cast<double>(n) / half;
511 const double w = besselI0(beta * std::sqrt(std::max(0.0, 1.0 - r * r)))
512 / besselI0(beta);
513 const double sinc = ((j & 1) ? -1.0 : 1.0) / (pi<double> * static_cast<double>(n));
514 h[static_cast<size_t>(j)] = sinc * w;
515 sum += 2.0 * h[static_cast<size_t>(j)];
516 }
517 s.taps.resize(static_cast<size_t>(s.pairs));
518 for (int j = 0; j < s.pairs; ++j)
519 s.taps[static_cast<size_t>(j)] = static_cast<T>(h[static_cast<size_t>(j)] / sum);
520 s.centre = static_cast<T>(0.5 / sum);
521 s.kernel.resize(static_cast<size_t>(2 * s.pairs));
522 for (int j = 0; j < s.pairs; ++j)
523 {
524 s.kernel[static_cast<size_t>(s.pairs - 1 - j)] = s.taps[static_cast<size_t>(j)];
525 s.kernel[static_cast<size_t>(s.pairs + j)] = s.taps[static_cast<size_t>(j)];
526 }
527 }
528
530 [[nodiscard]] static double stageGain(const Stage& s, double hz, double rate) noexcept
531 {
532 double a = static_cast<double>(s.centre);
533 for (int j = 0; j < s.pairs; ++j)
534 a += 2.0 * static_cast<double>(s.taps[static_cast<size_t>(j)])
535 * std::cos(twoPi<double> * hz / rate * static_cast<double>(2 * j + 1));
536 return std::abs(a);
537 }
538
539 void designBank()
540 {
541 const double fs = sampleRate_;
542 const double bin = fs / static_cast<double>(windowSize_);
543 std::array<double, kNumOctaves> pass {};
544 levels_ = 0;
545 for (int o = 0; o < kNumOctaves; ++o)
546 {
547 pass[static_cast<size_t>(o)] = noteHz(kFirstMidi + 12 * o + 11) + kMarginBins * bin;
548 int d = 0;
549 while (d < kMaxLevels
550 && fs / std::exp2(d + 1) >= kRateSeparation * pass[static_cast<size_t>(o)]
551 && (windowSize_ >> (d + 1)) >= kMinOctaveWindow)
552 ++d;
553 octaves_[static_cast<size_t>(o)].level = d;
554 levels_ = std::max(levels_, d);
555 }
556
557 stages_.assign(static_cast<size_t>(levels_), Stage {});
558 for (int k = 0; k < levels_; ++k)
559 {
560 double p = 0.0;
561 for (int o = 0; o < kNumOctaves; ++o)
562 if (octaves_[static_cast<size_t>(o)].level > k)
563 p = std::max(p, pass[static_cast<size_t>(o)]);
564 Stage& s = stages_[static_cast<size_t>(k)];
565 designStage(s, p, fs / std::exp2(k));
566 const size_t phase = static_cast<size_t>(s.historyHalf() + (hopSize_ >> (k + 1)));
567 s.even.assign(phase, T(0));
568 s.odd.assign(phase, T(0));
569 }
570
571 // Group delay to each level, in input samples, and the alignment of
572 // every octave's window onto the slowest one's span. The window of
573 // level d ends (2^d - 1) - delay(d) samples from the frame boundary,
574 // so reading `offset` samples further back moves it 2^d each.
575 auto levelDelay = [&](int d) {
576 int g = 0;
577 for (int s = 0; s < d; ++s) g += stages_[static_cast<size_t>(s)].delay() << s;
578 return g - ((1 << d) - 1);
579 };
580 int target = 0;
581 for (const Octave& o : octaves_) target = std::max(target, levelDelay(o.level));
582 analysisDelay_ = target;
583
584 for (int o = 0; o < kNumOctaves; ++o)
585 {
586 Octave& oc = octaves_[static_cast<size_t>(o)];
587 const int d = oc.level;
588 oc.length = windowSize_ >> d;
589 const double step = std::exp2(d);
590 oc.offset = std::max(0, static_cast<int>(std::lround(
591 static_cast<double>(target - levelDelay(d)) / step)));
592 oc.history.assign(static_cast<size_t>(oc.length + oc.offset), T(0));
593
594 const double rate = fs / step;
595 const double norm = 2.0 / static_cast<double>(oc.length);
596 oc.rows.assign(static_cast<size_t>(24 * oc.length), T(0));
597 for (int k = 0; k < 12; ++k)
598 {
599 const double hz = noteHz(kFirstMidi + 12 * o + k);
600 double gain = 1.0;
601 for (int s = 0; s < d; ++s)
602 gain *= stageGain(stages_[static_cast<size_t>(s)], hz, fs / std::exp2(s));
603 const double scale = norm / std::max(gain, 1e-6);
604 const double w = twoPi<double> * hz / rate;
605 T* cosRow = oc.rows.data() + static_cast<size_t>(k) * static_cast<size_t>(oc.length);
606 T* sinRow = oc.rows.data() + static_cast<size_t>(12 + k) * static_cast<size_t>(oc.length);
607 for (int i = 0; i < oc.length; ++i)
608 {
609 const double hann = 0.5 - 0.5 * std::cos(twoPi<double> * static_cast<double>(i)
610 / static_cast<double>(oc.length));
611 const double a = w * static_cast<double>(i);
612 cosRow[i] = static_cast<T>(scale * hann * std::cos(a));
613 sinRow[i] = static_cast<T>(scale * hann * std::sin(a));
614 }
615 }
616 }
617
618 input_.assign(static_cast<size_t>(hopSize_), T(0));
619 for (int d = 1; d <= levels_; ++d)
620 levelBlock_[static_cast<size_t>(d)].assign(static_cast<size_t>(hopSize_ >> d), T(0));
621 }
622
624 void advance(int count) noexcept
625 {
626 inputFill_ += count;
627 if (inputFill_ >= hopSize_)
628 {
629 inputFill_ = 0;
630 analyze();
631 }
632 }
633
635 static void runStage(Stage& s, const T* x, int n, T* DSPARK_RESTRICT y) noexcept
636 {
637 constexpr int W = simd::kVecWidth<T>;
638 using O = simd::Vec<T, W>;
639 const int hist = s.historyHalf();
640 T* e = s.even.data();
641 T* o = s.odd.data();
642 const int outN = n / 2;
643 int m = 0;
644 for (; m + W <= outN; m += W)
645 {
646 typename O::V ev, od;
647 O::loadDeinterleave(x + 2 * m, ev, od);
648 O::store(e + hist + m, ev);
649 O::store(o + hist + m, od);
650 }
651 for (; m < outN; ++m)
652 {
653 e[hist + m] = x[2 * m];
654 o[hist + m] = x[2 * m + 1];
655 }
656 // Output j: the symmetric odd taps over odd samples j .. j + 2K + 1,
657 // plus the centre tap on the even sample between them, j + K + 1.
658 simd::firCorrelate(o, s.kernel.data(), 2 * s.pairs, y, outN, T(1));
659 const T* centre = e + s.pairs;
660 const auto c = O::set1(s.centre);
661 int k = 0;
662 for (; k + W <= outN; k += W)
663 O::store(y + k, O::madd(c, O::load(centre + k), O::load(y + k)));
664 for (; k < outN; ++k)
665 {
666 // The vector multiply-add's rounding at the tail too.
667 T lanes[W];
668 O::store(lanes, O::madd(c, O::set1(centre[k]), O::set1(y[k])));
669 y[k] = lanes[0];
670 }
671 std::memmove(e, e + outN, static_cast<size_t>(hist) * sizeof(T));
672 std::memmove(o, o + outN, static_cast<size_t>(hist) * sizeof(T));
673 }
674
675 void analyze() noexcept
676 {
677 // Decimate the hop through every level that an octave reads.
678 const T* source = input_.data();
679 int n = hopSize_;
680 for (int d = 0; d < levels_; ++d)
681 {
682 T* out = levelBlock_[static_cast<size_t>(d + 1)].data();
683 runStage(stages_[static_cast<size_t>(d)], source, n, out);
684 source = out;
685 n /= 2;
686 }
687
688 // Note energies -> chroma, tracking the lowest sounding note: the
689 // bass is the standard root disambiguator (e.g. Dsus4 and Gsus2 are
690 // the same pitch-class set; the bass decides which one you played).
691 std::array<double, 12> chroma {};
692 std::array<double, static_cast<std::size_t>(kNumNotes)> noteE {};
693 double total = 0.0, maxNote = 0.0;
694 for (int o = 0; o < kNumOctaves; ++o)
695 {
696 Octave& oc = octaves_[static_cast<size_t>(o)];
697 const int fresh = hopSize_ >> oc.level;
698 const int keep = oc.length + oc.offset - fresh;
699 T* h = oc.history.data();
700 std::memmove(h, h + fresh, static_cast<size_t>(keep) * sizeof(T));
701 std::memcpy(h + keep, oc.level == 0 ? input_.data()
702 : levelBlock_[static_cast<size_t>(oc.level)].data(),
703 static_cast<size_t>(fresh) * sizeof(T));
704 const T* window = h; // the oldest `length` of length + offset
705 for (int k = 0; k < 12; ++k)
706 {
707 const T* cosRow = oc.rows.data() + static_cast<size_t>(k) * static_cast<size_t>(oc.length);
708 const T* sinRow = oc.rows.data() + static_cast<size_t>(12 + k) * static_cast<size_t>(oc.length);
709 const double re = static_cast<double>(simd::dotProduct(cosRow, window, oc.length));
710 const double im = static_cast<double>(simd::dotProduct(sinRow, window, oc.length));
711 const double e = re * re + im * im;
712 const int n12 = 12 * o + k;
713 noteE[static_cast<std::size_t>(n12)] = e;
714 chroma[static_cast<std::size_t>((kFirstMidi + n12) % 12)] += e;
715 total += e;
716 maxNote = std::max(maxNote, e);
717 }
718 }
719 // Publish the frame for consumers of this front end before any of the
720 // chord-specific work, and count it even when the frame turns out to
721 // be silent: a consumer polls the count to tell a new frame from the
722 // previous one, so a frame that fails to increment it would be lost.
723 for (int pc = 0; pc < 12; ++pc)
724 chromaFrame_[static_cast<std::size_t>(pc)] =
725 static_cast<T>(chroma[static_cast<std::size_t>(pc)]);
726 ++frameCount_;
727
728 // Lowest LOCAL maximum: window-lobe leakage spreads energy onto
729 // neighbouring semitones, so a plain threshold would pick a sidelobe.
730 int bassPc = -1;
731 for (int n12 = 0; n12 < kNumNotes; ++n12)
732 {
733 const double e = noteE[static_cast<std::size_t>(n12)];
734 const double prev = (n12 > 0) ? noteE[static_cast<std::size_t>(n12 - 1)] : 0.0;
735 const double next = (n12 + 1 < kNumNotes) ? noteE[static_cast<std::size_t>(n12 + 1)] : 0.0;
736 if (e > 0.15 * maxNote && e >= prev && e >= next)
737 {
738 bassPc = (kFirstMidi + n12) % 12;
739 break;
740 }
741 }
742 // A NaN sample travels through the decimators and the dot products
743 // into the energies; the comparisons below are false for NaN, so it
744 // is caught here like silence rather than published.
745 if (!(total >= 1e-12) || !std::isfinite(total))
746 {
747 // Silence: drop confidence but keep the last chord displayed.
748 Result held = unpack(packed_.load(std::memory_order_relaxed));
749 held.confidence = 0.0f;
750 packed_.store(pack(held), std::memory_order_relaxed);
751 return;
752 }
753
754 double norm = 0.0;
755 for (const double c : chroma) norm += c * c;
756 norm = std::sqrt(norm);
757
758 // Cosine match against every template at every root.
759 double best = 0.0, second = 0.0;
760 int bestRoot = -1;
761 ChordType bestType = ChordType::None;
762 for (int root = 0; root < 12; ++root)
763 {
764 for (const auto& tpl : kTemplates)
765 {
766 double inSum = 0.0;
767 for (int iv = 0; iv < 12; ++iv)
768 if (tpl.mask & (1u << iv))
769 inSum += chroma[static_cast<std::size_t>((root + iv) % 12)];
770 double score = inSum / (norm * std::sqrt(static_cast<double>(tpl.count)));
771 if (root == bassPc)
772 score *= 1.25; // the bass note names the chord
773 if (score > best)
774 {
775 second = best;
776 best = score;
777 bestRoot = root;
778 bestType = tpl.type;
779 }
780 else if (score > second)
781 {
782 second = score;
783 }
784 }
785 }
786
787 // Confidence: absolute quality times the margin over the runner-up.
788 const double margin = (best > 1e-9) ? std::clamp((best - second) / best * 4.0, 0.0, 1.0)
789 : 0.0;
790 const auto confidence = static_cast<float>(std::clamp(best, 0.0, 1.0) * (0.5 + 0.5 * margin));
791
792 Result out;
793 if (confidence >= threshold_.load(std::memory_order_relaxed))
794 {
795 out.rootPitchClass = bestRoot;
796 out.type = bestType;
797 out.confidence = confidence;
798 }
799 else
800 {
801 out = unpack(packed_.load(std::memory_order_relaxed)); // hold
802 out.confidence = confidence;
803 }
804 packed_.store(pack(out), std::memory_order_relaxed);
805 }
806
807 // Pack the result into one atomic word (no torn reads cross-thread).
808 [[nodiscard]] static std::uint64_t pack(const Result& r) noexcept
809 {
810 const auto conf = static_cast<std::uint32_t>(std::clamp(r.confidence, 0.0f, 1.0f) * 65535.0f);
811 return (static_cast<std::uint64_t>(static_cast<std::uint8_t>(r.rootPitchClass + 1)) << 24)
812 | (static_cast<std::uint64_t>(static_cast<std::uint8_t>(r.type)) << 16)
813 | conf;
814 }
815
816 [[nodiscard]] static Result unpack(std::uint64_t v) noexcept
817 {
818 Result r;
819 r.rootPitchClass = static_cast<int>((v >> 24) & 0xFF) - 1;
820 r.type = static_cast<ChordType>((v >> 16) & 0xFF);
821 r.confidence = static_cast<float>(v & 0xFFFF) / 65535.0f;
822 return r;
823 }
824
825 // -- Members --------------------------------------------------------------------
826 double sampleRate_ = 48000.0;
827 int windowSize_ = 4096;
828 int hopSize_ = 2048;
829 int levels_ = 0;
830 int analysisDelay_ = 0;
831 std::atomic<bool> prepared_ { false };
832
833 std::vector<T> input_;
834 int inputFill_ = 0;
835 std::vector<Stage> stages_;
836 std::array<std::vector<T>, static_cast<std::size_t>(kMaxLevels + 1)> levelBlock_;
837 std::array<Octave, static_cast<std::size_t>(kNumOctaves)> octaves_;
838
839 // Latest analysis frame, owned by the thread that pushes samples.
840 std::array<T, 12> chromaFrame_ {};
841 std::uint64_t frameCount_ = 0;
842
843 std::atomic<std::uint64_t> packed_ { 0 };
844 std::atomic<float> threshold_ { 0.55f };
845};
846
847} // namespace dspark
Non-owning view over audio channel data.
Definition AudioBuffer.h:53
Monophonic-buffer chord recognition with confidence gating.
int getWindowSize() const noexcept
void pushSamples(std::span< const T > samples) noexcept
Feeds mono samples directly.
bool prepare(const AudioSpec &spec, int windowSize=0)
Prepares the analysis pipeline.
void processBlock(AudioBufferView< const T > buffer) noexcept
Feeds a block (channels averaged to mono).
std::uint64_t getFrameCount() const noexcept
Number of analysis frames produced since prepare()/reset().
int getHopSize() const noexcept
const std::array< T, 12 > & getChroma() const noexcept
The chroma vector of the most recent analysis frame.
void setConfidenceThreshold(float threshold) noexcept
Confidence below which the previous chord is held (default 0.55).
Result getChord() const noexcept
float getConfidenceThreshold() const noexcept
int getAnalysisDelaySamples() const noexcept
static int getChordName(const Result &result, char *dest, int size) noexcept
Writes a human-readable chord name ("C", "F#m7", "Bbsus4"...).
ChordType
Recognized chord families.
void reset() noexcept
Clears the analysis state and forgets the held chord.
constexpr int delay
Delay of the projected output behind the newest input, in samples.
std::uint16_t NoteSet
A 12-bit bitmask representing the 12 pitch-classes of the chromatic scale.
constexpr std::string_view noteName(int midi, int root=0) noexcept
Returns a human-readable note name for the given MIDI note (0..127).
float dotProduct(const float *DSPARK_RESTRICT a, const float *DSPARK_RESTRICT b, int count) noexcept
Computes the dot product of two arrays.
Definition SimdOps.h:644
void firCorrelate(const T *x, const T *h, int taps, T *DSPARK_RESTRICT y, int n, T gain) noexcept
Block FIR as a valid correlation: y[i] = gain * sum_j h[j] * x[i + j] for i in [0,...
Definition SimdOps.h:1496
Main namespace for the DSPark framework.
constexpr T pi
Pi (3.14159...) for the given floating-point type.
Definition DspMath.h:48
Describes the audio environment for a DSP processor.
Definition AudioSpec.h:37
constexpr bool isValid() const noexcept
Checks if the specification contains valid, processable parameters.
Definition AudioSpec.h:71
double sampleRate
Sample rate in Hz.
Definition AudioSpec.h:45
One detection result.
int rootPitchClass
0 = C ... 11 = B; -1 = none.