module Diffs where import Data.Int (Int32, ) import Control.Arrow (arr, (<<<), (^<<), ) import qualified Synthesizer.Causal.Process as Causal import qualified Sound.SoxLib as SoxLib import qualified Data.StorableVector.Lazy as SVL import qualified Synthesizer.Storable.Signal as SigSt import qualified Synthesizer.Generic.Signal as Sig import qualified Synthesizer.Basic.Binary as Bin import qualified Synthesizer.Generic.Analysis as Ana import qualified Synthesizer.Generic.Cut as Cut import qualified Synthesizer.Generic.Fourier as Four import qualified Foreign.Storable as Stor import qualified Number.Complex as C import qualified Algebra.Additive as A import qualified Algebra.Transcendental as T import Graphics.Matplotlib import Driver import Types calcDiff :: IO () calcDiff = withAudio "out002.wav" $ \a' -> withAudio "out004.wav" $ \b' -> do let a = prepare a' b = prepare b' maxLen = min (Cut.length a) (Cut.length b) l = maxLen `div` 5 -- take first 20% sa = Cut.take l a sb = Cut.take l b fsa = fourTrafo $ padWithZeros sa fsb = fourTrafo $ padWithZeros sb --negb = Causal.apply (arr (*(-1))) sb --conjb = Causal.apply (arr conjugate) sb let corr = Four.transformBackward (Sig.zipWith (*) (Four.transformForward fsa) (Causal.apply (arr C.conjugate) $ Four.transformForward fsb)) print $ Cut.length sa print $ Cut.length sb print $ Cut.length corr let reals = (Causal.apply (arr $ C.real) corr) :: SVL.Vector Float imgs = (Causal.apply (arr $ C.imag) corr) :: SVL.Vector Float ys = SVL.unpack reals :: [Float] --zs = SVL.unpack imgs :: [Float] xs = [1..length ys] onscreen $ line xs ys --onscreen $ line xs zs prepare :: SVL.Vector Int32 -> SVL.Vector (C.T Float) prepare sig = head . map (Causal.apply (arr Bin.toCanonical)) . SVL.deinterleave 2 $ sig readFirst :: IO (SVL.Vector Int32) readFirst = withAudio "out003.wav" $ \sig -> do let s = Cut.take 100 sig return s --padWithZeros :: SVL.Vector (C.T Float) -> SVL.Vector (C.T Float) padWithZeros x = pad <> x <> pad where zeros = SVL.repeat SVL.defaultChunkSize 0 l = SVL.length x pad = SVL.take (l `div` 2) zeros sine :: SVL.Vector Float sine = SVL.pack SVL.defaultChunkSize $ map sin [0::Float,0.1..] plotVec :: SVL.Vector Float -> IO () plotVec v = let ys = SVL.unpack v xs = [1..length ys] in onscreen $ line xs ys fourTrafo :: (A.C a, T.C a, Stor.Storable a) => SVL.Vector a -> SVL.Vector a fourTrafo = Causal.apply (arr $ C.real) . Four.transformForward . Causal.apply (arr C.fromReal) four :: FilePath -> IO () four input = withAudio input $ \a' -> do let a = prepare a' sa = a r = Four.transformForward sa reals = (Causal.apply (arr $ C.real) r) :: SVL.Vector Float ys = SVL.unpack reals :: [Float] xs = [1..length ys] print $ SVL.length r onscreen $ line xs ys