清空記錄
歷史記錄
取消
清空記錄
歷史記錄
本文基于IDO-SBC3968 Ubuntu 系統(tǒng)通過Python腳本實(shí)現(xiàn)錄音和播放功能。
IDO-SBC3968采用RK3399國產(chǎn)六核64位CPU高性能處理器,支持4K HDMI2.0顯示,接口豐富,擁有千兆以太網(wǎng),全協(xié)議TypeC接口,USB3.0 ,eDP 和 雙通道LVDS 屏幕接口,支持4G/5G模塊和WIFI藍(lán)牙,支持6軸G-Sensor,支持Linux/Android系統(tǒng),適用于高端商顯,銀行自助終端,工業(yè)計(jì)算機(jī),機(jī)器人等諸多行業(yè)。
產(chǎn)品規(guī)格書:IDO-SBC3968 產(chǎn)品手冊
配置前需要安裝好相應(yīng)的依賴庫。
#apt-get update
#apt-get install python3
#apt-get install portaudio19-dev python-all-dev python3-all-dev
#apt-get install libasound-dev libportaudio2 libportaudiocpp0
#apt-get install python3-pip
#pip3 install pyaudio
在編輯腳本前需要知道使用的是哪個(gè)聲卡。
可以通過“aplay -l ”獲取。
#aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: rockchipes8316c [rockchip,es8316-codec], device 0: ff880000.i2s-ES8316 HiFi ES8316 HiFi-0 []
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: Audio [USB Audio], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
由上面的打印可知USB聲卡是Card1。
編輯python 錄音腳本。
#vi record.py
import pyaudio
import wave
import os
CHUNK = 44100 #采樣頻率
FORMAT = pyaudio.paInt16
CHANNELS = 1 #聲卡通道
RATE = 44100
RECORD_SECONDS = 5 #時(shí)長
WAVE_OUTPUT_FILENAME = "output.wav" #導(dǎo)出的音頻文件
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
input_device_index = 1,
frames_per_buffer=CHUNK)
print("recording...")
frames = []
count=0
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
#for i in range(0, 5):
#while count < 5:
data = stream.read(CHUNK)
frames.append(data)
# count += 1
print("finished recording")
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
錄音
#python3 ./record.py
錄音結(jié)束后我們就可以得到 output.wav 錄音文件了。
#vi play.py
import pyaudio
import wave
CHUNK = 44100
FILENAME = './output.wav'
def play(filename = FILENAME):
wf = wave.open(filename, 'rb')
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output_device_index=0,
output=True)
data = wf.readframes(CHUNK)
while data != b'':
stream.write(data)
data = wf.readframes(CHUNK)
stream.stop_stream()
stream.close()
p.terminate()
play()
播放
#python3 ./play.py
通過以上指令,就可以播放剛錄的音頻文件了。