This section describes several python audio processing libraries and python audio processing libraries.

Source: Internet
Author: User
Tags id3 id3 tag pscp secure copy

This section describes several python audio processing libraries and python audio processing libraries.

  1. eyeD3

Search for python mp3 process directly on google. This third-party library is recommended. Let's take a look at the official introduction.

About

EyeD3 is a Python tool for working with audio files, specifically mp3 files containing ID3 metadata (I. e. song info ).

It provides a command-line tool (eyeD3) And a Python library (import eyed3) That can be used to write your own applications or plugins that are callable from the command-line tool.

For example, to set some song information in an mp3 file calledsong.mp3:

$ eyeD3 -a Nobunny -A "Love Visions" -t "I Am a Girlfriend" -n 4 song.mp3

To put it simply, the eyeD3 library is used to process MP3 files, especially files with ID3 metadata (generally MP3 files contain additional information, such as singers and albums, how to extract the information ). EyeD3 provides two usage methods. One is to use command line to directly execute eyeD3 --... you can also use import eyed3 to import MP3 files.

The preceding example is an officially provided statement executed using the eyeD3 command line.-a is short for "-- artist", that is, to add the singer information.-A is short for "-- album, that is, add the album information.-t is short for -- title, that is, add the song name.-n is short for -- track-num, that is, add the number of tracks. These are generally the default attributes of the MP3 file ID3 tag. If we enter eyeD3 song.mp3 directly, the basic information of the song will be displayed, which is roughly as follows:

$ EyeD3 song.mp3

Song.mp3 [3.06 MB] running ID3 v2.4: title: I Am a Girlfriendartist: Nobunnyalbum: Love Visionsalbum artist: Various Artiststrack: 4 bytes -------------------------------------------------------------------------
If you use eyeD3 in windows, you may find that if you enter eyeD3 directly in cmd, you will be prompted that the command cannot be found, and this problem occurs.
Because I directly installed pip, I went to the site-packages directory and did not find the executable file named eyeD3.exe, all of which are. py files. This is not described on the official website,
I went to google for a long time and did not find the corresponding solution. No way, just try it in linux. After pip install eyeD3 in linux
Shell execution of eyeD3 song.mp3 is no problem. 1 is the execution result (Part) under ubuntu14 ):

Figure 1

We can see that common information is displayed. By the way, I uploaded this MP3 file from windows to unix. In the past, I uploaded files in windows and linux in a way similar to Baidu cloud,
However, this transfer method is sometimes too troublesome. So I searched the internet and found Putty. PuTTY is an SSH and telnet client, developed originally by Simon Tatham for the Windows platform.
PuTTY is open source software that is available with source code and is developed and supported by a group of volunteers.
That is, Putty is an SSH (Secure Shell protocol) Remote logon client, used in windows. In this: http://www.chiark.greenend.org.uk /~ Sgtatham/putty/download.html. After the installation is downloaded, open putty and the following page appears (Figure 2 ):


Figure 2
At this time, open your linux machine. Of course, I installed it in the vmwarevm. Enter the ifconfig command in the shell and find your linux IP address, as shown in 3:

                           Figure 3

For example, the address of my machine is 192.168.152.130. Enter the IP address in the Host Name column in Figure 2. Note that the default port is 22. Do not change it. Then click open. A login interface is displayed, and enter
You can connect the user name and password to linux through ssh. Bytes
Several methods for transferring files between unix and windows. I will only talk about the first method, using putty's pscp secure copy (secure copy ). After you install putty, an executable file named pscp.exe is generated and added to the directory
Environment variable. Then run the command pscp myfile.txt shs @ unixserver:/home/shs in cmd to copy your file myfile.txt to the/home/shs directory in linux. To explain, you need
Replace it with your username. @ is followed by your linux IP address./home/shs is the location where you want to move the file. After execution, the file will be copied successfully. If you are not interested in other methods, you can search by yourself or read the article I mentioned above.
Back to the topic, after the command line of eyeD3 is finished, let's talk about how to use it in Python. Let's look at the official example:
 1 import eyed3 2  3 audiofile = eyed3.load("song.mp3") 4 audiofile.tag.artist = u"Nobunny" 5 audiofile.tag.album = u"Love Visions" 6 audiofile.tag.album_artist = u"Various Artists" 7 audiofile.tag.title = u"I Am a Girlfriend" 8 audiofile.tag.track_num = 4 9 10 audiofile.tag.save()

The above Code uses import eyed3 to import the eyeD3 library, and then load the mp3 file using the load method. The following lines set the ID3 tag, such as artist and album. You can see the Code directly, let's not talk about it. To display the ID3 tag information in an mp3 file, print the corresponding tag directly, for example, print (audiofile. tag. artist) and so on. Of course, the premise is that your MP3 metadata has to store this information. In fact, there are some more complex and advanced usage, so I will not talk about it. If you are interested, please go to the official document at: http://eyed3.nicfit.net/index.html. EyeD3 is mainly used to process the metadata of MP3 files. Other libraries are used for parsing audio files.

 

