@@ -10,7 +10,9 @@ import Darwin
// /
// / A c c u m u l a t e s s a m p l e s i n t o a r i n g b u f f e r a n d r e - e v a l u a t e s e v e r y ` h o p S i z e ` s a m p l e s .
// / B e t w e e n h o p s t h e o u t p u t h o l d s t h e p r e v i o u s r e s u l t — f r e q u e n c y s t a b i l i s e s a f t e r
// / o n e o r t w o h o p s ( ~ 4 6 m s a t t h e d e f a u l t s e t t i n g s a n d 4 4 1 0 0 H z s a m p l e r a t e ) .
// / o n e o r t w o h o p s ( ~ 4 6 m s a t t h e d e f a u l t s e t t i n g s , ` d e c i m a t i o n : 1 ` , a n d 4 4 1 0 0 H z s a m p l e
// / r a t e ) . S e t t l e t i m e s c a l e s w i t h ` d e c i m a t i o n ` ( e a c h h o p s p a n s ` d e c i m a t i o n × ` m o r e
// / w a l l - c l o c k t i m e ) , s o ` d e c i m a t i o n : 4 ` s t a b i l i s e s i n ~ 1 8 4 m s .
// /
// / # # O u t p u t
// / R e t u r n s a t w o - c h a n n e l b u f f e r :
@@ -19,10 +21,13 @@ import Darwin
// /
// / # # P a r a m e t e r s
// / - ` i n ` : s i g n a l t o a n a l y s e ( A u d i o - r a t e i n p u t e v a l u a t e d p e r s a m p l e )
// / - ` f f t S i z e ` : a n a l y s i s w i n d o w i n s a m p l e s ( m u s t b e p o w e r o f 2 , s e t a t i n i t )
// / - ` h o p S i z e ` : s a m p l e s b e t w e e n r e - a n a l y s e s ( s e t a t i n i t ; d e f a u l t ` f f t S i z e / 2 ` )
// / - ` f f t S i z e ` : a n a l y s i s w i n d o w i n * * d e c i m a t e d * * s a m p l e s ( m u s t b e p o w e r o f 2 , s e t a t i n i t )
// / - ` h o p S i z e ` : d e c i m a t e d s a m p l e s b e t w e e n r e - a n a l y s e s ( s e t a t i n i t ; d e f a u l t ` f f t S i z e / 2 ` )
// / - ` m i n F r e q ` : l o w e r s e a r c h b o u n d i n H z ( d e f a u l t 6 0 . 0 )
// / - ` m a x F r e q ` : u p p e r s e a r c h b o u n d i n H z ( d e f a u l t 3 0 0 0 . 0 )
// / - ` d e c i m a t i o n ` : k e e p o n l y e v e r y N t h i n p u t s a m p l e b e f o r e a n a l y s i s ( d e f a u l t ` 1 ` ) , l o w e r i n g
// / t h e e f f e c t i v e r a t e t o ` s a m p l e R a t e / d e c i m a t i o n ` t o r e a c h l o w e r f u n d a m e n t a l s . C a l l e r m u s t
// / b a n d - l i m i t b e l o w t h e d e c i m a t e d N y q u i s t f i r s t . S e e t h e ` i n i t ` d o c f o r f u l l d e t a i l .
// /
// / # # U s a g e
// / ` ` ` s w i f t
@@ -62,6 +67,7 @@ public final class FreqTracker: GeneratorBase, AudioGenerator {
private let _hopSize : Int
private let _minFreq : Double
private let _maxFreq : Double
private let _decimation : Int
// MARK: - I n p u t
@@ -82,6 +88,11 @@ public final class FreqTracker: GeneratorBase, AudioGenerator {
private var _lastFreq : Double = 0.0
private var _lastConfidence : Double = 0.0
// C o u n t s r a w i n p u t s a m p l e s t o w a r d t h e n e x t d e c i m a t e d ( s t o r e d ) s a m p l e . O n l y e v e r y
// ` _ d e c i m a t i o n ` - t h i n p u t s a m p l e i s w r i t t e n i n t o t h e r i n g ; t h e r i n g / h o p m a c h i n e r y t h e n
// o p e r a t e s e n t i r e l y i n t h e d e c i m a t e d t i m e b a s e .
private var _decimationCounter : Int = 0
// MARK: - O u t p u t
// MARK: - F a n - o u t c a c h e
@@ -89,18 +100,32 @@ public final class FreqTracker: GeneratorBase, AudioGenerator {
// / - P a r a m e t e r s :
// / - i n p u t : S i g n a l t o t r a c k .
// / - f f t S i z e : A n a l y s i s w i n d o w i n s a m p l e s . P o w e r o f 2 i n [ 6 4 , 5 1 2 ] . D e f a u l t 5 1 2 .
// / L a r g e r v a l u e s i n c r e a s e l a t e n c y a n d Y I N c o s t ( O ( h a l f S i z e ² ) p e r h o p ) ;
// / 5 1 2 g i v e s ~ 5 . 8 m s a t 4 4 1 0 0 H z a n d i s t h e m a x i m u m R T - s a f e v a l u e .
// / - h o p S i z e : S a m p l e s b e t w e e n a n a l y s i s u p d a t e s . D e f a u l t ` f f t S i z e / 2 ` .
// / - f f t S i z e : A n a l y s i s w i n d o w i n * * d e c i m a t e d * * s a m p l e s . P o w e r o f 2 i n [ 6 4 , 5 1 2 ] .
// / D e f a u l t 5 1 2 . L a r g e r v a l u e s i n c r e a s e l a t e n c y a n d Y I N c o s t ( O ( h a l f S i z e ² )
// / p e r h o p ) ; 5 1 2 g i v e s ~ 5 . 8 m s a t 4 4 1 0 0 H z a n d i s t h e m a x i m u m R T - s a f e v a l u e .
// / - h o p S i z e : D e c i m a t e d s a m p l e s b e t w e e n a n a l y s i s u p d a t e s . D e f a u l t ` f f t S i z e / 2 ` .
// / - m i n F r e q : M i n i m u m t r a c k e d f r e q u e n c y i n H z . D e f a u l t 6 0 . 0 .
// / - m a x F r e q : M a x i m u m t r a c k e d f r e q u e n c y i n H z . D e f a u l t 3 0 0 0 . 0 .
// / - d e c i m a t i o n : K e e p o n l y e v e r y ` d e c i m a t i o n ` - t h i n p u t s a m p l e b e f o r e a n a l y s i s , l o w e r i n g
// / t h e e f f e c t i v e s a m p l e r a t e t o ` s a m p l e R a t e / d e c i m a t i o n ` . D e f a u l t ` 1 ` ( n o
// / d e c i m a t i o n — o r i g i n a l b e h a v i o u r ) . A 5 1 2 - s a m p l e w i n d o w c a n t h e n s p a n
// / ` d e c i m a t i o n × ` m o r e t i m e , e x t e n d i n g t h e * * m i n i m u m * * d e t e c t a b l e f r e q u e n c y
// / d o w n t o r o u g h l y ` s a m p l e R a t e / ( d e c i m a t i o n × ( f f t S i z e / 2 − 1 ) ) ` w h i l e k e e p i n g
// / Y I N ' s O ( h a l f S i z e ² ) c o s t f i x e d — i d e a l f o r l o w - p i t c h e d m o n o p h o n i c s o u r c e s
// / ( b a s s , l o w g u i t a r s t r i n g s ) w h e r e h i g h - f r e q u e n c y r e s o l u t i o n i s n ' t n e e d e d .
// / * * T h e c a l l e r m u s t b a n d - l i m i t t h e i n p u t b e l o w t h e d e c i m a t e d N y q u i s t
// / ( ` s a m p l e R a t e / ( 2 × d e c i m a t i o n ) ` ) f i r s t * * — d e c i m a t i o n d o e s n o a n t i - a l i a s
// / f i l t e r i n g o f i t s o w n . T h e ` m a x F r e q ` s e a r c h b o u n d m u s t a l s o s t a y b e l o w t h a t
// / d e c i m a t e d N y q u i s t . L a t e n c y t o a s t a b l e r e a d i n g s c a l e s w i t h ` d e c i m a t i o n `
// / ( e a c h h o p s p a n s ` d e c i m a t i o n × ` m o r e w a l l - c l o c k t i m e ) , s o ` d e c i m a t i o n : 4 `
// / s e t t l e s i n ~ 4 × t h e u n d e c i m a t e d h o p - b a s e d t i m e .
public init (
in input : any ValidGeneratorInput ,
fftSize : Int = 512 ,
hopSize : Int ? = nil ,
minFreq : Double = 60.0 ,
maxFreq : Double = 3000.0
maxFreq : Double = 3000.0 ,
decimation : Int = 1
) {
precondition ( fftSize >= 64 && fftSize <= 512 && ( fftSize & ( fftSize - 1 ) ) = = 0 ,
" FreqTracker: fftSize must be a power of 2 in [64, 512]; got \( fftSize ) . "
@@ -108,6 +133,8 @@ public final class FreqTracker: GeneratorBase, AudioGenerator {
+ " Reduce fftSize or implement background-thread YIN in a future phase. " )
precondition ( minFreq > 0 && maxFreq > minFreq ,
" FreqTracker: minFreq must be > 0 and < maxFreq " )
precondition ( decimation >= 1 ,
" FreqTracker: decimation must be >= 1; got \( decimation ) . " )
_fftSize = fftSize
_hopSize = hopSize ? ? fftSize / 2
@@ -119,6 +146,7 @@ public final class FreqTracker: GeneratorBase, AudioGenerator {
+ " Larger values cause the YIN window to read stale data. " )
_minFreq = minFreq
_maxFreq = maxFreq
_decimation = decimation
let resolvedInput : any ValidGeneratorInput = input . _resolvedInput ( )
_input = resolvedInput
_inputRing = [ Double ] ( repeating : 0.0 , count : fftSize )
@@ -135,16 +163,23 @@ public final class FreqTracker: GeneratorBase, AudioGenerator {
_checkCount ( count )
let sr : Double = graph . engine . sampleRate
// A n a l y s i s r u n s i n t h e d e c i m a t e d t i m e b a s e : t h e r i n g h o l d s e v e r y ` _ d e c i m a t i o n ` - t h
// i n p u t s a m p l e , s o l a g → f r e q u e n c y c o n v e r s i o n u s e s t h e d e c i m a t e d e f f e c t i v e r a t e .
let effectiveRate : Double = sr / Double ( _decimation )
let inputSamples : [ [ Double ] ] = _input . calculatedSamples ( graph : graph , count : count )
guard let ins : [ Double ] = inputSamples . first else {
// N o i n p u t — a d v a n c e r i n g w i t h z e r o s , s t i l l f i r e h o p s s o s t a t e a d v a n c e s c o r r e c t l y .
for _ in 0. . < count {
_inputRing [ _ringPos % _fftSize ] = 0.0
_ringPos &+= 1
_samplesUntilHop -= 1
if _samplesUntilHop <= 0 {
_samplesUntilHop = _hopSize
_runYIN ( sampleRate : sr )
_decimationCounter &+= 1
if _decimationCounter >= _decimation {
_decimationCounter = 0
_inputRing [ _ringPos % _fftSize ] = 0.0
_ringPos &+= 1
_samplesUntilHop -= 1
if _samplesUntilHop <= 0 {
_samplesUntilHop = _hopSize
_runYIN ( sampleRate : effectiveRate )
}
}
}
for i : Int in 0. . < count {
@@ -155,15 +190,21 @@ public final class FreqTracker: GeneratorBase, AudioGenerator {
}
for i : Int in 0. . < count {
// W r i t e i n c o m i n g s a m p l e i n t o r i n g b u f f e r .
let sample : Double = Double ( ins [ i ] )
_inputRing [ _ringPos % _fftSize ] = sample
_ringPos &+= 1
// W r i t e e v e r y ` _ d e c i m a t i o n ` - t h i n c o m i n g s a m p l e i n t o t h e r i n g b u f f e r . N a i v e
// d e c i m a t i o n ( t a k e - e v e r y - N t h ) i s c o r r e c t b e c a u s e t h e c a l l e r b a n d - l i m i t s b e l o w
// t h e d e c i m a t e d N y q u i s t — s e e t h e ` d e c i m a t i o n ` p a r a m e t e r d o c s .
_decimationCounter &+= 1
if _decimationCounter >= _decimation {
_decimationCounter = 0
let sample : Double = Double ( ins [ i ] )
_inputRing [ _ringPos % _fftSize ] = sample
_ringPos &+= 1
_samplesUntilHop -= 1
if _samplesUntilHop <= 0 {
_samplesUntilHop = _hopSize
_runYIN ( sampleRate : sr )
_samplesUntilHop -= 1
if _samplesUntilHop <= 0 {
_samplesUntilHop = _hopSize
_runYIN ( sampleRate : effectiveRate )
}
}
_result [ 0 ] [ i ] = _lastFreq
_result [ 1 ] [ i ] = _lastConfidence