42
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\$
5
  • 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, 2014 at 21:43
  • \$\begingroup\$ Every program that prints "\a" to a terminal fails if xterm uses a visible bell. \$\endgroup\$
    – kernigh
    Jun 18, 2014 at 23:42
  • 19
    \$\begingroup\$ rm / will cause a lot of screaming. \$\endgroup\$
    – cjfaure
    Jun 22, 2014 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, 2015 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, 2016 at 19:40

67 Answers 67

48
votes
\$\begingroup\$

bash (13)

sudo rm -rf /

The faster the hard drive the better the sound. Don't work with SSDs.
(Don't try this at home, sudo rm -rf / erases everything on your hard drive)

\$\endgroup\$
7
  • 7
    \$\begingroup\$ I think that one should come with a disclaimer -- "Don't try this at home" :D \$\endgroup\$
    – daniero
    Nov 24, 2012 at 20:28
  • 22
    \$\begingroup\$ Yeah, that's the sound when your computer reads your mail really fast (rm -rf). \$\endgroup\$
    – FUZxxl
    Dec 3, 2012 at 22:27
  • 70
    \$\begingroup\$ Wait, you got it wrong. The task is to make the computer produce a sound, not the user... though I have to admit that scream was quite impressive... \$\endgroup\$ Dec 3, 2012 at 22:51
  • 4
    \$\begingroup\$ It doesn't do anything without --no-preserve-root, except maybe on ancient systems. \$\endgroup\$
    – alexia
    Jun 9, 2014 at 14:58
  • 1
    \$\begingroup\$ @nyuszika7h ancient as in before 2005. \$\endgroup\$
    – Jwosty
    Jun 16, 2014 at 22:21
43
votes
\$\begingroup\$

*sh (5)

eject

(does not work if you have no CD/DVD or similar drive..)

\$\endgroup\$
5
  • \$\begingroup\$ I like this one. But, on my Mac (Mac OS X), the command is not found : sh: eject: command not found \$\endgroup\$ Jun 7, 2014 at 20:27
  • 3
    \$\begingroup\$ Nicolas: right, on Mac, the eject "command" seems to be much shorter: (: \$\endgroup\$
    – mykhal
    Jun 10, 2014 at 7:59
  • \$\begingroup\$ @ mykhal — If you go this way, I have a solution that works on all modern Macs : 🔊 \$\endgroup\$ Jun 11, 2014 at 21:44
  • 1
    \$\begingroup\$ This fails with OpenBSD. usage: eject [-t] device The device argument is not optional! The shortest that works here is eject cd0 for 9 characters. Also, if the drive has no tray (as with a slot-loading drive), then eject cd0 makes no noise unless a disc is in the drive. \$\endgroup\$
    – kernigh
    Jun 18, 2014 at 23:32
  • 1
    \$\begingroup\$ kernigh: you're right. on the other hand, it might succeed on many systems without a sound card or speaker, where most of the other solutions would fail \$\endgroup\$
    – mykhal
    Jun 19, 2014 at 14:36
36
votes
\$\begingroup\$

Befunge, 0

If I read the spec correctly, this is an endless loop.
Endless loop = you'll hear your CPU cooler spin up.

\$\endgroup\$
27
votes
\$\begingroup\$

sh 7

w|aplay

Plays a short noise.

Also 7:

aplay *

Assembly

Another non-bell solution. Resulting binary is just 15 bytes.

mov al, 182
out 43h, al
out 42h, al
mov al, 16
out 42h, al
mov al, 3
out 61h, al
ret

Assemble with nasm sound.asm -o sound.com. Can be tried with dosbox sound.com.

\$\endgroup\$
1
  • 9
    \$\begingroup\$ +1 for not just relying on a terminal to translate BEL characters to beeps. \$\endgroup\$ Nov 18, 2012 at 12:28
25
votes
\$\begingroup\$

brainfuck: 8

+++++++.

Prints the bell character.

\$\endgroup\$
2
  • 16
    \$\begingroup\$ +1 BF Solution both understandable AND shorter than C. \$\endgroup\$ Nov 18, 2012 at 0:27
  • 15
    \$\begingroup\$ A brainfuck is easier to mentally digest than C... we should learn from this incident. \$\endgroup\$ Dec 18, 2012 at 14:10
22
votes
\$\begingroup\$

bash (Mac OS X) 5

say a

although stylistically I prefer the somewhat longer:

say 'Hello, Code Golf !'
\$\endgroup\$
4
  • 1
    \$\begingroup\$ If your Mac is set to English, you could also do say 'hahahahahahahahahahahahahahahahahahahahahahahahahaj'. \$\endgroup\$
    – tomsmeding
    Dec 29, 2012 at 14:23
  • 21
    \$\begingroup\$ Oooh... I've got ssh access to an osx box... \$\endgroup\$
    – boothby
    May 4, 2013 at 17:43
  • 1
    \$\begingroup\$ This command works in AppleScript. I did not know it works in the Terminal too. Funny with ssh ! \$\endgroup\$ Jun 7, 2014 at 20:30
  • 1
    \$\begingroup\$ Corollary on Linux: espeak... \$\endgroup\$
    – daviewales
    Jun 18, 2014 at 8:57
20
votes
\$\begingroup\$

dc, 2 chars

this one prints the bell character, too:

7P

Run with $ dc <<< 7P or save 7P to file and run $ dc file.

Also:

Befunge, 2 chars

Similar but infinitly looping and beeping:

7,
\$\endgroup\$
7
  • 1
    \$\begingroup\$ Could you please elaborate what language that is? I never heard about it, and its name is too short (and a very common abbreviation) to be efficiently searched for. \$\endgroup\$
    – vsz
    Nov 24, 2012 at 17:35
  • 3
    \$\begingroup\$ From the man-page: dc is a reverse-polish desk calculator which supports unlimited precision arithmetic. -- it's included in most linux/unix distros as far as I know. \$\endgroup\$
    – daniero
    Nov 24, 2012 at 20:12
  • \$\begingroup\$ A few more examples if you're interested: 1, 2 and 3. Seen a few others use it here as well. \$\endgroup\$
    – daniero
    Nov 24, 2012 at 20:20
  • 9
    \$\begingroup\$ @vsz dc is – of course – turing complete, as any better calculator such as ghc should be. \$\endgroup\$
    – FUZxxl
    Dec 3, 2012 at 21:54
  • 4
    \$\begingroup\$ All sounds can be disabled. Just turn the volume down. \$\endgroup\$
    – daviewales
    Jun 18, 2014 at 8:53
18
votes
\$\begingroup\$

Haha, good ol' QBASIC code.

BEEP
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Ah, the fun that could be had with the PLAY command! For reminiscence, see e.g. this video. \$\endgroup\$
    – Jussi M
    Mar 12, 2014 at 19:19
  • 1
    \$\begingroup\$ I'm 15, xD. We learned QBASIC in school. \$\endgroup\$ Mar 14, 2014 at 8:29
16
votes
\$\begingroup\$

Python 3.3.3, one character

<bell character>

The error message will contain a bell character, causing the sound.

Python 3.3.3, 10 characters

If errors are not allowed, this solution won't output the error message, only the sound.

print('<bell character>')
\$\endgroup\$
2
  • \$\begingroup\$ Why the downvote, this code actually produces a sound. \$\endgroup\$
    – user12487
    Jan 1, 2014 at 18:14
  • 1
    \$\begingroup\$ 1 char O_o nice +1 \$\endgroup\$
    – Timtech
    Jan 2, 2014 at 1:38
13
votes
\$\begingroup\$

Golfscript 3

'.'

(where the . is in fact a BELL character)

The HEX representation of the above code is:

27 07 27
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Ouch, Golfscript strikes again! \$\endgroup\$
    – vsz
    Nov 17, 2012 at 18:27
11
votes
\$\begingroup\$

Java - 222

Enough of this bell character or predefined beep function stuff, this makes a real sound :)

import javax.sound.sampled.*;class S{static{try{SourceDataLine
l=AudioSystem.getSourceDataLine(new
AudioFormat(4000,8,1,0<1,0>1));l.open();l.start();for(byte
i=9;i!=0;i+=9)l.write(new byte[]{i},0,1);}catch(Exception e){}}}
\$\endgroup\$
5
  • 18
    \$\begingroup\$ Java truly sucks for golf \$\endgroup\$
    – boothby
    May 4, 2013 at 17:44
  • 1
    \$\begingroup\$ @boothby not always, check this out \$\endgroup\$ May 6, 2013 at 1:07
  • 20
    \$\begingroup\$ Okay, Java sucks for golf except when the challenge is to throw errors. ;) \$\endgroup\$
    – boothby
    May 6, 2013 at 1:47
  • \$\begingroup\$ @boothby What about this? codegolf.stackexchange.com/a/28818/24965 \$\endgroup\$
    – jobukkit
    Jun 11, 2014 at 14:25
  • 2
    \$\begingroup\$ @JopVernooij That's not golf, that's an underhanded popularity contest. \$\endgroup\$
    – Jwosty
    Jun 16, 2014 at 22:26
