# Output a sound of a certain frequency

This is a rather simple challenge, but I couldn't find any question that was really similar to it. The challenge is to take a frequency in using STDIN or an equivalent, and then output a tone that matches that frequency, in Hz, for 5 seconds. For example

Input: 400
Output: (a tone of 400 Hz with a duration of 5 seconds)

## Rules

• Input must be taken in through STDIN, or your language's equivalent
• The answer must be a full program
• Builtins may be used
• The frequency will be anywhere from 50 - 5000 Hz
• The output must be played for 5 seconds
• The output must be in the form of a sine wave

## Test cases

Input: 440

Output:

Input: 200

Output:

Input: 4000

Output:

• This is not about finding the language with the shortest solution for this (there are some where the empty program does the trick) - this is about finding the shortest solution in every language. Therefore, no answer will be marked as accepted.

• Unlike our usual rules, feel free to use a language (or language version) even if it's newer than this challenge. Languages specifically written to submit a 0-byte answer to this challenge are fair game but not particularly interesting.

Note that there must be an interpreter so the submission can be tested. It is allowed (and even encouraged) to write this interpreter yourself for a previously unimplemented language.

Also note that languages do have to fullfil our usual criteria for programming languages.

## Catalogue

The Stack Snippet at the bottom of this post generates the catalogue from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

• Would a function be acceptable? – a spaghetto Nov 15 '15 at 21:51
• @quartata It should be a full program. – Chris Loonam Nov 15 '15 at 21:51
• I see that a lot of people are using a beep() function. Is it guaranteed that that is a sine wave? – geokavel Nov 16 '15 at 5:19
• Windows' System.Console.Beep thing is a sine wave on my (modern, Windows 10) machine, so I think that counts. Solutions on PPCG generally needn't be portable. – Lynn Nov 16 '15 at 11:32
• Is printing out an audio file a valid way to "output a tone"? – Ilmari Karonen Nov 17 '15 at 3:10

# QBasic, 18 bytes (disqualified)

Like @pabouk mentioned, this uses the PC speaker, so it plays a square wave, not a sine wave like the problem asks. (This requirement was added to the problem after this answer was posted, hence the votes.) I'll leave it here for posterity anyway.

INPUT F
SOUND F,91

Play a sound at the inputted frequency for 91 ticks, which is equal to 5 seconds.

• This program does not satisfy the last requirement: "The output must be in the form of a sine wave". SOUND command uses the PC speaker which as designed was driven by a timer chip 8254 which produces a square wave. --- Though in modern PCs 8254 could be missing correct emulation of the SOUND command should produce a square wave. – pabouk Nov 16 '15 at 9:56
• This requirement was added after I posted my answer. :/ I'll add a note, but I feel weird about deleting this answer when it's the one voted to the top. – Lynn Nov 16 '15 at 11:33

# Python2, 40 bytes

from winsound import*
Beep(input(),5000)

Only works on Windows.

• Would 5e3 work? – Lynn Nov 15 '15 at 22:00
• @Mauris No, I had already tried that but it wants integers only. – orlp Nov 15 '15 at 22:01

## Mathematica, 42 bytes

Well if we can use built-ins...

Input[]
EmitSound@Play[Sin[2t%Pi],{t,0,5}]

Thanks to the requirement for a full program, this was the first time I got to use my recently discovered golfing tip of using % (result of last evaluation) to save two bytes.

Mathematica also has a built-in Sound which takes a pitch and a duration as arguments, but unfortunately the pitch has to be given as a musical note. Specifying your own sound wave via Play seems to be the only way to work with a frequency.

# C#, 80 bytes

class P{static void Main(string[]a){System.Console.Beep(int.Parse(a[0]),5000);}}
• Do you know what is the alternative for Beeping in Java? – user41805 Nov 18 '15 at 20:23

## MATLAB, 36 bytes

sound(sin(pi*input('')*(0:8^-4:10)))

Thanks to flawr for saving two bytes.

• You can save one character by using 2^-12 instead of 1/4096. – flawr Nov 17 '15 at 10:49
• Oh silly me, 8^-4 is even shorter! – flawr Nov 18 '15 at 16:28

## PowerShell, 32 bytes

