module Csound.Typed.Opcode.Miscellaneous (
    
    
    
    fareylen, fareyleni, modmatrix, pwd, system_i, system, tableshuffle, tableshufflei) where

import Control.Applicative
import Control.Monad.Trans.Class
import Csound.Dynamic
import Csound.Typed

-- 

-- | 
-- returns the length of a Farey Sequence.
--
-- This opcode can be used in conjunction with GENfarey.
-- It calculates the length of Farey Sequence Fn. Its length is given by:
-- |Fn| = 1 + SUM over n phi(m) 
-- where phi(m) is Euler's totient function, which gives the number of integers ≤ m that are coprime to m.
--
-- > kfl  fareylen  kfn
--
-- csound doc: <http://www.csounds.com/manual/html/fareylen.html>
fareylen ::  Tab -> Sig
fareylen b1 = Sig $ f <$> unTab b1
    where f a1 = opcs "fareylen" [(Kr,[Kr])] [a1]

-- | 
-- returns the length of a Farey Sequence.
--
-- This opcode can be used in conjunction with GENfarey.
-- It calculates the length of Farey Sequence Fn. Its length is given by:
-- |Fn| = 1 + SUM over n phi(m) 
-- where phi(m) is Euler's totient function, which gives the number of integers ≤ m that are coprime to m.
--
-- > ifl  fareyleni  ifn
--
-- csound doc: <http://www.csounds.com/manual/html/fareyleni.html>
fareyleni ::  Tab -> D
fareyleni b1 = D $ f <$> unTab b1
    where f a1 = opcs "fareyleni" [(Ir,[Ir])] [a1]

-- | 
-- Modulation matrix opcode with optimizations for sparse matrices.
--
-- The opcode can be used to let a large number of k-rate modulator
--       variables modulate a large number of k-rate parameter variables,
--       with arbitrary scaling of each modulator-to-parameter
--       connection.  Csound ftables are used to hold both the input
--       (parameter)  variables, the modulator variables, and the scaling
--       coefficients. Output variables are written to another Csound ftable.
--
-- >  modmatrix  iresfn, isrcmodfn, isrcparmfn, imodscale, inum_mod, \\
-- >     inum_parm, kupdate
--
-- csound doc: <http://www.csounds.com/manual/html/modmatrix.html>
modmatrix ::  Tab -> Tab -> Tab -> D -> D -> D -> Sig -> SE ()
modmatrix b1 b2 b3 b4 b5 b6 b7 = SE $ (depT_ =<<) $ lift $ f <$> unTab b1 <*> unTab b2 <*> unTab b3 <*> unD b4 <*> unD b5 <*> unD b6 <*> unSig b7
    where f a1 a2 a3 a4 a5 a6 a7 = opcs "modmatrix" [(Xr,[Ir,Ir,Ir,Ir,Ir,Ir,Kr])] [a1
                                                                                  ,a2
                                                                                  ,a3
                                                                                  ,a4
                                                                                  ,a5
                                                                                  ,a6
                                                                                  ,a7]

-- | 
-- Asks the underlying operating system for the current directory
--       name as a string.
--
-- pwd call the operating system to determine
--       the current directory (folder).  pwd runs
--       at i-time only.
--
-- > Sres  pwd  
--
-- csound doc: <http://www.csounds.com/manual/html/pwd.html>
pwd ::   Str
pwd  = Str $ return $ f 
    where f  = opcs "pwd" [(Sr,[])] []

-- | 
-- Call an external program via the system call
--
-- system and system_i call
--     any external command understood by the operating system, similarly
--     to the C function     system(). system_i runs
--     at i-time only, while 
--       system runs both at initialization and
--       performance time.
--
-- > ires  system_i  itrig, Scmd, [inowait]
--
-- csound doc: <http://www.csounds.com/manual/html/system.html>
system_i ::  D -> Str -> D
system_i b1 b2 = D $ f <$> unD b1 <*> unStr b2
    where f a1 a2 = opcs "system_i" [(Ir,[Ir,Sr,Ir])] [a1,a2]

-- | 
-- Call an external program via the system call
--
-- system and system_i call
--     any external command understood by the operating system, similarly
--     to the C function     system(). system_i runs
--     at i-time only, while 
--       system runs both at initialization and
--       performance time.
--
-- > kres  system  ktrig, Scmd, [knowait]
--
-- csound doc: <http://www.csounds.com/manual/html/system.html>
system ::  Sig -> Str -> Sig
system b1 b2 = Sig $ f <$> unSig b1 <*> unStr b2
    where f a1 a2 = opcs "system" [(Kr,[Kr,Sr,Kr])] [a1,a2]

-- | 
-- shuffles the content of a function table so that each element of the source
--       table is put into a different random position.
--
-- This opcode can be used in order to shuffle the content of
--       function tables into a random order but without loosing any of
--       the elements. Imagine shuffling a deck of cards. Each element of
--       the table is copied to a different random position. If that
--       position was already chosen before then the next free position
--       is chosen. The length of the table remains the same.
--
-- >  tableshuffle  ktablenum
--
-- csound doc: <http://www.csounds.com/manual/html/tableshuffle.html>
tableshuffle ::  Sig -> SE ()
tableshuffle b1 = SE $ (depT_ =<<) $ lift $ f <$> unSig b1
    where f a1 = opcs "tableshuffle" [(Xr,[Kr])] [a1]

-- | 
-- shuffles the content of a function table so that each element of the source
--       table is put into a different random position.
--
-- This opcode can be used in order to shuffle the content of
--       function tables into a random order but without loosing any of
--       the elements. Imagine shuffling a deck of cards. Each element of
--       the table is copied to a different random position. If that
--       position was already chosen before then the next free position
--       is chosen. The length of the table remains the same.
--
-- >  tableshufflei  itablenum
--
-- csound doc: <http://www.csounds.com/manual/html/tableshuffle.html>
tableshufflei ::  D -> SE ()
tableshufflei b1 = SE $ (depT_ =<<) $ lift $ f <$> unD b1
    where f a1 = opcs "tableshufflei" [(Xr,[Ir])] [a1]