# Play a sound, any sound [closed]

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.
• ^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. – user19214 Jun 17 '14 at 21:43
• Every program that prints "\a" to a terminal fails if xterm uses a visible bell. – kernigh Jun 18 '14 at 23:42
• rm / will cause a lot of screaming. – cjfaure Jun 22 '14 at 17:23
• My left fan has been dying noisily for a while. Would that be a valid 0 bit answer? – Édouard Sep 29 '15 at 0:11
• 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. – mbomb007 Dec 2 '16 at 19:40

# 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,

• 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. – vsz Nov 24 '12 at 17:35
• 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. – daniero Nov 24 '12 at 20:12
• A few more examples if you're interested: 1, 2 and 3. Seen a few others use it here as well. – daniero Nov 24 '12 at 20:20
• @vsz dc is – of course – turing complete, as any better calculator such as ghc should be. – FUZxxl Dec 3 '12 at 21:54
• All sounds can be disabled. Just turn the volume down. – daviewales Jun 18 '14 at 8:53

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

• I think that one should come with a disclaimer -- "Don't try this at home" :D – daniero Nov 24 '12 at 20:28
• Yeah, that's the sound when your computer reads your mail really fast (rm -rf). – FUZxxl Dec 3 '12 at 22:27
• 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... – ceased to turn counterclockwis Dec 3 '12 at 22:51
• It doesn't do anything without --no-preserve-root, except maybe on ancient systems. – nyuszika7h Jun 9 '14 at 14:58
• @nyuszika7h ancient as in before 2005. – Jwosty Jun 16 '14 at 22:21

# *sh (5)

eject


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

• I like this one. But, on my Mac (Mac OS X), the command is not found : sh: eject: command not found – Nicolas Barbulesco Jun 7 '14 at 20:27
• Nicolas: right, on Mac, the eject "command" seems to be much shorter: (: – mykhal Jun 10 '14 at 7:59
• @ mykhal — If you go this way, I have a solution that works on all modern Macs : 🔊 – Nicolas Barbulesco Jun 11 '14 at 21:44
• 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. – kernigh Jun 18 '14 at 23:32
• 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 – mykhal Jun 19 '14 at 14:36

## Befunge, 0

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

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

• +1 for not just relying on a terminal to translate BEL characters to beeps. – Ilmari Karonen Nov 18 '12 at 12:28

## brainfuck: 8

+++++++.


Prints the bell character.

• +1 BF Solution both understandable AND shorter than C. – OldCurmudgeon Nov 18 '12 at 0:27
• A brainfuck is easier to mentally digest than C... we should learn from this incident. – Camilo Martin Dec 18 '12 at 14:10

### bash (Mac OS X) 5

say a


although stylistically I prefer the somewhat longer:

say 'Hello, Code Golf !'

• If your Mac is set to English, you could also do say 'hahahahahahahahahahahahahahahahahahahahahahahahahaj'. – tomsmeding Dec 29 '12 at 14:23
• Oooh... I've got ssh access to an osx box... – boothby May 4 '13 at 17:43
• This command works in AppleScript. I did not know it works in the Terminal too. Funny with ssh ! – Nicolas Barbulesco Jun 7 '14 at 20:30
• Corollary on Linux: espeak... – daviewales Jun 18 '14 at 8:57

Haha, good ol' QBASIC code.

BEEP

• Ah, the fun that could be had with the PLAY command! For reminiscence, see e.g. this video. – Jussi M Mar 12 '14 at 19:19
• I'm 15, xD. We learned QBASIC in school. – Soham Chowdhury Mar 14 '14 at 8:29

# 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>')

• Why the downvote, this code actually produces a sound. – user12487 Jan 1 '14 at 18:14
• 1 char O_o nice +1 – Timtech Jan 2 '14 at 1:38

### Golfscript 3

'.'


(where the . is in fact a BELL character)

The HEX representation of the above code is:

27 07 27

• Ouch, Golfscript strikes again! – vsz Nov 17 '12 at 18:27

# 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){}}}

• Java truly sucks for golf – boothby May 4 '13 at 17:44
• @boothby not always, check this out – aditsu May 6 '13 at 1:07
• Okay, Java sucks for golf except when the challenge is to throw errors. ;) – boothby May 6 '13 at 1:47
• @boothby What about this? codegolf.stackexchange.com/a/28818/24965 – stommestack Jun 11 '14 at 14:25
• @JopVernooij That's not golf, that's an underhanded popularity contest. – Jwosty Jun 16 '14 at 22:26

