Generate white noise

15

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.

Giulio Muscarello

Posted 2014-03-05T16:00:26.887

Reputation: 298

What about WebAudioAPI? I coded js program, that plays white noise – Евгений Новиков – 2017-08-02T09:53:25.790

The problem spec seems to be describing a code colfing challenge, but the tags say otherwise. – undergroundmonorail – 2014-03-05T16:02:42.137

Whoops! I'll fix that. – Giulio Muscarello – 2014-03-05T16:03:36.037

Perhaps the challenge should be revised to only allow "existing third party libraries" instead of "any library". – jpmc26 – 2014-03-06T01:02:28.153

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. – Tim Seguine – 2014-03-06T15:13:03.503

Answers

4

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.

Jason R

Posted 2014-03-05T16:00:26.887

Reputation: 156

10

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

TheDoctor

Posted 2014-03-05T16:00:26.887

Reputation: 7 793

4Somehow I don't think the file header can be entirely random / the same as the hard disk drive first sector – John Dvorak – 2014-03-05T16:10:37.360

@JanDvorak - it is for me – TheDoctor – 2014-03-05T16:11:02.357

1Herr Doctor may be right, if the test harness is VLC. VLC is pretty forgiving. – Jonathan Van Matre – 2014-03-05T16:12:21.987

@jon hm... Should have known anything goes in VLC – John Dvorak – 2014-03-05T16:19:53.323

1You've generated noise, but is it white noise? – Stephen Melvin – 2014-03-05T16:20:51.493

1/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. – Giulio Muscarello – 2014-03-05T16:24:32.280

1You can save five characters by executing the command while the working directory is /dev. – David Richerby – 2014-03-06T07:15:54.553

@Steve: The noise should be white, provided that /dev/random outputs a stream of bytes that are jointly uncorrelated with one another. In that case, the autocorrelation function for the random process corresponding to the stream of samples will be zero for all nonzero lags. In the frequency domain, this corresponds to a flat power spectral density, which is the definition of white noise. It's impossible to generate perfectly white noise with pseudorandom number generators, as there is still some amount of correlation between samples, but it's good enough for engineering work. – Jason R – 2014-03-06T12:30:29.137

1You can save three characters by choosing a one-letter-long filename. – Giulio Muscarello – 2014-03-06T13:31:13.223

4

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

DavidC

Posted 2014-03-05T16:00:26.887

Reputation: 24 524

4

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.

Digital Trauma

Posted 2014-03-05T16:00:26.887

Reputation: 64 644

You could save about 1,20 characters by reading /dev/random instead of /dev/urandom, since speed is not a requirement. – Giulio Muscarello – 2014-03-05T21:14:43.133

1output is to stdin? – user253751 – 2014-03-06T00:57:51.130

4

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
}

Jonathan Van Matre

Posted 2014-03-05T16:00:26.887

Reputation: 2 307

one vote for use of Supercollider. There are a lot of other puzzles it'd be GREAT for. Check out [tag:music] and [tag:audio] – Not that Charles – 2014-03-06T15:58:07.203

0

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

ceilingcat

Posted 2014-03-05T16:00:26.887

Reputation: 5 503

0

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()}}

Евгений Новиков

Posted 2014-03-05T16:00:26.887

Reputation: 987