Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
Signed-off-by: huangziyi <[email protected]>
  • Loading branch information
MrThanlon committed Dec 27, 2023
1 parent 5d4356a commit c296dab
Show file tree
Hide file tree
Showing 126 changed files with 19,919 additions and 1,183 deletions.
187 changes: 105 additions & 82 deletions share/qtcreator/examples/01-Media/acodec.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,132 +1,155 @@
from media.pyaudio import *
from media.media import *
import media.g711 as g711
from mpp.payload_struct import *
# g711 encode/decode example
#
# Note: You will need an SD card to run this example.
#
# You can collect raw data and encode it into g711 or decode it into raw data output.


from media.pyaudio import * #导入pyaudio模块,用于采集和播放音频
from media.media import * #导入media模块,用于初始化vb buffer
import media.g711 as g711 #导入g711模块,用于g711编解码
from mpp.payload_struct import * #导入payload模块,用于获取音视频编解码类型
import os

def exit_check():
try:
os.exitpoint()
except KeyboardInterrupt as e:
print("user stop: ", e)
return True
return False

def encode_audio(filename, duration):
CHUNK = int(44100/25)
FORMAT = paInt16
CHANNELS = 2
RATE = 44100
CHUNK = int(44100/25) #设置音频chunk值
FORMAT = paInt16 #设置采样精度
CHANNELS = 2 #设置声道数
RATE = 44100 #设置采样率

p = PyAudio()
p.initialize(CHUNK)
enc = g711.Encoder(K_PT_G711A,CHUNK)
ret = media.buffer_init()
if ret:
print("record_audio, buffer_init failed")
p.initialize(CHUNK) #初始化PyAudio对象
enc = g711.Encoder(K_PT_G711A,CHUNK) #创建g711编码器对象
media.buffer_init() #vb buffer初始化

enc.create()
enc.create() #创建编码器
#创建音频输入流
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)

frames = []

#采集音频数据编码并存入列表
for i in range(0, int(RATE / CHUNK * duration)):
frame_data = stream.read()
data = enc.encode(frame_data)
frames.append(data)

stream.stop_stream()
stream.close()
p.terminate()
enc.destroy()

frame_data = stream.read() #从音频输入流中读取音频数据
data = enc.encode(frame_data) #编码音频数据为g711
frames.append(data) #将g711编码数据保存到列表中
if exit_check():
break

stream.stop_stream() #停止音频输入流
stream.close() #关闭音频输入流
p.terminate() #释放音频对象
enc.destroy() #销毁g711音频编码器

#将g711编码数据存入文件中
with open(filename,mode='w') as wf:
wf.write(b''.join(frames))

media.buffer_deinit()
media.buffer_deinit() #释放vb buffer

def decode_audio(filename):
#读取音频文件
wf = open(filename,mode='rb')
FORMAT = paInt16
CHANNELS = 2
RATE = 44100
CHUNK = int(RATE/25)

#播放音频操作
wf = open(filename,mode='rb') #打开g711文件
FORMAT = paInt16 #设置音频chunk值
CHANNELS = 2 #设置声道数
RATE = 44100 #设置采样率
CHUNK = int(RATE/25) #设置音频chunk值

p = PyAudio()
p.initialize(CHUNK)
dec = g711.Decoder(K_PT_G711A,CHUNK)
ret = media.buffer_init()
if ret:
print("play_audio, buffer_init failed")
p.initialize(CHUNK) #初始化PyAudio对象
dec = g711.Decoder(K_PT_G711A,CHUNK) #创建g711解码器对象
media.buffer_init() #vb buffer初始化

dec.create()
dec.create() #创建解码器

#创建音频输出流
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
output=True,
frames_per_buffer=CHUNK)

stream_len = CHUNK*CHANNELS*2//2
stream_data = wf.read(stream_len)
stream_len = CHUNK*CHANNELS*2//2 #设置每次读取的g711数据流长度
stream_data = wf.read(stream_len) #从g711文件中读取数据

#解码g711文件并播放
while stream_data:
frame_data = dec.decode(stream_data)
stream.write(frame_data)
stream_data = wf.read(stream_len)
frame_data = dec.decode(stream_data) #解码g711文件
stream.write(frame_data) #播放raw数据
stream_data = wf.read(stream_len) #从g711文件中读取数据
if exit_check():
break

stream.stop_stream()
stream.close()
p.terminate()
dec.destroy()
wf.close()
stream.stop_stream() #停止音频输入流
stream.close() #关闭音频输入流
p.terminate() #释放音频对象
dec.destroy() #销毁解码器
wf.close() #关闭g711文件

media.buffer_deinit()
media.buffer_deinit() #释放vb buffer

def loop_codec(duration):
CHUNK = int(44100/25)
FORMAT = paInt16
CHANNELS = 2
RATE = 44100
CHUNK = int(44100/25) #设置音频chunk值
FORMAT = paInt16 #设置采样精度
CHANNELS = 2 #设置声道数
RATE = 44100 #设置采样率

p = PyAudio()
p.initialize(CHUNK)
dec = g711.Decoder(K_PT_G711A,CHUNK)
enc = g711.Encoder(K_PT_G711A,CHUNK)
ret = media.buffer_init()
if ret:
print("loop_audio, buffer_init failed")
p.initialize(CHUNK) #初始化PyAudio对象
dec = g711.Decoder(K_PT_G711A,CHUNK) #创建g711解码器对象
enc = g711.Encoder(K_PT_G711A,CHUNK) #创建g711编码器对象
media.buffer_init() #vb buffer初始化

dec.create()
enc.create()
dec.create() #创建g711解码器
enc.create() #创建g711编码器

#创建音频输入流
input_stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)

#创建音频输出流
output_stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
output=True,
frames_per_buffer=CHUNK)

#从音频输入流中获取数据->编码->解码->写入到音频输出流中
for i in range(0, int(RATE / CHUNK * duration)):
frame_data = input_stream.read()
stream_data = enc.encode(frame_data)
frame_data = dec.decode(stream_data)
output_stream.write(frame_data)


input_stream.stop_stream()
output_stream.stop_stream()
input_stream.close()
output_stream.close()
p.terminate()
dec.destroy()
enc.destroy()

media.buffer_deinit()

#encode_audio('/sdcard/app/test.g711a', 15)
#decode_audio('/sdcard/app/test.g711a')
loop_codec(15)
print("audio codec sample done")
frame_data = input_stream.read() #从音频输入流中获取raw音频数据
stream_data = enc.encode(frame_data) #编码音频数据为g711
frame_data = dec.decode(stream_data) #解码g711数据为raw数据
output_stream.write(frame_data) #播放raw数据
if exit_check():
break

input_stream.stop_stream() #停止音频输入流
output_stream.stop_stream() #停止音频输出流
input_stream.close() #关闭音频输入流
output_stream.close() #关闭音频输出流
p.terminate() #释放音频对象
dec.destroy() #销毁g711解码器
enc.destroy() #销毁g711编码器

media.buffer_deinit() #释放vb buffer

if __name__ == "__main__":
os.exitpoint(os.EXITPOINT_ENABLE)
print("audio codec sample start")
#encode_audio('/sdcard/app/test.g711a', 15) #采集并编码g711文件
#decode_audio('/sdcard/app/test.g711a') #解码g711文件并输出
loop_codec(15) #采集音频数据->编码g711->解码g711->播放音频
print("audio codec sample done")
Loading

0 comments on commit c296dab

Please sign in to comment.