46239a1d7d525335e0aa0aab7b87664cecd56d0b
[ghc-hetmet.git] / ghc / tests / programs / dmgob_native2 / Main.lhs
1 \begin{verbatim}
2
3 > module Main(main) where
4
5 >#ifndef __GLASGOW_HASKELL__
6 > import Trace
7 > import Maybe   -- an hbc library module defining the ``Maybe'' type
8 >#endif
9
10 > import Native  -- an hbc library module for native-mode binary IO
11
12 > import LPA     -- the linear predictive analysis module
13
14
15 > main = getContents    >>= \bs ->
16 >        putStr (program bs)
17 > {- 1.2
18 > main = readChan stdin exit                            $ \bs ->
19 >        appendChan stdout (program bs) exit done
20 > -}
21 > {- ORIGINAL: partain:
22 > main = getArgs exit                                   $ \args ->
23 >        case args of
24 >        [file1, file2] -> readFile file1 exit          $ \bs ->
25 >                          writeFile file2 (program bs) exit done
26 >        _              -> error usage
27 > -}
28
29 > usage = "usage: lpa  <speech file>  <output file>"
30
31
32 > window_width      = 384 :: Int  -- 24 ms @ 16 kHz
33 > window_offset     = 160 :: Int  -- 10 ms @ 16 kHz
34 > p                 =  14 :: Int  -- LP analysis order
35 > q                 =  16 :: Int  -- cepstral analysis order
36
37
38 > readRawSpeech :: Bytes -> Signal Int
39 > readRawSpeech bs =
40 >       case bytesToShortInt bs of
41 >       Nothing      -> if null bs then [] else error read_error
42 >       Just (v,bs') -> v : readRawSpeech bs'
43
44 > read_error = "Left-over byte encountered by readRawSpeech"
45
46
47 > castSignal :: Signal Int -> Signal Float
48 > castSignal = map fromInt
49
50
51 > program :: Bytes -> String
52 > {-TEST:
53 > program bs
54 >  = let signal = readRawSpeech bs in
55 >    -- trace (shows (take 200 signal) "\n\n") (
56 >    ((foldr writesFrame []
57 >         . map (analyze p q)
58 >         . windows window_width window_offset
59 >         . preemph 0.95
60 >         . castSignal) signal)
61 >    -- )
62 > -}
63 >
64 > program = foldr writesFrame []
65 >         . map (analyze p q)
66 >         . windows window_width window_offset
67 >         . preemph 0.95
68 >         . castSignal
69 >         . readRawSpeech
70
71 \end{verbatim}
72
73         It only remains to define a function for writing the analysis
74 parameters to a file.
75         \begin{verbatim}
76
77 > writesFrame :: (Float, [Float]) -> Bytes -> Bytes
78 > writesFrame (log_energy, cep) bs =
79 >       showBytes log_energy (listShowBytes cep bs)
80
81 \end{verbatim}
82