• Is the space really needed? – kasperd Nov 16 '15 at 7:33
• wow, sloppy of me – Nacht Nov 16 '15 at 21:28

# FakeASM, 12 bytes

RDA
BEEP 5e3

Works with the Windows reference implementation (download). It calls Windows' Beep function, which is a sine wave on modern platforms.

• Where's the STDIN? – cat Dec 19 '15 at 13:16
• RDA reads a number from STDIN and puts it in register A. – Lynn Dec 19 '15 at 15:43
• I ended up reading the spec and noticing that but I forgot to delete my comment. thanks, though – cat Dec 19 '15 at 15:43

# Bash + X11, 27 20 bytes

xset b 9 $1 5;echo This contains an unprintable, so here's a hexdump: 0000000: 7873 6574 2062 2039 2024 3120 353b 6563 xset b 9$1 5;ec
0000010: 686f 2007                                ho .

This takes the frequency as a command-line argument and plays the appropriate beep at a volume of 9% (since no volume was specified).

(Note: I was unable to test this due to some issues with my computer, but I'm 99% sure it works.)

• Is there a \x07 in there? – TheDoctor Nov 15 '15 at 23:19
• No, xset does all the beeping stuff. – a spaghetto Nov 15 '15 at 23:20
• @TheDoctor Oh I see what you mean... it does need a \x07 to make the actual noise. Fixing. – a spaghetto Nov 15 '15 at 23:45
• echo <BEL> where <BEL> is the actual character, saves 6 bytes. – Dennis Nov 16 '15 at 16:35
• @Dennis D'oh. Thanks! – a spaghetto Nov 16 '15 at 17:14

## JavaScript, 114 bytes

p=prompt();c=new AudioContext;with(c.createOscillator()){frequency.value=p;connect(c.destination);start();stop(5)}

Requires a somewhat cutting-edge browser, enter the frequency in the prompt. JSFiddle

• This won't play for 5 seconds because prompt() is called after the audio timer is started at new AudioContext(). I believe the shortest you can get this is 115 bytes with c=new AudioContext(p=prompt());with(c.createOscillator()){frequency.value=p;connect(c.destination);start();stop(5)}. – user81655 Nov 16 '15 at 3:45
• Thanks for the heads up, I appreciate the idea of using 'with' as well. – Nickson Nov 16 '15 at 4:00
• I've not seen <script> tags as being required for JavaScript to considered a full program before. I think you're good for the 114. Also, removing the p assignment, and just setting frequency.value=prompt() should get you down to 110. – Mwr247 Nov 16 '15 at 21:11
• @Mwr247 Unfortunately, as user81655 pointed out, the timer which determines when the audio stops starts when the AudioContext is created. The prompt has to appear before then, or a slow input could result shorter audio or no audio at all. – Nickson Nov 16 '15 at 23:13
• p=prompt();with(new AudioContext)with(createOscillator())frequency.value=p,connect(destination),start(),stop(5) this saves 3 bytes – Patrick Roberts Feb 7 '17 at 15:09

# Bash + Linux utils, 95

bc -l<<<"obase=16;for(;t<5;t+=1/8000){a=s($1*t*6.3);scale=0;a*30/1+99;scale=9}"|xxd -p -r|aplay This is a true sine wave. No beeps. Input frequency entered via the command-line: ./hz.sh 440 # Processing, 148114 106 bytes import processing.sound.*; Engine.start().sinePlay(int(loadStrings("s")[0]),1,0,0,0);delay(5000);exit(); (For some reason Processing requires both using the import statement and the new line, otherwise it does not recognize the library.) I still haven't figured out how to pass arguments into Processing, though I know it's possible, so this code requires having a file called "s" in the sketch folder which has the frequency value. If I can figure out how to pass in arguments I could replace file loading with args[0]. ## VB.net, 90 bytes, 74 bytes Module m Sub Main(a() as String) Console.Beep(a(0),5000) End Sub End Module Thanks to Sehnsucht Module m Sub Main() Console.Beep(My.Application.CommandLineArgs.First,5000) End Sub End Module This is my first post so if I did any thing wrong please let me know • That could be reduced getting the command line argument in main args instead Main(a()As String and Beep(a(0),5000) reducing the total to 74 bytes according to byte counter – Sehnsucht Nov 19 '15 at 19:01 # Turbo/Borland/Free/GNU Pascal, 95 bytes Due to issues with the delay function on modern computers (well, anything faster than 200Mhz) trying to run Turbo / Borland pascal, this might not wait 5 seconds, even with a patched CRT library Program a;Uses crt;Var i,k:Integer;BEGIN Val(ParamStr(1),i,k);Sound(i);Delay(5000);NoSound;END. The String to Integer conversion can be done shorter (77 bytes) on FreePascal, and modern derivates, as they have the StrToInt function: Program a;Uses crt;BEGIN Sound(StrToInt(ParamStr(1)));Delay(5000);NoSound;END. • As far as I know, a Pascal program will work fine with all compilers even without the Program a; part, so you can win 10 bytes with omitting it. – vsz Nov 17 '15 at 6:03 • @vsz FPC might be fine (so I can remove that from the second snippet), I still have to check some ancient ones, like TP5.5, as far as I remember they required it. – SztupY Nov 17 '15 at 9:51 • The requirements say “Input must be taken in through STDIN”, so skip that lengthy ParamStr() and just Read() it as required: pastebin.com/1Tw2d0D6 – manatwork Jan 24 '16 at 19:46 ## Perl, 27 bytes Basically a Perl version of the Python answer (also only works on Windows), if we're allowing modules. use Audio::Beep;beep<>,5000 # Vitsy + X11, 20 bytes "5 "WX" 9 b tesx",7O A translation of my bash answer. Does not work in the online interpreter (obviously). Takes input as any non-numeric character followed by the frequency (so for an input of 440 Hz you could do "a440"). ## Explanation "5 "WX" 9 b tesx",7O "5 " Push " 5" WX Reads input and removes the first character (which is used to force string context) " 9 b tesx" Push "xset b 9 " , Pop everything and execute as a shell command. 7O Output bell char. • Cool answer. Nice work. – Rɪᴋᴇʀ Jan 24 '16 at 17:51 • You used Vitsy. <3 My child is redeemed. – Addison Crump Jan 24 '16 at 17:59 # C with WinAPI, 82 bytes #include<windows.h> #include<stdio.h> main(){int x;scanf("%i",&x);Beep(x,5000);} Uses the WinAPI Beep() function. • Don't you need to pass &x to scanf? Also you can declare x as a parameter to main and let the compiler default its type. – Neil Nov 18 '15 at 0:59 • @Neil, I don't know how I mistyped it without passing &x to scanf(). Fixed now; thanks. – user2064000 Nov 18 '15 at 14:55 # Hassium, 38 Bytes func main()Console.beep(input(), 5000) ## Shadertoy GLSL Sound Shader, 86 #define F 440.0 vec2 mainSound(float t){return vec2(sin(6.3*F*t)*(t<5.0?1.0:0.0));} "Input" is given via #define. Outputs a sinewave with approximate frequency of FHz. Rounded 2*Pi to 6.3, instead of "default" 6.2831, but sounds pretty much the same. Sadly there isn't much to golf here. • Input should be taken via STDIN, not hardcoded. – flawr Nov 17 '15 at 10:47 • @flawr I don`t know any methods of precise input on Shadertoy. Please enlighten me. – Kroltan Nov 17 '15 at 11:58 • This is should be your task, not mine. But I do not know Shadertoy anyway so I cannot help you. – flawr Nov 17 '15 at 12:11 • @flawr the only method of reasonable precision would be the cursor position, but that is only available to image shaders, not sound shaders. So I'm pretty much stuck with defines. – Kroltan Nov 17 '15 at 12:14 • Can you load files? That's what I did. – geokavel Nov 17 '15 at 19:01 # Jolf, 4 bytes, noncompeting This language addition came after the challenge. Αc5j Αc create a beep 5 five seconds long j with the input as a frequency The default wave is a sine wave. # SmileBASIC, 84 bytes INPUT F N=LOG(F/440,POW(2,1/12))+57BGMPLAY FORMAT$("@D%D@255T12N%D",(N-(N<<0))*63,N)

Converts from Hz to half steps, and plays the a certain note with the a detune value to produce the frequency.