10
votes
\$\begingroup\$

DOS Prompt / DOS Script

a:

Requires 3.5" floppy drive :)

\$\endgroup\$
5
  • \$\begingroup\$ What magic is this? \$\endgroup\$
    – seequ
    Jun 8, 2014 at 21:44
  • 1
    \$\begingroup\$ It causes DOS to try to read from the floppy, which does cause a sound. It is, however, wrong that this needs a 3.5" floppy drive; a 5.25" floppy drive works even better. \$\endgroup\$
    – celtschk
    Jun 15, 2014 at 14:02
  • \$\begingroup\$ Aye. Wouldn't the 5.25" normally be b:? \$\endgroup\$
    – svidgen
    Jun 15, 2014 at 15:26
  • 1
    \$\begingroup\$ Try a CD drive with a defective disk inside, works great too :) \$\endgroup\$
    – Alejandro
    Jun 15, 2014 at 17:53
  • \$\begingroup\$ The drive letter in DOS depended on which connectors of the cable the drive was attached to (and on newer machines, a BIOS setting). My first computer only had a 5.25" drive and I had to attach it as master (making it the A drive), since it couldn't boot from the slave. \$\endgroup\$
    – Dennis
    Oct 2, 2014 at 21:47
9
votes
\$\begingroup\$

