Detour: A Simple White Noise Generator
For this week’s blog post I decided I would just investigate
the process of generating audio to begin with. We did some manipulation of
audio but I thought it would be helpful to dive into an easier example of generating
audio so the way it works will be clearer. To demonstrate this I will work off
of an example listed in the JUCE framework tutorials. There is an outline for
how to build a white noise generator and it is helpful for understanding how to
generate audio.
This project is built differently from the starting code we
are given when we choose to generate a new audio plugin. You can go to the JUCE
website and download the code from their tutorial page. The code that actually
generates the white noise is contained within a method called
getNextAudioBlock(), but this same code can be placed inside the processBlock()
function of the code we used before and it will do the same thing. I wanted to
take a step back and hit this information just because we can apply all of the
things I have addressed before (using the processBlock() method to generate/
process audio, and to adjusting parameters like volume with a slider. I think it
will be helpful for anyone reading this post to be able to put this quick and
easy project together and be able to actually create something that they will
be able to hear without having to use some kind of audio interface and/or instrument.
In other words, this project can be created without any additional equipment
than your computer, so it is very accessible.
Once you download
the code from the JUCE tutorial page you will already be able to run this
project as it is all made for you, but I will walk through the code, explain
what it does, and relate it to what I intend to do moving forward with
different distortion effects. This project just contains a main.cpp file and a
few different head files. You can choose to include whichever you want and
explore the different capabilities of each. When you are creating audio applications
using the JUCE framework it is important to know which application template you
choose. The template we chose before was the basic plugin template which would
be desired for creating different effects, using MIDI control devices (think keyboards
for creating and recording synth sounds), and loading the application into a
digital audio workstation. For simple applications like the white noise
generator its easier just to use the basic audio application template, and this
is what they did for the tutorial. This is not very well explained and using
the different templates gives you different function names for audio processing
so I thought I would explain that here.
The getNextAudioBlock() function -where the white noise will
be generated- is contained in the head files. The first head file is just a
basic noise generator with no UI components, the second contains a volume level
slider, and the third is the same as the second but with some code that will smooth
out sharp changes in volume to prevent any kind of audio artifacts like pops
from being introduced by sudden volume changes. The first two are what I will
explain here. To create a white noise generator, all you need to do is generate
random output values between -1 and 1, as this is how sound is represented in
the digital world. Outputting values of close to or at -1 or 1 can result in
very loud output if you have powerful enough speakers so be careful outputting
values this high. Random float values in this range can be generated by the
juce::Random object. The getNextFloat() method is called to create a new random
value. In this tutorial the random values are multiplied by 0.25 and subtracted
by 0.125. This results in values between 0.125 and -0.125 – a much quieter
signal. To output this, each sample in the audio output buffer must be set
equal to this random value. When the program is run, random noise will be
generated and you will hear it output to whatever speakers you are using, be
they headphones, built in speakers, or external monitors. The next head file
contains code for a simple volume slider like we implemented before, only this
time it does not affect audio input, it just affects the values being generated
randomly. The slider value is set between 0 and 0.25 and the random values are
multiplied by this value, reducing the volume or allowing it to go up to 0.25.
The point of stepping into this tutorial before dealing with
more audio input processing is just to demonstrate clearly what is happening and
to give you an example you can build yourself and play with. If you do not have
an instrument and audio interface in hand to use this is something you can build
and ask me questions about. Maybe you can even use the white noise generator to
help you sleep, I know that is something some people enjoy. Random output
values generate random noise, but output that follows a wave function will be heard
as a note. Waves that are shaped and “clipped” will result in a distorted sound
and that is the essence of what I was trying to explain before.
What type of random-number distribution is that Random object intended to generate? (uniform, Gaussian, etc.)
ReplyDelete