module Synthesizer.State.ToneModulation (
Cell,
makeCell,
interpolateCell,
Prototype,
makePrototype,
sampledToneCell,
oscillatorCells,
checkNonNeg,
oscillatorCoords,
limitRelativeShapes,
limitMinRelativeValues,
) where
import qualified Synthesizer.Basic.ToneModulation as ToneMod
import qualified Synthesizer.Causal.Oscillator.Core as Osci
import qualified Synthesizer.Causal.Process as Causal
import qualified Synthesizer.Interpolation as Interpolation
import qualified Synthesizer.Generic.Signal as SigG
import qualified Synthesizer.State.Signal as SigS
import qualified Synthesizer.Basic.Phase as Phase
import qualified Algebra.RealField as RealField
import qualified Algebra.Additive as Additive
import Data.Ord.HT (limit, )
import NumericPrelude.Numeric
import NumericPrelude.Base
import Prelude ()
type Cell sig y = SigS.T (sig y)
interpolateCell ::
(SigG.Read sig y) =>
Interpolation.T a y ->
Interpolation.T b y ->
(a, b) ->
Cell sig y -> y
interpolateCell ipLeap ipStep (qLeap,qStep) =
Interpolation.func ipLeap qLeap .
SigS.map (Interpolation.func ipStep qStep . SigG.toState)
data Prototype sig a v =
Prototype {
protoMarginLeap,
protoMarginStep :: Interpolation.Margin,
protoIpOffset :: Int,
protoPeriod :: a,
protoPeriodInt :: Int,
protoShapeLimits :: (a,a),
protoSignal :: sig v
}
makePrototype ::
(RealField.C a, SigG.Read sig v) =>
Interpolation.Margin ->
Interpolation.Margin ->
a -> sig v -> Prototype sig a v
makePrototype marginLeap marginStep period tone =
let periodInt = round period
ipOffset =
ToneMod.interpolationOffset marginLeap marginStep periodInt
len = SigG.length tone
(lower,upper) =
ToneMod.shapeLimits marginLeap marginStep periodInt len
limits =
if lower > upper
then error "min>max"
else (fromIntegral lower, fromIntegral upper)
in Prototype {
protoMarginLeap = marginLeap,
protoMarginStep = marginStep,
protoIpOffset = ipOffset,
protoPeriod = period,
protoPeriodInt = periodInt,
protoShapeLimits = limits,
protoSignal = tone
}
sampledToneCell ::
(RealField.C a, SigG.Transform sig v) =>
Prototype sig a v -> a -> Phase.T a -> ((a,a), Cell sig v)
sampledToneCell p shape phase =
let (n, q) =
ToneMod.flattenShapePhase (protoPeriodInt p) (protoPeriod p)
(limit (protoShapeLimits p) shape, phase)
in (q,
SigS.iterate (SigG.drop (protoPeriodInt p)) $
SigG.drop (n protoIpOffset p) $
protoSignal p)
oscillatorCells :: (RealField.C t, SigG.Transform sig y) =>
Interpolation.Margin ->
Interpolation.Margin ->
t -> sig y -> (t, SigS.T t) -> (Phase.T t, SigS.T t) ->
SigS.T ((t,t), Cell sig y)
oscillatorCells
marginLeap marginStep period sampledTone shapes freqs =
let periodInt = round period
margin =
ToneMod.interpolationNumber marginLeap marginStep periodInt
ipOffset =
ToneMod.interpolationOffset marginLeap marginStep periodInt
(skips,coords) =
SigS.unzip $
oscillatorCoords periodInt period
(limitRelativeShapes marginLeap marginStep periodInt shapes)
freqs
in SigS.zipWith
(\(k,q) (_n,ptr) ->
(q, makeCell periodInt $
SigG.drop (checkNonNeg $ periodInt+k) ptr))
coords $
SigS.switchL (error "list of pointers must not be empty") (flip const) $
SigS.scanL
(\ (n,ptr) d -> SigG.dropMarginRem margin (n+d) ptr)
(0, sampledTone)
(SigS.switchL skips
(\s -> SigS.cons (s (ipOffset + periodInt)))
skips)
checkNonNeg :: (Ord a, Additive.C a, Show a) => a -> a
checkNonNeg x =
if x<zero
then error ("unexpected negative number: " ++ show x)
else x
makeCell :: (SigG.Transform sig y) => Int -> sig y -> Cell sig y
makeCell periodInt =
SigS.takeWhile (not . SigG.null) .
SigS.iterate (SigG.drop periodInt)
oscillatorCoords :: (RealField.C t) =>
Int -> t ->
(t, SigS.T t) -> (Phase.T t, SigS.T t) ->
SigS.T (ToneMod.Coords t)
oscillatorCoords periodInt period
(shape0, shapes) (phase, freqs) =
let shapeOffsets =
SigS.scanL
(\(_,s) c -> splitFraction (s+c))
(splitFraction shape0) shapes
phases =
let Just (s,ss) =
SigS.viewL $
SigS.map (\(n,_) -> fromIntegral n / period) $
shapeOffsets
in Osci.freqMod
(Phase.decrement s phase)
`Causal.apply`
(SigS.zipWith () freqs ss)
in SigS.zipWith
(\(d,s) p -> (d, ToneMod.flattenShapePhase periodInt period (s,p)))
shapeOffsets
phases
limitRelativeShapes :: (RealField.C t) =>
Interpolation.Margin ->
Interpolation.Margin ->
Int -> (t, SigS.T t) -> (t, SigS.T t)
limitRelativeShapes marginLeap marginStep periodInt =
limitMinRelativeValues $ fromIntegral $
ToneMod.interpolationOffset marginLeap marginStep periodInt + periodInt
limitMinRelativeValues :: (Additive.C t, Ord t) =>
t -> (t, SigS.T t) -> (t, SigS.T t)
limitMinRelativeValues xMin (x0, xs) =
let x1 = xMinx0
in if x1<=zero
then (x0, xs)
else (xMin,
SigS.crochetL
(\x lim ->
let d = xlim
in Just $ if d>=zero
then (d,zero) else (zero, negate d)) x1 xs)