java.awt.Toolkit beep()错误

时间:2016-02-13 19:21:57

标签: java audio random

好的,所以我快速做到这一点。所以我已经快速将一段c ++代码移植到java,因为一个库不能在ubuntu上工作。我一直在打猎,我需要通过扬声器发出哔哔声。但它需要具有不同的频率。它根据这些随机数的结果连接到两个随机数发生器,它应该以不同的频率发出哔哔声。这不是很好。有没有需要被喂食的论据?这是代码

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++;
    }

}

}

再次不确定我是否只是愚蠢而且这只是不能使用该语言,或者如果我错过了它需要的论点但是我已经看了一遍但没有骰子

2 个答案:

答案 0 :(得分:1)

我曾经遇到过类似的问题。我找到了一个用aavwalker提到的javax.sound库生成一个曲调的例子。

{{1}}

它完全符合我的目的。所以你可能只是用someNiceSoundClass.getSound(int,int)用一些值替换你的tk.beep()调用(你可以用随机数计算)。

答案 1 :(得分:0)

我不确定声音的工具包,我认为可以做到,但是我手头上已经摆弄了Windows机器上的javax.sound库。我想这个库应该可以移植到Ubuntu,但可能会有一些警告。

Sub Macro1()
    Dim N As Long, i As Long, c As Collection
    Dim K As Long, s As String, M As Long
    Dim j As Long
    Set c = New Collection
    Set c = Nothing
    Set c = New Collection
    Range("B:B").Copy Range("D1")

    ActiveSheet.Range("D:D").RemoveDuplicates Columns:=1, Header:=xlYes

    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("D1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("D:D")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    On Error Resume Next
    N = Cells(Rows.Count, "D").End(xlUp).Row
    M = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 2 To N
        K = 5
        Set c = Nothing
        Set c = New Collection
        s = Cells(i, "D").Value
        For j = 2 To M
            If s = Cells(j, "B").Value Then
                c.Add Cells(j, "A").Value, CStr(Cells(j, "A").Value)
                If Err.Number = 0 Then
                    Cells(i, K).Value = Cells(j, "A").Value
                    K = K + 1
                Else
                    Err.Number = 0
                End If
            End If
        Next j
    Next i
    On Error GoTo 0
End Sub

我对非wav文件没有任何好运,但我主要是在Windows机器上测试它。 Clip有一些其他方法可以说连续循环,直到调用stop等。 https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/Clip.html如果您决定尝试使用此路线而不是工具包,可能会有助于更多挖掘。