16
\$\begingroup\$

The challenge is: generate an audio file made of white noise.
Requirements and instructions:

  • Your program must generate an audio file containing only white noise, meaning its intensity is the same for all (reasonable) frequencies and nonzero [see an example plot];
  • One must be able to play the audio file on the latest version of VLC [at the time of writing your answer];
  • The program doesn't need cryptographically-strong randomness, rand functions or reading from /dev/rand is okay;
  • The program must be able to generate at least 1 hour of audio, at least in theory (meaning system limitations like maximum filesize don't apply);
  • Your score is the number of bytes in your source code, plus all bonuses that apply;
  • Using any third-party, external library is okay;
  • The program must work without access to the Internet.

Bonuses are:

  • -15%: Allow to specify the format of the audio file (at least two choices; the number of possible choices doesn't change the score);
  • -10%: Allow to specify the duration of the audio file;
  • -5%: Allow to specify the bitrate of the audio file.

The settings can be stored in variables, files, or given as command line parameters, your choice. Percentages are calculated from the original number of bytes, before any bonus is applied.

\$\endgroup\$
5
  • \$\begingroup\$ The problem spec seems to be describing a code colfing challenge, but the tags say otherwise. \$\endgroup\$ Mar 5, 2014 at 16:02
  • \$\begingroup\$ Whoops! I'll fix that. \$\endgroup\$ Mar 5, 2014 at 16:03
  • \$\begingroup\$ Perhaps the challenge should be revised to only allow "existing third party libraries" instead of "any library". \$\endgroup\$
    – jpmc26
    Mar 6, 2014 at 1:02
  • \$\begingroup\$ Are you going to be a stickler on the noise being white? If you really want to verify: its autocorrelogram should have a clear spike around zero, and almost nothing at any other delay. If you aren't going to be a stickler, I say that it is pointless to specify that it be white noise. Just say noise. \$\endgroup\$ Mar 6, 2014 at 15:13
  • \$\begingroup\$ What about WebAudioAPI? I coded js program, that plays white noise \$\endgroup\$ Aug 2, 2017 at 9:53

7 Answers 7

10
\$\begingroup\$

Bash, 34

dd if=/dev/sda of=file.wav count=1

If you don't want hard drive 'randomness', (a lot slower)

dd if=/dev/random of=file.wav count=9

SPIN, 28

word x=0
repeat
 word[?x]=?x
\$\endgroup\$
9
  • 4
    \$\begingroup\$ Somehow I don't think the file header can be entirely random / the same as the hard disk drive first sector \$\endgroup\$ Mar 5, 2014 at 16:10
  • 1
    \$\begingroup\$ Herr Doctor may be right, if the test harness is VLC. VLC is pretty forgiving. \$\endgroup\$ Mar 5, 2014 at 16:12
  • 1
    \$\begingroup\$ You've generated noise, but is it white noise? \$\endgroup\$ Mar 5, 2014 at 16:20
  • 1
    \$\begingroup\$ /dev/random also exists on Macs, yay! Unfortunately iTunes can't play the resulting wav file. Not that I can complain - I opted for VLc because of its forgiveness and multitude of native codecs. \$\endgroup\$ Mar 5, 2014 at 16:24
  • 1
    \$\begingroup\$ You can save five characters by executing the command while the working directory is /dev. \$\endgroup\$ Mar 6, 2014 at 7:15
5
\$\begingroup\$

Bash + ALSA, score:44 (52 chars - (10% + 5%) bonuses)

Longer than the other bash answer, but accepts duration and bitrate. Also adds a reasonably correct header to the file, so should be reasonably portable:

arecord -r$2|head -c44;head -c$[$2*$1] /dev/urandom

Save as a script, chmod +x it and run:

$ ./wav.sh 1 44100 > c.wav
Recording WAVE 'stdin' : Unsigned 8 bit, Rate 44100 Hz, Mono
$ 

Note, .wav output is to stdout, so it must be redirected to a file.

\$\endgroup\$
2
  • \$\begingroup\$ You could save about 1,20 characters by reading /dev/random instead of /dev/urandom, since speed is not a requirement. \$\endgroup\$ Mar 5, 2014 at 21:14
  • 1
    \$\begingroup\$ output is to stdin? \$\endgroup\$
    – user253751
    Mar 6, 2014 at 0:57
4
\$\begingroup\$

MATLAB, 25

wavwrite(rand(8e3,1),'a')

writes a new WAV file to disk called a. It has a sample rate of 8 kHz and a 16 bits per sample in signed integer format. The source data is uniformly distributed on the interval [0,1], which is mapped to the interval [0,32767] after the conversion to integer format.

MATLAB, 28 - 4 (10% + 5%) = 24

I'm not sure what the OP meant about how settings could be stored in variables, but I interpreted it in a way that is favorable to this case. Assuming that:

  • The desired bit rate (in bits/second) is provided by the user in the variable b. The bits per sample is hard-coded at 16.

  • The desired duration of the file (in samples) is given in the variable d.

The result is:

wavwrite(rand(d,1),b/16,'a')

MATLAB, 16 - 4 (15% + 10%) = 12

Adding another layer of sleaze in pursuit of bonuses, I make another assumption: the desired function to use to output the file should be specified in the variable f. Then the code simplifies to:

f(rand(d,1),'a')

Allowable values for the function are:

f = @wavwrite

or

f = @auwrite

Each function will cause the above snippet to write out a file of the appropriate format (WAV or .au) at a sample rate of 8 kHz with the specified duration. I took off the bonus for specification of the bitrate here, because auwrite defaults to 8 bits per sample instead of 16 like wavwrite does. I don't see a way to harmonize the two without using more characters.

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

Mathematica 52 - 5 = 47

g exports a white noise .wav file of s seconds and 8000 bps.

g@s_:=Export["p.wav",RandomReal@{-1,1}~Play~{t,0,s}]

Example: a 6 second white noise file is exported.

g[6]

p.wav

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

Supercollider, 89 - 10% = 80.1 bytes

Sadly, despite being deliberately made for generation of sound/audio, this language is not going to win here. But it's the first appearance of Supercollider on Code Golf, so that's cool!

This submission loses primarily because setting up recording and making it happen is a verbose process owing to the client/server design of this language. Still, it's a cool language with a lot of power in very little code when you ask it to do things more complex than mere white noise.

File duration is set by changing the wait() value. I could put it in a variable, but there's really no point to that as Supercollider has no stdio to speak of. The interactivity is in manipulating the code live while the server is still playing. Essentially, the IDE is the I/O (unless you build a UI for your creation).

Here's the golfed version:

{WhiteNoise.ar(1)}.play;s.prepareForRecord;Routine.run{s.record;wait(99);s.stopRecording}

Here's a golfed version with the option to record in either aiff or wav, and specify a sample format (int16, int8, and float are all options). Unfortunately, even with all the bonuses, the version above fares better. This would be 139 - 30% = 97.3 bytes.

s.recSampleFormat='int16';s.recHeaderFormat='wav';{WhiteNoise.ar(1)}.play;s.prepareForRecord;Routine.run{s.record;wait(99);s.stopRecording}

And here's an ungolfed version of the latter, so you can see what's going on.

s.recSampleFormat='int16';
s.recHeaderFormat='wav';

{WhiteNoise.ar(1)}.play;
s.prepareForRecord;

Routine.run{
    s.record;
    wait(99);
    s.stopRecording
}
\$\endgroup\$
1
  • \$\begingroup\$ one vote for use of Supercollider. There are a lot of other puzzles it'd be GREAT for. Check out music and audio \$\endgroup\$ Mar 6, 2014 at 15:58
1
\$\begingroup\$

JavaScript, 167 bytes

CAUTION: Decrease volume before run. White noise is nasty

Don't generates file, maybe not what was expected.

-4 bytes hack play noise on left channel only

c=new AudioContext()
n=c.createScriptProcessor(s=512)
n.connect(c.destination)
n.onaudioprocess=e=>{a=s;while(a--){e.outputBuffer.getChannelData(0)[a]=Math.random()}}

\$\endgroup\$
1
  • \$\begingroup\$ Man, didn't read your caution .... nearly was falling from the sofa... \$\endgroup\$ Sep 27, 2022 at 18:58
0
\$\begingroup\$

C 127 115 bytes

#define H htonl
main(c){for(write(1,(int[]){H(779316836),H(24),-1,H(2),H(8000),H(1)},24);;write(1,&c,1))c=rand();}

The majority of the code writes the header for a *.au file. This prints a pseudorandom sound file to standard out.

The sample rate can be adjusted by changing the 8000.

The duration can be adjusted by hitting ctrl-c whenever you want to stop :-)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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