Ii. pydub

In the first example, eyeD3 can only process MP3 files, which is relatively simple. The pydub library described below is much more powerful. Old rules, or
Let's take a look at its official introduction:
Manipulate audio with a simple and easy high level interface http://pydub.com for one sentence, simple, easy to use processing audio
Hey, isn't that what we're looking. Github Project address: https://github.com/jiaaro/pydub/ has more than 1800 star, this description
Library is still very popular. Installation is simple. You can directly install pip install pydub. However, note the following:
Dependencies
 

You can open and save WAV files with pure python. For opening and saving non-wav files-like mp3-you'll need ffmpeg orlibav.

Here, the built-in python wave module can only process audio files in wav format. If you want to process files similar to MP3 format, You have to install ffmpeg or libav.

What is ffmpeg?

A complete, cross-platform solution to record, convert and stream audio and video.

Ffmpeg is a cross-platform tool that can be used to record and convert audio and video. If you have worked on digital signal processing, you should be familiar with it. There is also a libav, which is actually a branch derived from ffmpeg. The function is similar to ffmpeg. You can download either of them. Select an executable file for installation in windows.

Let's take a look at the examples on the official website.

I: Open mp3, mp4, and other files

You can use the following command:

 1 from pydub import AudioSegment 2  3 song = AudioSegment.from_wav("never_gonna_give_you_up.wav") 4 song = AudioSegment.from_mp3("never_gonna_give_you_up.mp3") 5  6 ogg_version = AudioSegment.from_ogg("never_gonna_give_you_up.ogg") 7 flv_version = AudioSegment.from_flv("never_gonna_give_you_up.flv") 8  9 mp4_version = AudioSegment.from_file("never_gonna_give_you_up.mp4", "mp4")10 wma_version = AudioSegment.from_file("never_gonna_give_you_up.wma", "wma")11 aac_version = AudioSegment.from_file("never_gonna_give_you_up.aiff", "aac")

You can open any file types supported by ffmpeg. From the above, we can see that there are mainly from_filetype () methods. filetype is a specific file type, such as wav and mp3.

Or the generic from_file () method, but this method must specify the type of the file to be opened in the second parameter, and the returned results are all AudioSegment objects.

II: cutting audio

1 # pydub does things in milliseconds2 ten_seconds = 10 * 10003 4 first_10_seconds = song[:ten_seconds]5 6 last_5_seconds = song[-5000:]

Note that the standard time in pydub is millisecond. The above code gets the first 10 seconds of music and the last 5 seconds, which is very simple.

III: adjust the volume

1 # boost volume by 6dB2 beginning = first_10_seconds + 63 4 # reduce volume by 3dB5 end = last_5_seconds - 3

+ 6 indicates that the Music volume is increased by 6 decibels, and-3 indicates that the Music volume is reduced by 3 decibels.

IV: splice two pieces of music

without_the_middle = beginning + end
without_the_middle.duration_seconds

The spliced music duration is the sum of the two music durations. You can use the. duration_seconds method to obtain the length of a piece of music. This is the same as the result obtained using len (audio)/1000.0.

V: reverse)

1 # song is not modified2 # AudioSegments are immutable3 backwards = song.reverse()

Note that the AudioSegment object is unchangeable. The preceding reverse method does not change the song object, but returns a new AudioSegment object. This is also true for other methods. Reverse simply starts playing the music in reverse order from the end to the head. I tried it and found it really interesting after the conversion.

VI: crossfade)

1 # 1.5 second crossfade2 with_style = beginning.append(end, crossfade=1500)

Crossfade is used to smoothly transition a piece of music to another piece of music. The above crossfade = 1500 indicates that the transition time is 1.5 seconds.

VII: repeat (repeat)

# repeat the clip twicedo_it_over = with_style * 2

The above code replays the Music twice.

VIII: fade in and fade out (gradually increasing and decreasing)

# 2 sec fade in, 3 sec fade outawesome = do_it_over.fade_in(2000).fade_out(3000)

Gradually increase by 2 seconds, gradually decrease by 3 seconds

XI: save)

awesome.export("mashup.mp3", format="mp3")awesome.export("mashup.mp3", format="mp3", tags={'artist': 'Various artists', 'album': 'Best of 2011', 'comments': 'This album is awesome!'})

The two storage formats are shown here. The export method is used to specify the storage format and the format parameter is used. However, the second method has one more tags parameter, in fact, it is easy to understand that the ID3 tag information of a song is saved.

The above is only a preliminary introduction to the use of pydub, there are a lot of other functions, please go to the official API documentation: https://github.com/jiaaro/pydub/blob/master/API.markdown

The introduction is very detailed.

 

Iii. PyAudio


It is also a powerful audio processing library. Official introduction:

PyAudio provides Python bindings for PortAudio, the cross-platform audio I/O library. with PyAudio, you can easily use Python to play and record audio on a variety of platforms. pyAudio is too red:

  • PyPortAudio/fastaudio: Python bindings for PortAudio v18 API.
  • TkSnack: cross-platform sound toolkit for Tcl/Tk and Python.

