Skip to content

Hints and Tips (* this has content)

Mr Sensorium edited this page Jun 15, 2013 · 3 revisions

Efficient code for smooth audio

  • Write speed-efficient code. This can be tricky, because the Arduino IDE sets the compiler for small code size rather than speed!
  • Pre-calculate where possible. Do as much as you can in setup() and updateControl(), and keep updateAudio() lean.
  • An oscilloscope is useful for timing your code. Mozzi has macros to flip digital pin 13 without interfering much with the audio timing. Include utils.h in your sketch and put SET_PIN13_OUT in setup(), then use SET_PIN13_HIGH and SET_PIN13_LOW around the code you want to time.
  • Use integer and fixed point maths, and avoid floating point, particularly in updateAudio().
  • Avoid division! Use powers of two for numbers where you can, and use bit-shifting arithmetic instead of multiplication and division where possible. Shifting by 8 or 16 is faster than other amounts.

Data types

  • Take care declaring and casting variables - this can affect speed and also be a source of unexpected sounds.
  • For speed, use the smallest data types you can, and unsigned where possible.
  • Make sure there's room for intermediate results!
  • Mozzi has a collection of fixed-point number types and conversion macros in mozzi_fixmath.h. These are usually much faster than floating point, but they can over - or under - flow if you're not careful.

Listening and analysing

  • A computer line-in works well for listening, and some good tools to record or examine your sounds are Audacity, Baudline signal analyser, and SignalScope.
    In Audacity, set the Project Rate to Mozzi's audio rate of 16384 Hz before recording. This will make the PWM artifacts disappear and the waves will look like normal, non-PWM sound. You need to adjust the recording level by ear, because the sound distorts at a lower level than expected even though the levels in the window look OK. If you're stuck with a fixed recording rate and you need to see what's happening in your synthesis, filtering makes a big difference. There's a puredata patch in Mozzi/pd with 4 4000Hz filters in series which can be used with a utility like Jack or Soundflower to filter the audio before it gets to your sound program. This allows you to check what's happening in real time.
  • Printing to the Arduino Serial monitor is a useful tool for debugging, but beware of trying to print too much or your whole computer might freeze! It's mostly OK to print from updateControl() with a control rate of 64, and it works well with a baud rate of 115200. Notice that serial adds size to your sketch and can often disrupt audio, so check if you've got print statements interfering if you're hearing audio clicks. One more thing: to print audio rate debugging info, don't use the Arduino serial monitor, try CoolTerm, it's a great application which performs much better and can capture incoming data to a file.
  • A simple hardware filter is recommended for sending the output to an amplifier/speaker. See the circuits page in the wiki page in the wiki.

Keep up!

  • Mozzi is actively developing, so make sure you've got the latest version.
  • Try the Mozzi forum.