7

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.)

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
  • 3
    FYI, the standard way of getting a user's attention in Windows is to flash the taskbar; playing a sound is annoying. – Glenn Maynard Nov 02 '10 at 14:52
  • Thanks. The customer asked for a beep. – Ram Rachum Nov 02 '10 at 17:56
  • Sound is anything but simple, regardless of the O/S or language environment. In Jython, you are using the Java environment so that is where you should look for anything out of the ordinary. – Michael Dillon Feb 14 '11 at 01:15
  • see solution only using python: http://stackoverflow.com/questions/4467240/play-simple-beep-with-python-without-external-library – Loic Mouchard Mar 07 '16 at 13:02

6 Answers6

4

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)

user489041
  • 27,916
  • 55
  • 135
  • 204
  • Any way to do that without dealing with a file? Like, doing a sine wave or something? I'm completely ignorant in Java. – Ram Rachum Nov 02 '10 at 17:57
  • Sure is, loop over: amplitude * (math.sin(frequency * 2 * math.pi * count) to produce a sin wave where count is the time step. As you iterate, save the result, and you will have essentially an array of raw sound data. Do with it what you would like. – user489041 Nov 02 '10 at 19:10
  • Damn, it says `cannot import name newAudioClip`. Probably it's not included with Sikuli. Any clue what to do? – Ram Rachum Nov 05 '10 at 11:33
  • Hmm, Try giving the second example a shot. Let me know if that works. – user489041 Nov 05 '10 at 14:04
2

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)

1

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.

David
  • 3,843
  • 33
  • 36
0

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")
wardies
  • 1,149
  • 10
  • 14
0

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()
Tenzin
  • 2,415
  • 2
  • 23
  • 36
0

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

Surfdork
  • 91
  • 1
  • 8