@@ -8,53 +8,48 @@ import Foundation
import LyrebirdSynth
import LyrebirdUI
// / A l o o p - r e c o r d e r p e d a l : r e c o r d a t a k e i n t o a b u f f e r , t h e n p l a y i t b a c k l o o p i n g , w i t h
// / o v e r d u b , o v e r d u b - u n d o , r e v e r s e p l a y b a c k , f e e d b a c k - d e c a y o v e r d u b , a n d a c r o s s f a d e d
// / t o t a l - l o o p m u t e . W h e n t h e b o a r d s u p p l i e s a t e m p o ` ` L y r e b i r d C l o c k ` ` t h e t r a n s p o r t i s
// / b e a t - q u a n t i z e d — r e c o r d s t a r t a n d l o o p c l o s e b o t h s n a p t o t h e n e x t b e a t , s o e v e r y l o o p
// / i s a w h o l e n u m b e r o f b e a t s l o n g .
// / A m u l t i - s l o t l o o p - r e c o r d e r p e d a l : r e c o r d i n d e p e n d e n t l o o p s i n t o a n y o f s e v e r a l s l o t s , t h e n
// / s w i t c h b e t w e e n t h e m , w i t h o v e r d u b , o v e r d u b - u n d o , r e v e r s e p l a y b a c k , f e e d b a c k - d e c a y o v e r d u b ,
// / a n d a c r o s s f a d e d t o t a l - l o o p m u t e . W h e n t h e b o a r d s u p p l i e s a t e m p o ` ` L y r e b i r d C l o c k ` ` t h e
// / t r a n s p o r t i s b e a t - q u a n t i z e d — r e c o r d s t a r t a n d l o o p c l o s e b o t h s n a p t o t h e n e x t b e a t , s o
// / e v e r y l o o p i s a w h o l e n u m b e r o f b e a t s l o n g .
// /
// / S i g n a l c h a i n ( i n s i d e ` e f f e c t ( _ : ) ` ) :
// / S i g n a l c h a i n ( i n s i d e ` e f f e c t ( _ : ) ` ) , p e r s l o t ` i ` p l u s o n e s h a r e d p l a y e r :
// / ` ` `
// / d r y → R e c o r d B u f f e r . A u d i o ( p r i m a r y t a k e , p a s s t h r o u g h w h i l e r e c o r d i n g ) ─ ┐
// / d r y → R e c o r d B u f f e r . A u d i o ( o v e r d u b , g a t e d b y o v e r d u b O n × i s P l a y i n g , │ ( w r i t e o n l y )
// / o l d c o n t e n t s c a l e d b y f e e d b a c k ) │
// / l o o p B u f f e r → P l a y B u f f e r . A u d i o ( l o o p , c u b i c , r a t e ± 1 ) │
// / p r i m a r y p a s s t h r o u g h ↔ ( l a g g e d l o o p O n × i s P l a y i n g ) ↔ P l a y B u f f e r . A u d i o ─ ┴ → w e t
// / d r y → R e c o r d B u f f e r . A u d i o p r i m a r y [ i ] ( t a k e c a p t u r e , r u n - g a t e d b y t r a n s p o r t ) ─ ┐ ( w r i t e o n l y ,
// / d r y → R e c o r d B u f f e r . A u d i o o v e r d u b [ i ] ( g a t e d b y o v e r d u b O n × i s P l a y i n g × s l o t i ) │ R o o t R e g i s t e r i n g )
// / b u f f e r s [ a c t i v e S l o t ] → P l a y B u f f e r . A u d i o ( l o o p , c u b i c , r a t e ± 1 , s w a p p e d ─ ─ ─ ┤
// / o n s l o t c h a n g e v i a s e t B u f f e r ) │
// / d r y ↔ ( l a g g e d l o o p O n × i s P l a y i n g ) ↔ P l a y B u f f e r . A u d i o ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ → w e t
// / ` ` `
// /
// / A s i n g l e f i x e d g e n e r a t o r g r a p h i s b u i l t o n c e ( L y r e b i r d ' s n o d e t r e e d o e s n o t s u p p o r t
// / r e b u i l d i n g a s y n t h ' s i n t e r n a l g e n e r a t o r s ) . E v e r y t r a n s p o r t a c t i o n i s e x p r e s s e d a s a
// / l i v e p a r a m e t e r c h a n g e t h e g r a p h a l r e a d y r e a d s e v e r y b l o c k ( ` $ o v e r d u b O n ` , ` $ l o o p O n ` ,
// / ` $ i s P l a y i n g ` , ` $ r e v e r s e ` , ` $ f e e d b a c k ` , ` $ p l a y T r i g g e r ` ) , p l u s , f o r a c t i o n s w i t h a g e n u i n e
// / n o n - R T s i d e e f f e c t ( c l e a r i n g t h e b u f f e r , n a r r o w i n g / r e s t o r i n g ` l o o p L e n g t h ` , s n a p s h o t t i n g
// / f o r u n d o ) , a n e x p l i c i t m e t h o d .
// / A s i n g l e f i x e d g e n e r a t o r g r a p h i s b u i l t o n c e . O n l y o n e s l o t r e c o r d s o r o v e r d u b s a t a t i m e
// / ( g a t e d s o i n a c t i v e s l o t s k e e p t h e i r c o n t e n t ) ; t h e s h a r e d p l a y e r ' s s o u r c e b u f f e r i s s w a p p e d
// / t o t h e a c t i v e s l o t w i t h ` ` P l a y B u f f e r / A u d i o / s e t B u f f e r ( _ : e n g i n e : ) ` ` . E v e r y t r a n s p o r t a c t i o n i s
// / a l i v e p a r a m e t e r c h a n g e t h e g r a p h a l r e a d y r e a d s e a c h b l o c k , p l u s e x p l i c i t m e t h o d s f o r t h e
// / g e n u i n e n o n - R T s i d e e f f e c t s ( c l e a r i n g a b u f f e r , n a r r o w i n g ` l o o p L e n g t h ` , s n a p s h o t t i n g f o r u n d o ,
// / s w a p p i n g t h e p l a y e r ' s b u f f e r ) .
// /
// / # # T r a n s p o r t ( b e a t - q u a n t i z e d w h e n a c l o c k i s p r e s e n t )
// / - ` ` t o g g l e R e c o r d S t o p ( ) ` ` : ` . e m p t y ` → a r m / b e g i n r e c o r d i n g ; ` . r e c o r d i n g ` → c l o s e t h e l o o p
// / a n d s t a r t l o o p i n g . N o - o p w h i l e ` . p l a y i n g ` — u s e ` ` r e s e t L o o p ( ) ` ` f o r a n e w t a k e .
// / - ` o v e r d u b O n ` ( v i a ` ` t o g g l e O v e r d u b ( ) ` ` ) : l a y e r s n e w i n p u t o n t o t h e l o o p ; e n g a g i n g i t f i r s t
// / s n a p s h o t s t h e l o o p s o ` ` u n d o O v e r d u b ( ) ` ` c a n r e v e r t t h e p a s s .
// / - ` l o o p O n ` ( s w i t c h ) : c r o s s f a d e d t o t a l m u t e w i t h o u t s t o p p i n g t h e p l a y e r .
// / - ` r e v e r s e ` ( s w i t c h ) : f l i p s p l a y b a c k d i r e c t i o n ( ` r a t e ` 1 → − 1 ) w i t h o u t a r e b u i l d .
// / - ` ` u n d o O v e r d u b ( ) ` ` : r e s t o r e s t h e l o o p t o t h e s n a p s h o t t a k e n w h e n o v e r d u b w a s l a s t e n g a g e d .
// / - ` ` r e s e t L o o p ( ) ` ` : c l e a r s t h e b u f f e r a n d r e t u r n s t o ` . e m p t y ` .
// / - ` ` t o g g l e R e c o r d S t o p ( ) ` ` : ` . e m p t y ` → a r m / b e g i n r e c o r d i n g t h e a c t i v e s l o t ; ` . r e c o r d i n g ` →
// / c l o s e t h e l o o p a n d s t a r t l o o p i n g . N o - o p w h i l e ` . p l a y i n g ` — u s e ` ` r e s e t L o o p ( ) ` ` .
// / - ` ` s e l e c t S l o t ( _ : ) ` ` / ` ` c y c l e S l o t ( ) ` ` : s w i t c h t h e a c t i v e s l o t . A r e c o r d e d s l o t r e s u m e s
// / l o o p i n g f r o m i t s h e a d ; a n e m p t y s l o t i s s i l e n t a n d r e a d y t o r e c o r d .
// / - ` o v e r d u b O n ` ( v i a ` ` t o g g l e O v e r d u b ( ) ` ` ) : l a y e r s i n p u t o n t o t h e a c t i v e s l o t ; e n g a g i n g i t
// / f i r s t s n a p s h o t s t h e l o o p s o ` ` u n d o O v e r d u b ( ) ` ` c a n r e v e r t t h e p a s s .
// / - ` l o o p O n ` / ` r e v e r s e ` ( s w i t c h e s ) : c r o s s f a d e d m u t e / l i v e p l a y b a c k - d i r e c t i o n f l i p .
// / - ` ` r e s e t L o o p ( ) ` ` : c l e a r s t h e a c t i v e s l o t a n d r e t u r n s i t t o ` . e m p t y ` .
// /
// / # # K n o b s
// / - ` c r o s s f a d e T i m e ` : F a d e t i m e c o n s t a n t , i n s e c o n d s , f o r ` l o o p O n ` a n d t h e r e c o r d → p l a y h a n d o f f .
// / - ` p l a y b a c k L e v e l ` : L o o p p l a y b a c k g a i n , i n d B .
// / - ` o v e r d u b L e v e l ` : O v e r d u b i n p u t g a i n , i n d B .
// / - ` f e e d b a c k ` : R e t a i n e d l e v e l o f e x i s t i n g l o o p c o n t e n t o n e a c h o v e r d u b p a s s , ` [ 0 , 1 ] ` .
// / ` 1 . 0 ` ( d e f a u l t ) k e e p s o l d l a y e r s f o r e v e r ; l o w e r v a l u e s f a d e t h e m f o r a d e c a y i n g d u b .
// / - ` c r o s s f a d e T i m e ` , ` p l a y b a c k L e v e l ` , ` o v e r d u b L e v e l ` , ` f e e d b a c k ` ( s e e t h e p r o p e r t y d o c s ) .
public final class LooperPedal : Pedal {
// / O n e - l i n e d e s c r i p t i o n s h o w n i n t h e p i c k e r a n d i n f o p a n e l .
public override var summary : String {
" Records a loop, then plays it back with overdub, reverse, and a crossfaded mute ."
" Multi-slot looper: record several loops , switch between them, overdub, and reverse."
}
// / D o u b l e - w i d t h c a r d : t h e l o o p e r c a r r i e s a f u l l t r a n s p o r t p l u s a l o o p t i m e l i n e a n d r e a d s
// / m o r e c o m f o r t a b l y i n a w i d e r e n c l o s u r e .
// / D o u b l e - w i d t h c a r d : t h e l o o p e r c a r r i e s a f u l l t r a n s p o r t p l u s a s l o t s t r i p a n d t i m e l i n e .
public override var widthUnits : Int { 2 }
// MARK: - K n o b s
@@ -66,42 +61,47 @@ public final class LooperPedal: Pedal {
// / L o o p p l a y b a c k g a i n , i n d B .
@ UIParam public var playbackLevel : Double = 0.0
// / O v e r d u b i n p u t g a i n , i n d B . S c a l e s o n l y t h e n e w m a t e r i a l b e i n g l a y e r e d i n — t h e
// / e x i s t i n g l o o p c o n t e n t i s n e v e r a t t e n u a t e d b y t h i s k n o b .
// / O v e r d u b i n p u t g a i n , i n d B . S c a l e s o n l y t h e n e w m a t e r i a l b e i n g l a y e r e d i n .
@ UIParam public var overdubLevel : Double = 0.0
// / R e t a i n e d l e v e l o f t h e e x i s t i n g l o o p c o n t e n t o n e a c h o v e r d u b p a s s , ` [ 0 , 1 ] ` . ` 1 . 0 `
// / k e e p s p r i o r l a y e r s i n d e f i n i t e l y ; l o w e r v a l u e s f a d e t h e m a l i t t l e o n e v e r y p a s s f o r a
// / d e c a y i n g " s o u n d - o n - s o u n d " f e e d b a c k l o o p .
// / k e e p s p r i o r l a y e r s i n d e f i n i t e l y ; l o w e r v a l u e s f a d e t h e m f o r a d e c a y i n g f e e d b a c k l o o p .
@ UIParam public var feedback : Double = 1.0
// MARK: - S w i t c h e s
// / O v e r d u b g a t e . ` 0 ` = o f f , ` 1 ` = o n . L i v e - g a t e d — h a s n o e f f e c t b e f o r e a l o o p e x i s t s .
// / O v e r d u b g a t e . ` 0 ` = o f f , ` 1 ` = o n . L i v e - g a t e d — a f f e c t s o n l y t h e a c t i v e , p l a y i n g s l o t .
@ UIParam public var overdubOn : Double = 0.0
// / T o t a l l o o p m u t e . ` 0 ` = m u t e d ( s i l e n t ) , ` 1 ` = a u d i b l e . T r a n s i t i o n s a r e s m o o t h e d b y
// / ` c r o s s f a d e T i m e ` .
// / T o t a l l o o p m u t e . ` 0 ` = m u t e d , ` 1 ` = a u d i b l e . S m o o t h e d b y ` c r o s s f a d e T i m e ` .
@ UIParam public var loopOn : Double = 1.0
// / P l a y b a c k d i r e c t i o n . ` 0 ` = f o r w a r d , ` 1 ` = r e v e r s e . R e a d b y t h e p l a y e r ' s ` r a t e ` i n p u t
// / a s ` 1 − 2 · r e v e r s e ` , s o t o g g l i n g f l i p s t h e l o o p w i t h o u t r e b u i l d i n g t h e g r a p h .
// / P l a y b a c k d i r e c t i o n . ` 0 ` = f o r w a r d , ` 1 ` = r e v e r s e . R e a d a s ` 1 − 2 · r e v e r s e ` .
@ UIParam public var reverse : Double = 0.0
// MARK: - I n t e r n a l l i v e s t a t e
// / ` 1 ` o n c e a t a k e h a s b e e n r e c o r d e d a n d l o o p i n g p l a y b a c k h a s s t a r t e d , e l s e ` 0 ` .
// / R e a d b y t h e a u d i o g r a p h ( g a t e s o v e r d u b a n d t h e l o o p c r o s s f a d e ) — n o t a u s e r k n o b .
// / ` 1 ` w h i l e t h e a c t i v e s l o t i s l o o p i n g p l a y b a c k , e l s e ` 0 ` . R e a d b y t h e a u d i o g r a p h .
@ UIParam public var isPlaying : Double = 0.0
// / P l a y - h e a d r e s e t t r i g g e r f o r t h e l o o p p l a y e r . P u l s e d ` 0 → 1 ` a t l o o p c l o s e s o p l a y b a c k
// / s t a r t s p h a s e - a l i g n e d t o f r a m e 0 ( t h e a n c h o r f o r t h e t i m e l i n e r e a d - o u t ) ; r e a r m e d t o ` 0 `
// / o n ` ` r e s e t L o o p ( ) ` ` . R e a d b y t h e p l a y e r ' s ` t r i g g e r ` i n p u t . N o t a u s e r k n o b .
// / P l a y - h e a d r e s e t t r i g g e r f o r t h e l o o p p l a y e r . P u l s e d ` 0 → 1 ` a t l o o p c l o s e / s l o t s w i t c h .
@ UIParam public var playTrigger : Double = 0.0
// / L E D s t a t e f o r t h e S t o p f o o t s w i t c h : ` 1 ` w h i l e a r e c o r d e d l o o p i s h a l t e d , e l s e ` 0 ` .
@ UIParam public var stoppedIndicator : Double = 0.0
// / L E D s t a t e f o r t h e C u e S t o p f o o t s w i t c h : ` 1 ` w h i l e a s t o p - a t - l o o p - e n d i s a r m e d , e l s e ` 0 ` .
// / A l s o t h e s i n g l e s o u r c e o f t r u t h f o r ` ` i s S t o p A t L o o p E n d A r m e d ` ` .
@ UIParam public var cueStopArmed : Double = 0.0
// MARK: - C o n s t a n t s
// / M a x i m u m r e c o r d a b l e t a k e l e n g t h , i n s e c o n d s .
// / N u m b e r o f i n d e p e n d e n t l o o p s l o t s . E a c h s l o t a l l o c a t e s i t s o w n ` m a x D u r a t i o n S e c o n d s `
// / b u f f e r , s o p e r - i n s t a n c e m e m o r y i s r o u g h l y ` s l o t C o u n t × m a x D u r a t i o n S e c o n d s × s a m p l e R a t e ×
// / 8 b y t e s ` ( ≈ 2 1 M B p e r s l o t a t 4 4 . 1 k H z / 6 0 s — ~ 8 5 M B f o r f o u r ) . R a i s e w i t h t h a t i n m i n d .
public static let slotCount : Int = 4
// / M a x i m u m r e c o r d a b l e t a k e l e n g t h p e r s l o t , i n s e c o n d s .
private static let maxDurationSeconds : Double = 60.0
// / D e f a u l t c r o s s f a d e d u r a t i o n , i n s e c o n d s .
@@ -113,31 +113,50 @@ public final class LooperPedal: Pedal {
// / F o r w a r d p l a y b a c k r a t e , a n d t h e r a t e o f f s e t t h e r e v e r s e s p a n i s s u b t r a c t e d f r o m .
private static let forwardRate : Double = 1.0
// / S m a l l e s t p e r m i t t e d l o o p l e n g t h , i n f r a m e s , s o a d e g e n e r a t e z e r o - l e n g t h t a k e c a n ' t
// / p r o d u c e a d i v i d e - b y - z e r o i n t h e p l a y e r o r t h e t i m e l i n e r e a d - o u t .
// / S m a l l e s t p e r m i t t e d l o o p l e n g t h , i n f r a m e s , s o a d e g e n e r a t e t a k e c a n ' t d i v i d e b y z e r o .
private static let minimumLoopFrames : Int = 1
// / F r a m e s t o w a i t a f t e r d i s a b l i n g o v e r d u b b e f o r e r e s t o r i n g a n u n d o s n a p s h o t , s o t h e
// / R T - s i d e o v e r d u b w r i t e r ( i t s ` r u n ` g a t e r a m p s c l o s e d o v e r o n e c o n t r o l b l o c k ) h a s f u l l y
// / s t o p p e d w r i t i n g t h e l o o p b u f f e r b e f o r e t h e m a i n t h r e a d o v e r w r i t e s i t . T w o m a x i m u m
// / b l o c k s ( ` b l o c k S i z e ≤ 5 1 2 ` ) i s a c o m f o r t a b l e m a r g i n .
// / F r a m e s t o w a i t a f t e r d i s a b l i n g o v e r d u b b e f o r e r e s t o r i n g a n u n d o s n a p s h o t , s o t h e R T - s i d e
// / o v e r d u b w r i t e r ( i t s ` r u n ` g a t e r a m p s c l o s e d o v e r o n e c o n t r o l b l o c k ) h a s f u l l y s t o p p e d
// / w r i t i n g t h e l o o p b u f f e r b e f o r e t h e m a i n t h r e a d o v e r w r i t e s i t . T w o m a x i m u m b l o c k s .
private static let undoGuardFrames : Int = 1024
private let crossfadeTimeRange : ClosedRange < Double > = 0.0 . . . 2.0
private let levelRangeDb : ClosedRange < Double > = - 24.0 . . . 12.0
private let feedbackRange : ClosedRange < Double > = 0.0 . . . 1.0
// MARK: - B u f f e r + g e n e r a t o r s
// MARK: - B u f f e r s + g e n e r a t o r s ( o n e s e t p e r s l o t , o n e s h a r e d p l a y e r )
// / T h e l o o p b u f f e r , s i z e d t o ` m a x D u r a t i o n S e c o n d s ` a t t h e e n g i n e s a m p l e r a t e . A l l o c a t e d
// / i n ` e f f e c t ( _ : ) ` ( c a l l e d f r o m ` b u i l d ( ) ` ) , n o t ` i n i t ` , b e c a u s e ` s a m p l e R a t e ` i s o n l y s e t
// / b y ` P e d a l B o a r d . a d d ( _ : ) ` a f t e r c o n s t r u c t i o n b u t b e f o r e t h e e n g i n e b u i l d s t h e g r a p h .
private var loopBuffer : LyrebirdBuffer ?
// / P e r - s l o t l o o p b u f f e r s , s i z e d t o ` m a x D u r a t i o n S e c o n d s ` . A l l o c a t e d i n ` e f f e c t ( _ : ) ` b e c a u s e
// / ` s a m p l e R a t e ` i s o n l y s e t b y ` P e d a l B o a r d . a d d ( _ : ) ` a f t e r c o n s t r u c t i o n .
private var buffers : [ LyrebirdBuffer ] = [ ]
private var primaryRecorder : RecordBuffer . Audio ?
private var overdubRecorder : RecordBuffer . Audio ?
private var primaryRecorders : [ RecordBuffer . Audio ] = [ ]
private var overdubRecorders : [ RecordBuffer . Audio ] = [ ]
// / P e r - s l o t o v e r d u b g a t e ( ` 1 ` f o r t h e a c t i v e s l o t , ` 0 ` o t h e r w i s e ) , m u l t i p l i e d i n t o e a c h
// / o v e r d u b r e c o r d e r ' s ` r u n ` s o o n l y t h e a c t i v e s l o t l a y e r s n e w m a t e r i a l .
private var slotGateNodes : [ ParameterNode ] = [ ]
// / T h e s h a r e d p l a y e r . I t s s o u r c e b u f f e r i s s w a p p e d t o t h e a c t i v e s l o t o n ` ` s e l e c t S l o t ( _ : ) ` ` .
private var player : PlayBuffer . Audio ?
// MARK: - P e r - s l o t s t a t e
private var activeSlot : Int = 0
// / C l o s e d - l o o p l e n g t h , i n f r a m e s , p e r s l o t ( ` 0 ` = e m p t y ) .
private var loopFrames : [ Int ] = [ ]
// / C l o s e d - l o o p l e n g t h , i n w h o l e b e a t s , p e r s l o t ( ` 0 ` u n l e s s b e a t - q u a n t i z e d ) .
private var loopBeats : [ Double ] = [ ]
// / S a m p l e t i m e p l a y b a c k b e g a n l o o p i n g ( p l a y - h e a d a t f r a m e 0 ) , p e r s l o t — t i m e l i n e a n c h o r .
private var playStartSample : [ SampleTime ] = [ ]
// / U n d o s n a p s h o t p e r s l o t , c a p t u r e d w h e n o v e r d u b w a s l a s t e n g a g e d o n t h a t s l o t .
private var overdubUndoSnapshots : [ [ Double ] ? ] = [ ]
// MARK: - T r a n s p o r t s t a t e
private enum Phase {
@@ -146,38 +165,29 @@ public final class LooperPedal: Pedal {
case recording
case armedStop
case playing
case stopped
}
// / T r a n s p o r t p h a s e o f t h e * a c t i v e * s l o t .
private var phase : Phase = . empty
// / B e a t - b o u n d a r y s a m p l e t i m e a t w h i c h t h e c u r r e n t t a k e s t a r t e d r e c o r d i n g , a n d a t w h i c h
// / t h e l o o p c l o s e s . B o t h a r e c a p t u r e d f r o m t h e c l o c k s o t h e l o o p l e n g t h i s b e a t - e x a c t .
// / B e a t - b o u n d a r y s a m p l e t i m e a t w h i c h t h e a c t i v e s l o t s t a r t e d r e c o r d i n g .
private var recordStartSample : SampleTime = 0
// / S a m p l e t i m e p l a y b a c k b e g a n l o o p i n g ( p l a y - h e a d a t f r a m e 0 ) — t h e t i m e l i n e a n c h o r .
private var playStartSample : SampleTime = 0
// / L e n g t h o f t h e c l o s e d l o o p i n f r a m e s ( ` 0 ` u n t i l a t a k e i s r e c o r d e d ) .
private var loopFrames : Int = 0
// / L e n g t h o f t h e c l o s e d l o o p i n w h o l e b e a t s ( ` 0 ` u n t i l a b e a t - q u a n t i z e d t a k e i s c l o s e d ) .
private var loopBeats : Double = 0.0
// / S n a p s h o t o f t h e l o o p t a k e n w h e n o v e r d u b w a s l a s t e n g a g e d , f o r ` ` u n d o O v e r d u b ( ) ` ` .
private var overdubUndoSnapshot : [ Double ] ?
// / B a c k g r o u n d r o u t i n e t h a t w a i t s f o r t h e n e x t b e a t b e f o r e a p p l y i n g a t r a n s p o r t a c t i o n .
// / S t o p p e d ( n o n - b l o c k i n g ) b e f o r e s t a r t i n g a n e w o n e , a n d t o r n d o w n o n ` ` r e s e t L o o p ( ) ` ` .
private var transportRoutine : LyrebirdRoutine ?
// / B a c k g r o u n d r o u t i n e t h a t d e f e r s a n u n d o r e s t o r e u n t i l t h e R T o v e r d u b g a t e h a s c l o s e d .
private var undoRoutine : LyrebirdRoutine ?
// / M o n o t o n i c t r a n s p o r t - a c t i o n g e n e r a t i o n . B u m p e d o n e v e r y a r m a n d o n ` ` r e s e t L o o p ( ) ` ` ; a
// / d e f e r r e d b e a t c a l l b a c k c a r r i e s t h e g e n e r a t i o n i t w a s a r m e d u n d e r a n d i s d r o p p e d i f t h e
// / g e n e r a t i o n h a s s i n c e a d v a n c e d — s o a c a l l b a c k w h o s e a c t i o n w a s s u p e r s e d e d ( a n e w p r e s s ,
// / a r e s e t ) c a n n e v e r a p p l y t o t h e w r o n g t a k e . G u a r d s t h e ` D i s p a t c h Q u e u e . m a i n . a s y n c ` h o p ,
// / w h i c h ` L y r e b i r d R o u t i n e . s t o p ( ) ` c a n n o t i t s e l f c a n c e l o n c e e n q u e u e d .
// / B a c k g r o u n d r o u t i n e t h a t w a i t s f o r t h e l o o p b o u n d a r y b e f o r e a n a r m e d " s t o p a t e n d " .
private var stopRoutine : LyrebirdRoutine ?
// / M o n o t o n i c a c t i o n g e n e r a t i o n . B u m p e d o n e v e r y a r m , s l o t s w i t c h , o v e r d u b p r e s s , a n d r e s e t ;
// / a d e f e r r e d b e a t / u n d o c a l l b a c k c a r r i e s t h e g e n e r a t i o n i t w a s a r m e d u n d e r a n d i s d r o p p e d i f
// / t h e g e n e r a t i o n h a s s i n c e a d v a n c e d — s o a s u p e r s e d e d c a l l b a c k c a n n e v e r a p p l y t o t h e w r o n g
// / s t a t e . G u a r d s t h e ` D i s p a t c h Q u e u e . m a i n . a s y n c ` h o p , w h i c h ` L y r e b i r d R o u t i n e . s t o p ( ) ` c a n n o t
// / i t s e l f c a n c e l o n c e e n q u e u e d .
// / ( i n t e r n a l , n o t p r i v a t e , o n l y s o t e s t s c a n a s s e r t t h e g e n e r a t i o n - d r o p o f a s t a l e c a l l b a c k . )
private ( set ) var transportGeneration : Int = 0
@@ -203,20 +213,30 @@ public final class LooperPedal: Pedal {
]
}
// / T r a n s p o r t r o w ( b e l o w t h e k n o b s ) : t h e t w o s t a t e t o g g l e s f l a n k t h e t w o s t o p s .
public override var effectTransportButtons : [ PedalButton ] {
[
PedalButton ( label : " Loop " , node : $ loopOn ) ,
PedalButton ( label : " Stop " , indicator : $ stoppedIndicator ) { [ weak self ] in self ? . stopPlayback ( ) } ,
PedalButton ( label : " Cue Stop " , indicator : $ cueStopArmed ) { [ weak self ] in self ? . toggleStopAtLoopEnd ( ) } ,
PedalButton ( label : " Reverse " , node : $ reverse ) ,
]
}
// / M o m e n t a r y a c t i o n r o w .
public override var effectButtons : [ PedalButton ] {
[
PedalButton ( label : " Record / Stop " ) { [ weak self ] in self ? . toggleRecordStop ( ) } ,
PedalButton ( label : " Overdub " ) { [ weak self ] in self ? . toggleOverdub ( ) } ,
PedalButton ( label : " Undo " ) { [ weak self ] in self ? . undoOverdub ( ) } ,
PedalButton ( label : " Loop " , node : $ loopOn ) ,
PedalButton ( label : " Reverse " , node : $ reverse ) ,
PedalButton ( label : " Slot " ) { [ weak self ] in self ? . cycleSlot ( ) } ,
PedalButton ( label : " Reset " ) { [ weak self ] in self ? . resetLoop ( ) } ,
]
}
// MARK: - U I r e a d - o u t s
// / P u b l i c l y - o b s e r v a b l e t r a n s p o r t p h a s e f o r t h e U I ( L E D c o l o u r , a r m e d a n i m a t i o n , t i m e l i n e ) .
// / P u b l i c l y - o b s e r v a b l e t r a n s p o r t p h a s e o f t h e a c t i v e s l o t ( L E D c o l o u r , a r m e d s t a t e , t i m e l i n e ) .
public var transportState : LooperTransportState {
switch phase {
case . empty : return . empty
@@ -224,19 +244,31 @@ public final class LooperPedal: Pedal {
case . recording : return . recording
case . armedStop : return . armedStop
case . playing : return . playing
case . stopped : return . stopped
}
}
// / C l o s e d - l o o p l e n g t h i n f r a m e s ( ` 0 ` u n t i l a t a k e i s r e c o r d e d ) .
public var loopFrameCount : Int { loopFrames }
// / ` t r u e ` w h i l e a " s t o p a t l o o p e n d " i s a r m e d a n d w a i t i n g f o r t h e b o u n d a r y .
public var isStopAtLoopEndArmed : Bool { cueStopArmed >= 0.5 }
// / C l o s e d - l o o p l e n g t h i n s e c o n d s ( ` 0 ` u n t i l a t a k e i s r e c o r d e d ) .
public var loopLengthSeconds : Double { Double ( loopFrames ) / sampleRate }
// / I n d e x o f t h e a c t i v e l o o p s l o t , i n ` 0 . . < s l o t C o u n t ` .
public var activeSlotIndex : Int { activeSlot }
// / C l o s e d - l o o p l e n g t h i n w h o l e b e a t s ( ` 0 ` u n l e s s t h e t a k e w a s b e a t - q u a n t i z e d ) .
public var loopLengthBeats : Double { loopBeats }
// / ` t r u e ` i f s l o t ` i n d e x ` h o l d s a r e c o r d e d l o o p .
public func slotHasContent ( _ index : Int ) -> Bool {
index >= 0 && index < loopFrames . count && loopFrames [ index ] > 0
}
// / N o r m a l i s e d p l a y - h e a d p o s i t i o n i n ` [ 0 , 1 ) ` w h i l e ` . p l a y i n g ` , e l s e ` n i l ` .
// / A c t i v e - s l o t l o o p l e n g t h i n f r a m e s ( ` 0 ` u n t i l a t a k e i s r e c o r d e d ) .
public var loopFrameCount : Int { loopFrames . indices . contains ( activeSlot ) ? loopFrames [ activeSlot ] : 0 }
// / A c t i v e - s l o t l o o p l e n g t h i n s e c o n d s .
public var loopLengthSeconds : Double { Double ( loopFrameCount ) / sampleRate }
// / A c t i v e - s l o t l o o p l e n g t h i n w h o l e b e a t s ( ` 0 ` u n l e s s t h e t a k e w a s b e a t - q u a n t i z e d ) .
public var loopLengthBeats : Double { loopBeats . indices . contains ( activeSlot ) ? loopBeats [ activeSlot ] : 0.0 }
// / N o r m a l i s e d p l a y - h e a d p o s i t i o n i n ` [ 0 , 1 ) ` w h i l e t h e a c t i v e s l o t i s ` . p l a y i n g ` , e l s e ` n i l ` .
// /
// / D e r i v e d f r o m t h e e n g i n e s a m p l e c l o c k a n d t h e f r a m e - 0 p l a y b a c k a n c h o r , s o i t t r a c k s t h e
// / a u d i o p l a y - h e a d r e g a r d l e s s o f t e m p o . T h e p l a y e r ' s p l a y - h e a d i s r e s e t b y a ` p l a y T r i g g e r `
@@ -244,13 +276,13 @@ public final class LooperPedal: Pedal {
// / p l a y - h e a d b y u p t o o n e b l o c k ( ≤ ~ 1 1 m s ) — i m p e r c e p t i b l e f o r a U I s w e e p . R e v e r s e p l a y b a c k
// / i s r e f l e c t e d b y m i r r o r i n g t h e f o r w a r d p r o g r e s s , m a t c h i n g t h e p l a y e r ' s n e g a t e d ` r a t e ` .
public func playbackPosition01 ( ) -> Double ? {
guard phase = = . playing , loopFrames > 0 , let engine : SynthesisEngine = hostEngine else {
return nil
}
let elapsed : SampleTime = engine . currentTime - playStartSample
guard phase = = . playing , loopFrameCount > 0 ,
let engine : SynthesisEngine = hostEngine ,
playStartSample . indices . contains ( activeSlot ) else { return nil }
let elapsed : SampleTime = engine . currentTime - playStartSample [ activeSlot ]
guard elapsed >= 0 else { return 0.0 }
let framesInto : Int = Int ( elapsed % SampleTime ( loopFrames ) )
let forward : Double = Double ( framesInto ) / Double ( loopFrames )
let framesInto : Int = Int ( elapsed % SampleTime ( loopFrameCount ) )
let forward : Double = Double ( framesInto ) / Double ( loopFrameCount )
return reverse >= 0.5 ? ( 1.0 - forward ) : forward
}
@@ -258,37 +290,50 @@ public final class LooperPedal: Pedal {
public override func effect ( _ dry : any ValidGeneratorInput ) -> any ValidGeneratorInput {
let maxFrames : Int = Int ( LooperPedal . maxDurationSeconds * sampleRate )
let buffer : LyrebirdBuffer = LyrebirdBuffer ( count : maxFrames , channelCount : 1 )
loopBuffer = buffer
let primary : RecordBuffer . Audio = RecordBuffer . Audio (
input : dry ,
buffer : buffer ,
loop : false ,
doneAction : . nothing
)
primary . run = 0.0
primaryRecorder = primary
for i : Int in 0 . . < LooperPedal . slotCount {
let buffer : LyrebirdBuffer = LyrebirdBuffer ( count : maxFrames , channelCount : 1 )
buffers . append ( buffer )
let overdub : RecordBuffer . Audio = RecordBuffer . Audio (
input : dry ,
buffer : buffer ,
loop : true ,
doneAction : . nothing
)
// L i v e - g a t e d : o n l y w r i t e s w h i l e a l o o p e x i s t s a n d o v e r d u b i s s w i t c h e d o n . T h e e x i s t i n g
// l o o p c o n t e n t i s s c a l e d b y ` f e e d b a c k ` o n e a c h p a s s ( 1 . 0 = k e e p , < 1 . 0 = d e c a y ) .
overdub . $ run . source = $ overdubOn * $ isPlaying
overdub . $ recLevel . source = DbAmp . Control ( $ overdubLevel )
overdub . $ preLevel . source = $ feedback
overdubRecorder = overdub
let primary : RecordBuffer . Audio = RecordBuffer . Audio (
input : dry ,
buffer : buffer ,
loop : false ,
doneAction : . nothing
)
primary . run = 0.0
primaryRecorders . append ( primary )
// A c t i v e - s l o t g a t e : ` 1 ` o n l y f o r s l o t 0 a t b u i l d ( t h e d e f a u l t a c t i v e s l o t ) .
let activeAtBuild : Double = i = = activeSlot ? 1.0 : 0.0
let gate : ParameterNode = ParameterNode ( activeAtBuild )
slotGateNodes . append ( gate )
let overdub : RecordBuffer . Audio = RecordBuffer . Audio (
input : dry ,
buffer : buffer ,
loop : true ,
doneAction : . nothing
)
// L i v e - g a t e d : w r i t e s o n l y w h i l e t h i s s l o t i s a c t i v e , p l a y i n g , a n d o v e r d u b i s o n .
// E x i s t i n g c o n t e n t i s s c a l e d b y ` f e e d b a c k ` e a c h p a s s ( 1 . 0 = k e e p , < 1 . 0 = d e c a y ) .
overdub . $ run . source = $ overdubOn * $ isPlaying * gate
overdub . $ recLevel . source = DbAmp . Control ( $ overdubLevel )
overdub . $ preLevel . source = $ feedback
overdubRecorders . append ( overdub )
}
loopFrames = [ Int ] ( repeating : 0 , count : LooperPedal . slotCount )
loopBeats = [ Double ] ( repeating : 0.0 , count : LooperPedal . slotCount )
playStartSample = [ SampleTime ] ( repeating : 0 , count : LooperPedal . slotCount )
overdubUndoSnapshots = [ [ Double ] ? ] ( repeating : nil , count : LooperPedal . slotCount )
// P l a y b a c k r a t e : f o r w a r d a t + 1 , r e v e r s e a t − 1 , s w i t c h e d l i v e o f f ` $ r e v e r s e ` .
let playbackRate : any ValidGeneratorInput =
LooperPedal . forwardRate - LooperPedal . reverseRateSpan * $ reverse
let loopPlayer : PlayBuffer . Audio = PlayBuffer . Audio (
buffer : buffer ,
buffer : buffers [ activeSlot ] ,
loop : true ,
interpolation : . cubic ,
rate : playbackRate ,
@@ -297,33 +342,101 @@ public final class LooperPedal: Pedal {
)
player = loopPlayer
// L o o p a u d i b l e o n l y w h i l e p l a y i n g a n d n o t m u t e d ; s m o o t h e d t o a v o i d c l i c k s o n e i t h e r
// t h e r e c o r d → p l a y h a n d o f f o r a l o o p O n t o g g l e .
// L o o p a u d i b l e o n l y w h i l e p l a y i n g a n d n o t m u t e d ; s m o o t h e d t o a v o i d c l i c k s .
let combinedGate : any ValidGeneratorInput = $ isPlaying * $ loopOn
let smoothedGate : Lag . Control = Lag . Control ( input : combinedGate , time : $ crossfadeTime )
let unityGain : Double = 1.0
let dryGate : any ValidGeneratorInput = unityGain - smoothedGate
return ( primary * dryGate ) + ( loopPlayer * smoothedGate )
// T h e r e c o r d e r s a r e ` R o o t R e g i s t e r i n g ` a n d r u n f o r t h e i r w r i t e s i d e e f f e c t ; t h e m i x i s
// t h e d r y p a s s t h r o u g h p l u s t h e s h a r e d p l a y e r .
return ( dry * dryGate ) + ( loopPlayer * smoothedGate )
}
// MARK: - T r a n s p o r t
// / ` . e m p t y ` → a r m s / b e g i n s r e c o r d i n g . ` . r e c o r d i n g ` → c l o s e s t h e l o o p a n d s t a r t s l o o p i n g .
// / N o - o p w h i l e ` . p l a y i n g ` o r a l r e a d y a r m e d .
// / ` . e m p t y ` → a r m / b e g i n r e c o r d i n g t h e a c t i v e s l o t . ` . r e c o r d i n g ` → c l o s e t h e l o o p a n d s t a r t
// / l o o p i n g . N o - o p w h i l e ` . p l a y i n g ` o r a l r e a d y a r m e d .
public func toggleRecordStop ( ) {
switch phase {
case . empty :
armRecording ( )
case . recording :
armStopAndPlay ( )
case . stopped :
resumePlayback ( )
case . armedRecord , . armedStop , . playing :
break
}
}
// / T o g g l e s t h e o v e r d u b g a t e . E n g a g i n g i t ( o f f → o n ) f i r s t s n a p s h o t s t h e c u r r e n t l o o p s o
// / ` ` u n d o O v e r d u b ( ) ` ` c a n r e v e r t t h e p a s s .
// / I m m e d i a t e l y h a l t s l o o p p l a y b a c k o f t h e a c t i v e s l o t . T h e r e c o r d e d c o n t e n t i s r e t a i n e d —
// / p r e s s R e c o r d / S t o p ( o r s w i t c h s l o t s ) t o s t a r t i t a g a i n . C a n c e l s a n y a r m e d " s t o p a t e n d " .
public func stopPlayback ( ) {
guard phase = = . playing else { return }
transportGeneration += 1
stopRoutine ? . stop ( )
stopRoutine = nil
cueStopArmed = 0.0
isPlaying = 0.0
stoppedIndicator = 1.0
phase = . stopped
}
// / A r m s ( o r , i f a l r e a d y a r m e d , c a n c e l s ) a s t o p t h a t f i r e s w h e n t h e a c t i v e l o o p n e x t r e a c h e s
// / i t s e n d . N o - o p u n l e s s a l o o p i s p l a y i n g . W i t h n o e n g i n e t o s c h e d u l e a g a i n s t , s t o p s n o w .
public func toggleStopAtLoopEnd ( ) {
if isStopAtLoopEndArmed {
stopRoutine ? . stop ( )
stopRoutine = nil
cueStopArmed = 0.0
transportGeneration += 1
return
}
guard phase = = . playing , loopFrameCount > 0 , playStartSample . indices . contains ( activeSlot ) else { return }
transportGeneration += 1
let generation : Int = transportGeneration
guard let engine : SynthesisEngine = hostEngine else {
stopPlayback ( )
return
}
// S a m p l e t i m e o f t h e n e x t l o o p b o u n d a r y a f t e r n o w .
let anchor : SampleTime = playStartSample [ activeSlot ]
let frames : SampleTime = SampleTime ( loopFrameCount )
let elapsed : SampleTime = max ( 0 , engine . currentTime - anchor )
let boundary : SampleTime = anchor + ( elapsed / frames + 1 ) * frames
cueStopArmed = 1.0
stopRoutine ? . stop ( )
let routine : LyrebirdRoutine = LyrebirdRoutine { [ weak self ] ctx in
try ctx . waitUntil ( sampleTime : boundary )
DispatchQueue . main . async { self ? . applyStopAtLoopEnd ( generation : generation ) }
}
stopRoutine = routine
routine . start ( engine : engine )
}
// / A p p l i e s a n a r m e d " s t o p a t e n d " o n c e t h e b o u n d a r y i s r e a c h e d , u n l e s s s u p e r s e d e d .
func applyStopAtLoopEnd ( generation : Int ) {
guard generation = = transportGeneration , phase = = . playing else { return }
cueStopArmed = 0.0
isPlaying = 0.0
stoppedIndicator = 1.0
phase = . stopped
}
// / R e s u m e s l o o p i n g p l a y b a c k o f a s t o p p e d s l o t f r o m i t s h e a d .
private func resumePlayback ( ) {
guard phase = = . stopped , loopFrameCount > 0 , playStartSample . indices . contains ( activeSlot ) else { return }
transportGeneration += 1
isPlaying = 1.0
stoppedIndicator = 0.0
phase = . playing
playStartSample [ activeSlot ] = hostEngine ? . currentTime ? ? 0
pulsePlayTrigger ( )
}
// / T o g g l e s t h e o v e r d u b g a t e o n t h e a c t i v e s l o t . E n g a g i n g i t ( o f f → o n ) f i r s t s n a p s h o t s t h e
// / c u r r e n t l o o p s o ` ` u n d o O v e r d u b ( ) ` ` c a n r e v e r t t h e p a s s .
public func toggleOverdub ( ) {
// A n y o v e r d u b p r e s s s u p e r s e d e s a p e n d i n g d e f e r r e d a c t i o n ( e . g . a n i n - f l i g h t u n d o
// r e s t o r e ) — a d v a n c e t h e g e n e r a t i o n s o t h a t s t a l e c a l l b a c k i s d r o p p e d , a n d s t o p t h e
@@ -336,23 +449,22 @@ public final class LooperPedal: Pedal {
overdubOn = engaging ? 1.0 : 0.0
}
// / R e s t o r e s t h e l o o p t o t h e s n a p s h o t c a p t u r e d w h e n o v e r d u b w a s l a s t e n g a g e d . N o - o p i f n o
// / s n a p s h o t e x i s t s . D i s e n g a g e s o v e r d u b , t h e n r e s t o r e s t h e b u f f e r o n l y o n c e t h e R T o v e r d u b
// / w r i t e r h a s a c t u a l l y s t o p p e d , s o t h e m a i n - t h r e a d b u l k w r i t e c a n ' t r a c e t h e R T t h r e a d ' s
// / l o c k - f r e e w r i t e s i n t o t h e s a m e b u f f e r .
// / R e s t o r e s t h e a c t i v e s l o t t o t h e s n a p s h o t c a p t u r e d w h e n o v e r d u b w a s l a s t e n g a g e d . N o - o p i f
// / n o s n a p s h o t e x i s t s . D i s e n g a g e s o v e r d u b , t h e n r e s t o r e s t h e b u f f e r o n l y o n c e t h e R T o v e r d u b
// / w r i t e r h a s a c t u a l l y s t o p p e d , s o t h e m a i n - t h r e a d b u l k w r i t e c a n ' t r a c e t h e R T t h r e a d .
public func undoOverdub ( ) {
guard let buffer : LyrebirdBuffer = loopBuffer ,
let snapshot : [ Double ] = overdubUndoSnapshot ,
snapshot . count <= buffer . totalSampleCount else { return }
guard buffers . indices . contains ( activeSlot ) ,
let snapshot : [ Double ] = overdubUndoSnapshots [ activeSlot ] ,
snapshot . count <= buffers [ activeSlot ] . totalSampleCount else { return }
overdubOn = 0.0
// A d v a n c e t h e g e n e r a t i o n a n d c a p t u r e i t : a l a t e r r e s e t / n e w t a k e / o v e r d u b p r e s s b u m p s
// t h e g e n e r a t i o n a n d d r o p s t h i s r e s t o r e , s o i t c a n n e v e r s t o m p a b u f f e r t h e u s e r h a s
// s i n c e c l e a r e d o r r e - r e c o r d e d .
// A d v a n c e a n d c a p t u r e t h e g e n e r a t i o n : a l a t e r r e s e t / n e w t a k e / s l o t s w i t c h / o v e r d u b
// p r e s s b u m p s i t a n d d r o p s t h i s r e s t o r e , s o i t c a n ' t s t o m p a b u f f e r s i n c e c h a n g e d .
transportGeneration += 1
let generation : Int = transportGeneration
let slot : Int = activeSlot
guard let engine : SynthesisEngine = hostEngine else {
// N o e n g i n e ( s t a n d a l o n e / h e a d l e s s t e s t s ) : s i n g l e - t h r e a d e d , r e s t o r e i m m e d i a t e l y .
applyUndoRestore ( snapshot , generation : generation )
applyUndoRestore ( snapshot , slot : slot , generation : generation )
return
}
// O v e r d u b ' s ` r u n ` g a t e r a m p s t o z e r o o v e r o n e c o n t r o l b l o c k , s o t h e r e c o r d e r m a y w r i t e
@@ -361,61 +473,123 @@ public final class LooperPedal: Pedal {
let target : SampleTime = engine . currentTime + SampleTime ( LooperPedal . undoGuardFrames )
let routine : LyrebirdRoutine = LyrebirdRoutine { [ weak self ] ctx in
try ctx . waitUntil ( sampleTime : target )
DispatchQueue . main . async { self ? . applyUndoRestore ( snapshot , generation : generation ) }
DispatchQueue . main . async { self ? . applyUndoRestore ( snapshot , slot : slot , generation : generation ) }
}
undoRoutine = routine
routine . start ( engine : engine )
}
// / W r i t e s a n u n d o s n a p s h o t b a c k i n t o t h e l o o p b u f f e r , u n l e s s i t s g e n e r a t i o n h a s b e e n
// / s u p e r s e d e d b y a r e s e t , a n e w t a k e , o r a s u b s e q u e n t o v e r d u b / u n d o p r e s s .
func applyUndoRestore ( _ snapshot : [ Double ] , generation : Int ) {
// / W r i t e s a n u n d o s n a p s h o t b a c k i n t o s l o t ` s l o t ` ' s b u f f e r , u n l e s s i t s g e n e r a t i o n h a s b e e n
// / s u p e r s e d e d b y a r e s e t , a n e w t a k e , a s l o t s w i t c h , o r a s u b s e q u e n t o v e r d u b / u n d o p r e s s .
func applyUndoRestore ( _ snapshot : [ Double ] , slot : Int , generation : Int ) {
guard generation = = transportGeneration ,
let buffer : LyrebirdBuffer = loopBuffer ,
snapshot . count <= buffer . totalSampleCount else { return }
buffer . setRangeValues ( snapshot , startingAt : 0 )
buffers . indices . contains ( slot ) ,
snapshot . count <= buffers [ slot ] . totalSampleCount else { return }
buffers [ slot ] . setRangeValues ( snapshot , startingAt : 0 )
}
// / C l e a r s t h e l o o p a n d r e t u r n s t o ` . e m p t y ` , r e a d y f o r a f r e s h t a k e .
// / A d v a n c e s t o t h e n e x t s l o t ( w r a p p i n g ) , a s t h e ` S l o t ` f o o t s w i t c h d o e s .
public func cycleSlot ( ) {
selectSlot ( ( activeSlot + 1 ) % LooperPedal . slotCount )
}
// / S w i t c h e s t h e a c t i v e s l o t . A r e c o r d e d s l o t r e s u m e s l o o p i n g f r o m i t s h e a d ; a n e m p t y s l o t i s
// / s i l e n t a n d r e a d y t o r e c o r d . N o - o p w h i l e r e c o r d i n g o r a r m e d ( f i n i s h o r r e s e t f i r s t ) .
public func selectSlot ( _ index : Int ) {
guard index >= 0 , index < LooperPedal . slotCount , index != activeSlot ,
buffers . indices . contains ( index ) , loopFrames . indices . contains ( index ) else { return }
guard phase = = . empty || phase = = . playing || phase = = . stopped else { return }
// T h e R T p l a y e r ' s b u f f e r c a n o n l y b e s w a p p e d w i t h a n e n g i n e ; w i t h o u t o n e t h e s w i t c h
// w o u l d d e s y n c t h e U I / t r a n s p o r t s t a t e f r o m t h e ( u n c h a n g e d ) a u d i o , s o r e q u i r e i t .
guard let engine : SynthesisEngine = hostEngine else { return }
transportGeneration += 1
undoRoutine ? . stop ( )
undoRoutine = nil
stopRoutine ? . stop ( )
stopRoutine = nil
cueStopArmed = 0.0
stoppedIndicator = 0.0
overdubOn = 0.0
if slotGateNodes . indices . contains ( activeSlot ) { slotGateNodes [ activeSlot ] . value = 0.0 }
activeSlot = index
if slotGateNodes . indices . contains ( index ) { slotGateNodes [ index ] . value = 1.0 }
player ? . setBuffer ( buffers [ index ] , engine : engine )
if loopFrames [ index ] > 0 {
isPlaying = 1.0
phase = . playing
playStartSample [ index ] = engine . currentTime
pulsePlayTrigger ( )
} else {
isPlaying = 0.0
phase = . empty
playTrigger = 0.0
}
}
// / C l e a r s t h e a c t i v e s l o t a n d r e t u r n s i t t o ` . e m p t y ` , r e a d y f o r a f r e s h t a k e .
public func resetLoop ( ) {
// S t o p ( n o n - b l o c k i n g ) a n y p e n d i n g t r a n s p o r t / u n d o r o u t i n e s a n d a d v a n c e t h e g e n e r a t i o n
// s o a n a l r e a d y - e n q u e u e d b e a t c a l l b a c k i s d r o p p e d r a t h e r t h a n a p p l i e d t o t h e n e w t a k e .
// S t o p ( n o n - b l o c k i n g ) a n y p e n d i n g r o u t i n e s a n d a d v a n c e t h e g e n e r a t i o n s o a n e n q u e u e d
// b e a t / u n d o c a l l b a c k i s d r o p p e d r a t h e r t h a n a p p l i e d t o t h e n e w t a k e .
transportRoutine ? . stop ( )
transportRoutine = nil
undoRoutine ? . stop ( )
undoRoutine = nil
stopRoutine ? . stop ( )
stopRoutine = nil
cueStopArmed = 0.0
stoppedIndicator = 0.0
transportGeneration += 1
primaryRecorder ? . run = 0.0
primaryRecorder ? . requestReset ( )
overdubRecorder ? . requestReset ( )
if let buffer : LyrebirdBuffer = loopBuffer {
buffer . clear ( )
buffer . setLoopLength ( buffer . count )
if primaryRecorders . indices . contains ( activeSlot ) {
primaryRecorders [ activeSlot ] . run = 0.0
primaryRecorders [ activeSlot ] . requestReset ( )
}
if overdubRecorders . indices . contains ( activeSlot ) {
overdubRecorders [ activeSlot ] . requestReset ( )
}
if buffers . indices . contains ( activeSlot ) {
buffers [ activeSlot ] . clear ( )
buffers [ activeSlot ] . setLoopLength ( buffers [ activeSlot ] . count )
}
isPlaying = 0.0
overdubOn = 0.0
loopOn = 1.0
playTrigger = 0.0
loopFrames = 0
loopBeats = 0.0
overdubUndoSnapshot = nil
if loopFrames . indices . contains ( activeSlot ) { loopFrames [ activeSlot ] = 0 }
if loopBeats . indices . contains ( activeSlot ) { loopBeats [ activeSlot ] = 0.0 }
if overdubUndoSnapshots . indices . contains ( activeSlot ) { overdubUndoSnapshots [ activeSlot ] = nil }
phase = . empty
}
// MARK: - T r a n s p o r t h e l p e r s
// / S n a p s h o t s t h e c l o s e d - l o o p c o n t e n t i n t o ` o v e r d u b U n d o S n a p s h o t ` . C a l l e d w i t h o v e r d u b o f f ,
// / s o o n l y t h e l o o p p l a y e r r e a d s t h e b u f f e r c o n c u r r e n t l y ( a l o c k - f r e e r e a d i s s a f e ) .
// / S n a p s h o t s t h e a c t i v e s l o t ' s c l o s e d - l o o p c o n t e n t i n t o ` o v e r d u b U n d o S n a p s h o t s ` . C a l l e d w i t h
// / o v e r d u b o f f , s o o n l y t h e l o o p p l a y e r r e a d s t h e b u f f e r c o n c u r r e n t l y ( a l o c k - f r e e r e a d i s s a f e ) .
private func captureOverdubSnapshot ( ) {
guard let buffer : LyrebirdBuffer = loopBuffer , loopFrames > 0 else { return }
// M o n o b u f f e r : l i n e a r s t o r a g e i n d e x = = f r a m e i n d e x . C a l l e d w i t h o v e r d u b o f f ( o n l y t h e
// p l a y e r r e a d s c o n c u r r e n t l y ) , s o a l o c k - f r e e r e a d o f t h e s t o r a g e i s s a f e .
let frameCount : Int = min ( loopFrames , buffer . totalSampleCount )
guard buffers . indices . contains ( activeSlot ) , loopFrameCount > 0 else { return }
let buffer : LyrebirdBuffer = buffers [ activeSlot ]
// M o n o b u f f e r : l i n e a r s t o r a g e i n d e x = = f r a m e i n d e x .
let frameCount : Int = min ( loopFrameCount , buffer . totalSampleCount )
var snapshot : [ Double ] = [ Double ] ( repeating : 0.0 , count : frameCount )
for i : Int in 0 . . < frameCount {
snapshot [ i ] = buffer [ i ]
}
overdubUndoSnapshot = snapshot
overdubUndoSnapshots [ activeSlot ] = snapshot
}
// / P u l s e s ` p l a y T r i g g e r ` ` 0 → 1 ` a c r o s s t w o r u n - l o o p t u r n s s o t h e p l a y e r s e e s a c l e a n r e s e t
// / e d g e e v e n w h e n t h e t r i g g e r w a s a l r e a d y h i g h ( e . g . s w i t c h i n g b e t w e e n r e c o r d e d s l o t s ) .
private func pulsePlayTrigger ( ) {
playTrigger = 0.0
// G u a r d t h e d e l a y e d e d g e w i t h t h e g e n e r a t i o n ( b u m p e d b y e v e r y t r a n s p o r t a c t i o n ) s o a
// r e c o r d / r e s e t / s w i t c h l a n d i n g i n t h e a s y n c g a p c a n ' t f i r e a s t a l e t r i g g e r o n t h e p l a y e r .
let generation : Int = transportGeneration
DispatchQueue . main . async { [ weak self ] in
guard let self , self . transportGeneration = = generation else { return }
self . playTrigger = 1.0
}
}
// / A t r a n s p o r t t r a n s i t i o n d e f e r r e d t o a b e a t b o u n d a r y .
@@ -436,9 +610,8 @@ public final class LooperPedal: Pedal {
runQuantized ( . finishStop )
}
// / A p p l i e s ` a c t i o n ` o n t h e m a i n t h r e a d o n c e t h e c l o c k r e a c h e s t h e n e x t b e a t , v i a a
// / b a c k g r o u n d w a i t r o u t i n e . W i t h n o c l o c k / e n g i n e , a p p l i e s i t s y n c h r o n o u s l y f o r
// / u n - q u a n t i z e d t r a n s p o r t ( a n d h e a d l e s s t e s t s ) .
// / A p p l i e s ` a c t i o n ` o n t h e m a i n t h r e a d o n c e t h e c l o c k r e a c h e s t h e n e x t b e a t , v i a a b a c k g r o u n d
// / w a i t r o u t i n e . W i t h n o c l o c k / e n g i n e , a p p l i e s i t s y n c h r o n o u s l y f o r u n - q u a n t i z e d t r a n s p o r t .
private func runQuantized ( _ action : QuantizedAction ) {
transportGeneration += 1
let generation : Int = transportGeneration
@@ -459,7 +632,7 @@ public final class LooperPedal: Pedal {
}
// / R u n s t h e d e f e r r e d t r a n s p o r t t r a n s i t i o n ( o n t h e m a i n t h r e a d ) , u n l e s s i t s g e n e r a t i o n h a s
// / b e e n s u p e r s e d e d b y a n e w e r a r m o r a r e s e t .
// / b e e n s u p e r s e d e d b y a n e w e r a r m , a s l o t s w i t c h , o r a r e s e t .
private func applyQuantizedTransport ( _ action : QuantizedAction , generation : Int , at beatSample : SampleTime ? ) {
guard generation = = transportGeneration else { return }
switch action {
@@ -469,16 +642,17 @@ public final class LooperPedal: Pedal {
}
func beginRecording ( at beatSample : SampleTime ? ) {
guard phase = = . armedRecord else { return }
guard phase = = . armedRecord , primaryRecorders . indices . contains ( activeSlot ) else { return }
recordStartSample = beatSample ? ? ( hostEngine ? . currentTime ? ? 0 )
primaryRecorder ? . run = 1.0
primaryRecorders [ activeSlot ] . run = 1.0
phase = . recording
}
func finishRecordingAndPlay ( at beatSample : SampleTime ? ) {
guard phase = = . armedStop ,
let primary : RecordBuffer . Audio = primaryRecorder ,
let buffer : LyrebirdBuffer = loopBuffer else { return }
guard phase = = . armedStop , primaryRecorders . indices . contains ( activeSlot ) ,
buffers . indices . contains ( activeSlot ) else { return }
let primary : RecordBuffer . Audio = primaryRecorders [ activeSlot ]
let buffer : LyrebirdBuffer = buffers [ activeSlot ]
primary . run = 0.0
let closeSample : SampleTime = beatSample ? ? ( hostEngine ? . currentTime ? ? 0 )
@@ -486,16 +660,16 @@ public final class LooperPedal: Pedal {
// B e a t - q u a n t i z e d : l o o p l e n g t h i s t h e e x a c t w h o l e - b e a t s p a n b e t w e e n t h e t w o b e a t
// b o u n d a r i e s , i n d e p e n d e n t o f a n y r e c o r d i n g - p o s i t i o n s n a p s h o t s l o p .
let spanFrames : Int = Int ( closeSample - recordStartSample )
loopFrames = max ( LooperPedal . minimumLoopFrames , spanFrames )
loopBeats = beatsForSpan ( frames : loopFrames , clock : clock )
loopFrames [ activeSlot ] = max ( LooperPedal . minimumLoopFrames , spanFrames )
loopBeats [ activeSlot ] = beatsForSpan ( frames : loopFrames [ activeSlot ] , clock : clock )
} else {
// U n - q u a n t i z e d f a l l b a c k : t r i m t o t h e r e c o r d e r ' s f r a m e c o u n t .
loopFrames = max ( LooperPedal . minimumLoopFrames , primary . recordedFrameCount )
loopBeats = 0.0
loopFrames [ activeSlot ] = max ( LooperPedal . minimumLoopFrames , primary . recordedFrameCount )
loopBeats [ activeSlot ] = 0.0
}
buffer . setLoopLength ( loopFrames )
playStartSample = closeSample
buffer . setLoopLength ( loopFrames [ activeSlot ] )
playStartSample [ activeSlot ] = closeSample
playTrigger = 1.0
isPlaying = 1.0
phase = . playing