2016-02-13 87 views
0

好吧,我會盡快解決。所以我已經將一段C++代碼移植到了java上,因爲這個庫在Ubuntu上不起作用。我一直在四處打獵,我需要通過揚聲器發出嗶嗶聲。但它需要有不同的頻率。它被連接到兩個隨機數發生器,取決於這些隨機數的結果,它應該以不同的頻率發出嘟嘟聲。這不是很好。有沒有需要補充的論點或什麼?這裏的代碼java.awt.Toolkit蜂鳴聲()錯誤

import java.util.Random; 
import java.awt.Toolkit; 

public class RandomSinging { 


public static void main(String args[]) { 
    // TODO code application logic here 

    Toolkit tk = Toolkit.getDefaultToolkit(); 

    double c3 = 130.81; 
    double cS3 = 138.59; 
    double d3 = 146.83; 
    double dS3 = 155.56; 
    double e3 = 164.81; 
    double f3 = 174.61; 
    double fS3 = 185; 
    double g3 = 196; 
    double gS3 = 207.65; 
    double a3 = 220; 
    double aS3 = 233.08; 
    double b3 = 246.94; 

    double c4 = 261.63; 
    double cS4 = 277.18; 
    double d4 = 293.66; 
    double dS4 = 311.13; 
    double e4 = 329.63; 
    double f4 = 349.23; 
    double fS4 = 369.99; 
    double g4 = 392; 
    double gS4 = 415.3; 
    double a4 = 440; 
    double aS4 = 466.16; 
    double b4 = 493.88; 

    double c5 = 523.25; 
    double cS5 = 554.37; 
    double d5 = 587.33; 
    double dS5 = 622.25; 
    double e5 = 659.25; 
    double f5 = 698.46; 
    double fS5 = 739.99; 
    double g5 = 783.99; 
    double gS5 = 830.61; 
    double a5 = 880; 
    double aS5 = 962.33; 
    double b5 = 987.77; 

    int dummyInt = 0; 

    Random toneInt = new Random(); 
    Random octInt = new Random(); 

    while (dummyInt > -1){ 
     toneInt.nextInt(1); 
     octInt.nextInt(1); 

     System.out.print("toneInt; "); 
     System.out.println(toneInt.nextInt(12)); 
     System.out.print("octInt; "); 
     System.out.println(octInt.nextInt(3)); 

     if (octInt.equals(0)){ 
      if (toneInt.equals(0)) tk.beep(); 
      else if (toneInt.equals(1)) tk.beep(); 
      else if (toneInt.equals(2)) tk.beep(); 
      else if (toneInt.equals(3)) tk.beep(); 
      else if (toneInt.equals(4)) tk.beep(); 
      else if (toneInt.equals(5)) tk.beep(); 
      else if (toneInt.equals(6)) tk.beep(); 
      else if (toneInt.equals(7)) tk.beep(); 
      else if (toneInt.equals(8)) tk.beep(); 
      else if (toneInt.equals(9)) tk.beep(); 
      else if (toneInt.equals(10)) tk.beep(); 
      else if (toneInt.equals(11)) tk.beep(); 
     } 

     if (octInt.equals(1)){ 
      if (toneInt.equals(0)) tk.beep(); 
      else if (toneInt.equals(1)) tk.beep(); 
      else if (toneInt.equals(2)) tk.beep(); 
      else if (toneInt.equals(3)) tk.beep(); 
      else if (toneInt.equals(4)) tk.beep(); 
      else if (toneInt.equals(5)) tk.beep(); 
      else if (toneInt.equals(6)) tk.beep(); 
      else if (toneInt.equals(7)) tk.beep(); 
      else if (toneInt.equals(8)) tk.beep(); 
      else if (toneInt.equals(9)) tk.beep(); 
      else if (toneInt.equals(10)) tk.beep(); 
      else if (toneInt.equals(11)) tk.beep(); 
     } 
     if (octInt.equals(2)){ 
      if (toneInt.equals(0)) tk.beep(); 
      else if (toneInt.equals(1)) tk.beep(); 
      else if (toneInt.equals(2)) tk.beep(); 
      else if (toneInt.equals(3)) tk.beep(); 
      else if (toneInt.equals(4)) tk.beep(); 
      else if (toneInt.equals(5)) tk.beep(); 
      else if (toneInt.equals(6)) tk.beep(); 
      else if (toneInt.equals(7)) tk.beep(); 
      else if (toneInt.equals(8)) tk.beep(); 
      else if (toneInt.equals(9)) tk.beep(); 
      else if (toneInt.equals(10)) tk.beep(); 
      else if (toneInt.equals(11)) tk.beep(); 
     } 
     dummyInt++; 
    } 

} 

}

