White NoiseWhite noise is a combination of all the different sound frequencies audible to the human ear. This is similar to the static noise you hear from your television when it loses reception. To create white noise in Quorum, we begin by creating an Audio object as well as an AudioSamples object. Next we must set the channels within our noise and specify how long we want our noise to play for (in seconds). Then we assign values within our AudioSamples object to create the sounds. Finally we load our Audio object and play it until it finishes.
Below is an example of a simple white noise generator producing mono sound:
use Libraries.Compute.Random use Libraries.Sound.AudioSamples use Libraries.Sound.Audio Random random AudioSamples samples Audio noise // We set the number of channels and how long we want our noise to play for. samples:SetChannels(1) // This will produce mono sound samples:SetSizeInSeconds(4) // This will play the sound for 4 seconds // To simulate white noise, we must first generate a series of random numbers within the range of -1 and 1. integer counter = 0 repeat while counter < samples:GetSize() // We set the randomly generated number at index (counter) within the channel. samples:Set(counter, random:RandomNumber() * 2 - 1, 0) counter = counter + 1 end // Finally, we load and play the noise we created until it finishes. noise:Load(samples) noise:PlayUntilDone()
The AudioSamples object holds the discrete values of a signal. It can be used to create audio rather than just loading one. The object contains information for the number of channels it holds, the sample’s length in terms of seconds, and the number of samples per channel. A sample with a single channel will produce a mono sound while a dual channel sample produces stereo sound. Mono sound plays the same signal to all speakers. Stereo sounds assign each channel with a corresponding speaker so the signals in each speaker differ. You must first specify how many channels you want the object to hold by calling the SetChannels action. You can change the default sampling rate (44.1 kHz) by calling the SetSamplesPerSecond action. For most cases, leaving the sampling rate of 44.1 kHz will suffice. To change the amount in seconds of audio the AudioSamples object can hold, call the SetSizeInSeconds action.
In the example above, we go through the AudioSamples object by using a counter that increments until it reaches the amount of samples stored in each channel which is set by using the GetSize action. As we go through the object, we set the sample value to the randomly generated number through Random’s RandomNumber action. Each value in the AudioSamples must be between -1 and 1, so when using the RandomNumber action (which returns between 0 and 1), we multiply the random value by 2 then subtract 1 to fit it into the range -1 and 1.
Each channel requires its own values. If we had multiple channels, we would have to provide the samples for each one. In that case the code would look like this:
integer counter = 0 repeat while counter < samples:GetSize() i = 0 repeat while i < samples:GetChannels() samples:Set(counter, random:RandomNumber() * 2 - 1, i) i = i + 1 end counter = counter + 1 end
Playing the Sound
To play the white noise we just created, we use Audio object. The Audio object is used to load our AudioSamples object using the Load action followed by playback of the sound using the PlayUntilDone action. We call these actions as follows:
For the duration of the playback, you should be hearing something similar to the sound of a radio on an unknown station.
Congratulations! You were able to create a white noise generator and successfully played it back. There are also other noises you can create like pink and brown noise. Check them out and see if you can generate them yourself! You can run this program offline or online in a web browser.
In the next tutorial, we will discuss Wave forms, which describes how to create different kinds of sounds..