Pyaudio allows you to bind a cross-platform PortAudio library for processing audio input and output. PyAudio allows you to easily record and play audio.

Take a look at the official documentation (https://people.csail.mit.edu/hubert/pyaudio/docs/) to provide a quick start code.

 1 """PyAudio Example: Play a wave file.""" 2  3 import pyaudio 4 import wave 5 import sys 6  7 CHUNK = 1024 8  9 if len(sys.argv) < 2:10     print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])11     sys.exit(-1)12 13 wf = wave.open(sys.argv[1], 'rb')14 15 # instantiate PyAudio (1)16 p = pyaudio.PyAudio()17 18 # open stream (2)19 stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),20                 channels=wf.getnchannels(),21                 rate=wf.getframerate(),22                 output=True)23 24 # read data25 data = wf.readframes(CHUNK)26 27 # play stream (3)28 while len(data) > 0:29     stream.write(data)30     data = wf.readframes(CHUNK)31 32 # stop stream (4)33 stream.stop_stream()34 stream.close()35 36 # close PyAudio (5)37 p.terminate()

Of course, this provides the form of Receiving audio files using command line parameters. CHUNK is the number of audio bytes read at a time, p = pyaudio. PyAudio () initializes

PyAudio object, and then use its open method to open an input and output stream. If output = True is specified, this is an output stream, that is, we add data to the stream, if it is changed to input = True, It is the input stream. Generally, it is the standard audio device of the device. For the computer, it may be the microphone to read the audio data. Open a. wav file using wave, and then use the readframes method to read so much data from CHUNK each time, write the data into the stream until the reading ends. The audio data written into stream will be played continuously through the microphone, so we can hear the music. At the end, pay attention to closing the corresponding object to release the resource.

Another method is to use the callback function. The Code is as follows:

 1 """PyAudio Example: Play a wave file (callback version).""" 2  3 import pyaudio 4 import wave 5 import time 6 import sys 7  8 if len(sys.argv) < 2: 9     print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])10     sys.exit(-1)11 12 wf = wave.open(sys.argv[1], 'rb')13 14 # instantiate PyAudio (1)15 p = pyaudio.PyAudio()16 17 # define callback (2)18 def callback(in_data, frame_count, time_info, status):19     data = wf.readframes(frame_count)20     return (data, pyaudio.paContinue)21 22 # open stream using callback (3)23 stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),24                 channels=wf.getnchannels(),25                 rate=wf.getframerate(),26                 output=True,27                 stream_callback=callback)28 29 # start the stream (4)30 stream.start_stream()31 32 # wait for stream to finish (5)33 while stream.is_active():34     time.sleep(0.1)35 36 # stop stream (6)37 stream.stop_stream()38 stream.close()39 wf.close()40 41 # close PyAudio (7)42 p.terminate()

I will not elaborate on it.

Next, let's look at a code that uses pyaudio + numpy + pylab to visualize audio. The code below opens the computer's microphone, receives audio input, and then displays it as an image.

 1 # -*- coding: utf-8 -*- 2 """ 3 Created on Fri May 12 10:30:00 2017 4 @author: Lyrichu 5 @description: show the sound in graphs 6 """ 7 import pyaudio 8 import numpy as np 9 import pylab10 import time11 12 RATE = 4410013 CHUNK = int(RATE/20) # RATE/number of updates per second14 15 def sound_plot(stream):16     t1 = time.time() # time starting17     data = np.fromstring(stream.read(CHUNK),dtype = np.int16)18     pylab.plot(data)19     pylab.title(i)20     pylab.grid()21     pylab.axis([0,len(data),-2**8,2**8])22     pylab.savefig("sound.png",dpi=50)23     pylab.show(block = False)
    time.sleep(0.5)24 pylab.close('all')25 print("took %.2f ms." % (time.time() - t1)*1000)26 27 if __name__ == '__main__':28 p = pyaudio.PyAudio()29 stream = p.open(format = pyaudio.paInt16,channels = 1,rate = RATE,30 input = True,frames_per_buffer = CHUNK)31 for i in range(int(20*RATE/CHUNK)): 32 # for 10 seconds33 sound_plot(stream)34 stream.stop_stream()35 stream.close()36 p.terminate()

The Code should be easier to understand. The following figure shows an image (figure 4 ):

Figure 4

Note that if you do not execute the plot command of pylab or matplotlib in interactive commands, the plt. show () function is a block function, which leads to the final

Plt. close ('all') close all windows will only be executed after the image is manually closed. We cannot see any continuously changed images. To solve this problem, we will. set the block parameter of the show () function to False, so that the show function is not a block function, and you can directly execute plt. close ('all') command, in order not to see the changes because the image is refreshed too fast, so use time. sleep (0.5) pause for 0.5 seconds.

 

In fact, there are still pygame modules (a game module of python) and librosa libraries (Professional Digital Signal Processing libraries) that haven't been introduced yet. I have the opportunity to learn more. Stay tuned!

 

 






 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.