J (2)

a.

This prints all the characters from 0 to 255, that includes the bell.

If I have to do it without printing anything else, it's 4 characters:

7{a.
\$\endgroup\$
9
votes
\$\begingroup\$

Pascal: 30 characters

uses Crt;begin
Sound(999)
end.

This takes advantage of the lack of any rule regarding the sound's duration. So just turns on the speaker on 999 Hz and lets it so. (At least until another program calls Nosound or the computer is turned off.)

\$\endgroup\$
5
  • \$\begingroup\$ And the sound keeps screaming even after the program has quit ? I like this one ! :-) \$\endgroup\$ Jun 22, 2014 at 16:35
  • \$\begingroup\$ @NicolasBarbulesco, exactly. That was the normal behaviour of all programs on DOS, including those written in C or assembly. \$\endgroup\$
    – manatwork
    Jun 23, 2014 at 9:46
  • \$\begingroup\$ -2: Change to 9 Hz: Sound(9). \$\endgroup\$ Jul 19, 2016 at 11:54
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ, would be hard to prove the sound is actually generated. Frequency Range of Human Hearing \$\endgroup\$
    – manatwork
    Jul 19, 2016 at 12:05
  • \$\begingroup\$ @manatwork Well, add an identical code with another frequency that can be heard. Because the codes are identical, it can be proved. (the number only changes) \$\endgroup\$ Jul 19, 2016 at 12:08
8
votes
\$\begingroup\$

Mathematica 6

This will evoke the system beep. On my computer, it is currently a chirp.

Beep[]
\$\endgroup\$
8
votes
\$\begingroup\$

Bash, 22

Back in the 90's, my brother taught me this one -- to be used in a computer lab where one has remote access and knows a person to be alone in said lab. Kids these days won't know what a computer lab is... but oh well.

