【问题标题】:Call in 3G modem Python, PyAudio调用 3G 调制解调器 Python、PyAudio
【发布时间】:2014-06-19 23:30:57
【问题描述】:

我有两个 python 脚本。第一个使用 At 命令调用,第二个接收音频并将其发送到计算机麦克风。问题是当我发送音频时调制解调器会重置或收听速度很慢。

首先(docall.py)

import serial

se = serial.Serial()
se.port = 5
se.baudrate = 9600 # may be different
se.timeout = 0.5

a=1
se.open()
cont=0
while (a==1):
 if not se.isOpen():
  se.open()
 if cont == 1:
  se.write('AT+CHUP;\r\n')
  se.write('ATD*264;\r\n')
 if cont == 2:
  se.write('AT^DDSETEX?;\r\n')
 if cont == 3:
  se.write('AT^DDSETEX=2;\r\n')

 line = se.read(1024)
 print (line)
 cont=cont+1

se.close()

第二个(audio.py)

import serial
import pyaudio
import wave
import time

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 8000
RECORD_SECONDS = 10
WIDTH = 2

pe = pyaudio.PyAudio()

stream= pe.open(format=pe.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                output=True,
                frames_per_buffer=CHUNK)

mic= pe.open(format=pyaudio.paInt16,
                channels=1,
                rate=8000,
                input=True,
                frames_per_buffer=128)

se = serial.Serial()
se.port = 3
se.baudrate = 9600 # may be different
se.timeout = 0.5
se.open()
se.writeTimeout = 0

streaming = []
frames = []
while True:
 re= se.read(CHUNK)
 stream.write(re)
 se.write(mic.read(612))

【问题讨论】:

  • 代码本身听起来不错,虽然第一步中的状态机构造非常笨拙,而且在 Python 中也有更简单的方法。你怎么知道这是一个 Python 问题而不是硬件问题......?
  • 因为当我使用调制解调器编译的软件进行测试时,通话听得很好。
  • 所以您将audio.py 替换为其他程序,而单独留下docall.py,它可以正常工作吗?如果是这样,那么您至少已将问题范围缩小到audio.py。听起来您需要验证 audio.py 是否与编译后的程序(用 C 语言编写?)完全相同。

标签: python call pyaudio


【解决方案1】:

您需要将帧缓冲区设置为 160 以匹配设备速率。我正在使用 pa 设备回调来写入和读取设备,键数是每个缓冲区 320 帧。

device = Serial('/dev/tty.HUAWEIMobile-Diag', timeout=None,  writeTimeout=0)

time.sleep(0.1) #this sleep needed for device buffer clean
device.flush()
device.flushInput()
device.flushOutput()

uni_stream = pe.open(format=pyaudio.paInt16,
                     channels=1,
                     rate=8000,
                     input=True,
                     output=True,
                     frames_per_buffer=160, stream_callback=uni_callback)

def uni_callback(in_data, frame_count, time_info, status):
    data = device.read(320)
    device.write(in_data)

    return (data, pyaudio.paContinue)

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多