-- Copyright 2019 Evan Laforge
-- This program is distributed under the terms of the GNU General Public
-- License 3.0, see COPYING or http://www.gnu.org/licenses/gpl-3.0.txt

module Synth.Sampler.Patch.Metronome (patches) where
import qualified Data.Map as Map
import qualified Data.Set as Set

import qualified Util.Num as Num
import qualified Util.Lists as Lists
import qualified Cmd.Instrument.ImInst as ImInst
import qualified Perform.Im.Patch as Im.Patch
import qualified Perform.NN as NN
import qualified Perform.Pitch as Pitch

import qualified Synth.Sampler.Patch as Patch
import qualified Synth.Sampler.Patch.Lib.Util as Util
import qualified Synth.Sampler.Sample as Sample
import qualified Synth.Shared.Control as Control
import qualified Synth.Shared.Note as Note
import qualified Synth.Shared.Signal as Signal

import           Global


patches :: [Patch.Patch]
patches :: [Patch]
patches = (forall a. a -> [a] -> [a]
:[]) forall a b. (a -> b) -> a -> b
$ (Error -> Patch
Patch.patch Error
"metronome")
    { _dir :: FilePath
Patch._dir = FilePath
"metronome"
    , _convert :: Note -> ConvertM Sample
Patch._convert = Note -> ConvertM Sample
convert
    , _allFilenames :: Set FilePath
Patch._allFilenames = forall a. Ord a => [a] -> Set a
Set.fromList forall a b. (a -> b) -> a -> b
$ forall k a. Map k a -> [a]
Map.elems Map NoteNumber FilePath
nnToSample
    , _karyaPatch :: Patch
Patch._karyaPatch = Patch -> Patch
ImInst.make_patch forall a b. (a -> b) -> a -> b
$ Patch
Im.Patch.patch
        { patch_controls :: Map Control Error
Im.Patch.patch_controls = forall a. Monoid a => [a] -> a
mconcat
            [ Map Control Error
Control.supportPitch
            , Map Control Error
Control.supportDyn
            ]
        }
    }

convert :: Note.Note -> Patch.ConvertM Sample.Sample
convert :: Note -> ConvertM Sample
convert Note
note = do
    NoteNumber
pitch <- forall (m :: * -> *). MonadError Error m => Note -> m NoteNumber
Util.initialPitch Note
note
    let (FilePath
fname, Y
ratio) = forall a. Map NoteNumber a -> NoteNumber -> (a, Y)
Util.findPitchRatio Map NoteNumber FilePath
nnToSample NoteNumber
pitch
    let dynVal :: Y
dynVal = Control -> Note -> Y
Note.initial0 Control
Control.dynamic Note
note
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ (FilePath -> Sample
Sample.make FilePath
fname)
        { envelope :: Signal
Sample.envelope = forall {k} (kind :: k). Y -> Signal kind
Signal.constant (Y
dynVal forall a. Num a => a -> a -> a
+ Y
0.35)
        , ratios :: Signal
Sample.ratios = forall {k} (kind :: k). Y -> Signal kind
Signal.constant Y
ratio
        }

nnToSample :: Map Pitch.NoteNumber FilePath
nnToSample :: Map NoteNumber FilePath
nnToSample = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall a b. (a -> b) -> a -> b
$
    forall a b. [a] -> [b] -> [(a, b)]
zip (forall a. Int -> [a] -> [a]
take Int
13 forall a b. (a -> b) -> a -> b
$ forall a. Num a => a -> a -> [a]
Lists.range_ NoteNumber
NN.c3 NoteNumber
3)
        (forall a b. (a -> b) -> [a] -> [b]
map (\Integer
i -> FilePath
"s-" forall a. Semigroup a => a -> a -> a
<> Error -> FilePath
untxt (forall a. Show a => Int -> a -> Error
Num.zeroPad Int
3 Integer
i) forall a. Semigroup a => a -> a -> a
<> FilePath
".flac") [Integer
1..])