speechmarker.java
来自「It is the Speech recognition software. 」· Java 代码 · 共 461 行 · 第 1/2 页
JAVA
461 行
return (int) (audio.getValues().length * 1000.0f / audio.getSampleRate()); } /** * Read the starting frames until the utterance has started. * * @throws DataProcessingException if a data processing error occurs */ private void readInitialFrames() throws DataProcessingException { int nonSpeechTime = 0; int minSpeechTime = (startSpeechTime > speechLeader) ? startSpeechTime : speechLeader; while (!inSpeech) { Data audio = readData(); if (audio == null) { return; } else { sendToQueue(audio); if (audio instanceof SpeechClassifiedData) { nonSpeechTime += getAudioTime((SpeechClassifiedData) audio); SpeechClassifiedData data = (SpeechClassifiedData) audio; if (data.isSpeech()) { boolean speechStarted = handleFirstSpeech(data); if (speechStarted) { // System.out.println("Speech started !!!"); addSpeechStart(); inSpeech = true; break; } } } } int i = 0; // prune any excessive non-speech while (nonSpeechTime > minSpeechTime) { Data next = (Data) outputQueue.get(i); if (next instanceof SpeechClassifiedData) { int thisAudioTime = getAudioTime((SpeechClassifiedData) next); if (nonSpeechTime - thisAudioTime >= minSpeechTime) { outputQueue.remove(i); nonSpeechTime -= thisAudioTime; } } i++; } } } /** * Handles an SpeechClassifiedData object that can possibly be the first in * an utterance. * * @param audio the SpeechClassifiedData to handle * * @return true if utterance/speech has started for real, false otherwise */ private boolean handleFirstSpeech(SpeechClassifiedData audio) throws DataProcessingException { int speechTime = getAudioTime(audio); // System.out.println("Entering handleFirstSpeech()"); // try to read more that 'startSpeechTime' amount of // audio that is labeled as speech (the condition for speech start) while (speechTime < startSpeechTime) { Data next = readData(); sendToQueue(next); if (next == null) { return false; } if (next instanceof SpeechClassifiedData) { if (!((SpeechClassifiedData) next).isSpeech()) { return false; } else { speechTime += getAudioTime(audio); } } } return true; } /** * Backtrack from the current position to add a SPEECH_START Signal * to the outputQueue. */ private void addSpeechStart() { long lastCollectTime = 0; long firstSampleNumber = 0; int silenceLength = 0; ListIterator i = outputQueue.listIterator(outputQueue.size()-1); // backtrack until we have 'speechLeader' amount of non-speech while (silenceLength < speechLeader && i.hasPrevious()) { Data current = (Data) i.previous(); if (current instanceof SpeechClassifiedData) { SpeechClassifiedData data = (SpeechClassifiedData) current; if (data.isSpeech()) { silenceLength = 0; } else { silenceLength += getAudioTime(data); } lastCollectTime = data.getCollectTime(); firstSampleNumber = data.getFirstSampleNumber(); } else if (current instanceof DataStartSignal) { i.next(); // put the SPEECH_START after the UTTERANCE_START break; } else if (current instanceof DataEndSignal) { throw new Error("No UTTERANCE_START after UTTERANCE_END"); } } if (speechLeader > 0) { assert lastCollectTime != 0; } // add the SPEECH_START i.add(new SpeechStartSignal(lastCollectTime)); } /** * Given a non-speech frame, try to read more non-speech frames * until we think its the end of utterance. * * @param audio a non-speech frame * * @return true if speech has really ended, false if speech * has not ended */ private boolean readEndFrames(SpeechClassifiedData audio) throws DataProcessingException { boolean speechEndAdded = false; boolean readTrailer = true; int originalLast = outputQueue.size() - 1; int silenceLength = getAudioTime(audio); // read ahead until we have 'endSilenceTime' amount of silence while (silenceLength < endSilenceTime) { Data next = readData(); if (next instanceof SpeechClassifiedData) { SpeechClassifiedData data = (SpeechClassifiedData) next; sendToQueue(data); if (data.isSpeech()) { // if speech is detected again, we're still in // an utterance return false; } else { // it is non-speech silenceLength += getAudioTime(data); } } else if (next instanceof DataEndSignal) { sendToQueue(next); readTrailer = false; break; } else if (next instanceof Signal) { throw new Error("Illegal signal: " + next.toString()); } } if (readTrailer) { // read ahead until we have 'speechTrailer' amount of silence while (!speechEndAdded && silenceLength < speechTrailer) { Data next = readData(); if (next instanceof SpeechClassifiedData) { SpeechClassifiedData data = (SpeechClassifiedData) next; if (data.isSpeech()) { // if we have hit speech again, then the current // speech should end sendToQueue(new SpeechEndSignal(data.getCollectTime())); sendToQueue(data); speechEndAdded = true; break; } else { silenceLength += getAudioTime(data); sendToQueue(data); } } else if (next instanceof DataEndSignal) { sendToQueue(new SpeechEndSignal(((Signal)next).getTime())); sendToQueue(next); speechEndAdded = true; } else { throw new Error("Illegal signal: " + next.toString()); } } } if (!speechEndAdded) { // iterate from the end of speech and read till we // have 'speechTrailer' amount of non-speech, and // then add an SPEECH_END ListIterator i = outputQueue.listIterator(originalLast); long nextCollectTime = 0; // the 'firstSampleNumber' of SPEECH_END actually contains // the last sample number of the segment long lastSampleNumber = 0; silenceLength = 0; while (silenceLength < speechTrailer && i.hasNext()) { Data next = (Data) i.next(); if (next instanceof DataEndSignal) { i.previous(); break; } else if (next instanceof SpeechClassifiedData) { SpeechClassifiedData data = (SpeechClassifiedData) next; nextCollectTime = data.getCollectTime(); assert !data.isSpeech(); silenceLength += getAudioTime(data); lastSampleNumber = data.getFirstSampleNumber() + data.getValues().length - 1; } } if (speechTrailer > 0) { assert nextCollectTime != 0 && lastSampleNumber != 0; } i.add(new SpeechEndSignal(nextCollectTime)); } // System.out.println("Speech ended !!!"); return true; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?