-- 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

-- | Split some types out of Audio, to reduce dependencies.
module Util.Audio.AudioT where
import qualified Util.Serialize as Serialize

import           Global


-- | Should be >=0.
newtype Frames = Frames Int
    deriving (Int -> Frames -> ShowS
[Frames] -> ShowS
Frames -> String
(Int -> Frames -> ShowS)
-> (Frames -> String) -> ([Frames] -> ShowS) -> Show Frames
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Frames] -> ShowS
$cshowList :: [Frames] -> ShowS
show :: Frames -> String
$cshow :: Frames -> String
showsPrec :: Int -> Frames -> ShowS
$cshowsPrec :: Int -> Frames -> ShowS
Show, Frames -> Frames -> Bool
(Frames -> Frames -> Bool)
-> (Frames -> Frames -> Bool) -> Eq Frames
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Frames -> Frames -> Bool
$c/= :: Frames -> Frames -> Bool
== :: Frames -> Frames -> Bool
$c== :: Frames -> Frames -> Bool
Eq, Eq Frames
Eq Frames
-> (Frames -> Frames -> Ordering)
-> (Frames -> Frames -> Bool)
-> (Frames -> Frames -> Bool)
-> (Frames -> Frames -> Bool)
-> (Frames -> Frames -> Bool)
-> (Frames -> Frames -> Frames)
-> (Frames -> Frames -> Frames)
-> Ord Frames
Frames -> Frames -> Bool
Frames -> Frames -> Ordering
Frames -> Frames -> Frames
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Frames -> Frames -> Frames
$cmin :: Frames -> Frames -> Frames
max :: Frames -> Frames -> Frames
$cmax :: Frames -> Frames -> Frames
>= :: Frames -> Frames -> Bool
$c>= :: Frames -> Frames -> Bool
> :: Frames -> Frames -> Bool
$c> :: Frames -> Frames -> Bool
<= :: Frames -> Frames -> Bool
$c<= :: Frames -> Frames -> Bool
< :: Frames -> Frames -> Bool
$c< :: Frames -> Frames -> Bool
compare :: Frames -> Frames -> Ordering
$ccompare :: Frames -> Frames -> Ordering
Ord, Integer -> Frames
Frames -> Frames
Frames -> Frames -> Frames
(Frames -> Frames -> Frames)
-> (Frames -> Frames -> Frames)
-> (Frames -> Frames -> Frames)
-> (Frames -> Frames)
-> (Frames -> Frames)
-> (Frames -> Frames)
-> (Integer -> Frames)
-> Num Frames
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> Frames
$cfromInteger :: Integer -> Frames
signum :: Frames -> Frames
$csignum :: Frames -> Frames
abs :: Frames -> Frames
$cabs :: Frames -> Frames
negate :: Frames -> Frames
$cnegate :: Frames -> Frames
* :: Frames -> Frames -> Frames
$c* :: Frames -> Frames -> Frames
- :: Frames -> Frames -> Frames
$c- :: Frames -> Frames -> Frames
+ :: Frames -> Frames -> Frames
$c+ :: Frames -> Frames -> Frames
Num, Num Frames
Ord Frames
Num Frames -> Ord Frames -> (Frames -> Rational) -> Real Frames
Frames -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: Frames -> Rational
$ctoRational :: Frames -> Rational
Real, Int -> Frames
Frames -> Int
Frames -> [Frames]
Frames -> Frames
Frames -> Frames -> [Frames]
Frames -> Frames -> Frames -> [Frames]
(Frames -> Frames)
-> (Frames -> Frames)
-> (Int -> Frames)
-> (Frames -> Int)
-> (Frames -> [Frames])
-> (Frames -> Frames -> [Frames])
-> (Frames -> Frames -> [Frames])
-> (Frames -> Frames -> Frames -> [Frames])
-> Enum Frames
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Frames -> Frames -> Frames -> [Frames]
$cenumFromThenTo :: Frames -> Frames -> Frames -> [Frames]
enumFromTo :: Frames -> Frames -> [Frames]
$cenumFromTo :: Frames -> Frames -> [Frames]
enumFromThen :: Frames -> Frames -> [Frames]
$cenumFromThen :: Frames -> Frames -> [Frames]
enumFrom :: Frames -> [Frames]
$cenumFrom :: Frames -> [Frames]
fromEnum :: Frames -> Int
$cfromEnum :: Frames -> Int
toEnum :: Int -> Frames
$ctoEnum :: Int -> Frames
pred :: Frames -> Frames
$cpred :: Frames -> Frames
succ :: Frames -> Frames
$csucc :: Frames -> Frames
Enum, Enum Frames
Real Frames
Real Frames
-> Enum Frames
-> (Frames -> Frames -> Frames)
-> (Frames -> Frames -> Frames)
-> (Frames -> Frames -> Frames)
-> (Frames -> Frames -> Frames)
-> (Frames -> Frames -> (Frames, Frames))
-> (Frames -> Frames -> (Frames, Frames))
-> (Frames -> Integer)
-> Integral Frames
Frames -> Integer
Frames -> Frames -> (Frames, Frames)
Frames -> Frames -> Frames
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: Frames -> Integer
$ctoInteger :: Frames -> Integer
divMod :: Frames -> Frames -> (Frames, Frames)
$cdivMod :: Frames -> Frames -> (Frames, Frames)
quotRem :: Frames -> Frames -> (Frames, Frames)
$cquotRem :: Frames -> Frames -> (Frames, Frames)
mod :: Frames -> Frames -> Frames
$cmod :: Frames -> Frames -> Frames
div :: Frames -> Frames -> Frames
$cdiv :: Frames -> Frames -> Frames
rem :: Frames -> Frames -> Frames
$crem :: Frames -> Frames -> Frames
quot :: Frames -> Frames -> Frames
$cquot :: Frames -> Frames -> Frames
Integral, Get Frames
Putter Frames
Putter Frames -> Get Frames -> Serialize Frames
forall a. Putter a -> Get a -> Serialize a
get :: Get Frames
$cget :: Get Frames
put :: Putter Frames
$cput :: Putter Frames
Serialize.Serialize)

instance Pretty Frames where
    pretty :: Frames -> Text
pretty (Frames Int
n) = Int -> Text
forall a. Pretty a => a -> Text
pretty Int
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"f"

type Rate = Int
type Seconds = Double

framesToSeconds :: Rate -> Frames -> Seconds
framesToSeconds :: Int -> Frames -> Seconds
framesToSeconds Int
rate (Frames Int
frames) = Int -> Seconds
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
frames Seconds -> Seconds -> Seconds
forall a. Fractional a => a -> a -> a
/ Int -> Seconds
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
rate