簡體   English   中英

在Ubuntu上用Java定義AudioFormat

[英]Defining AudioFormat in Java on Ubuntu

我想知道如何在Ubuntu上定義剪輯的AudioFormat。 在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);
    }
}

錯誤消息如下:

    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!

通常,我會使用stream.getFormat()獲得格式,但是在這種情況下,我想“播放一首歌曲”的隨機數介於-1和1之間,這將導致隨機噪聲。 我已經閱讀了與類似問題有關的問題,但是它們通常只想獲取文件,而這是不同的。 感謝任何提供幫助的人!

不要在程序中使用IcedTea JRE。 我通過webupd8 ppa在Oracle Java 8上的ubuntu上運行了您提供的代碼; 而且有效。 像安裝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