43
votes
\$\begingroup\$

Your program has to make the computer produce a sound, any sound.

Shortest code wins, not sooner than 10 days after the first valid answer. If there is a tie, the one submitted sooner, wins.

  • The program should run on a reasonable, not too uncommon personal computer.
  • Opening a pop-up, message box, etc. (for example, on a web page) does not count, as they might or might not produce a sound, depending on a lot of settings.
  • Just entering a wrong command or invalid character on a console and receiving a warning beep does not count, just as the compiler/interpreter/OS beeping on an error or crash does not count either. Your code must be a valid program.
\$\endgroup\$
  • 1
    \$\begingroup\$ ^G aka system bell or OS beep may be suppressed on many systems too (e.g. switched to be a visible "bell") and should be handled like sounds of popus and so on too: Not a valid solution. \$\endgroup\$ – user19214 Jun 17 '14 at 21:43
  • \$\begingroup\$ Every program that prints "\a" to a terminal fails if xterm uses a visible bell. \$\endgroup\$ – kernigh Jun 18 '14 at 23:42
  • 19
    \$\begingroup\$ rm / will cause a lot of screaming. \$\endgroup\$ – cjfaure Jun 22 '14 at 17:23
  • \$\begingroup\$ My left fan has been dying noisily for a while. Would that be a valid 0 bit answer? \$\endgroup\$ – Édouard Sep 29 '15 at 0:11
  • 1
    \$\begingroup\$ I think this question should be reopened. The joke answers that rely on the fan spinning or the CD tray opening are invalid, since the question says "play a sound". Seems pretty clear to me that the output needs to come from the speakers as a result of the program, not the OS or the user or the hardware. \$\endgroup\$ – mbomb007 Dec 2 '16 at 19:40

67 Answers 67

2
votes
\$\begingroup\$

Bash, 26

Standard unix (not modern linux, sadly, unless you have alsa-oss installed; the BSDs are fine):

cat /dev/random > /dev/dsp
\$\endgroup\$
  • 4
    \$\begingroup\$ You can save 2 bytes by removing the whitespace around >. \$\endgroup\$ – nyuszika7h Jun 8 '14 at 21:34
2
votes
\$\begingroup\$

Scala: 44

java.awt.Toolkit.getDefaultToolkit().beep();

same as @nrubin29's answer, but shorter

\$\endgroup\$
2
votes
\$\begingroup\$

Linux shell (6 characters)

This requires a correctly configured analog modem (you also might need to run it as root):

wvdial
\$\endgroup\$
2
votes
\$\begingroup\$

GolfScript (3)

Since everyone else is doing this anyway:

'^G'

where ^G is the bell character.

Hex dump:

0000000: 2707 27                                  '.'
\$\endgroup\$
  • \$\begingroup\$ Now what is it, '^G' or "^G"? \$\endgroup\$ – celtschk Jun 15 '14 at 15:34
  • \$\begingroup\$ It doesn't matter, but I'll fix the inconsistency. \$\endgroup\$ – nyuszika7h Jun 15 '14 at 15:34
  • \$\begingroup\$ This is a duplicate of this answer. \$\endgroup\$ – Dennis Jan 24 '16 at 4:22
1
vote
\$\begingroup\$

sh 6

echo .

(where the . is in fact a BELL character)

The HEX representation of the above code is:

65 63 68 6f 20 07
\$\endgroup\$
1
vote
\$\begingroup\$

Autoit 6

Beep()

Although declaring a function would allow for less characters, not counting the declaration:

z()
Func z()
Beep()
EndFunc
\$\endgroup\$
  • 5
    \$\begingroup\$ That way that you made the program longer? It didn't make it shorter. \$\endgroup\$ – boothby May 4 '13 at 17:45
  • 1
    \$\begingroup\$ Writing own functions count toward the the length. otherwise everything would be a how can I call the method with less chars and some languages allow that with 1 char. \$\endgroup\$ – Johannes Kuhn May 10 '13 at 0:15
1
vote
\$\begingroup\$

ATARI BASIC (4 chars)

?"E"

