packages feed

synthesizer-llvm-0.5: src/Synthesizer/LLVM/Server/SampledSound.hs

module Synthesizer.LLVM.Server.SampledSound where

import Synthesizer.LLVM.Server.Common (Real, )

import qualified Sound.Sox.Read          as SoxRead
import qualified Sound.Sox.Option.Format as SoxOption
import Control.Exception (bracket, )

import qualified Synthesizer.Storable.Signal      as SigSt
import qualified Data.StorableVector.Lazy         as SVL

import Data.Tuple.HT (mapPair, )

import Prelude hiding (Real, length, )



data T =
   Cons {
      body :: SigSt.T Real,
      positions :: Positions,
      period :: Real
   }

data Positions =
   Positions {
      start, length,
      loopStart, loopLength :: Int
   }


load :: FilePath -> IO (SVL.Vector Real)
load path =
   bracket (SoxRead.open SoxOption.none path) SoxRead.close $
   SoxRead.withHandle1 (SVL.hGetContentsSync SVL.defaultChunkSize)


type Info = (FilePath, [Positions], Real)


parts :: T -> (SigSt.T Real, SigSt.T Real, SigSt.T Real)
parts smp =
   let pos = positions smp
       (attack,sustain) =
          mapPair
             (SigSt.drop (start pos),
              SigSt.take (loopLength pos)) $
          SigSt.splitAt (loopStart pos) $
          body smp
       release =
          SigSt.drop (loopStart pos + loopLength pos) $
          SigSt.take (start     pos + length     pos) $
          body smp
   in  (attack, sustain, release)



-- * examples

tomatensalatPositions :: [Positions]
tomatensalatPositions =
   Positions      0 29499  12501 15073 :
   Positions  29499 31672  38163 17312 :
   Positions  67379 28610  81811 10667 :
   Positions  95989 31253 106058 16111 :
   Positions 127242 38596 136689 11514 :
   []

tomatensalat :: Info
tomatensalat =
   ("tomatensalat2.wav", tomatensalatPositions, 324.5)


halPositions :: [Positions]
halPositions =
--   Positions   2371 25957   7362  6321 :
   Positions   2371 25957 (2371+25957) 1 :
   Positions  40546 34460  63540  9546 :
   Positions  79128 32348  94367 14016 :
   Positions 112027 21227 125880  5500 :
   Positions 146057 23235 168941   352 :
   []

hal :: Info
hal =
   ("haskell-in-leipzig2.wav", halPositions, 316)


graphentheoriePositions :: [Positions]
graphentheoriePositions =
   Positions      0 29524  13267 14768 :
   Positions  29524 35333  47624  9968 :
   Positions  64857 31189  73818 16408 :
   Positions  96046 31312 106206 18504 :
   Positions 127358 32127 132469 16530 :
   []

graphentheorie :: Info
graphentheorie =
   ("graphentheorie0.wav", graphentheoriePositions, 301.15)