module Sound.SC3.Common.Buffer where
import Data.List
import qualified Sound.SC3.Common.Math as S
blend :: Num a => a -> a -> a -> a
blend :: a -> a -> a -> a
blend a
z a
i a
j = a
i a -> a -> a
forall a. Num a => a -> a -> a
+ (a
z a -> a -> a
forall a. Num a => a -> a -> a
* (a
j a -> a -> a
forall a. Num a => a -> a -> a
- a
i))
clipAt :: Int -> [a] -> a
clipAt :: Int -> [a] -> a
clipAt Int
ix [a]
c = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 then [a] -> a
forall a. [a] -> a
last [a]
c else if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 then [a]
c [a] -> Int -> a
forall a. [a] -> Int -> a
!! Int
0 else [a]
c [a] -> Int -> a
forall a. [a] -> Int -> a
!! Int
ix
blendAtBy :: (Integral i,RealFrac n) => (i -> t -> n) -> n -> t -> n
blendAtBy :: (i -> t -> n) -> n -> t -> n
blendAtBy i -> t -> n
f n
ix t
c =
let m :: i
m = n -> i
forall a b. (RealFrac a, Integral b) => a -> b
floor n
ix
m' :: n
m' = i -> n
forall a b. (Integral a, Num b) => a -> b
fromIntegral i
m
in n -> n -> n -> n
forall a. Num a => a -> a -> a -> a
blend (n -> n -> n
forall a. Num a => a -> a -> a
S.absdif n
ix n
m') (i -> t -> n
f i
m t
c) (i -> t -> n
f (i
m i -> i -> i
forall a. Num a => a -> a -> a
+ i
1) t
c)
blendAt :: RealFrac a => a -> [a] -> a
blendAt :: a -> [a] -> a
blendAt = (Int -> [a] -> a) -> a -> [a] -> a
forall i n t.
(Integral i, RealFrac n) =>
(i -> t -> n) -> n -> t -> n
blendAtBy Int -> [a] -> a
forall a. Int -> [a] -> a
clipAt
resamp1_gen :: (Integral i,RealFrac n) => i -> i -> (i -> t -> n) -> t -> i -> n
resamp1_gen :: i -> i -> (i -> t -> n) -> t -> i -> n
resamp1_gen i
n i
r i -> t -> n
f t
c =
let n' :: n
n' = i -> n
forall a b. (Integral a, Num b) => a -> b
fromIntegral i
n
fwd :: n
fwd = (i -> n
forall a b. (Integral a, Num b) => a -> b
fromIntegral i
r n -> n -> n
forall a. Num a => a -> a -> a
- n
1) n -> n -> n
forall a. Fractional a => a -> a -> a
/ (n
n' n -> n -> n
forall a. Num a => a -> a -> a
- n
1)
gen :: a -> n
gen a
i = (i -> t -> n) -> n -> t -> n
forall i n t.
(Integral i, RealFrac n) =>
(i -> t -> n) -> n -> t -> n
blendAtBy i -> t -> n
f (a -> n
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
i n -> n -> n
forall a. Num a => a -> a -> a
* n
fwd) t
c
in i -> n
forall a. Integral a => a -> n
gen
resamp1 :: RealFrac n => Int -> [n] -> [n]
resamp1 :: Int -> [n] -> [n]
resamp1 Int
n [n]
c =
let gen :: Int -> n
gen = Int -> Int -> (Int -> [n] -> n) -> [n] -> Int -> n
forall i n t.
(Integral i, RealFrac n) =>
i -> i -> (i -> t -> n) -> t -> i -> n
resamp1_gen Int
n ([n] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [n]
c) Int -> [n] -> n
forall a. Int -> [a] -> a
clipAt [n]
c
in (Int -> n) -> [Int] -> [n]
forall a b. (a -> b) -> [a] -> [b]
map Int -> n
gen [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
normalizeSum :: (Fractional a) => [a] -> [a]
normalizeSum :: [a] -> [a]
normalizeSum [a]
l = let n :: a
n = [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [a]
l in (a -> a) -> [a] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
n) [a]
l
normalise_rng :: Fractional n => (n,n) -> (n,n) -> [n] -> [n]
normalise_rng :: (n, n) -> (n, n) -> [n] -> [n]
normalise_rng (n
il,n
ir) (n
l,n
r) = (n -> n) -> [n] -> [n]
forall a b. (a -> b) -> [a] -> [b]
map (\n
e -> n -> n -> n -> n -> n -> n
forall a. Fractional a => a -> a -> a -> a -> a -> a
S.sc3_linlin n
e n
il n
ir n
l n
r)
normalize :: (Fractional n, Ord n) => n -> n -> [n] -> [n]
normalize :: n -> n -> [n] -> [n]
normalize n
l n
r [n]
c = (n, n) -> (n, n) -> [n] -> [n]
forall n. Fractional n => (n, n) -> (n, n) -> [n] -> [n]
normalise_rng ([n] -> n
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [n]
c,[n] -> n
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [n]
c) (n
l,n
r) [n]
c
t2_window :: Integral i => i -> [t] -> [(t,t)]
t2_window :: i -> [t] -> [(t, t)]
t2_window i
n [t]
x =
case [t]
x of
t
i:t
j:[t]
_ -> (t
i,t
j) (t, t) -> [(t, t)] -> [(t, t)]
forall a. a -> [a] -> [a]
: i -> [t] -> [(t, t)]
forall i t. Integral i => i -> [t] -> [(t, t)]
t2_window i
n (i -> [t] -> [t]
forall i a. Integral i => i -> [a] -> [a]
genericDrop i
n [t]
x)
[t]
_ -> []
t2_adjacent :: [t] -> [(t,t)]
t2_adjacent :: [t] -> [(t, t)]
t2_adjacent = Int -> [t] -> [(t, t)]
forall i t. Integral i => i -> [t] -> [(t, t)]
t2_window (Int
2::Int)
t2_overlap :: [b] -> [(b,b)]
t2_overlap :: [b] -> [(b, b)]
t2_overlap [b]
x = [b] -> [b] -> [(b, b)]
forall a b. [a] -> [b] -> [(a, b)]
zip [b]
x ([b] -> [b]
forall a. [a] -> [a]
tail [b]
x)
t2_concat :: [(a,a)] -> [a]
t2_concat :: [(a, a)] -> [a]
t2_concat [(a, a)]
x =
case [(a, a)]
x of
[] -> []
(a
i,a
j):[(a, a)]
x' -> a
i a -> [a] -> [a]
forall a. a -> [a] -> [a]
: a
j a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [(a, a)] -> [a]
forall a. [(a, a)] -> [a]
t2_concat [(a, a)]
x'
from_wavetable :: Num n => [n] -> [n]
from_wavetable :: [n] -> [n]
from_wavetable = ((n, n) -> n) -> [(n, n)] -> [n]
forall a b. (a -> b) -> [a] -> [b]
map ((n -> n -> n) -> (n, n) -> n
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry n -> n -> n
forall a. Num a => a -> a -> a
(+)) ([(n, n)] -> [n]) -> ([n] -> [(n, n)]) -> [n] -> [n]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [n] -> [(n, n)]
forall t. [t] -> [(t, t)]
t2_adjacent
to_wavetable :: Num a => [a] -> [a]
to_wavetable :: [a] -> [a]
to_wavetable = [a] -> [a]
forall a. Num a => [a] -> [a]
to_wavetable_nowrap ([a] -> [a]) -> ([a] -> [a]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
0])
to_wavetable_nowrap :: Num a => [a] -> [a]
to_wavetable_nowrap :: [a] -> [a]
to_wavetable_nowrap =
let f :: (b, b) -> (b, b)
f (b
e0,b
e1) = (b
2 b -> b -> b
forall a. Num a => a -> a -> a
* b
e0 b -> b -> b
forall a. Num a => a -> a -> a
- b
e1,b
e1 b -> b -> b
forall a. Num a => a -> a -> a
- b
e0)
in [(a, a)] -> [a]
forall a. [(a, a)] -> [a]
t2_concat ([(a, a)] -> [a]) -> ([a] -> [(a, a)]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, a) -> (a, a)) -> [(a, a)] -> [(a, a)]
forall a b. (a -> b) -> [a] -> [b]
map (a, a) -> (a, a)
forall b. Num b => (b, b) -> (b, b)
f ([(a, a)] -> [(a, a)]) -> ([a] -> [(a, a)]) -> [a] -> [(a, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [(a, a)]
forall t. [t] -> [(t, t)]
t2_overlap
sineGen :: (Floating n,Enum n) => Int -> [n] -> [n] -> [[n]]
sineGen :: Int -> [n] -> [n] -> [[n]]
sineGen Int
n =
let incr :: n
incr = (n
2 n -> n -> n
forall a. Num a => a -> a -> a
* n
forall a. Floating a => a
pi) n -> n -> n
forall a. Fractional a => a -> a -> a
/ Int -> n
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
ph :: n -> [n]
ph n
partial = Int -> [n] -> [n]
forall a. Int -> [a] -> [a]
take Int
n [n
0,n
incr n -> n -> n
forall a. Num a => a -> a -> a
* n
partial ..]
f :: n -> n -> n -> [n]
f n
h n
amp n
iph = (n -> n) -> [n] -> [n]
forall a b. (a -> b) -> [a] -> [b]
map (\n
z -> n -> n
forall a. Floating a => a -> a
sin (n
z n -> n -> n
forall a. Num a => a -> a -> a
+ n
iph) n -> n -> n
forall a. Num a => a -> a -> a
* n
amp) (n -> [n]
ph n
h)
in (n -> n -> n -> [n]) -> [n] -> [n] -> [n] -> [[n]]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 n -> n -> n -> [n]
f [n
1..]
sineFill :: (Ord n,Floating n,Enum n) => Int -> [n] -> [n] -> [n]
sineFill :: Int -> [n] -> [n] -> [n]
sineFill Int
n [n]
amp [n]
iph =
let t :: [[n]]
t = Int -> [n] -> [n] -> [[n]]
forall n. (Floating n, Enum n) => Int -> [n] -> [n] -> [[n]]
sineGen Int
n [n]
amp [n]
iph
in n -> n -> [n] -> [n]
forall n. (Fractional n, Ord n) => n -> n -> [n] -> [n]
normalize (-n
1) n
1 (([n] -> n) -> [[n]] -> [n]
forall a b. (a -> b) -> [a] -> [b]
map [n] -> n
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([[n]] -> [[n]]
forall a. [[a]] -> [[a]]
transpose [[n]]
t))