📄 utterance.java
字号:
/* * Copyright 1999-2002 Carnegie Mellon University. * Portions Copyright 2002 Sun Microsystems, Inc. * Portions Copyright 2002 Mitsubishi Electric Research Laboratories. * All Rights Reserved. Use is subject to license terms. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. * */package edu.cmu.sphinx.frontend.util;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import javax.sound.sampled.AudioFileFormat;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;/** * Represents the complete audio data of an utterance. */public class Utterance { private String name; private ByteArrayOutputStream audioBuffer; private AudioFormat audioFormat; /** * Constructs a default Utterance object. * * @param name the name of this Utterance, e.g., it can be the * name of the audio file where the audio came from * @param format the audio format of this Utterance */ public Utterance(String name, AudioFormat format) { this.name = name; this.audioFormat = format; this.audioBuffer = new ByteArrayOutputStream(); } /** * Returns the name of this Utterance. * * @return the name of this Utterance */ public String getName() { return name; } /** * Returns the audio format of this Utterance. * * @return the audio format */ public AudioFormat getAudioFormat() { return audioFormat; } /** * Adds an audio frame into this Utterance. * * @param audio the audio frame to add */ public void add(byte[] audio) { synchronized (audioBuffer) { audioBuffer.write(audio, 0, audio.length); } } /** * Returns the complete audio stream of this utterance. * * @return the complete audio stream */ public byte[] getAudio() { return audioBuffer.toByteArray(); } /** * Returns the amount of time (in seconds) this Utterance lasts. * * @return how long is this utterance */ public float getAudioTime() { return ((float) audioBuffer.size()) / (audioFormat.getSampleRate() * audioFormat.getSampleSizeInBits()/8); } /** * Save the audio as a WAV file in the given file. * * @param fileName the name of the audio file * @param fileFormat the audio file format */ public void save(String fileName, AudioFileFormat.Type fileFormat) throws IOException { File file = new File(fileName); byte[] audio = getAudio(); AudioInputStream ais = new AudioInputStream ((new ByteArrayInputStream(audio)), getAudioFormat(), audio.length); AudioSystem.write(ais, fileFormat, file); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -