{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE Rank2Types #-}
module Synthesizer.Dimensional.RateAmplitude.File (
   write,
   writeTimeVoltage,
   writeTimeVoltageMonoDoubleToInt16,
   writeTimeVoltageStereoDoubleToInt16,
   renderTimeVoltageMonoDoubleToInt16,
   renderTimeVoltageStereoDoubleToInt16,
  ) where

import qualified Sound.Sox.Write as Write
import qualified Sound.Sox.Option.Format as SoxOpt
import qualified Sound.Sox.Frame as Frame
import qualified Synthesizer.Basic.Binary as BinSmp
import qualified Data.StorableVector.Lazy.Builder as Builder
import Foreign.Storable (Storable, )

import qualified Synthesizer.Dimensional.Rate as Rate
import qualified Synthesizer.Dimensional.Amplitude as Amp

import qualified Synthesizer.Dimensional.Process as Proc
import qualified Synthesizer.Dimensional.Signal.Private as SigA

import qualified Synthesizer.Frame.Stereo as Stereo

import qualified Synthesizer.Storable.Signal as SigSt

import qualified Synthesizer.State.Signal as Sig

import qualified Algebra.ToInteger      as ToInteger
import qualified Algebra.Module         as Module
import qualified Algebra.RealField      as RealField
import qualified Algebra.Field          as Field

import qualified Algebra.DimensionTerm as Dim
import qualified Number.DimensionTerm  as DN


import System.Exit(ExitCode)

import NumericPrelude.Numeric
import NumericPrelude.Base



type Signal u t v y yv =
   SigA.T (Rate.Dimensional u t) (Amp.Dimensional v y) (Sig.T yv)

{- |
The output format is determined by SoX by the file name extension.
The sample precision is determined by the provided 'Builder.Builder' function.

Example:

> import qualified Data.StorableVector.Lazy.Builder as Builder
>
> write (DN.frequency one) (DN.voltage one) (\i -> Builder.put (i::Int16)) "test.aiff" sound
-}
{-# INLINE write #-}
write ::
    (Bounded int, ToInteger.C int, Storable int, Frame.C int, BinSmp.C yv,
     Dim.C u, RealField.C t,
     Dim.C v, Module.C y yv, Field.C y) =>
   DN.T (Dim.Recip u) t ->
   DN.T v y ->
   (int -> Builder.Builder int) ->
   FilePath ->
   Signal u t v y yv ->
   IO ExitCode
write :: forall int yv u t v y.
(Bounded int, C int, Storable int, C int, C yv, C u, C t, C v,
 C y yv, C y) =>
T (Recip u) t
-> T v y
-> (int -> Builder int)
-> FilePath
-> Signal u t v y yv
-> IO ExitCode
write T (Recip u) t
freqUnit T v y
amp int -> Builder int
put FilePath
name Signal u t v y yv
sig =
   let opts :: T
opts =
          Int -> T
SoxOpt.numberOfChannels forall a b. (a -> b) -> a -> b
$
          forall yv (sig :: * -> *). C yv => sig yv -> Int
BinSmp.numberOfSignalChannels forall a b. (a -> b) -> a -> b
$
          forall rate amplitude body. T rate amplitude body -> body
SigA.body Signal u t v y yv
sig
       sampleRate :: t
sampleRate =
          forall u a. (C u, C a) => T u a -> T u a -> a
DN.divToScalar (forall rate amp sig. T (Actual rate) amp sig -> rate
SigA.actualSampleRate Signal u t v y yv
sig) T (Recip u) t
freqUnit
   in  forall y (sig :: * -> *).
C y =>
(Handle -> sig y -> IO ())
-> T -> T -> FilePath -> Int -> sig y -> IO ExitCode
Write.extended forall a. Storable a => Handle -> Vector a -> IO ()
SigSt.hPut T
opts T
SoxOpt.none FilePath
name
          (forall a b. (C a, C b) => a -> b
round t
sampleRate)
          (forall a. Storable a => ChunkSize -> Builder a -> Vector a
Builder.toLazyStorableVector ChunkSize
SigSt.defaultChunkSize forall a b. (a -> b) -> a -> b
$
           forall m a. Monoid m => (a -> m) -> T a -> m
Sig.foldMap (forall a int out.
(C a, Bounded int, C int, Monoid out) =>
(int -> out) -> a -> out
BinSmp.outputFromCanonical int -> Builder int
put) forall a b. (a -> b) -> a -> b
$
           -- ToDo: flip DN.divToScalar -> ampToScalar
           forall y yv (sig :: * -> *) amp rate.
(C y yv, Transform sig yv) =>
(amp -> y) -> T rate (Numeric amp) (sig yv) -> sig yv
SigA.vectorSamples (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall u a. (C u, C a) => T u a -> T u a -> a
DN.divToScalar T v y
amp) Signal u t v y yv
sig)


{-# INLINE writeTimeVoltage #-}
writeTimeVoltage ::
    (Bounded int, ToInteger.C int, Storable int, Frame.C int, BinSmp.C yv,
     RealField.C t,
     Module.C y yv, Field.C y) =>
   (int -> Builder.Builder int) ->
   FilePath ->
   Signal Dim.Time t Dim.Voltage y yv ->
   IO ExitCode
writeTimeVoltage :: forall int yv t y.
(Bounded int, C int, Storable int, C int, C yv, C t, C y yv,
 C y) =>
(int -> Builder int)
-> FilePath -> Signal Time t Voltage y yv -> IO ExitCode
writeTimeVoltage =
   forall int yv u t v y.
(Bounded int, C int, Storable int, C int, C yv, C u, C t, C v,
 C y yv, C y) =>
T (Recip u) t
-> T v y
-> (int -> Builder int)
-> FilePath
-> Signal u t v y yv
-> IO ExitCode
write (forall a. a -> Frequency a
DN.frequency forall a. C a => a
one) (forall a. a -> Voltage a
DN.voltage forall a. C a => a
one)



{-# INLINE writeTimeVoltageMonoDoubleToInt16 #-}
writeTimeVoltageMonoDoubleToInt16 ::
   FilePath ->
   Signal Dim.Time Double Dim.Voltage Double Double ->
   IO ExitCode
writeTimeVoltageMonoDoubleToInt16 :: FilePath -> Signal Time Double Voltage Double Double -> IO ExitCode
writeTimeVoltageMonoDoubleToInt16 FilePath
name Signal Time Double Voltage Double Double
sig =
   let rate :: Double
rate = forall u a. C u => u -> T u a -> a
DN.toNumberWithDimension Frequency
Dim.frequency (forall rate amp sig. T (Actual rate) amp sig -> rate
SigA.actualSampleRate Signal Time Double Voltage Double Double
sig)
   in  forall y (sig :: * -> *).
C y =>
(Handle -> sig y -> IO ())
-> T -> FilePath -> Int -> sig y -> IO ExitCode
Write.simple forall a. Storable a => Handle -> Vector a -> IO ()
SigSt.hPut T
SoxOpt.none FilePath
name (forall a b. (C a, C b) => a -> b
round Double
rate)
          (forall a rate.
C a =>
T rate (Dimensional Voltage a) (T a) -> T Int16
SigA.toStorableInt16Mono Signal Time Double Voltage Double Double
sig)


{-# INLINE writeTimeVoltageStereoDoubleToInt16 #-}
writeTimeVoltageStereoDoubleToInt16 ::
   FilePath ->
   Signal Dim.Time Double Dim.Voltage Double (Stereo.T Double) ->
   IO ExitCode
writeTimeVoltageStereoDoubleToInt16 :: FilePath
-> Signal Time Double Voltage Double (T Double) -> IO ExitCode
writeTimeVoltageStereoDoubleToInt16 FilePath
name Signal Time Double Voltage Double (T Double)
sig =
   let rate :: Double
rate = forall u a. C u => u -> T u a -> a
DN.toNumberWithDimension Frequency
Dim.frequency (forall rate amp sig. T (Actual rate) amp sig -> rate
SigA.actualSampleRate Signal Time Double Voltage Double (T Double)
sig)
   in  forall y (sig :: * -> *).
C y =>
(Handle -> sig y -> IO ())
-> T -> FilePath -> Int -> sig y -> IO ExitCode
Write.simple forall a. Storable a => Handle -> Vector a -> IO ()
SigSt.hPut T
SoxOpt.none FilePath
name (forall a b. (C a, C b) => a -> b
round Double
rate)
          (forall a rate.
(C a a, C a) =>
T rate (Dimensional Voltage a) (T (T a)) -> T (T Int16)
SigA.toStorableInt16Stereo Signal Time Double Voltage Double (T Double)
sig)

{-# INLINE renderTimeVoltageMonoDoubleToInt16 #-}
renderTimeVoltageMonoDoubleToInt16 ::
   DN.T Dim.Frequency Double ->
   FilePath ->
   (forall s. Proc.T s Dim.Time Double (SigA.R s Dim.Voltage Double Double)) ->
   IO ExitCode
renderTimeVoltageMonoDoubleToInt16 :: T Frequency Double
-> FilePath
-> (forall s. T s Time Double (R s Voltage Double Double))
-> IO ExitCode
renderTimeVoltageMonoDoubleToInt16 T Frequency Double
rate FilePath
name forall s. T s Time Double (R s Voltage Double Double)
sig =
   FilePath -> Signal Time Double Voltage Double Double -> IO ExitCode
writeTimeVoltageMonoDoubleToInt16 FilePath
name (forall u t amp sig.
C u =>
T (Recip u) t
-> (forall s. T s u t (T (Phantom s) amp sig))
-> T (Dimensional u t) amp sig
SigA.render T Frequency Double
rate forall s. T s Time Double (R s Voltage Double Double)
sig)

{-# INLINE renderTimeVoltageStereoDoubleToInt16 #-}
renderTimeVoltageStereoDoubleToInt16 ::
   DN.T Dim.Frequency Double ->
   FilePath ->
   (forall s. Proc.T s Dim.Time Double (SigA.R s Dim.Voltage Double (Stereo.T Double))) ->
   IO ExitCode
renderTimeVoltageStereoDoubleToInt16 :: T Frequency Double
-> FilePath
-> (forall s. T s Time Double (R s Voltage Double (T Double)))
-> IO ExitCode
renderTimeVoltageStereoDoubleToInt16 T Frequency Double
rate FilePath
name forall s. T s Time Double (R s Voltage Double (T Double))
sig =
   FilePath
-> Signal Time Double Voltage Double (T Double) -> IO ExitCode
writeTimeVoltageStereoDoubleToInt16 FilePath
name (forall u t amp sig.
C u =>
T (Recip u) t
-> (forall s. T s u t (T (Phantom s) amp sig))
-> T (Dimensional u t) amp sig
SigA.render T Frequency Double
rate forall s. T s Time Double (R s Voltage Double (T Double))
sig)