简体   繁体   中英

java.awt.Toolkit beep() error

ok so i'll make this quick. so i've ported a quick piece of c++ code to java because the one library wouldn't work on ubuntu. i've been hunting around and i need to make a beeping sound through the speakers. but it needs to be of varying frequencies. it's hooked up to two random number generators depending on the results of these random numbers it's supposed to beep at a different frequency. this is not working very well. is there an argument that needs to be fed in or something? here's the code

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

}

}

again not sure if i'm just stupid and this just can't work with the language or if i'm missing an argument it needs but i've looked all over but no dice.

I once had a similar problem. I found an example for generating a tune with the javax.sound library which aetherwalker mentioned.

 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();

}

It work's perfectly for my purpose. So you might just replace your tk.beep() call with someNiceSoundClass.getSound(int,int) with certain values (which you might calculate from the random numbers).

I'm not sure about Toolkit for sound, I think it can be done, but off hand I've been fiddling with the javax.sound library on a Windows machine. I imagine that library should port fine to Ubuntu, but probably expect some caveats.

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();
    }
}

I haven't had any luck with non-wav files, but I've mostly been testing this on a Windows machine. Clip has some other methods to say loop continuously until stop is called and such. https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/Clip.html May help for more digging if you decide to try this route instead of toolkit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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