cd /dev;cat sda1>audio
\$\endgroup\$
7
  • 1
    \$\begingroup\$ I get bash: audio: Permission denied even running under sudo. :( \$\endgroup\$
    – Riking
    May 5, 2013 at 4:46
  • 2
    \$\begingroup\$ This worked on Debian as of 2006. I can't get it to work on my Ubuntu box :*( \$\endgroup\$
    – boothby
    May 5, 2013 at 15:41
  • 1
    \$\begingroup\$ I think you mean dsp, not audio. See below for a similar example I wrote using /dev/random \$\endgroup\$
    – user12447
    Jan 2, 2014 at 1:42
  • 3
    \$\begingroup\$ or cat /dev/sda | aplay \$\endgroup\$
    – TheDoctor
    Mar 4, 2014 at 0:23
  • 1
    \$\begingroup\$ @TheDoctor or aplay</dev/sda (UUOC ;-)) \$\endgroup\$ Jun 12, 2014 at 17:06
7
votes
\$\begingroup\$

cat/type/PHP 1

This is actually polyglot. It runs in cat (or Microsoft Cat called type) and PHP.

$ xxd file
0000000: 07                                       .

Execution:

$ xxd -r > file
0000000: 07                                       .
^D
$ cat file # cat could be replaced with type (on Windows) or with php
\$\endgroup\$
4
  • \$\begingroup\$ What is the program ? How to run it ? This is not clear. \$\endgroup\$ Jun 7, 2014 at 20:36
  • \$\begingroup\$ @NicolasBarbulesco: The program is one byte (\x07), however because it's binary, I cannot paste it here. You can run it in PHP CLI, with php. \$\endgroup\$ Jun 8, 2014 at 7:01
  • \$\begingroup\$ It is not clear. But it works ! With cat. The trick is the ASCII char 7 “bell”. \$\endgroup\$ Jun 8, 2014 at 15:48
  • \$\begingroup\$ That's not a serious attempt. \$\endgroup\$
    – user48538
    Mar 3, 2016 at 11:10
6
votes
\$\begingroup\$

brainfuck, 5 bytes

+[.+]

prints all chars including bell

\$\endgroup\$
5
votes
\$\begingroup\$

R 9

The bell character

cat("\a")
\$\endgroup\$
5
votes
\$\begingroup\$

Ruby, 7

$><<?\a

Tested on Windows with Ruby 1.9.3.

\$\endgroup\$
5
votes
\$\begingroup\$

DOS, 5

Indirect, requires user cooperation.

pause
\$\endgroup\$
10
  • 6
    \$\begingroup\$ Run the program. Cooperate. Listen. \$\endgroup\$
    – boothby
    May 4, 2013 at 17:36
  • 2
    \$\begingroup\$ You have to press a key... which makes a sound. @mana He said it was indirect - pause didn't make the sound, YOU did. \$\endgroup\$
    – Riking
    May 5, 2013 at 4:48
  • 2
    \$\begingroup\$ @Riking, how that conforms with the “Your program has to make the computer produce a sound” part of the requirement? \$\endgroup\$
    – manatwork
    May 5, 2013 at 9:52
  • 5
    \$\begingroup\$ I do not understand programmers on puzzle sites who cannot think outside of the box. \$\endgroup\$
    – boothby
    May 5, 2013 at 18:20
  • 4
    \$\begingroup\$ I wasn't criticizing your answer. I was criticizing the question. A pointless question will obviously get pointless answers. Your answer then make as much sense as any other answer to that stupid question. So, i'm giving you +1. \$\endgroup\$
    – user8059
    May 6, 2013 at 2:00
5
votes
\$\begingroup\$

PowerShell, 24 17

Since nobody has posted this version yet:

[console]::beep()

UPDATE: Removed frequencies so it uses the default frequency and duration (MSDN says the default is 800 hertz and 200 milliseconds)

\$\endgroup\$
1
  • \$\begingroup\$ Surely you could at least use smaller numbers \$\endgroup\$ Jan 13, 2014 at 17:06
4
votes
\$\begingroup\$

Python 9 chars


produces beep sound when executed.

