Forum
    • Categories
    • Register
    • Login

    [BLOG] How does DSP work? [Part 1]

    Scheduled Pinned Locked Moved C++ Development
    6 Posts 5 Posters 51 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • griffinboyG
      griffinboy
      last edited by griffinboy

      How does DSP work? [Part 1] (an article for absolute beginners)

      Audio programming does not look like audio.

      c5313d73-44fb-4b88-9622-2f597f65f9ee-dsp-math-sketch.png

      It looks like values and numbers.

      And that's exactly what makes DSP possible.
      Once sound is represented by numbers, code can manipulate it!

      Without further ado, let me explain how audio gets represented as data, and how changing that data changes what you hear.

      Audio as Data

      A speaker makes sound by moving.

      400e18a1-9d5c-4313-bfe2-03ea50b775fe-speaker-waveform.gif

      The line at the top is a waveform.

      For now, think of it as the shape the speaker is following.

      When the waveform moves up, the cone moves one way.
      When the waveform moves down, the cone moves the other way.

      That movement pushes air, and we hear it as sound.


      Real audio is generally faster and messier, but the idea does not change:
      a speaker is following a changing value over time.

      So, if we want to create a sound,
      we need to create a waveform.

      The waveform as numbers

      In digital audio, a waveform is stored as numbers.
      A tiny piece of a waveform might look like this:

      [0.00,  0.70,  0.82,  0.27,  -0.51,  -0.86,  -0.51,  0.27]
      

      So, what is this?

      Well, digital audio does not store a "picture" of a waveform.
      It stores the height over time.

      Plot those heights from left to right, and the waveform appears.

      1e87fe4d-41b8-4f27-a13b-4c8f12a555f8-sample-array-to-plot.png

      These height numbers are called samples.
      Because each number is one tiny "sample" of the waveform height at one moment in time.

      The order matters because the order is time:

      73a0dd64-1d05-42a1-9db1-893ecfa99eec-sample-table-playback.gif

      The array of data is the digital waveform. The drawing is the same data made easier to see.

      Lots of samples

      The example I showed you above, is tiny on purpose.
      Real audio has far more samples than this.

      I'm sure you've heard of "Sample rate".

      44.1k, 48k. 96k, etc.
      With 48k being a common DAW sample rate.

      Well, running a program at 48 kHz means that one second of mono audio contains 48,000 sample values.

      That's the kind of high resolution that produces smooth waves.
      The simple drawings in this article are just a readable version.
      But real audio is the same thing packed more tightly in time.

      5719a9b4-2c59-4ce5-ab1a-b71224c0bb95-dense-sample-waveform.png

      (^ That's just something to be aware of. You don't have to worry about it for now, since I'll be continuing to use small pieces of audio for the sake of this article).


      So far we have looked at audio as one long strip of sample values.
      But we have not talked about how a plugin generates this big stream of audio.

      So let's talk about chunks / buffers.

      Chunks (Buffers)

      Real time DSP does not work on a big, long, sound in one go.
      - It processes chunks of audio.

      9c4096e2-3694-4ded-9d60-66cffae5cc0f-waveform-buffer-split.png

      A DSP effect takes a short chunk of audio, processes it, then moves on to the next chunk.

      An effect is like this:

      A chunk comes in.
      The effect changes the numbers inside it.
      The chunk goes out.
      Then the next chunk comes in.

      This is the basic shape of a normal audio effect.

      da8e66ea-dfc9-4e57-a281-63b38c6b8106-buffer-through-dsp.gif

      (The reason we process in small chunks of sound is efficiency related. But it's also practical:
      It's quite useful to have access to a whole portion of audio at a time)

      A simple example: volume

      Let's look at a real-life example.
      Volume is a good example because it's easy to see.

      cfe8b407-2b25-4c75-82df-5d5077c169cc-hise-gain-node.png

      You probably already know:
      Volume is the height of the waveform.

      Making a waveform less tall means the speaker isn't moving as much.
      In other words, height affects volume.

      de975f42-89a3-4f64-877b-554074d41d3c-waveform-volume-scale.gif

      ...And earlier we saw that we store waveforms as height values.

      (you can see where I'm going with this).

      If we want to make the waveform quieter,
      all we need to do is make the sample values smaller.

      For example if we multiply every sample by 0.5 (half):

      The shape is the same.
      The height is half as large.

      9388e6dc-9e8b-4ee8-9422-5066a1096341-sample-gain-comparison.png

      We've made the waveform half as loud.

      The algorithm for a volume effect then boils down to:

      f837f20d-a86c-4c31-a30c-97ae75417488-hise-gain-node-code.png

      In pseudocode form:

      receive buffer
      
      for each sample in the buffer
      {
          sample = sample * gain;
      }
      
      ^ do the above for every buffer we receive
      

      So:

      If gain is 1.0, the sample values stay the same.
      If gain is 0.5, the waveform is half as tall.
      If gain is 2.0, the waveform is twice as loud/tall.
      If gain is 0.0, every sample becomes zero, which is silence.

      In actual C++ DSP:

      0ab2a9dd-9b10-4a29-b1ee-36186842477b-hise-gain-node-cpp-code.png

      (^ I bet you understand this now!)

      The main part of the effect is a function that receives a buffer of samples (a chunk) and processes them using math.
      That's what DSP code looks like!

      The real HISE gain node has some extra controls and parameter smoothing, but the core audio operation is still this simple multiplication.

      The Lesson

      Honestly that's pretty much it.
      DSP is not very complicated.

      Most Audio effects work by:

      Take a buffer of sample values,
      change those values,
      and pass the buffer onward

      A Gain effect multiplies the samples.
      
      A Distortion bends the waveform shape by changing the samples.
      
      A Delay stores samples and plays them back later.
      

      Oscillators are a slightly different case. They create new sample values rather than affecting incoming ones.

      0409a709-0ce4-4805-84c9-c680373dfdf4-oscillator-block-factory.gif

      ^ And I'm if being 100% honest, this isn't quite the whole picture.

      If we zoom out a little, you'll see that there is actually a bigger loop going on.
      Where something is sending us a buffer, and asking our DSP to process or fill it.
      Then it takes that buffer back and sends it to the next place.

      (in our case, that "something" is HISE / JUCE).

      f79a05e7-920f-44c5-96e3-bcb2cc2b72f7-plugin-chain-routing.gif

      For an effect, something is going to send us a chunk and say "please process this" and we do that.

      For an oscillator, we might be given an empty chunk and we just need to write a waveform into the chunk and send that back.

      To Recap

      1. The table, the waveform, the buffer.
      They are different visualizations of the same thing:
      sample values over time.

      9ae6b3ad-0c76-440b-9247-d89d85dac4cf-recap-signal-views.png

      2. Audio plugins work by processing chunks of samples. These chunks are passed around between different oscillators and effects.

      f79a05e7-920f-44c5-96e3-bcb2cc2b72f7-plugin-chain-routing.gif

      3. Down the line, part of the program will mix the chunks and send them to the speaker and we will hear the waveform (we don't usually have to worry about that part, Hise or Juce will take care of those parts for us).

      a62c0ed0-1d53-4476-89d3-bc2628f0ba84-output-stream-to-speaker.gif

      Next: filters and other effects

      I know what you're thinking:
      Gain makes sense, but how do we create filters and other kinds of effects? What kind of maths causes those?

      Look forward to [Part 2].

      David HealeyD ulrikU lalalandsynthL 3 Replies Last reply Reply Quote 8
      • David HealeyD
        David Healey @griffinboy
        last edited by

        @griffinboy Nice, would work well in the HISE documentation

        Free HISE Bootcamp Full Course for beginners.
        YouTube Channel - HISE tutorials
        My Patreon - More HISE tutorials

        griffinboyG 1 Reply Last reply Reply Quote 1
        • griffinboyG
          griffinboy @David Healey
          last edited by griffinboy

          @David-Healey
          Thank you!
          Maybe with some refinement :)

          It's far from perfect.

          This explanation is just my own attempt to boil down the theory into a simple form.
          ...and it still ended up a bit of a ramble!

          I think it's quite a tricky subject to teach, because in real life there are so many implications and exceptions to every rule.
          Even simple DSP can look a bit monstrous when you layer ontop all of the common optimizations and C++ tricks.

          1 Reply Last reply Reply Quote 1
          • ulrikU
            ulrik @griffinboy
            last edited by

            @griffinboy Thanks, that's well explained!
            Looking forward to next lesson.

            Hise Develop branch
            MacOs 15.6.1, Xcode 16.2
            http://musikboden.se

            1 Reply Last reply Reply Quote 2
            • lalalandsynthL
              lalalandsynth @griffinboy
              last edited by

              @griffinboy exciting :)

              https://trikk.studio/
              https://www.instagram.com/trikkstudio/

              1 Reply Last reply Reply Quote 0
              • S
                scottmire
                last edited by

                Thank you so much for doing this. Truly great.

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post

                16

                Online

                2.4k

                Users

                13.9k

                Topics

                120.8k

                Posts