Close
0%
0%

Speaker Games

Demoing our little speaker module with a fun musical game!

Similar projects worth following
While thinking about a demo project for the H07R30 speaker module, I had the idea of building a simple musical game! The module plays few musical notes and you have to repeat the tune via the exact attached push buttons. The speaker will reply back to you if your answer is correct or wrong! The game is fun to code and play and shows you examples of playing music tunes and storing and playing embedded WAVE files.

What is it?

A single Hexabitz module (audio amplifier and speaker) and five push buttons are used to play a simple musical game.

Components

  • 1 x H07R30 Hexabitz Audio Speaker and Headphone Jack Module
  • 5 x TL59FF160Q Momentary tactile push button

Estimated hardware build time: 5 minutes 

Estimated software development time: < 1 hour

Have suggestions about how to improve our demo projects? Feel free to comment below or send us a private message!

Description

The push buttons are soldered to each of the module ports (P1 to P5). Clicking the buttons, triggers playing a particular musical note. There are two modes for the game and you can cycle through them by pressing the button connected to P5 for 2 seconds. Whenever a mode change is requested, the module blinks its indicator LED and pronounces the mode name:

  • Learn mode: In this mode, you can click the buttons to hear musical notes and memorize it.
  • Play mode: In this mode, the module plays few musical notes in series. After it stops playing, you need to press the correct buttons to reproduce exact same notes within 10 seconds (note timing is not required to reproduce). If you succeed, the module blinks its LED and replies with "Correct" and if you fail, the module replies with "Wrong". After that the game repeats again.

The game difficulty level increases the more correct answers you guess in a session. The difficulty level is controlled by number of musical notes and their timing.

Check out the game in action!

How does it work?

The instructions section of this project lists a step-by-step guide to creating and embedding your wave files. Once your done, you can start coding your game! The project files are attached to file section and here's a high level description of what the firmware does.

The first section before the super loop in the FronEndTask handles initializations. We define all the buttons, link them with click events and a press-for-2-seconds event for P5, and play a startup chiptune! Then we initialize the timeout timer.

Inside the super loop, we verify the mode is play and the timer has already expired and then we check whether the player has entered all correct notes or not and we play the appropriate wave file with some LED blinks. If the answer is successful, we increment difficult level by one.

Then we initialize all sequences and generate a new challenge tune.

The challenge tune is generated by a random sequence from 1 to 5 (matching the buttons on ports P1 to P5). The number of notes in the sequence and the delay between them is affected by difficulty level divided by 5. Each note is played once its generated as well.

The function responsible about playing notes simple plays a specific musical note corresponding to each port number. Not that matching ports and musical notes is completely up to you.

FInally we have the callbacks for buttons events. The click callback plays wherever note associated with this button and adds this note to player sequence. Once the sequence count reaches the count of challenge notes the timeout timer is reset to -8 seconds so that it triggers the result check in 2 seconds.

The button press callback simply cycles between play and train modes. Note that this event is a latching one. Thus, you need to manually reset it using resetButtonEvent() API otherwise it will keep triggering.


Waveform Audio File Format (WAV) - 85.58 kB - 12/04/2018 at 21:45

Download

Waveform Audio File Format (WAV) - 83.33 kB - 12/04/2018 at 21:45

Download

Waveform Audio File Format (WAV) - 90.08 kB - 12/04/2018 at 21:45

Download

Waveform Audio File Format (WAV) - 81.08 kB - 12/04/2018 at 21:45

Download

C Source File - 74.89 kB - 12/04/2018 at 21:45

Download

View all 9 files

  • 1
    Generate your speech WAVE files

    First step is to get some good .WAVE files for the speech or words you need in your project. You can, of course, record your own or somebody's else voice and you can search for the files online and download them. However, to meet your exact needs, you'll probably need a text-to-speech service. There are many options online, from free websites with clunky voices to Google and IBM ultra-realistic, deep learning text-to-speech engines. Here's are some good free options:

    • https://www.text2speech.org/. Download WAVE files directly. Medium quality.
    • IBM's Watson Text to Speech demo.  Note that you can type something in a the demo and download an MP3 file for free (and without registration). Just use any MP3 to WAVE converter to convert the files.

    I generated using the IBM's engine four WAVE files for: "Learn", "Play", "Correct" and "Wrong". You can download them in the file section.

  • 2
    Convert the WAVE files into embedded C code

    You can use this nice free software WAVToCode to convert WAVE files into a C files. Check their help page for instructions on using the software.

    The DAC in this module accepts only 8 or 12 bit samples but this software generates 8, 16 and 24. Load each file to the software, pick "8 bit" for No. of bits per channel option and click Mix. Then use your mouse buttons to add two markers before and after the wave to remove empty sections. From the menu Tools you can listen to your mixed wave and then convert it into a C file from the menu File. Choose Unsigned for the output. The original C files generated from this converter are available in the file section marked with "orig_".

    Note: If the wave sound in the module is weak, you need to set the autoscale option to Normalize.

  • 3
    Modify your wave C files to include in the project

    You need to perform some minor edits on the the wave C files before you can use them in the speaker module project:

    1. Remove #define NUM_ELEMENTS line and add  the definition #include "wave.h" instead.
    2. Replace the array data[NUM_ELEMENTS] with a meaningful definition, e.g., waveByteCode_Correct[WAVEBYTECODE_CORRECT_LENGTH]
    3. Replace unsigned char with const uint8_t.

View all 6 instructions

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates