Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to play Music with MicroPython

Shulou Source: shulou.com Published: 2022-06-01 03:58:14 09月20日 Update

Today, I would like to share with you the relevant knowledge about how to play music in MicroPython. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

0x01 experimental equipment

1 TPYBoardv102 development board

1 headset or stereo

Some DuPont lines

Preparation for 0x02

1. First of all, we need a .wav audio file that can be played. Through the description and test of the document, it is found that the music can only be played for about 2 seconds, and the file size should be less than 50KB, otherwise an error will be prompted.

2. The format requirements of wav: bit rate 8-bit, sampling frequency 22050Hz. You can directly use the avconv tool to convert the mp3 tool into a playable wav file.

3. Ubuntu installation method:

Sudo apt-get install ffmpegsudo apt-get install libav-tools

4. After the installation is complete, execute the command directly.

Avconv-I 2.mp3-ar 22050-codec pcm_u8 test2.wav

5, also need a TF memory card, into the source code and wav audio files, the development board itself will be insufficient FLASH space.

0x03 program source code

Main.py

Import wavefrom pyb import DACdac = DAC (1) f = wave.open ('/ sd/test2.wav') dac.write_timed (f.readframes (f.getnframes ()), f.getframerate ())

From the above source code, we can see that the wave class library is mainly used.

6. Click the red box to download wave.py directly, and then introduce chunk.py into the wave file. Click the blue box to download directly.

7. Copy main.py, wave.py and chunk.py directly to the TF card of the development board.

8. Click in the green box to download the official wav playback file test.wav.

9, through the above I have made a test2.wav file, you can also try to make your own audio files to play.

0x04 playback effect

Let's first understand the structure of the headphone plug.

10. The above picture shows three segments of headphone plugs, such as our audio plugs, radio plugs and so on.

11. The above is a four-segment picture of headphone plugs. The headphone plugs we usually use on mobile phones are usually four-segment, with more Mike functions in the middle. There may be some international standards, MIC Mike and GND grounding position will be reversed.

12. As you can see from the main.py source code, we are using DAC (1), so we need to connect the X5 pin to the left channel, the GND pin and the GND on the headphone plug.

TPYBoard v102

earphone plug

X5

Left channel (L)

GND

GND

13. After connecting the cable, run the program again, and we can hear the sound.

These are all the contents of the article "how to play Music in MicroPython". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Plugs files headphones source code knowledge articles music audio development content sound channels tools programs faceted audio mic production different big above Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Redmi NVidia macOS