Python Open WAV file operation wav file
Using Python to open a WAV audio file, and then analyze the WAV file data storage format, after the format can be very convenient to do some signal processing operations. The information for the WAV file given by Wikipedia is as follows
waveform Audio File Format ( WAVE , or more commonly Known as WAV due to Its filename extension -both pronounced "wave") (rarely, Audio for Windows ) is a microsoft and ibm audio file format Storing an audio bitstream on pcs. It is a application of The resource interchange File format (RIFF) bitstream Format method for Stori ng data in "chunks", and thus are also close to The 8svx and The aiff format used On amiga an D macintosh computers, respectively. It is the main format used On windows systems for raw and typically uncompressed audio. The usual bitstream encoding is the linear pulse-code modulation (LPCM) format.
Audio format Data There are also some standard format descriptions on the web, such as wave PCM soundfile format http://soundfile.sapp.org/doc/WaveFormat/
For the processing of wave files in Python, refer to the official documentation https://docs.python.org/3/library/wave.html
Of course, most of the time, the ability to filter information, in the use of Python to process WAV files only need a few of these operations, do not have to master each, when needed to query the document.
Attention
A large paragraph of text to describe what WAV audio files are not for the purpose of programming exercises, human energy is limited and impossible to master all knowledge points at the same time. In the beginning, learning the principles of sound, and then verifying that the work should be done by information coding researchers, and as an engineer should focus on the replication of the code. Instead of daoxing, read the audio file, obtain various parameters according to the API provided by the system, and then query the parameter information. Programming practice to think about how to use the general in the processing of audio files. There is a process is to import audio files, analysis of waveforms, such as Fourier transform operations, as data preprocessing, can proceed to the next numerical data processing, in fact, the most desired process of audio processing is nothing more than described above. So under normal circumstances, but also to know the meaning of the data in the audio file, with the analysis of computer science is the need to know its data structure.
Audio file information
Used python to process WAV file packet wave, read wave information, traverse parameters
Import'child1.wav'= we.open (filename) for inch Enumerate (Wave.getparams ()): Print (item)
Query the description for Wave.getparams () in the official documentation
Wave_read.
getparams
()
Returns a namedtuple()
(nchannels, sampwidth, framerate, nframes, comptype, compname)
, equivalent to output of the get*()
methods.
Output information (channel, sample width, frame rate, number of frames, unique identifier, lossless)
The number of sample points is 2510762, sampling frequency is 44100HZ, through these two parameters can be obtained the length of the sound signal
Each sample point is a bit = 2 bytes, then the number of sample points 2510762*2/(1024*1024) =4.78889MB, then this information is the file size information.
Check the time of the sound waveform
Child1.wav 4.78MB, Duration 56s
Time = 56.93337868480726
Based on the above wave PCM soundfile format information query. There is an impression that the WAV file consists of the following three parts:
1. "RIFF" Chunk descriptor 2.The "FMT" Sub-chunk 3.The "data" When this information is stored in the "ID", "Size", "format", the information marked the location of the data, "WAV" format by "FMT" and "data", consisting of two parts, wherein the "FMT" storage block is used to store the audio file format, "Data" storage block used to save the actual sound of the information, the physical description of the amplitude and time: Length (time) and amplitude, of course, the ear of the ears of the increase is the length, and the pitch. There is such a concept, only to think of "mono amplitude data for n*1 matrix points, stereo for n*2 matrix points", there is such an impression that the essence of audio signal processing is to deal with this array. This means that the array can be read, and the waveform graph is drawn directly to the information of the mating frequency.
Pay attention to what time
1. The value of one sample point represents the audio signal for a given time, and a sampling frame consists of the appropriate number of sample points and can form multiple channels of the audio signal.
2. For stereo signal a sampling frame has two sample points, one sample point corresponds to one channel. A sampling frame is transmitted as a single unit to a digital/analog converter (DAC) to ensure that the correct signal can be sent to the respective channel simultaneously.
3. The mono amplitude data is the n*1 matrix point, the stereo is the n*2 matrix point, then the information processing of the future file is realized by a matrix.
WAV file waveform
In order to draw the waveform diagram, the required parameters have time and amplitude information, the complete steps are as follows:
1. Import the WAV file into the python work environment.
2. Set the parameters, sound signal (time, amplitude, frequency).
3. Draw this information through the interface provided by Matplotlib.pyplot.
You can get a function that changes the amplitude over time.
1 ImportWave as we2 ImportMatplotlib.pyplot as Plt3 ImportNumPy as NP4 fromScipy.ioImportWavfile5 6filename ='Child1.wav'7WAVE =we.open (filename)8 Print('---------Sound Information------------')9 forIteminchEnumerate (Wave.getparams ()):Ten Print(item) OneA = Wave.getparams (). nframes#Total Frames Af = Wave.getparams (). framerate#Sampling Frequency -Sample_time = f/F#time interval of the sampling point -Time = a/f#the length of the sound signal theSample_frequency, audio_sequence =wavfile.read (filename) - Print(audio_sequence)#The "size" of each frame of the sound signal -X_seq =Np.arange (0,time,sample_time) - +Plt.plot (X_seq,audio_sequence,'Blue') -Plt.xlabel ("Time (s)") +Plt.show ()
Storage format for WAV files
Through the above operation process, we have learned a few information:
1. Through Python existing package wave can obtain several basic information of WAV file, such as the channel of the file, the sample width of the sound, the frame rate, the number of frames, whether the unique identification, whether lossless, about the WAV file processing information, you can first use a few lines of code to get the WAV file basic processing information, This information is then drawn out in the form of drawings.
The time in the 2.WAV file can be obtained by the number of sampling points and the size of the frequency, in the future can be added window, digital signal processing of some operations, the sound signal for further analysis.
In the 3.WAV file, the sound signal is stored as a n*1 matrix point or a n*2 matrix point, and the study of the sound signal can be transformed into the study of this array if the condition is mono or two channels.
Python Learning notes-Audio processing