Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to play Music with Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to play music in Python". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to play music in Python" can help you solve your doubts.

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 ())

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.

Source code and download address of wav file

Https://github.com/PakchoiFood/TPYBoardV102-PlayMusic

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.

Schematic diagram

TPYBoard v102

earphone plug

X5

Left channel (L)

GND

GND

Physical drawing

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

After reading this, the article "how to play music in Python" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report