再次不知道,如果我只是愚蠢,這只是不能用的語言進行工作,或者如果我失去了一個參數,它需要,但我看了所有結束但沒有骰子。

+0

Toolkit.beep確實不適合您的需求,因爲它只是發送基本音調到系統揚聲器 – MadProgrammer

回答

0

我不確定Toolkit是否適合聲音,我認爲它可以完成,但是我一直在用Windows機器上的javax.sound庫進行操作。我想這個庫應該適用於Ubuntu,但可能會期待一些警告。

import java.io.File; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.Clip; 
import javax.sound.sampled.FloatControl; 

... 

public static void main(String[] args) { 
    try { 
     Clip clip = AudioSystem.getClip(); 

     File file = new File("[Some *.wav file]"); 
     AudioInputStream stream = AudioSystem.getAudioInputStream(file); 

     clip.open(stream); 
     // Control the volume 
     FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); 
     gainControl.setValue(-20.0f); 

     clip.start(); 
     // The clip won't keep the JVM up if you reach the "end" 
     // so the sleep is merely to let it play in the demo 
     Thread.sleep(1000); 
     clip.close(); 
    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
} 

我對非wav文件沒有任何好運,但我主要是在Windows機器上進行測試。 Clip有一些其他的方法來說不斷循環直到停止被調用等。 https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/Clip.html如果您決定嘗試此路線而不是工具包,可能有助於更多挖掘。

1

我曾經有過類似的問題。我發現了一個例子,用於生成aetherwalker提到的javax.sound庫的曲調。

public void getSound(int f, int time) throws LineUnavailableException{ 
    byte[] buf = new byte[ 1 ]; 
AudioFormat af = new AudioFormat((float)44100, 8, 1, true, false); 
    SourceDataLine sdl = AudioSystem.getSourceDataLine(af); 
sdl.open(); 
sdl.start(); 
for(int i = 0; i < 1000 * (float)time/1000; i++) { 
    double angle = i/((float)f/440) * 2.0 * Math.PI; 
    buf[ 0 ] = (byte)(Math.sin(angle) * 100); 
    sdl.write(buf, 0, 1); 
} 

sdl.drain(); 
sdl.stop(); 

} 

它的工作完全符合我的目的。所以你可以用someNiceSoundClass.getSound(int,int)替換你的tk.beep()調用某些值(你可以從隨機數中計算出來)。

+0

我覺得它實際上是相當相關的知道我是一個在java的新手,所以這是有點希臘語給我。如果你可以剖析這個與說評論比我會非常感激 – Lynnden

+0

還試圖插入這一點的代碼英寸我得到錯誤它說LineUnavailableException AudioFormat和SourceDataLine是「找不到符號」,所以我假設我沒有將它插入到適當的空間中。我想我不清楚我想要什麼。在C++中有一個函數Beep(頻率,時間),這就是我正在尋找像Java函數那樣的函數。我有頻率和時間,我只需要將它們插入一個功能。 – Lynnden

+0

提前一個問題:您是否想通過主板揚聲器或通過您的操作系統音頻設備發出嗶聲? – Jodn