module Sound.OpenSoundControl.Cast (f32_i32, i32_f32,
f64_i64, i64_f64,
str_cstr, cstr_str,
str_pstr, pstr_str) where
import Control.Monad.ST
import Data.Array.ST
import Data.Char
import Data.Int
import Data.Word
f32_i32 :: Float -> Int32
f32_i32 d = runST ((from_array =<< castSTUArray =<< unit_array d) :: ST s Int32)
i32_f32 :: Int32 -> Float
i32_f32 d = runST ((from_array =<< castSTUArray =<< unit_array d) :: ST s Float)
f64_i64 :: Double -> Int64
f64_i64 d = runST ((from_array =<< castSTUArray =<< unit_array d) :: ST s Int64)
i64_f64 :: Int64 -> Double
i64_f64 d = runST ((from_array =<< castSTUArray =<< unit_array d) :: ST s Double)
str_cstr :: String -> [Word8]
str_cstr s = map (fromIntegral . ord) s ++ [0]
cstr_str :: [Word8] -> String
cstr_str s = map (chr . fromIntegral) (takeWhile (/= 0) s)
str_pstr :: String -> [Word8]
str_pstr s = (fromIntegral (length s)) : map (fromIntegral . ord) s
pstr_str :: [Word8] -> String
pstr_str s = map (chr . fromIntegral) (drop 1 s)
unit_array :: (MArray a e m) => e -> m (a Int e)
unit_array = newArray (0, 0::Int)
from_array :: (MArray a e m) => a Int e -> m e
from_array = flip readArray 0