StdAudio.java 文件源码

java
阅读 23 收藏 0 点赞 0 评论 0

项目:MusicToGraph 作者:
/**
 * Saves the double array as an audio file (using .wav or .au format).
 *
 * @param filename
 *            the name of the audio file
 * @param samples
 *            the array of samples
 * @throws IllegalArgumentException
 *             if unable to save {@code filename}
 * @throws IllegalArgumentException
 *             if {@code samples} is {@code null}
 */
public static void save(String filename, double[] samples) {
    if (samples == null) {
        throw new IllegalArgumentException("samples[] is null");
    }

    // assumes 44,100 samples per second
    // use 16-bit audio, mono, signed PCM, little Endian
    AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
    byte[] data = new byte[2 * samples.length];
    for (int i = 0; i < samples.length; i++) {
        int temp = (short) (samples[i] * MAX_16_BIT);
        data[2 * i + 0] = (byte) temp;
        data[2 * i + 1] = (byte) (temp >> 8);
    }

    // now save the file
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        AudioInputStream ais = new AudioInputStream(bais, format, samples.length);
        if (filename.endsWith(".wav") || filename.endsWith(".WAV")) {
            AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(filename));
        } else if (filename.endsWith(".au") || filename.endsWith(".AU")) {
            AudioSystem.write(ais, AudioFileFormat.Type.AU, new File(filename));
        } else {
            throw new IllegalArgumentException("unsupported audio format: '" + filename + "'");
        }
    } catch (IOException ioe) {
        throw new IllegalArgumentException("unable to save file '" + filename + "'", ioe);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号