简体   繁体   English

在Ubuntu上用Java定义AudioFormat

[英]Defining AudioFormat in Java on Ubuntu

I would like to know how to define the AudioFormat of a clip on Ubuntu. 我想知道如何在Ubuntu上定义剪辑的AudioFormat。 On Windows, the following code works, but on Ubuntu 14.10, it does not. 在Windows上,以下代码有效,但在Ubuntu 14.10上则无效。

import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
import java.nio.ShortBuffer;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFormat.Encoding;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

class SongPlayer{    
    static int samplesPerSecond = 44100;

    // Plays back a raw audio buffer being passed.
    // @param song Raw signal of sound to be played. Amplitude in range [-1.0, 1.0]
    static void playSong(double[] song){
        AudioFormat format = new AudioFormat(
            Encoding.PCM_SIGNED,         // Encoding
            samplesPerSecond,            // sample rate
            Short.SIZE,                  // bits per sample
            1,                           // number of channels
            Short.SIZE / 8,              // bytes per frame
            samplesPerSecond,            // frame rate
            true);                       // big endian

        ByteBuffer buffer = ByteBuffer.allocate(song.length * (Short.SIZE / 8));
        ShortBuffer sb = buffer.asShortBuffer();
        for (double value : song){
            value = Math.max(-1.0,  Math.min(1.0, value));
            sb.put((short)(Short.MAX_VALUE * value));
        }

        AudioInputStream stream = new AudioInputStream(new ByteArrayInputStream(buffer.array()), format, song.length);

        try{
            Clip clip = AudioSystem.getClip();
            clip.open(stream);
            clip.start();
            Thread.sleep(1000 * 5);
            clip.drain();
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("Playback failed!");
        }
    }

    public static void main(String[] args){
        double[] noise = new double[1000000];

        for(int i = 0; i < 1000000; i++){
            noise[i] = 2 * Math.random() - 1;
        }

        playSong(noise);
    }
}

The error messages are the following: 错误消息如下:

    at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.createStream(PulseAudioDataLine.java:142)
    at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:99)
    at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:283)
    at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:402)
    at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:453)
    at SongPlayer.playSong(song.java:39)
    at SongPlayer.main(song.java:56)
Playback failed!

Usually, I would use stream.getFormat() to obtain the format, but in this case, I want to "play a song" of random numbers between -1 and 1, which will result to random noise. 通常,我会使用stream.getFormat()获得格式,但是在这种情况下,我想“播放一首歌曲”的随机数介于-1和1之间,这将导致随机噪声。 I have read the questions that concern similar problems, but they usually just want to oben a file, while this is something different. 我已经阅读了与类似问题有关的问题,但是它们通常只想获取文件,而这是不同的。 Thanks to anyone who offers any help! 感谢任何提供帮助的人!

Don't use the IcedTea JRE for your program. 不要在程序中使用IcedTea JRE。 I ran your provided code on ubuntu with Oracle Java 8 from the webupd8 ppa ; 我通过webupd8 ppa在Oracle Java 8上的ubuntu上运行了您提供的代码; and it works. 而且有效。 Install Oracle Java 8 like 像安装Oracle Java 8

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM