예제 #1
0
        /// <summary>
        /// Returns the next Data object, which is the mel cepstrum of the input frame. However, it can also be other Data objects like DataStartSignal.
        /// </summary>
        /// <returns>
        /// The next available Data object, returns null if no Data object is available.
        /// </returns>
        /// <exception cref="System.Exception">
        /// IOException closing cepstrum stream
        /// or
        /// IOException reading from cepstrum stream
        /// </exception>
        public override IData GetData()
        {
            IData data;

            if (_curPoint == -1)
            {
                data = new DataStartSignal(_sampleRate);
                _curPoint++;
            }
            else if (_curPoint == _numPoints)
            {
                if (_numPoints > 0)
                {
                    _firstSampleNumber =
                        (_firstSampleNumber - _frameShift + _frameSize - 1);
                }
                // send a DataEndSignal
                var numberFrames = _curPoint / _cepstrumLength;
                var totalSamples = (numberFrames - 1) * _frameShift + _frameSize;
                var duration     = (long)
                                   ((totalSamples / (double)_sampleRate) * 1000.0);

                data = new DataEndSignal(duration);

                try {
                    if (_binary)
                    {
                        _binaryStream.Close();
                    }
                    else
                    {
                        _est.Close();
                    }
                    _curPoint++;
                } catch (IOException ioe) {
                    throw new Exception("IOException closing cepstrum stream", ioe);
                }
            }
            else if (_curPoint > _numPoints)
            {
                data = null;
            }
            else
            {
                var vectorData = new double[_cepstrumLength];

                for (var i = 0; i < _cepstrumLength; i++)
                {
                    try {
                        if (_binary)
                        {
                            if (_bigEndian)
                            {
                                vectorData[i] = _binaryStream.ReadFloat();
                            }
                            else
                            {
                                vectorData[i] = Utilities.ReadLittleEndianFloat(_binaryStream);
                            }
                        }
                        else
                        {
                            vectorData[i] = _est.GetFloat("cepstrum data");
                        }
                        _curPoint++;
                    } catch (IOException ioe) {
                        throw new Exception("IOException reading from cepstrum stream", ioe);
                    }
                }

                // System.out.println("Read: " + curPoint);
                data = new DoubleData
                           (vectorData, _sampleRate, _firstSampleNumber);
                _firstSampleNumber += _frameShift;
                // System.out.println(data);
            }
            return(data);
        }
예제 #2
0
        /// <summary>
        /// Creates the grammar.
        /// </summary>
        /// <returns>The initial node for the grammar.</returns>
        protected override GrammarNode CreateGrammar()
        {
            GrammarNode initialNode = null;
            GrammarNode finalNode   = null;

            // first pass create the FST nodes
            int maxNodeId = CreateNodes(_path);

            // create the final node:
            finalNode = CreateGrammarNode(++maxNodeId, IDictionary.SilenceSpelling);
            finalNode.SetFinalNode(true);

            // replace each word node with a pair of nodes, which
            // consists of the word node and a new dummy end node, which is
            // for adding null or backoff transitions
            maxNodeId = ExpandWordNodes(maxNodeId);

            ExtendedStreamTokenizer tok = new ExtendedStreamTokenizer(_path, true);

            // Second pass, add all of the arcs

            while (!tok.IsEOF())
            {
                String token;
                tok.Skipwhite();
                token = tok.GetString();

                // System.out.println(token);

                if (token == null)
                {
                    break;
                }
                else if (token.Equals("I"))
                {
                    Debug.Assert(initialNode == null);
                    int    initialID = tok.GetInt("initial ID");
                    String nodeName  = "G" + initialID;

                    // TODO: FlatLinguist requires the initial grammar node
                    // to contain a single silence. We'll do that for now,
                    // but once the FlatLinguist is fixed, this should be
                    // returned to its former method of creating an empty
                    // initial grammar node
                    //          initialNode = createGrammarNode(initialID, false);

                    initialNode = CreateGrammarNode(initialID, IDictionary.SilenceSpelling);
                    _nodes.Put(nodeName, initialNode);

                    // optionally add a silence node
                    if (_addInitialSilenceNode)
                    {
                        GrammarNode silenceNode = CreateGrammarNode(++maxNodeId, IDictionary.SilenceSpelling);
                        initialNode.Add(silenceNode, LogMath.LogOne);
                        silenceNode.Add(initialNode, LogMath.LogOne);
                    }
                }
                else if (token.Equals("T"))
                {
                    int thisID = tok.GetInt("this id");
                    int nextID = tok.GetInt("next id");

                    GrammarNode thisNode = Get(thisID);
                    GrammarNode nextNode = Get(nextID);

                    // if the source node is an FSTGrammarNode, we want
                    // to join the endNode to the destination node

                    if (HasEndNode(thisNode))
                    {
                        thisNode = GetEndNode(thisNode);
                    }

                    float  lnProb = 0f;       // negative natural log
                    String output = tok.GetString();

                    if (output == null || output.Equals(","))
                    {
                        // these are epsilon (meaning backoff) transitions

                        if (output != null && output.Equals(","))
                        {
                            tok.GetString(); // skip the word
                            lnProb = tok.GetFloat("probability");
                        }

                        // if the destination node has been expanded
                        // we actually want to add the backoff transition
                        // to the endNode

                        if (HasEndNode(nextNode))
                        {
                            nextNode = GetEndNode(nextNode);
                        }
                    }
                    else
                    {
                        String word = tok.GetString();     // skip words
                        lnProb = tok.GetFloat("probability");

                        if (_ignoreUnknownTransitions && word.Equals("<unknown>"))
                        {
                            continue;
                        }

                        /*
                         * System.out.println(nextNode + ": " + output);
                         */
                        Debug.Assert(HasWord(nextNode));
                    }

                    thisNode.Add(nextNode, ConvertProbability(lnProb));
                }
                else if (token.Equals("F"))
                {
                    int   thisID = tok.GetInt("this id");
                    float lnProb = tok.GetFloat("probability");

                    GrammarNode thisNode = Get(thisID);
                    GrammarNode nextNode = finalNode;

                    if (HasEndNode(thisNode))
                    {
                        thisNode = GetEndNode(thisNode);
                    }

                    thisNode.Add(nextNode, ConvertProbability(lnProb));
                }
            }
            tok.Close();

            Debug.Assert(initialNode != null);

            return(initialNode);
        }