When friends get together, singing at the top of your lungs can be exhilarating; when alone, singing can be a way to release emotions. Do you also enjoy going to KTVs or playing music accompaniment on your phone and expressing your feelings through your voice?
In fact, our UNIHIKER M10 can do this as well. Today, we will use the UNIHIKER to create a “portable KTV” that can play accompaniment music and record singing.

Goal
Click the accompaniment song name to start playing the accompaniment and recording your singing. When the accompaniment stops, the recording is completed. After the recording is finished, click the “Play” button to play back the recorded audio; click the “Pause” button to pause audio playback.

Get to Know
1.Understand how USB speakers work
2.Master the methods for audio recording and playback on the UNIHIKER M10
Materials List
Hardware:

Note: The kit uses a USB speaker expansion 2-in-1 module. In addition to connecting to the UNIHIKER via a USB cable, it also supports:
- Connecting external headphones to meet more audio output needs;
- Cascading other USB devices (such as a camera).
Software:
Mind+ Programming Software x1(download at https://mindplus.cc/en)

Hands-on Practice
The main function of the Mini Karaoke Booth is to select different songs, record singing, and play it back. Next, we will divide the project into the following two tasks.
Task 1: Play song accompaniment
In this task, we will learn how to use the UNIHIKER to play music and complete the task of playing a selected accompaniment.
Task 2: Record and play singing
Based on Task 1, we will use the UNIHIKER’s audio recording blocks and further apply playback-related blocks to complete the Mini Karaoke Booth’s song recording and playback functions.
Task 1: Play song accompaniment
1.Hardware Connection
The UNIHIKER does not have a built-in speaker. To play music, an external USB speaker is required. Use a USB cable to connect the UNIHIKER to the USB Speaker Expansion 2-in-1 Module.
Note: Hereafter, the “USB Speaker Expansion 2-in-1 Module” will be referred to as the USB speaker. For more information about the USB speaker, please refer to the“Knowledge Base”.

After the speaker is connected successfully, use a USB cable to connect the UNIHIKER to the computer.
2.Software Preparation
Open Mind+ and follow the diagram below to complete the software preparation process.

3.Write a Program
In the Mini Karaoke Booth, we need to select a song and have the UNIHIKER play the accompaniment music. How can this be achieved? Let’s learn step by step.
(1) Playing Music on the UNIHIKER
Similar to displaying images on the UNIHIKER, to play music you first need to place the audio file in the same folder as the program, and then use blocks to play it.
Taking the song “Jingle Bells” as an example, first find the file “Jingle Bells.wav” in the assets folder and drag it into Project Files.

Then, we can use block to play the music on the UNIHIKER.
Use the “Start playing audio file in the background“ block, found under UNIHIKER → Audio recording & Playback, and fill in the name of the audio file you just added.

(2) Selecting and Playing a Song
To select and play a song, we can use the text callback function. That is, use “the click callback function of the object name as button_click1”, and set audio playback in the callback function.

Now we can use the same method to add the accompaniment for “So Many People in This World”. At the same time, to better display the selected accompaniment, add a text object showing the selected song. Refer to the sample code below.

4.Run the Program
Click Run. The UNIHIKER screen displays the accompaniment name. Clicking the accompaniment name will show the selected accompaniment below and start playback.
Note: If the sound you hear during playback is very low or there is no sound, refer to “USB Speaker Volume Adjustment Methods” in the Knowledge Base.

5.Have a try
Your Mini Karaoke Booth can now play selected accompaniments, but the UNIHIKER may play multiple accompaniments simultaneously in the background.
Now, please use the “playback_flag“ variable to ensure that when accompaniment is already playing, clicking another accompaniment name will not cause multiple tracks to play in the background.
Task 2: Recording and Playing Songs
1.Write a Program
Through Task 1, we have already completed accompaniment playback. Next, let’s implement recording and playback of singing.
(1) Recording Singing
The UNIHIKER block for recording audio is “Record seconds until the end file name ‘record.wav’”. When using it, you need to specify the recording duration and the file name for storing the recorded audio.

The simplest approach is to place the recording block directly after the playback block. However, this will cause display errors (the selected song name will only appear after recording finishes). Therefore, we usually solve this problem by starting a thread.

After recording ends, use the Stop Background Playback block under UNIHIKER → Audio Recording & Playback to stop the accompaniment and update the playback flag variable.

(2) Playback and Pause of Recordings
Next, we need to create Play and Pause buttons. Clicking Play starts background playback of the audio file record.wav. Clicking Pause pauses background playback. Don’t forget to update the playback flag variable as well.

Finally, we only need to complete the recording and playback code for the second accompaniment and appropriately beautify the interface to finish our Mini Karaoke Booth project.
You can design your own interface, or refer to this project by adding playback indicator icons and text prompts. The complete code is shown in the diagram below.


2.Run the Program
Click Run. After the program runs successfully, the UNIHIKER screen displays the accompaniment list, prompts, buttons, and more.
- Click an accompaniment name to start playing and recording singing.
- After the accompaniment stops, recording finishes.
- Click Play to play the recorded audio.
- Click Pause to pause audio playback.

Knowledge Base
1.Understanding USB Speakers
A USB speaker is a device that converts electrical signals into sound, with the electrical signal transmitted via a USB interface. Like buzzers, speakers produce sound through vibration. The difference lies in the input signals. Comparing passive buzzers with speakers: Passive buzzers receive square-wave signals. Different sounds are produced by changing the frequency of the square wave.
Note:For more information about buzzers, refer to UNIHIKER Graphical Python Beginner Tutorial – Lesson 8 .

The input signal of a speaker is an audio signal. What is an audio signal?
It can be understood as a sequence of electrical signals formed by storing speech, music, and other sounds in a device. Unlike square waves, audio signals have not only frequency but also amplitude changes, making them similar to sound waves. You can see audio waveforms in many places, such as the built-in voice recorder on a computer.

Internally, a speaker converts audio signals back into sound. From the perspective of signal processing, speakers handle signals much closer to real music and speech, which is why their sound is far richer and more pleasant than that of buzzers.

2. USB Speaker Volume Adjustment Methods
Adjust the volume via the UNIHIKER’s Terminal Area. Steps are as follows:
- Stop running the code.
- Enter the command “alsamixer” in the Terminal Area and press Enter.
- Use the Up and Down arrow keys to adjust the volume until it reaches the desired level.
- Press Esc to exit the settings interface.

3.Command Overview


Challenge
After learning this lesson, you should now have mastered the basic operations of audio recording and playback on the UNIHIKER. Now, try making a “Voice Notebook” to record your current mood or memorable moments.
Click the Record Note button. The UNIHIKER starts recording a voice note. After recording finishes, the record is displayed. Click a recorded entry to play the note. Click Pause Playback to stop audio playback.
Tip: You can use an index variable to display different recording entries. Interface design can refer to the diagram below.

More program and image resources can be downloaded from the link below.
https://drive.google.com/file/d/1E7Wl9V-KxfRBXVMDdm5ZCSoBWdB0DMwX/view?usp=drive_link








