Backbone - Singer Tone
 
Loading...
Searching...
No Matches
Backbone::SingerToneDSP Class Reference

Pitch-related DSP utilities and coarse-to-fine pitch detection. More...

Public Member Functions

 SingerToneDSP (double sampleRate, float minPitch, float maxPitch, float detectLevelThreshold)
 Initializes the DSP with sampling parameters and detection limits.
 
float DetectPitch (float[] samplesLo, float[] samplesHi, int numSamples)
 Detects pitch on a frame using low/high prefiltered signals and a level gate.
 
bool LevelIsAbove (float[] buffer, int len, float level)
 Returns true if any sample within the first len entries has absolute value at least level .
 
float MidiNoteToPitch (float note)
 Converts a floating MIDI note to pitch in Hz (limited by m_maxPitch).
 

Static Public Member Functions

static double CreateSineWave (float[] buffer, int numSamples, float sampleRate, float freq, float amplitude, double startAngle)
 Fills buffer with a sine wave. Returns the updated phase.
 
static void CopyBuffer (T[] source, int srcStart, T[] destination, int dstStart, int length)
 Copies a block of elements from source to destination .
 
static bool PitchToMidiNote (float pitch, out int note, out int cents)
 Converts a pitch in Hz to integer MIDI note and cents.
 
static float PitchToMidiNote (float pitch)
 Converts a pitch in Hz to floating MIDI note (note + cents/100).
 
static string GetNoteName (int note, bool sharps, bool showOctave)
 Formats a MIDI note to its musical note name.
 

Public Attributes

float MaxPitch => m_maxPitch
 Maximum detectable pitch (Hz).
 
float MinPitch => m_minPitch
 Minimum detectable pitch (Hz).
 
int MaxNote => m_maxNote
 Maximum MIDI note supported.
 
int MinNote => m_minNote
 Minimum MIDI note supported.
 

Static Public Attributes

static readonly double InvLn2 = 1.0 / Math.Log(2.0)
 1 / ln(2). Used to convert natural logarithms to log2.
 

Private Member Functions

int CourseIndexForFreq (float hz)
 Returns coarse index for a given frequency in Hz.
 
float DetectPitchLo (float[] samplesLo, float[] samplesHi)
 Coarse pass: scans candidate periods and refines around peaks.
 
float DetectPitchHi (float[] samples, int lowFreqIdx)
 High-resolution refinement around the best coarse candidate.
 
float InterpolateHermite (float fY0, float fY1, float fY2, float fY3, float frac)
 4-point, 3rd-order Hermite interpolation.
 
float InterpolateLinear (float y0, float y1, float frac)
 Linear interpolation.
 
float RatioAbsDiffLinear (float[] samples, int freqIdx, int blockLen, int stepSize, bool hiRes)
 Medium/low resolution ratio of absolute differences using linear interpolation.
 
float SumAbsDiffHermite (float[] samples, float fOffset, int blockLen, int stepSize)
 Medium/high resolution sum of absolute differences using Hermite interpolation.
 

Private Attributes

const int kCourseOctaveSteps = 96
 
const int kScanHiSize = 31
 
const float kScanHiFreqStep = 1.005f
 
const int kMinMidiNote = 21
 
const int kMaxMidiNote = 108
 
const int kSkipSize = 8
 
const int kPeakScanSize = 23
 
const float kPeakThresh1 = 200f
 
const float kPeakThresh2 = 600f
 
const int kHiResDeltaIdx = 5
 
const int kLoResDivisor = 10
 
const int kHiResStepMin = 1
 
const int kHiResStepMax = 5
 
const float kPointyNear = 1.2f
 
const float kPointyFar = 1.5f
 
const float kLoHiSwitchFreqHz = 250f
 
float m_minPitch
 
float m_maxPitch
 
int m_minNote
 
int m_maxNote
 
int m_blockLen14
 
int m_blockLen24
 
int m_blockLen34
 
int m_blockLen44
 
double m_sampleRate
 
float m_detectLevelThreshold
 
int m_numCourseSteps
 
float[] m_pCourseFreqOffset
 
float[] m_pCourseFreq
 
readonly float[] m_scanHiOffset = new float[kScanHiSize]
 
readonly float[] m_peakBuf = new float[kScanHiSize]
 
int m_prevPitchIdx
 
float[] m_detectCurve
 

Detailed Description

Pitch-related DSP utilities and coarse-to-fine pitch detection.

Constructor & Destructor Documentation

◆ SingerToneDSP()

Backbone::SingerToneDSP::SingerToneDSP ( double sampleRate,
float minPitch,
float maxPitch,
float detectLevelThreshold )

Initializes the DSP with sampling parameters and detection limits.

Parameters
sampleRateSampling rate in Hz.
minPitchMinimum detectable pitch in Hz.
maxPitchMaximum detectable pitch in Hz.
detectLevelThresholdAbsolute signal threshold to attempt detection.

Member Function Documentation

◆ CopyBuffer()

static void Backbone::SingerToneDSP::CopyBuffer ( T[] source,
int srcStart,
T[] destination,
int dstStart,
int length )
static

Copies a block of elements from source to destination .

Exceptions
ArgumentOutOfRangeExceptionIf length is negative.
ExceptionIf source or destination buffers are too small.

◆ CourseIndexForFreq()

int Backbone::SingerToneDSP::CourseIndexForFreq ( float hz)
private

Returns coarse index for a given frequency in Hz.

◆ CreateSineWave()

static double Backbone::SingerToneDSP::CreateSineWave ( float[] buffer,
int numSamples,
float sampleRate,
float freq,
float amplitude,
double startAngle )
static

Fills buffer with a sine wave. Returns the updated phase.

◆ DetectPitch()

float Backbone::SingerToneDSP::DetectPitch ( float[] samplesLo,
float[] samplesHi,
int numSamples )

Detects pitch on a frame using low/high prefiltered signals and a level gate.

Parameters
samplesLoLow-band samples.
samplesHiHigh-band samples.
numSamplesNumber of valid samples in the arrays.
Returns
Detected pitch in Hz, or 0 if not found.

◆ DetectPitchHi()

float Backbone::SingerToneDSP::DetectPitchHi ( float[] samples,
int lowFreqIdx )
private

High-resolution refinement around the best coarse candidate.

◆ DetectPitchLo()

float Backbone::SingerToneDSP::DetectPitchLo ( float[] samplesLo,
float[] samplesHi )
private

Coarse pass: scans candidate periods and refines around peaks.

◆ GetNoteName()

static string Backbone::SingerToneDSP::GetNoteName ( int note,
bool sharps,
bool showOctave )
static

Formats a MIDI note to its musical note name.

Parameters
noteMIDI note number.
sharpsIf true uses sharps, otherwise flats.
showOctaveIf true appends the octave number.
Returns
Formatted name or null if out of supported range.

◆ InterpolateHermite()

float Backbone::SingerToneDSP::InterpolateHermite ( float fY0,
float fY1,
float fY2,
float fY3,
float frac )
private

4-point, 3rd-order Hermite interpolation.

◆ InterpolateLinear()

float Backbone::SingerToneDSP::InterpolateLinear ( float y0,
float y1,
float frac )
private

Linear interpolation.

◆ LevelIsAbove()

bool Backbone::SingerToneDSP::LevelIsAbove ( float[] buffer,
int len,
float level )

Returns true if any sample within the first len entries has absolute value at least level .

◆ MidiNoteToPitch()

float Backbone::SingerToneDSP::MidiNoteToPitch ( float note)

Converts a floating MIDI note to pitch in Hz (limited by m_maxPitch).

◆ PitchToMidiNote() [1/2]

static float Backbone::SingerToneDSP::PitchToMidiNote ( float pitch)
static

Converts a pitch in Hz to floating MIDI note (note + cents/100).

◆ PitchToMidiNote() [2/2]

static bool Backbone::SingerToneDSP::PitchToMidiNote ( float pitch,
out int note,
out int cents )
static

Converts a pitch in Hz to integer MIDI note and cents.

◆ RatioAbsDiffLinear()

float Backbone::SingerToneDSP::RatioAbsDiffLinear ( float[] samples,
int freqIdx,
int blockLen,
int stepSize,
bool hiRes )
private

Medium/low resolution ratio of absolute differences using linear interpolation.

◆ SumAbsDiffHermite()

float Backbone::SingerToneDSP::SumAbsDiffHermite ( float[] samples,
float fOffset,
int blockLen,
int stepSize )
private

