Skip to content

Hints and Tips (* this has content)

sensorium edited this page Nov 19, 2012 · 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 fixedMath.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 you can record and examine your sounds with Audacity (http://audacity.sourceforge.net/) and Baudline signal analyser (http://www.baudline.com/). Set the Project Rate in Audacity to Mozzi's audio rate of 16384 Hz before recording, to make the PWM artifacts disappear, so the waves 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.
  • 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.
Clone this wiki locally