print'\a'
\$\endgroup\$
4
  • 1
    \$\begingroup\$ You can shorten this to 8 characters if you replace \a with the actual bell character (ASCII code 7). \$\endgroup\$
    – flornquake
    May 3, 2013 at 16:37
  • \$\begingroup\$ But how to print with the help of the value 7 ? \$\endgroup\$
    – Anbarasan
    May 6, 2013 at 3:55
  • 1
    \$\begingroup\$ You can type the bell character into your editor with Alt+07. Alternatively, you can run this snippet to create a python file with the 8-character solution: open("play_a_sound.py", 'w').write("print'"+chr(7)+"'") \$\endgroup\$
    – flornquake
    May 6, 2013 at 11:33
  • \$\begingroup\$ Got it now. Thanks ! \$\endgroup\$
    – Anbarasan
    May 7, 2013 at 3:55
4
votes
\$\begingroup\$

Applescript (4 Characters)

beep

This will play the default beep sound of OS X.

\$\endgroup\$
4
votes
\$\begingroup\$

Mathematica 7

Using Mathematica's built in text to speech capability.

Speak@x

Will make it say "x"

\$\endgroup\$
4
votes
\$\begingroup\$

top & aplay - 9

top has rhythm!

top|aplay

The periodic display sometimes does not feed the sound sink fast enough but aplay continues after grouching a bit... :-)

...this will run on many Linuxes...

Bonus:

While running this, type "s 1 ENTER" to get more beats per second. :-)

Party on!!! \o/

\$\endgroup\$
1
  • \$\begingroup\$ This is pure genius... :D \$\endgroup\$
    – univalence
    Aug 1, 2016 at 8:26
3
votes
\$\begingroup\$

><> 3

As in all answers, prints the bell (0x07) to stdout.

7o;
\$\endgroup\$
3
votes
\$\begingroup\$

Tcl, 1 char

-

replace - with the bell char.
It will print the bell char as part of the error message. Twice.

And if you think this is not a valid program, add this before the bell char:

proc \007 {} {};

It is a valid program, I just did not define the command, because I like the error message.

\$\endgroup\$
3
  • \$\begingroup\$ Is that based on some Tcl 8.6 feature? In Tcl 8.5 I got different result: pastebin.com/bh73UF6F Or are you on Windows? \$\endgroup\$
    – manatwork
    May 3, 2013 at 15:19
  • \$\begingroup\$ No. The 2nd part should show that this is a valid Tcl file. And it should beep 2x (the error message contains the command to be executed twice). Calling a command that does not exist is not an error for Tcl, it is only one if it can not find it. (e.g. parray, which is loaded on the first use) \$\endgroup\$ May 3, 2013 at 16:28
  • \$\begingroup\$ Oops. I misunderstood you. I thought you mean, it should beep with the proc declaration too. (Regarding the beep count, probably my terminal is doing something as puts [string repeat ".\a" 10] displays 10 dots but beeps only once.) \$\endgroup\$
    – manatwork
    May 3, 2013 at 16:37
3
votes
\$\begingroup\$

C: 19

main(){puts("\a");}

edit: header file is not needed for "puts"

\$\endgroup\$
6
  • 2
    \$\begingroup\$ You can remove the space between #include and < :) \$\endgroup\$
    – tomsmeding
    Nov 18, 2012 at 8:13
  • \$\begingroup\$ puts(char*);main(){puts("\a");} is smaller IMHO. \$\endgroup\$
    – FUZxxl
    Dec 3, 2012 at 22:26
  • \$\begingroup\$ You can just remove the #include. \$\endgroup\$
    – ugoren
    Dec 4, 2012 at 8:10
  • 1
    \$\begingroup\$ @FUZxxl How can you have an opinion about code size? \$\endgroup\$
    – Doorknob
    Dec 22, 2013 at 0:24
  • \$\begingroup\$ @DoorknobofSnow Because of the first amendment? \$\endgroup\$
    – FUZxxl
    Dec 22, 2013 at 0:35
2
votes
\$\begingroup\$

Ruby 8

 puts"\a"

Tested on Windows with Ruby 1.9.3.

\$\endgroup\$

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