程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python script reads PCM file and wav file at the same time

編輯:Python

Preface

When the client calls our test script , There is such a problem , Some customers use smart speakers tinycap/arecord When recording , The recorded audio is pcm Format , No wav Format . My test script only supports wav Format , This requires the customer to pcm The file to wav file . then , Some customers really don't know how to use Audition( This is the truth ). therefore , Write a can support reading at the same time pcm Document and wav Script for file .

The most important thing in this script is to use python Of try function . Let's start with try function .

What is? try function

try/except Statement is used to detect try Error in statement block , So that except Statement to capture exception information and handle . If you don't want to end your program when an exception occurs , Just in try Capture it in .

simply , Namely try If there are exceptions or errors in the statements in the module , The program will be executed instead except modular ,; If try The module executes normally without error , It won't be implemented except modular . If in try No exception occurred while clause was executing ,python Will perform else Statement after statement ( If there is else Words ). In code, it means :

try:
< sentence A> # If you run A normal , Then normal execution 
except:
< sentence B> # If you run A Report errors , Turn to running B
else:
< sentence C> # If A Normal execution , perform C, Usually used for printing 

therefore , The idea for testing scripts is :

try:
< Read wav Format file > # If it works properly , Read in wav File data 
except:
< Read pcm Format file > # If you run try Report errors , Read in pcm File data 
else:
<print(' Read in wav file !')> # For printing try result , Don't add 

Script implementation

Next is the code implementation process .python Middle reading wav And read in pcm The principle is different , call wave The library can be read directly wav Sampling rate of format 、 Number of bits 、 Number of channels, etc , But read pcm The format is to read in a set of data , Then input the number of channels and recombine them into n Channel data . therefore , Read pcm The file needs one more input parameter :channels.

another , Many students read pcm Files like to use AudioSegment library ,AudioSegment The advantage of is less code . I personally like to use wave library , Although it will be a little troublesome , But the data accuracy can be guaranteed after proficiency .AudioSegment Read pcm The process of format file is as follows :

#pcm The data can be read in this way 
from pydub import AudioSegment
import numpy as np
pcm_path = 'xxx.pcm'
voice_data = AudioSegment.from_file(file=pcm_path,sample_width=2, frame_rate=16000,channels=1,)
pcm_data = np.array(voice_data.get_array_of_samples())
pcm_data = pcm_data/32768.0

My test scripts use wave library , Read pcm The code is as follows :

pcmf = open(f, 'rb')
pcmdata = pcmf.read()
pcmf.close()
wavfile = wave.open(r'save_file.wav', 'wb')
wavfile.setnchannels(channels)
wavfile.setsampwidth(2)
wavfile.setframerate(16000)
wavfile.writeframes(pcmdata)
wavfile.close()
wavread = wave.open(r'save_file.wav','rb')
fs = wavread.getframerate() #sampling freqency
Nwavlen = wavread.getnframes() #num of total audio data points
Nchannel = wavread.getnchannels() #num of channels
wav_str = wavread.readframes(Nwavlen)
wav_int = np.frombuffer(wav_str, dtype=np.int16)
wav_data = np.reshape(wav_int,[Nwavlen,Nchannel]) #audio data on channels
wav_data = wav_data/2**15
print('fs',fs)
print('wav_data',wav_data.shape)
print(' Read in pcm file !')

The final result of the whole script is as follows :
Read in wav file :

Read in pcm file :

You can see ,wav Document and pcm The number of frames read into the file 、 The channel number 、 The sampling rate is the same , It indicates that the program runs successfully .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved