2011-12-21 10 views
1

実行時に手続き型のサウンドを生成するコードがたくさんあります。残念ながら、それはほんの数秒しか続きません。理想的には、私はそれを止めるように言うまで走ります。私はループについて話しているわけではありません。生成するアルゴリズムは、現時点で2^64サンプルを提供しているので、近い将来には尽きることはありません。 AudioInputStreamのコンストラクタは3番目の入力を受け取ります。これは理想的には削除できます。私はちょうど巨大な数字を提供することができますが、それはそれについて行くための間違った方法のように思えます。Javaの長さ無制限AudioInputStream

私はSourceDataLineを使用することを考えましたが、理想的にはオンデマンドと呼ばれ、先に実行してパスを書き込むことはありません。思考?

答えて

0

私自身の質問に答えているようです。

さらに調査するには、SourceDataLineを使用することが、操作に十分な時間を与えたときにブロックされるため、移動する方法です。

適切なJavadocがないことにお詫びします。

class SoundPlayer 
{ 
    // plays an InputStream for a given number of samples, length 
    public static void play(InputStream stream, float sampleRate, int sampleSize, int length) throws LineUnavailableException 
    { 
     // you can specify whatever format you want...I just don't need much flexibility here 
     AudioFormat format = new AudioFormat(sampleRate, sampleSize, 1, false, true); 
     AudioInputStream audioStream = new AudioInputStream(stream, format, length); 
     Clip clip = AudioSystem.getClip(); 
     clip.open(audioStream); 
     clip.start(); 
    } 

    public static void play(InputStream stream, float sampleRate, int sampleSize) throws LineUnavailableException 
    { 
     AudioFormat format = new AudioFormat(sampleRate, sampleSize, 1, false, true); 
     SourceDataLine line = AudioSystem.getSourceDataLine(format); 
     line.open(format); 
     line.start(); 
     // if you wanted to block, you could just run the loop in here 
     SoundThread soundThread = new SoundThread(stream, line); 
     soundThread.start(); 
    } 

    private static class SoundThread extends Thread 
    { 
     private static final int buffersize = 1024; 

     private InputStream stream; 
     private SourceDataLine line; 

     SoundThread(InputStream stream, SourceDataLine line) 
     { 
      this.stream = stream; 
      this.line = line; 
     } 

     public void run() 
     { 
      byte[] b = new byte[buffersize]; 
      // you could, of course, have a way of stopping this... 
      for (;;) 
      { 
       stream.read(b); 
       line.write(b, 0, buffersize); 
      } 
     } 
    } 
} 
+0

私はこの回答を2日間で受け入れると思います。他の誰かがより良い解決策を提示しない限り。 – skeggse