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