Audio output

So, the NES(Nintendo entertainment system) would output audio through five channels. These channels had their own wave form and the composers would use this for synthesizing music. For those who have made emulators, I would like to know how to output square, saw, sine, triangle and noise.
Let me start by going over some PCM basics (note I'm simplifying a lot here to get the concept across):

PCM audio data consists of a bunch of "samples". Each sample is basically a snapshot of an audio waveform at a single moment in time. Similar to how a "frame" is a snapshot of video for one moment in time. If you string a bunch of samples together, they'll form a wave -- and this wave actually represents how the speaker cone will move, and thus will dictate the type of sound that is generated.

Think of a line graph, where the X axis is time, and the Y axis is the value of each sample. When you connect the dots, you form the sound wave.

For example, let's say you have the following samples:

0, 2, 4, 5, 5, 5, 4, 2, 0, -2, -4, -5, -5, -5, -4, -2

This would produce the below waveform:

+5|   ***
  |  *   *
  |
  | *     *
  |
 0|*       *       
  |
  |         *     *
  |
  |          *   *
-5|           ***




Since sound is just vibrations, in order for anything to actually be audible, the waveform has to have movement. A simple tone is just a wave repeating itself.

The wider a repeating pattern is, the lower the pitch, whereas the narrower it is, the higher the pitch.
The taller a pattern is, the louder it is. Whereas the shorter it is, the quieter it is (with a flat line being silence).



Pulse waves are a simple alternating 'hi/lo' pattern... with the ratio of hi:lo being known as the "duty". A "square wave" is simply a pulse wave with 50% duty -- called that because it looks like a square.

50% Pulse Wave (aka, "square"):

****    ****
    |   |   |
    |   |   |
    ****    ****


25% Pulse Wave:

**      **  
  |     | |
  |     | |
  ******  ******


With waveforms, the "rounder" the wave, the softer the tone. So a 50% pulse wave will sound softer/smoother than a 25% pulse.




Triangles are a progressive rise/fall rather than a simple toggle:

   *           *
  * *         * *
 *   *       *   *
*     *     *     *
       *   *       *   *
        * *         * *
         *           *



Sawtooth is similar in that it has a progressive rise, but then a sudden drop
(or a progressive drop with a sudden rise):

     *     *
    * |   * |
   *  |  *  |
  *   | *   |
 *    |*    |
*     *     *



Noise is... well... noise. It's basically just random output which creates fuzzy audio. On the NES, the Noise channel was very similar to the Pulse channels in that it merely toggled between hi/low outputs... but the distance between each toggle was sort of randomized:

*    ***   **  *       ***      **********   **    ****    ****   ***
 |   |  |  | | ||      |  |     |         |  | |   |   |   |   |  |
 |   |  |  | | ||      |  |     |         |  | |   |   |   |   |  |
 |   |  |  | | ||      |  |     |         |  | |   |   |   |   |  |
 |   |  |  | | ||      |  |     |         |  | |   |   |   |   |  |
 ****   ***  ** *******   ******          ***  ****    ****    ***


I say "sort of" randomized because the NES allowed you to control the pitch of the noise, too. So it was like controlled randomness.






So as I said before... the "width" of a tone determines its pitch. Specifically, a tone is measured in Hertz. Hertz (in this context) is the number of times a pattern repeats in 1 second. So if you are repeating that triangle pattern 440 times in 1 second, then you are producing a 440 Hz tone -- or concert A. Or if you are repeating it 261.626 times in 1 second -- then you're producing middle C.


Ultimately, you generate the audio data for the waveform you want, and then you just output it as PCM.
How do I output it as PCM.
Last edited on
Get an audio lib.

SDL, SFML, BASS, FMOD all have PCM streaming capabilities.
Thanks.
Topic archived. No new replies allowed.