Animal sounds game (images and audio)

Animal sounds game (images and audio)

by Citilab Edutec

Boards
Boards:

Citilab ED1

Component/s
Components:

None
This activity is part of the Citilab Course. Check it out!

Playing Audio with MicroBlocks

Boards Only for boards with sound and a file system, such as ED1, micro:STEAMakers, M5Stack-Core, and others.

  • In other activities, we have seen how to generate sounds, for example, using the Tone or Ringtone libraries.

  • With MicroBlocks, it is possible to play audio files stored on the board using the WAV library from the Sound category.

WAV Library

  • The WAV library allows playing audio files in WAV format. This library does not support compressed files.

  • The file must be prepared in advance to reduce its size. Once ready on the computer, it should be uploaded to the board’s file system.

Preparing WAV Files

  • To minimize storage space or convert another file format to WAV, it is recommended to use the free software Audacity or an online converter.

  • Recommended settings to balance audio quality and file size: mono files, a sample rate of 22050 Hz, and 8-bit quantization.

Using Audacity
  • If the file is stereo, convert it to mono using the Mix option in the Tracks menu, which allows mixing stereo tracks into mono.

Convert to Mono

  • A maximum sample rate of 22050 Hz is recommended. To do this, select Resample… from the Tracks menu and also modify the Project Rate (Hz) at the bottom left of Audacity.

Convert to 22050 Hz

  • To save the file, choose Export → Export as WAV from the File menu. The default option is WAV (Microsoft) Signed 16-bit PCM, but selecting Unsigned 8-bit PCM will reduce the file size by half while maintaining sufficient quality for these devices.
Using Online-Converter
  • On the Online Converter website or similar, upload the audio file (mp3, ogg, wav, aac, mp4, etc.), select the recommended settings, and save the file to the computer.

  • Audio files take up significant filesystem space. A 30-second file at 22050 Hz, stored at 8 bits, takes up approximately 600 KB. Since the total file storage space on an ED1 is 2 MB, this allows for a maximum of 2 minutes of audio.

  • For smaller file sizes, use short audio and trim silent sections at the beginning and the end.

Uploading the Sound File to the Board

  • The easiest way to upload the file is by dragging it directly into MicroBlocks.

  • Another method is by enabling MicroBlocks’ advanced options. Click the options icon Options and select advanced mode. Then, in the file icon File Menu, choose put file on board, and select the file from the computer.

Upload File

Playing Audio in MicroBlocks

  • Once the library is loaded, it contains a single block, play WAV file _, where you enter the audio file name.

Play WAV

Clicking on the block will play the WAV file through the board’s speaker.

Displaying Images with MicroBlocks

Boards Only for boards with a file system and a TFT screen, such as ED1, M5Stack-Core, CoCube, Boardie, and others.

  • With MicroBlocks, it is possible to display image files stored on the board using the BMP library from the Graphics and Displays category.

BMP Library

  • The BMP library allows displaying BMP (Bitmap) images.

  • The file must be prepared beforehand to fit the screen and reduce its size. Once ready on the computer, it should be uploaded to the board’s file system.

Preparing BMP Files

  • To minimize storage space or convert another file format to BMP, it is recommended to use the free software GIMP or an online converter.
Preparing the Image with GIMP
  • First, resize the image to fit the screen. If the image is larger, only a portion will be displayed, and it will take up more space. Select Scale Image… from the Image menu and enter the screen dimensions (e.g., for ED1: 128x128).

Resize in GIMP

  • If the image proportions do not match the screen, crop it using Canvas Size in the Image menu.

  • Finally, save the image as a BMP file. Use File → Export As, choose the location and filename with a BMP extension, then open Advanced Options in the Export BMP Image dialog.

Export BMP in GIMP

  • It is recommended to use 16-bit formats, as other formats take up more space without any perceptible difference in quality on these boards.

Animal Sounds Game

  • Before programming the game, we need to load the images and sounds. Simply drag them into MicroBlocks or use put file on board from the File icon File Menu. Advanced options must be enabled to use this feature.

  • To check that everything is correct, use the file names block from the Others → Files library.

File names block

  • In the same library, we find the file system data block which shows information about the space used and the total.

File system info block

Displaying the Animals

  • To display an animal, use the display BMP file _ at x _ y_ block from the Graphics and Displays → BMP library.

Show BMP

Real Dog Image

  • Create a list with the animals names and a bmp variable to easily switch images.

Animal List and bmp Variable

  • Next, use the left and right buttons to switch animals. The bmp variable values must be limited.

Right Button

Playing Animal Sounds

  • To play sounds, use the play WAV file _ block from the Sound → WAV library.

Play WAV File

  • This time, use the up and down buttons and a wav variable to control the audio file to play.

Down Button

End of the Game

  • Finally, ensure that the displayed image and the sound match. Pressing the OK button will check whether the bmp and wav variables are the same.

  • If correct, the animal will be removed from the list, and a random one will be displayed and played.

Checking the Animal

  • Voices in WAV format can be added to confirm if the answer is correct or wrong. The voices in the example were generated using TTSMaker Free Text to Speech.

Final Challenge: Complete the Game!

Final Challenge

Solution to the Final Challenge

Final Challenge Solution

This activity is part of the Citilab Course.

⬅️ Previous activity

CC-BY-SA

© by Citilab Edutec 2019-2022.
This work is licensed under a CC-BY-SA 4.0 license.