Micro:bits, Microblocks and WAV files?

Hello,
I am using Microblocks to code the micro:bits with my middle school students. I have discovered how to import and play ringtones using the ringtones library. There is a WAV library also in Microblocks but I cannot figure out how to import a WAV file into Microblocks.

I think it would make micro:bit projects much more compelling if different sounds could be played.
I would greatly appreciate any help here.

Thanks so much,
Bob

Looking at the library, although I've not used it, I think it's targeted at ESP type devices rather than microbits

It seems that m:b is capable to emit, 10 bit samples over analog I/O pin, at 10Khz rate.

Raw sample data can be prepared/encoded the way Turgut Guneysu described for use with OLED Display.SNAP To MicroBlocks - Image & Code Transfer | MicroBlocks Wiki

bkahn,
Playing WAV files in MicroBlocks requires a board with a DAC chip; micro:bit does not have a DAC chip. DAC support can be verified by looking at the tech specs of the board. Adafruit boards: ItsyBitsy MO, Trinket MO, Gemma MO come to mind; also M5Stack, and others.

Then to load a file easily, it needs to be able to support a flash file system. In that case, one can enable menu command under Settings/show advanced. This will make the file put and get commands become visible. Use the file put command to copy a WAV file onto the board flash file system.

Once all this is done, you can use the WAV library to play the file by specifying the full file name, eg: zoop.wav.

A few things to keep in mind:

  • There are other ways to load files onto boards with flash file systems. But the method mentioned is the easiest way.
  • Speaker pins supported under the WAV play function is 25 and 26. If the board you are using has a different speaker pin number, you might have to change the pin number in the block.
  • WAV files come in different internal formats. The particular version play is looking for is RIFF. You can easily see this if you open the file in ie Notepad++:

RIFF�
WAVEfmt + + data�

[Reference for WAV/RIFF](WAV - Waveform Audio File Format

  • I noticed that if RIFF is not at the beginning of the file, it will not play the file.
  • If RIFF followed by WAVE followed by fmt is visibe, you are good to go.

Hope this works out for you.

And just a minor issue - can you click on the big pencil on the title of your 1st post and change the category to
image

Thanks :slight_smile:

Thank you for your responses. I do not see the big pencil on my 1st post.

Not a problem :slight_smile:

I made an audio playback "proof of concept" project.
Raw samples are played trough analog output (PWM@27, buzzer) of micro:bit v2 when button A is pressed.

Script source - copy&paste @ Microblocks IDE
GP Scripts

script 415 87 {
whenButtonPressed 'A'
for sample ('[data:asByteArray]' samples) {
  analogWriteOp 27 (sample * 8)
  waitMicros 200
}
}

script 808 77 {
whenStarted
samples = ''
forever {
  if ((size (getLastBroadcast)) > 10) {
    samples = ('[data:join]' samples (getLastBroadcast))
    sendBroadcast ''
    waitMillis 1
  }
}
}

There is also a Snap! "loader" project.
Sound samples from the Snap! project are prepared (downsampled normalized, scaled) and transmitted to Microblocks VM with broadcast over USB connection (I'm not aware of any other method to set large binary object for microbit projects).
SmallVM serial protocol

Hi dardoro,

I got some transmission across to mbv2, but the sound at the receiving end is very hard to make out.

It seems like it was playing some other data before the selected one. the actual selected one is always at the end and truncated.

after severall tries and resets, I got a reasonably decent hasta la vista. :rofl:

I'll try to play with it over the week end.

Yeees... It's a shame, but for the simplicity, there is no flow control of any kind ;(
At least playback delay control and reset buffer should be implemented e.g. by broadcasting of the short packet.

Snap! project, with the "ratio" set to 10, prepares data ~@5kHz sample rate (so a delay of 200 mikrosec is applied during playback). To ensure simple, flawless data transfer, bytes are further scaled to <128.

I was testing some, naive, downsampling/low pass algorithms - average over the range and every (1/ratio)th sample, but can't choose the better one.