简体   繁体   English

使用SL4A使用Python获取录制声音的幅度

[英]Get amplitude of recorded sound with Python using SL4A

I want to record sound for 1 second using a Python written app for SL4A and then find the max amplitude of the sound. 我想使用Python编写的SL4A应用程序录制声音1秒钟,然后找到声音的最大振幅。

Is there any part of the SL4A API I can use or part of the Python APIs that can be used? 我可以使用SL4A API的任何部分,还是可以使用的Python API的一部分? Or is there any python module I can install for this matter? 还是可以为此安装任何python模块?

I've searched the Python API from SL4A but haven't found anything related to amplitude. 我从SL4A搜索了Python API,但没有找到任何与振幅相关的信息。 Maybe I just missed it. 也许我只是想念它。

UPDATE: 更新:

I've managed to import the wave.py module into my code and try to open the recorded file. 我设法将wave.py模块导入到我的代码中,并尝试打开记录的文件。 But when I access 但是当我访问

wave.open("/sdcard/sl4aTemp/sound_sample.wav")

it throws this error, 它会引发此错误,

File does not start with RIFF id

For those of you who are curious how my code looks here it is: 对于那些好奇我的代码在这里看起来的人,它是:

import android, audioop, time, wave

droid = android.Android()

print "Recording starts in: "
for i in range(0,5):
    time.sleep(1)
    print str(5-i)
time.sleep(1)

print "Speak!"
droid.recorderStartMicrophone('/sdcard/sl4aTemp/sound_sample.wav')
time.sleep(3)
droid.recorderStop()

print "Processing file in:"
for i in range(0,3):
    print str(3-i)
    time.sleep(1)

filename = "/sdcard/sl4aTemp/sound_sample.wav"

if wave.open(filename,"r") == True:
    print "Processing " + filename
else:
    print "File not processed"

FILE=wave.open(filename,"r")
rez=FILE.readframes(30)
print str(rez)

The standard answer to sound processing with python is PyAudio, which is separate package that actually depends on PortAudio, which probably hasn't been built for android so it's not really a good solution (unless you feel like being a hero and trying to get it to build). 使用python进行声音处理的标准答案是PyAudio,这是一个独立的程序包,实际上依赖于PortAudio,它可能不是为android构建的,因此,它不是一个很好的解决方案(除非您觉得自己是英雄,并且想要获得它建立)。

Another option is the audioop module . 另一个选择是audioop模块 The problem there is that you'll need to convert whatever format that file is saved as into the format audioop accepts (strings of 8/16/32bit wide signed integer samples). 问题在于您需要将文件保存的任何格式转换为audioop接受的格式( audioop位宽的带符号整数样本的字符串)。 It's hard to say just how you'd do that, but if you're really lucky it'll be a .wav file and you can just use the wave module's readframes (which conveniently outputs the data as a string of bytes). 很难说出该怎么做,但是如果您真的很幸运,它将是一个.wav文件,您可以只使用wave模块的readframes (方便地将数据输出为字节字符串)。

So, if SL4A implements all of the python core, you may be able to do it with no dependencies. 因此,如果SL4A实现了所有python核心,那么您可以无依赖地做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM