Radu Gheorghiu
Radu Gheorghiu

Reputation: 20489

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.

Is there any part of the SL4A API I can use or part of the Python APIs that can be used? Or is there any python module I can install for this matter?

I've searched the Python API from SL4A but haven't found anything related to amplitude. 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. 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)

Upvotes: 1

Views: 1626

Answers (1)

bkconrad
bkconrad

Reputation: 2650

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).

Another option is the audioop module. 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). 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).

So, if SL4A implements all of the python core, you may be able to do it with no dependencies.

Upvotes: 1

Related Questions