Simple game sounds on piezo speaker

Posted on
  • I have a little piezo speaker hooked up to a Pico. I'd like to generate some simple 8-bit game-type sounds for it.

    What I'd really like to be able to do is export sounds created on (they have JSON export) and add them to Espruino projects.

    The sound would need to play in the background so I think this would need to be implemented as some kind of task, but beyond that am not sure where to start.

    Any ideas appreciated!

  • I'd never come across beepbox - that's really cool!

    Looking at the JSON it looks like you'll want to scan over it and change its form a bit first - basically go over all of patterns.notes.points and write them into times[patterns.notes.points.tick]=patterĀ­ns.notes.pitches.

    Then it'd just be a matter of using setInterval to scan over the times array one item at a time and do a beep if one was required.

    Of course you could use Waveform too, but I'd start out simple :)

  • I'll give it a go. I don't know how they do the 'trill' type sound when two notes are to be played together. I assume they alternate between the two notes very fast and setInterval may not be fast enough. Maybe this isn't how it's done but it doesn't sound like a proper chord. My music knowledge very limited!

  • In hardware there's a good hack for two notes at the same time - which is just to attach the piezo to two signal pins and use PWM on both (= two channels).

    However if you use Waveform then that automatically does polyphonic sounds - so you just need multiple Waveform classes and you can set them all playing on the same pin at once (although you have to keep the sample rate low - 4kHz or so - to make sure there's enough CPU time).

  • That's a good tip to use two output pins -- will try it!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Simple game sounds on piezo speaker

Posted by Avatar for jugglingcats @jugglingcats