## DOS Prompt / DOS Script

a:


Requires 3.5" floppy drive :)

• What magic is this? – seequ Jun 8 '14 at 21:44
• 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. – celtschk Jun 15 '14 at 14:02
• Aye. Wouldn't the 5.25" normally be b:? – svidgen Jun 15 '14 at 15:26
• Try a CD drive with a defective disk inside, works great too :) – Alejandro Jun 15 '14 at 17:53
• 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. – Dennis Oct 2 '14 at 21:47

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


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

• And the sound keeps screaming even after the program has quit ? I like this one ! :-) – Nicolas Barbulesco Jun 22 '14 at 16:35
• @NicolasBarbulesco, exactly. That was the normal behaviour of all programs on DOS, including those written in C or assembly. – manatwork Jun 23 '14 at 9:46
• -2: Change to 9 Hz: Sound(9). – Erik the Outgolfer Jul 19 '16 at 11:54
• @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ, would be hard to prove the sound is actually generated. Frequency Range of Human Hearing – manatwork Jul 19 '16 at 12:05
• @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) – Erik the Outgolfer Jul 19 '16 at 12:08

# Mathematica 6

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

Beep[]


## 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

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

# 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  • What is the program ? How to run it ? This is not clear. – Nicolas Barbulesco Jun 7 '14 at 20:36 • @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. – Konrad Borowski Jun 8 '14 at 7:01 • It is not clear. But it works ! With cat. The trick is the ASCII char 7 “bell”. – Nicolas Barbulesco Jun 8 '14 at 15:48 • That's not a serious attempt. – user48538 Mar 3 '16 at 11:10 ### brainfuck, 5 bytes +[.+]  prints all chars including bell ## R 9 The bell character cat("\a")  # Ruby, 7 $><<?\a


Tested on Windows with Ruby 1.9.3.

## DOS, 5

Indirect, requires user cooperation.

pause

• Run the program. Cooperate. Listen. – boothby May 4 '13 at 17:36
• You have to press a key... which makes a sound. @mana He said it was indirect - pause didn't make the sound, YOU did. – Riking May 5 '13 at 4:48
• @Riking, how that conforms with the “Your program has to make the computer produce a sound” part of the requirement? – manatwork May 5 '13 at 9:52
• I do not understand programmers on puzzle sites who cannot think outside of the box. – boothby May 5 '13 at 18:20
• 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. – user8059 May 6 '13 at 2:00

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

• Surely you could at least use smaller numbers – Dennis Jaheruddin Jan 13 '14 at 17:06

Python 9 chars

produces beep sound when executed.

print'\a'

• You can shorten this to 8 characters if you replace \a with the actual bell character (ASCII code 7). – flornquake May 3 '13 at 16:37
• But how to print with the help of the value 7 ? – Anbarasan May 6 '13 at 3:55
• 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)+"'") – flornquake May 6 '13 at 11:33
• Got it now. Thanks ! – Anbarasan May 7 '13 at 3:55

# Applescript (4 Characters)

beep


This will play the default beep sound of OS X.

# Mathematica 7

Using Mathematica's built in text to speech capability.

Speak@x


Will make it say "x"

# 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/

• This is pure genius... :D – Mega Man Aug 1 '16 at 8:26

## ><> 3

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

7o;


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

• 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? – manatwork May 3 '13 at 15:19
• 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) – Johannes Kuhn May 3 '13 at 16:28
• 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.) – manatwork May 3 '13 at 16:37

# C: 19

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


edit: header file is not needed for "puts"

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

### Ruby 8

 puts"\a"


Tested on Windows with Ruby 1.9.3.