(? is short for "print". E is in fact the "escape" symbol; see http://raster.infos.cz/atari/chars/atascii.htm, item 1B. Printing this symbol would make a characteristic noise.)

\$\endgroup\$
1
vote
\$\begingroup\$

PowerShell, 3

'^G'

Of course, the ^G has to be entered manually, or written to a file with another script, like the following:

"'$([char]7)'"|Set-Content beep.ps1
\$\endgroup\$
1
vote
\$\begingroup\$

VBScript

CreateObject("SAPI.SpVoice").speak "a"

Nice thing about this is you can make it say anything you want.

\$\endgroup\$
1
vote
\$\begingroup\$

Matlab UNTESTED (1)

I am not sure whether this works in matlab, as I don't have sound available, but is in fact a single character. Hence calling or printing it may produce the desired effect in some languages.

Matlab verified (4)

beep
\$\endgroup\$
  • \$\begingroup\$ Calling the character actually does some strange things. See this question if you are interested. \$\endgroup\$ – Dennis Jaheruddin Jan 13 '14 at 17:04
1
vote
\$\begingroup\$

Batch, 16

type sethc.exe

I discovered this. When I do this, I'll get the 'BEEP' sound from reading the < BELL > character.

\$\endgroup\$
1
vote
\$\begingroup\$

Windows batch script, 14

sort %comspec%

%comspec% just points to C:\Windows\System32\cmd.exe, and calling sort will find the bell character at some point in cmd's binary.

\$\endgroup\$
  • \$\begingroup\$ Doesn't seem to work on Windows 8.1 64-bit. \$\endgroup\$ – nyuszika7h Jun 8 '14 at 21:36
1
vote
\$\begingroup\$

JS, 117

new Audio('data:audio/wav;base64,UklGRl9vT19XQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YU'+Array(1e3).join(123)).play()
\$\endgroup\$
  • 1
    \$\begingroup\$ You can save 4 bytes by using audio instead of audio/wav, and 1 byte by using join(10) instead of join(123). \$\endgroup\$ – nyuszika7h Jun 8 '14 at 21:21
  • \$\begingroup\$ Even Array(10).join(1) appears to play a sound, but Array(99).join(9) is more audible. \$\endgroup\$ – nyuszika7h Jun 8 '14 at 21:29
1
vote
\$\begingroup\$

Java: 103

public class S {
    public static void main(String[] a) {
        java.awt.Toolkit.getDefaultToolkit().beep();
    }
}
\$\endgroup\$
  • \$\begingroup\$ Will fail to compile without class wrapper and import. \$\endgroup\$ – Avinash R Jun 8 '14 at 20:16
  • \$\begingroup\$ I didn't realize we needed everything. I'll add it. \$\endgroup\$ – nrubin29 Jun 8 '14 at 21:00
1
vote
\$\begingroup\$

Python3 34

Any of the python versions printing the bell character to stdout was working for me, so here it is one using the standard libarary, in Windows:

import winsound as w;w.Beep(99,99)

The semicolon could be replaced with a newline, but looks cooler this way :)

\$\endgroup\$
  • 2
    \$\begingroup\$ You can save a character by using from winsound import*;Beep(99,99). \$\endgroup\$ – nyuszika7h Jun 15 '14 at 9:45
1
vote
\$\begingroup\$

JavaScript - 99 103

Audio
  ("data:audio;base64,UklGRgWsAABXQVZFZm10IBAAAAABAAEARKwAAESsAAABAAgAZGF0YeGrAACAbebe")
.play();

*chirp* (in firefox, it is definitely a chirp)

