concatfiledatasource.java

来自「It is the Speech recognition software. 」· Java 代码 · 共 533 行 · 第 1/2 页

JAVA
533
字号
        addRandomSilence = ps.getBoolean            (PROP_ADD_RANDOM_SILENCE, PROP_ADD_RANDOM_SILENCE_DEFAULT);        maxSilence = ps.getInt (PROP_MAX_SILENCE, PROP_MAX_SILENCE_DEFAULT);        skip = ps.getInt (PROP_SKIP, PROP_SKIP_DEFAULT);        silenceFileName = ps.getString (PROP_SILENCE_FILE, PROP_SILENCE_FILE_DEFAULT);        startFile = ps.getInt            (PROP_START_FILE, PROP_START_FILE_DEFAULT);        totalFiles = ps.getInt            ( PROP_TOTAL_FILES, PROP_TOTAL_FILES_DEFAULT);        transcriptFile = ps.getString            ( PROP_TRANSCRIPT_FILE, PROP_TRANSCRIPT_FILE_DEFAULT);        batchFile = ps.getString            ( PROP_BATCH_FILE, PROP_BATCH_FILE_DEFAULT);    }    /**     * Initializes a ConcatFileDataSource.     */    public void initialize() {        super.initialize();        try {            File silenceFile = new File(silenceFileName);            silenceFileLength = silenceFile.length();            if (transcriptFile != null) {                transcript = new FileWriter(transcriptFile);            }            if (batchFile == null) {                throw new Error("BatchFile cannot be null!");            }            setInputStream                (new SequenceInputStream                 (new InputStreamEnumeration                  (batchFile, startFile, totalFiles)), batchFile);            referenceList = new LinkedList();        } catch (IOException e) {            e.printStackTrace(); //TODO fix this        }    }    /**     * Returns a list of all reference text. Implements the getReferences()     * method of ReferenceSource.     *     * @return a list of all reference text     */    public List getReferences() {        return referenceList;    }    /**     * Returns the name of the transcript file.     *     * @return the name of the transcript file     */    public String getTranscriptFile() {        return transcriptFile;    }    /**     * Returns the audio time in seconds represented by the given     * number of bytes.     *     * @param bytes the number of bytes     *     * @return the audio time     */    private float getSeconds(long bytes) {        return ((float) bytes/bytesPerSecond);    }        /**     * The work of the concatenating of the audio files are     * done here. The idea here is to turn the list of audio files into     * an Enumeration, and then fed it to a SequenceInputStream, giving     * the illusion that the audio files are concatenated, but only     * logically.     */    class InputStreamEnumeration implements Enumeration {        private int totalFiles;        private boolean inSilence;        private Random silenceRandom;        private BufferedReader reader;        InputStreamEnumeration(String batchFile, int startFile,                                int totalFiles)            throws IOException {            this.totalFiles = totalFiles;            reader = new BufferedReader(new FileReader(batchFile));            if (silenceFileName != null) {                inSilence = true;                silenceRandom = new Random(System.currentTimeMillis());                silenceCount = getSilenceCount();            }            // go to the start file            for (int i = 1; i < startFile; i++) {                reader.readLine();            }        }                /**         * Tests if this enumeration contains more elements.         *         * @return true if and only if this enumeration object contains          * at least one more element to provide; false otherwise.         */        public boolean hasMoreElements() {            if (nextFile == null) {                nextFile = readNext();            }            return (nextFile != null);        }                /**         * Returns the next element of this enumeration if this          * enumeration object has at least one more element to provide.         *         * @return the next element of this enumeration.         */        public Object nextElement() {            Object stream = null;            if (nextFile == null) {                nextFile = readNext();            }            if (nextFile != null) {                try {                    stream = new FileInputStream(nextFile);                    // System.out.println(nextFile);                    nextFile = null;                } catch (IOException ioe) {                    ioe.printStackTrace();                    throw new Error("Cannot convert " + nextFile +                                    " to a FileInputStream");                }            }            // close the transcript file no more files            if (stream == null && transcript != null) {                try {                    transcript.close();                } catch (IOException ioe) {                    ioe.printStackTrace();                }            }            return stream;        }                /**         * Returns the name of next audio file, taking into account         * file skipping and the adding of silence.         *         * @return the name of the appropriate audio file         */        public String readNext() {            if (!inSilence) {                return readNextDataFile();                   } else {                // return the silence file                String next = null;                if (silenceCount > 0) {                    next = silenceFileName;                    if (transcript != null) {                        writeSilenceToTranscript();                    }                    silenceCount--;                    if (silenceCount <= 0) {                        inSilence = false;                    }                }                return next;            }        }        /**         * Returns the next audio file.         *         * @return the name of the next audio file         */        private String readNextDataFile() {            try {                if (0 <= totalFiles &&                    totalFiles <= referenceList.size()) {                    return null;                }                String next = reader.readLine();                if (next != null) {                    String reference = BatchFile.getReference(next);                    referenceList.add(reference);                    next = BatchFile.getFilename(next);                    for (int i = 1; i < skip; i++) {                        reader.readLine();                    }                    if (silenceFileName != null && maxSilence > 0) {                        silenceCount = getSilenceCount();                        inSilence = true;                    }                    if (transcript != null) {                        writeTranscript(next, reference);                    }                }                return next;            } catch (IOException ioe) {                ioe.printStackTrace();                throw new Error("Problem reading from batch file");            }        }        /**         * Writes the transcript file.         *         * @param fileName the name of the decoded file         * @param reference the reference text         */        private void writeTranscript(String fileName, String reference) {            try {                File file = new File(fileName);                float start = getSeconds(totalBytes);                totalBytes += file.length();                float end = getSeconds(totalBytes);                transcript.write(context + " 1 " + fileName + " " + start +                                 " " + end + " <> " + reference + "\n");                transcript.flush();            } catch (IOException ioe) {                ioe.printStackTrace();            }        }        /**         * Writes silence to the transcript file.         */        private void writeSilenceToTranscript() {            try {                float start = getSeconds(totalBytes);                totalBytes += silenceFileLength;                float end = getSeconds(totalBytes);                transcript.write(context + " 1 " + GAP_LABEL + " " +                                 start + " " + end + " <>\n");                transcript.flush();            } catch (IOException ioe) {                ioe.printStackTrace();            }        }        /**         * Returns how many times the silence file should be added between         * utterances.         *         * @return the number of times the silence file should be added          *    between utterances         */        private int getSilenceCount() {            if (addRandomSilence) {                return silenceRandom.nextInt(maxSilence) + 1;            } else {                return maxSilence;            }        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?