summaryrefslogtreecommitdiff
path: root/src/GF/System/ATKSpeechInput.hs
blob: 8c0ed303e5681289d8fd4904cb22fcf82c43a54f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
----------------------------------------------------------------------
-- |
-- Module      : GF.System.ATKSpeechInput
-- Maintainer  : BB
-- Stability   : (stable)
-- Portability : (non-portable)
--
-- > CVS $Date: 2005/05/10 15:04:01 $ 
-- > CVS $Author: bringert $
-- > CVS $Revision: 1.2 $
--
-- Use ATK and Speech.ATKRec for speech input.
-----------------------------------------------------------------------------

module GF.System.ATKSpeechInput (recognizeSpeech) where

import GF.Infra.Ident (Ident, prIdent)
import GF.Infra.Option (Options)
import GF.Conversion.Types (CGrammar)
import GF.Speech.PrSLF

import Speech.ATKRec

import Control.Monad
import Data.IORef
import System.IO
import System.IO.Unsafe

-- FIXME: get these from somewhere else

config = "/home/bjorn/projects/atkrec/atkrec.cfg"

res = "/home/bjorn/src/atk/Resources"
hmmlist = res ++ "/UK_SI_ZMFCC/hmmlistbg"
mmf0 = res ++ "/UK_SI_ZMFCC/WI4"
mmf1 = res ++ "/UK_SI_ZMFCC/BGHMM2"
dict = res ++ "/beep.dct"

initialized :: IORef Bool
initialized = unsafePerformIO $ newIORef False

initATK :: IO ()
initATK = do
       b <- readIORef initialized
       when (not b) $ do
                      hPutStrLn stderr "Initializing..."
                      initialize config
                      loadHMMSet "hmm_english" hmmlist mmf0 mmf1
                      loadDict "dict_english" dict
                      writeIORef initialized True

recognizeSpeech :: Ident -- ^ Grammar name
	        -> Options -> CGrammar -> IO String
recognizeSpeech name opts cfg = 
    do
    let slf = slfPrinter name opts cfg
        n = prIdent name
        slfName = "gram_" ++ n
        recName = "rec_english_" ++ n
    initATK
    loadGrammarString slfName slf
    createRecognizer recName "hmm_english" "dict_english" slfName
    hPutStrLn stderr "Listening..."
    s <- recognize recName
    return s