\$\endgroup\$
  • \$\begingroup\$ This code throws the following error: TypeError: Failed to construct 'Audio': Please use the 'new' operator, this DOM object constructor cannot be called as a function. \$\endgroup\$ – starbeamrainbowlabs Jun 3 '14 at 11:03
  • \$\begingroup\$ firefox doesn't need that operator. however if i wanted to be pedantic (crossbrowser) i would definitely use 'new' (+4 characters) \$\endgroup\$ – bebe Jun 3 '14 at 11:34
  • \$\begingroup\$ I'm getting the error TypeError: Constructor Audio requires 'new' in Firefox Aurora 32. So yeah, it does. \$\endgroup\$ – nyuszika7h Jun 15 '14 at 9:44
  • \$\begingroup\$ oh dear.. "Omit the new keyword when possible" I had Firefox 29 now 30, it works like charm. (it's funny you try to prove your point with an alpha release) \$\endgroup\$ – bebe Jun 15 '14 at 10:16
1
vote
\$\begingroup\$

QBASIC (8)

PLAY "A"

Body must be at least 30 characters; you entered 19.

ಠ_ಠ

\$\endgroup\$
1
vote
\$\begingroup\$

Windows Command Promt

logoff

Indeed, this makes an OS sound but it is not OS beeping on an error or crash.

\$\endgroup\$
  • \$\begingroup\$ Which version of DOS has a logoff command? \$\endgroup\$ – celtschk Jun 15 '14 at 15:29
  • \$\begingroup\$ @celtschk I don't know why did I wrote that:D \$\endgroup\$ – totymedli Jun 15 '14 at 21:01
1
vote
\$\begingroup\$

Powershell, 4

"`a"

Sends a beep signal to the computer's speaker.

\$\endgroup\$
1
vote
\$\begingroup\$

Visual Basic 6

beep

VB 6, good times.

\$\endgroup\$
1
vote
\$\begingroup\$

C 23

Make a beep sound.

int main(){putchar(7);}
\$\endgroup\$
  • 3
    \$\begingroup\$ This should still compile without the int at the beginning \$\endgroup\$ – squeamish ossifrage Jun 17 '14 at 8:05
1
vote
\$\begingroup\$

Debianish solution, works on some other systems too - 9 chars

aplay /i*

...will play the files /initrd.img and initrd.img.old which are symlinks to some system startup ramdisks in directory /boot.

If you prefer the sound of kernels, try...

aplay /v*

...and look yourself why it works or fails on your system.

I got...

$ LANG=C ls -l / | grep ^[^d]
total 88
lrwxrwxrwx   1 root root    36 Dec 15  2012 initrd.img -> /boot/initrd.img-3.2.0-0.bpo.4-amd64
lrwxrwxrwx   1 root root    30 Dec  9  2012 initrd.img.old -> boot/initrd.img-2.6.32-5-amd64
lrwxrwxrwx   1 root root     4 Dec  9  2012 lib64 -> /lib
lrwxrwxrwx   1 root root    32 Dec 15  2012 vmlinuz -> boot/vmlinuz-3.2.0-0.bpo.4-amd64
lrwxrwxrwx   1 root root    27 Dec  9  2012 vmlinuz.old -> boot/vmlinuz-2.6.32-5-amd64

...here and that may or may not explain a lot to you and I demand the absence of solid facts and that I may or may not be called vroomfondel!

\$\endgroup\$
  • \$\begingroup\$ you are saying that your solution needs neccessary data - that may not even exist - from outside the code. i think it needs reasons to prove it's valid. \$\endgroup\$ – bebe Jun 17 '14 at 22:08
  • \$\begingroup\$ Oh... I didnt see my harddisk as "outside"! :-D ll think about a different solution for some minutes... \$\endgroup\$ – user19214 Jun 17 '14 at 22:18
  • \$\begingroup\$ Ok... let's narrow it down: Debian is a common system and has these links... they can be disabled by editing some config file in /etc/ but their existence can be widely assumed because most users don't touch/change the defaults... \$\endgroup\$ – user19214 Jun 17 '14 at 22:30
  • 1
    \$\begingroup\$ "The program should run on a reasonable, not too uncommon personal computer." was the wording I was looking for. And I think Debian not being configured to be far far away from the installation defaults on contemporary hardware does not describe an uncommon personal computer. \$\endgroup\$ – user19214 Jun 17 '14 at 22:42
1
vote
\$\begingroup\$

OS X Command Line

say a

This causes the computer to say the letter 'A'

\$\endgroup\$
  • \$\begingroup\$ I do not think say is a bash command. \$\endgroup\$ – user19214 Jun 17 '14 at 21:24
  • \$\begingroup\$ sorry, yeti. I was under the impression that OS X command line was Bash. Thank you for the correction \$\endgroup\$ – Trent Jun 18 '14 at 6:03
  • 2
    \$\begingroup\$ It may be BASH where you type say a but that does not make say a BASH command. So I meant something like "say is not part of the BASH language". Maybe that was trying to be too exact... it may have caused more confusion than needed... \$\endgroup\$ – user19214 Jun 18 '14 at 8:00
1
vote
\$\begingroup\$

PHP (CLI): 1 byte

 

Source code as seen in Notepad++:

source code

In the absence of <?php opening tag, source code is simply dumped to output.

Yeah, I know, another BEL answer, but still, tough to beat a single byte, especially in an interpreted language like PHP ;)

\$\endgroup\$
0
votes
\$\begingroup\$

k/Q (6 chars)

Bell character to stdout.

1"c"$7
\$\endgroup\$
0
votes
\$\begingroup\$

Windows Command Script - 1 Byte

As most of the answers, this also uses the bell character.

0x07

Note: Must be run from console to work, since the sound needs some time to trigger.

\$\endgroup\$
0
votes
\$\begingroup\$

Spoon, 13

1111111001010

Similarly to the BF solution, it prints the bell character.

\$\endgroup\$
0
votes
\$\begingroup\$

C++ 45

#include<iostream>
int main(){std::cout<<'\a';}
\$\endgroup\$
0
votes
\$\begingroup\$

ZX Spectrum BASIC (3)

SAVE STR$ PI

It is only 3 bytes because BASIC keywords (with surrounding spaces) occupy single byte in ZX spectrum's memory. Here's its hex representation:

F8 C1 A7

This program saves current program to audio tape under the name "3.1415927" which is the string representation of π.

In fact it require 7 keypresses: S Caps+Symbol Y Caps+Symbol M

\$\endgroup\$
0
votes
\$\begingroup\$

C#

System.Media.SystemSounds.Beep.Play();

It's long, but anyway...

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.