An audio waveform is a graph displaying amplitude changes over time. Zoomed out, a waveform looks rather simple and smooth, but zoomed in, we can see tiny variations – it is those variations that represent the sound.
To illustrate how a waveform works, imagine a speaker cone that's is at rest when the amplitude is at 0. If the amplitude moves to a negative value of 1, for example, then the speaker moves backward a little bit, or forward in the case of a positive value. For each amplitude variation, the speaker will move, making the air move, thus making your eardrums move.
The bigger the amplitude is in the waveform, the more the speaker cone moves in terms of distance, and the louder the sound. This is expressed in decibel (dB), a measure of sound pressure.
The faster the movement, the higher the pitch. This is expressed in hertz (Hz).
In the following image, we can see the MIDI file from the previous section played by instruments to make a WAV recording. The instrument that's being used is a 1982 Roland TR-808 drum sample pack. You can visually match some instruments, such as double the Conga Mid (MIDI note 48) at around 4.5 seconds. In the upper right corner, you can see a zoom of the waveform at 100th of a second to show the actual amplitude change:
The script for plotting a WAV file can be found in the GitHub code for this chapter in the Chapter01/provided folder. The script is called wav2plot.py.
In machine learning, using a raw audio waveform used to be uncommon as a data source since the computational load is bigger than other transformed representations, both in terms of memory and processing. But recent advances in the field, such as WaveNet models, makes it on par with other methods of representing audio, such as spectrograms, which were historically more popular for machine learning algorithms, especially for speech recognition and synthesis.
Bear in mind that training on audio is really cost-intensive because raw audio is a dense medium. Basically, a waveform is a digital recreation of a dynamic voltage over time. Simply put, a process called Pulse Code Modulation (PCM) assigns a bit value to each sample at the sampling rate you are running. The sampling rate for recording purposes is pretty standard: 44,100 Hz, which is called the Nyquist Frequency. But you don't always need a 44,100 Hz sample rate; for example, 16,000 Hz is more than enough to cover human speech frequencies. At that frequency, the first second of audio is represented by 16,000 samples.
If you want to know more about PCM, the sampling theory for audio, and the Nyquist Frequency, check out the Further reading section at the end of this chapter.
This frequency was chosen for a very specific purpose. Thanks to the Nyquist theorem, it allows us to recreate the original audio without a loss of sounds that humans can hear.
The human ear can hear sounds up to 20,000 Hz, so you need 40,000 Hz to represent it in a waveform since you need a negative value and a positive value to make a sound (see the explanation at the beginning of this subsection). Then, you can add 4,100 Hz for rounding errors on very low and very high frequencies to make 44,100 Hz.
This is a good example of a sampled (discrete) representation that can be reversed to its original continuous representation because the pitch spectrum the ear can hear is limited.
We'll look at audio representation in more detail in Chapter 5, Audio Generation with NSynth and GANSynth, since we are going to be using NSynth, a Wavenet model, to generate audio samples.