Ram Rachum
Ram Rachum

Reputation: 88528

Jython: Making a simple beep on Windows

I'm working with Sikuli, which (I think) is build on Jython. I want to make a script that does a small gentle beep to attract the user's attention. (This is on Windows.)

How can I do this? I see that the winsound module is not available on Jython.

(Note that I want to use the sound card, not the built-in beeper.)

Upvotes: 7

Views: 2570

Answers (6)

Tenzin
Tenzin

Reputation: 2505

Since you asked for a simple Sikuli/Python script, I tested this one out myself on Windows 10:

import java.awt.Toolkit 

class Main():
    def __init__(self):
        # Ask user input. 
        nValue = input('Please enter a value:')
        # Run the beep definition. 
        self.beepAway(nValue)

    def beepAway(self, nValue):
        # Beep nValue number of times, after each beep wait 2 seconds. 
        for i in range(int(nValue)):
            java.awt.Toolkit.getDefaultToolkit().beep()
            wait(2)

# Run class 
Main()

Upvotes: 0

wardies
wardies

Reputation: 1259

If you run Sikuli scripts from the command line, rather than through the IDE, then you can simply write the BEL character to the console and it will beep. This also works through RDP.

Edit: on Windows 7, this will now beep through the sound card, as you asked for. On Windows XP it will beep to the internal speaker (if present).

E.g. the following beeps twice:

print("\007\007")

Upvotes: 0

Liqun
Liqun

Reputation: 4171

Since we have access to the Java level in Sikuli (thanks to Jython), this should principally work:

import java.awt.Toolkit # only once per script
java.awt.Toolkit.getDefaultToolkit().beep()

Test passed on windows 7. You may get some detailed explanation here.

Upvotes: 1

Sergey Stefurak
Sergey Stefurak

Reputation: 161

You may do the fllowing using command line:

Execute "copy con beep.txt" type [ctrl+G] as input and then [ctrl+Z] followed by [Enter] to exit

Then run "type beep.txt" and you will hear a beep.

You may place "type beep.txt" in a batch file or use Ctrl+G directly in batch (which would produce error in command line with sound)

Upvotes: 2

Surfdork
Surfdork

Reputation: 91

Since you are using Sikuli you can to the following.

Add any mediafile such as any .mp3 on the desktop of a windows machine, asociate the file to a media player. Capture the image and include:

click(pattern(desktopnoiseicon.png)

alternatley you could execute the same task with openApp(C:\noise.mp3)

Sikuli gives the ability to find numerous workarounds

in SikuluXrc2 you could even point to a URL from your code without the need of setting a bundle path

Upvotes: 0

user489041
user489041

Reputation: 28294

If its Jython, then just use any of the Java classes that play sound. There are tons of them.

from java import net
from java.applet.Applet import newAudioClip
from java import io
url = io.File("fileName").toURL()
audio = newAudioClip(url)
audio.play()

import sun.audio
import java.io
inputStream = java.io.FileInputStream("test.wav")
audioStream = sun.audio.AudioStream(inputStream)
sun.audio.AudioPlayer.player.start(audioStream)

Upvotes: 4

Related Questions