Medium/high resolution sum of absolute differences using Hermite interpolation.

Member Data Documentation

◆ InvLn2

readonly double Backbone::SingerToneDSP::InvLn2 = 1.0 / Math.Log(2.0)
static

1 / ln(2). Used to convert natural logarithms to log2.

◆ kCourseOctaveSteps

const int Backbone::SingerToneDSP::kCourseOctaveSteps = 96
private

◆ kHiResDeltaIdx

const int Backbone::SingerToneDSP::kHiResDeltaIdx = 5
private

◆ kHiResStepMax

const int Backbone::SingerToneDSP::kHiResStepMax = 5
private

◆ kHiResStepMin

const int Backbone::SingerToneDSP::kHiResStepMin = 1
private

◆ kLoHiSwitchFreqHz

const float Backbone::SingerToneDSP::kLoHiSwitchFreqHz = 250f
private

◆ kLoResDivisor

const int Backbone::SingerToneDSP::kLoResDivisor = 10
private

◆ kMaxMidiNote

const int Backbone::SingerToneDSP::kMaxMidiNote = 108
private

◆ kMinMidiNote

const int Backbone::SingerToneDSP::kMinMidiNote = 21
private

◆ kPeakScanSize

const int Backbone::SingerToneDSP::kPeakScanSize = 23
private

◆ kPeakThresh1

const float Backbone::SingerToneDSP::kPeakThresh1 = 200f
private

◆ kPeakThresh2

const float Backbone::SingerToneDSP::kPeakThresh2 = 600f
private

◆ kPointyFar

const float Backbone::SingerToneDSP::kPointyFar = 1.5f
private

◆ kPointyNear

const float Backbone::SingerToneDSP::kPointyNear = 1.2f
private

◆ kScanHiFreqStep

const float Backbone::SingerToneDSP::kScanHiFreqStep = 1.005f
private

◆ kScanHiSize

const int Backbone::SingerToneDSP::kScanHiSize = 31
private

◆ kSkipSize

const int Backbone::SingerToneDSP::kSkipSize = 8
private

◆ m_blockLen14

int Backbone::SingerToneDSP::m_blockLen14
private

◆ m_blockLen24

int Backbone::SingerToneDSP::m_blockLen24
private

◆ m_blockLen34

int Backbone::SingerToneDSP::m_blockLen34
private

◆ m_blockLen44

int Backbone::SingerToneDSP::m_blockLen44
private

◆ m_detectCurve

float [] Backbone::SingerToneDSP::m_detectCurve
private

◆ m_detectLevelThreshold

float Backbone::SingerToneDSP::m_detectLevelThreshold
private

◆ m_maxNote

int Backbone::SingerToneDSP::m_maxNote
private

◆ m_maxPitch

float Backbone::SingerToneDSP::m_maxPitch
private

◆ m_minNote

int Backbone::SingerToneDSP::m_minNote
private

◆ m_minPitch

float Backbone::SingerToneDSP::m_minPitch
private

◆ m_numCourseSteps

int Backbone::SingerToneDSP::m_numCourseSteps
private

◆ m_pCourseFreq

float [] Backbone::SingerToneDSP::m_pCourseFreq
private

◆ m_pCourseFreqOffset

float [] Backbone::SingerToneDSP::m_pCourseFreqOffset
private

◆ m_peakBuf

readonly float [] Backbone::SingerToneDSP::m_peakBuf = new float[kScanHiSize]
private

◆ m_prevPitchIdx

int Backbone::SingerToneDSP::m_prevPitchIdx
private

◆ m_sampleRate

double Backbone::SingerToneDSP::m_sampleRate
private

◆ m_scanHiOffset

readonly float [] Backbone::SingerToneDSP::m_scanHiOffset = new float[kScanHiSize]
private

◆ MaxNote

int Backbone::SingerToneDSP::MaxNote => m_maxNote

Maximum MIDI note supported.

◆ MaxPitch

float Backbone::SingerToneDSP::MaxPitch => m_maxPitch

Maximum detectable pitch (Hz).

◆ MinNote

int Backbone::SingerToneDSP::MinNote => m_minNote

Minimum MIDI note supported.

◆ MinPitch

float Backbone::SingerToneDSP::MinPitch => m_minPitch

Minimum detectable pitch (Hz).


The documentation for this class was generated from the following file: