-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More modularization of data processing
- Loading branch information
1 parent
8ae7d0c
commit 165667d
Showing
7 changed files
with
278 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE BangPatterns #-} | ||
module VaporTrail.Codec.Bits (Endianness(..), bits, bitsLE, bitsBE) where | ||
|
||
import Control.Monad | ||
import Data.Bits | ||
import Data.Machine | ||
import VaporTrail.Codec.Type | ||
|
||
data Endianness | ||
= LittleEndian | ||
| BigEndian | ||
deriving (Eq, Read, Show, Enum, Ord) | ||
|
||
bitIndex :: Endianness -> Int -> Int -> Int | ||
bitIndex LittleEndian _ = \(!n) -> n | ||
bitIndex BigEndian !numBits = \(!n) -> numBits - 1 - n | ||
|
||
toBits :: forall b. FiniteBits b => Endianness -> Process b Bool | ||
toBits !endian = | ||
let numBits = finiteBitSize (zeroBits :: b) | ||
yieldBit !x !n = yield (testBit x (bitIndex endian numBits n)) | ||
in construct . forever $ do | ||
x <- await | ||
forM_ [0 .. numBits - 1] (yieldBit x) | ||
|
||
fromBits :: forall b. FiniteBits b => Endianness -> Process Bool b | ||
fromBits !endian = | ||
let numBits = finiteBitSize (zeroBits :: b) | ||
putBit !x !n True = setBit x (bitIndex endian numBits n) | ||
putBit !x _ False = x | ||
awaitBit !x !n = do | ||
b <- await | ||
return (putBit x n b) | ||
in construct . forever $ do | ||
x <- foldM awaitBit zeroBits [0 .. numBits - 1] | ||
yield x | ||
|
||
bits :: FiniteBits b => Endianness -> Codec b Bool | ||
bits !endian = Codec {codecEnc = toBits endian, codecDec = fromBits endian} | ||
|
||
bitsBE :: FiniteBits b => Codec b Bool | ||
bitsBE = bits BigEndian | ||
|
||
bitsLE :: FiniteBits b => Codec b Bool | ||
bitsLE = bits LittleEndian |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module VaporTrail.Codec.PCM (pcms16le) where | ||
|
||
import Control.Monad | ||
import Data.Bits | ||
import Data.Int | ||
import Data.Machine | ||
import Data.Word | ||
import VaporTrail.Codec.Type | ||
|
||
clip :: Float -> Float | ||
clip x = min 1 (max (-1) x) | ||
|
||
pcms16le :: Codec Float Word8 | ||
pcms16le = | ||
Codec | ||
{ codecEnc = | ||
let toInt16 :: Float -> Int16 | ||
toInt16 x = | ||
if x < 0 | ||
then floor (32768 * clip x) | ||
else floor (32767 * clip x) | ||
in construct . forever $ do | ||
x <- fmap toInt16 await | ||
let lo = fromIntegral (x .&. 0xFF) | ||
hi = fromIntegral (shiftR x 8 .&. 0xFF) | ||
yield lo | ||
yield hi | ||
, codecDec = | ||
let fromInt16 :: Int16 -> Float | ||
fromInt16 x = | ||
if x < 0 | ||
then fromIntegral x / 32768 | ||
else fromIntegral x / 32767 | ||
in construct . forever $ do | ||
l <- await | ||
h <- await | ||
let lo = fromIntegral l :: Int16 | ||
hi = fromIntegral h :: Int16 | ||
x = lo .|. shiftL hi 8 | ||
yield (fromInt16 x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{-# LANGUAGE RankNTypes #-} | ||
module VaporTrail.Codec.Type | ||
( Codec(..) | ||
) where | ||
|
||
import Data.Machine | ||
|
||
data Codec a b = Codec | ||
{ codecEnc :: Process a b | ||
, codecDec :: Process b a | ||
} | ||
|
Oops, something went wrong.