PDA

View Full Version : Help! PyQt4: get input level from the N900 Microphone


helex
2010-05-25, 19:58
Hi Folks! :)

In my opinion I have now a great Idea for a application to learn and contribute to the first N900 coding competition. :D

Yes, it will be great! But I don't want to share my Idea now... (Apple could steal it before I finished it) :)

It's a lot of work to do. So I will begin soon to finish before the deadline. I finished the script and some mockups for the UI, I know how to store in a Database, how to read binary files... I guess I can figure out how to draw images on the screen, store them on mmc1 and to see if the battery is charging. No problems so far for my, only work... except one of the core elements I don't know how to solve... :(

Without that the application is useless. So, I hope here is someone who can help me to finish my (top secret) project in PyQt4:

I need to read every 100ms the volume peak, the average input level or the maximum input level or something similar from the microphone. I don't want to record the stream or something else... just: noise out there, 20% of 100% maximum peak or 80%? A simple integer value how loud the environment is. :confused: I don't need more. :(

Are there someone out there who could show me the way?

I looked into the code of Tuner (http://maemo.org/packages/view/tuner/), but its to much for my needs and C++. :(

Is portaudio19-dev (http://maemo.org/packages/view/portaudio19-dev/) useful for me? :confused:

So please help so that I can start with my project! :)

mikec
2010-05-25, 20:01
If you are using PR1.2 then the QtMultimedia stuff is what you need.

see the blog

http://labs.trolltech.com/blogs/2010/05/18/qtmultimedia-in-action-a-spectrum-analyser/

helex
2010-05-25, 20:12
If you are using PR1.2 then the QtMultimedia stuff is what you need.

see the blog

http://labs.trolltech.com/blogs/2010/05/18/qtmultimedia-in-action-a-spectrum-analyser/

Mh, nice. PR1.2 is comming tomorrow. :D

I don't need it graphically shown... But I hope I can extract somewhere my needed Values... :)
Sadly, I can't find QMultimedia at the PyQt4 bindings: Class Reference (http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/classes.html)

Can I implement QMultimedia then in a direct way? :confused:

helex
2010-05-27, 16:05
Okay, here now the PyQt4 class reference for QtMultimedia (http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qtmultimedia.html)

But when I try to read from the defaultInputDevice() I get the error:
ALSA lib pcm.c:2211: (snd_pcm_open_noupdate) Unknown PCM null

So, to find out what I'm doing wrong I created this fast code:

from PyQt4 import QtMultimedia

devi = QtMultimedia.QAudioDeviceInfo.availableDevices(QtM ultimedia.QAudio.AudioInput)
for d in devi:
d.deviceName()

devo = QtMultimedia.QAudioDeviceInfo.availableDevices(QtM ultimedia.QAudio.AudioOutput)
for do in devo:
do.deviceName()

info = QtMultimedia.QAudioDeviceInfo(QtMultimedia.QAudioD eviceInfo.defaultInputDevice())
print info.deviceName()


I expected to have a result of input and output devices and the last one the default Input Device (microphone). But I get only null at the command line. The Lists devi and devo are empty.

What's wrong? Have I to activate the sound devices before? I haven't found anything like this in the class reference nor in your c++ example (http://qt.gitorious.org/qt/qt/blobs/4.6/demos/spectrum/app/engine.cpp). :(

mikec
2010-05-27, 18:57
I have not used it yet, but will do soon. Did you have a look at the multimedia example

http://doc.qt.nokia.com/4.6/examples-multimedia.html

And the ref python doc
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qtmultimedia.html#details

helex
2010-05-27, 21:48
I have not used it yet, but will do soon. Did you have a look at the multimedia example

http://doc.qt.nokia.com/4.6/examples-multimedia.html

No, I looked mostly in the engine module from the Trolltech Qt example (http://labs.trolltech.com/blogs/2010/05/18/qtmultimedia-in-action-a-spectrum-analyser/):
http://qt.gitorious.org/qt/qt/trees/4.6/demos/spectrum/app

But what I saw there was very similar to your example in:
http://doc.qt.nokia.com/4.6/multimedia-audioinput-audioinput-cpp.html


I tryed to translate it into Python. But I failed in the early begining with the identification of the audio devices:
http://doc.qt.nokia.com/4.6/multimedia-audiodevices-audiodevices-cpp.html


And the ref python doc
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qtmultimedia.html#details

Yes, I worked with it, but it seems I'm missing someting. :(

Here is the TESTING code I have at the moment: (I know, it's not well done and I need a Timer before stoping the recording. But without the fundamentals and essential defaultInputDevice its useless to polish it) :(


from PyQt4 import QtMultimedia
from PyQt4.QtCore import *

file = QFile()
file.setFileName("testMicIn.wav")
file.open(QIODevice.WriteOnly)

format = QtMultimedia.QAudioFormat()
format.setFrequency(8000)
format.setChannels(1)
format.setCodec("audio/pcm")
format.setByteOrder(QtMultimedia.QAudioFormat.Litt leEndian)
format.setSampleType(QtMultimedia.QAudioFormat.UnS ignedInt)

info = QtMultimedia.QAudioDeviceInfo(QtMultimedia.QAudioD eviceInfo.defaultInputDevice())

print "-"
devi = QtMultimedia.QAudioDeviceInfo.availableDevices(QtM ultimedia.QAudio.AudioInput)
for d in devi:
d.deviceName()

devo = QtMultimedia.QAudioDeviceInfo.availableDevices(QtM ultimedia.QAudio.AudioOutput)
for do in devo:
do.deviceName()

print "-"

print info.deviceName()

print info.isFormatSupported(format)

format = info.nearestFormat(format)
print info.isFormatSupported(format)



audio = QtMultimedia.QAudioInput(format, None)

audio.start(file)



audio.stop()
file.close()
del audio

gavinmitchell
2010-05-29, 22:41
I haven't tried any Qt4.6 development but have managed to get python to monitor mic audio levels using gstreamer tools

pipeline = gst.parse_launch("pulsesrc ! level message=true ! fakesink")
bus = pipeline.get_bus()
bus.enable_sync_message_emission()
bus.add_signal_watch()
bus.connect("message::element", on_message)
pipeline.set_state(gst.STATE_PLAYING)

then create a callback function that acts on the message retuned from the level monitor (you can control the frequency of the level sampling too)

noobmonkey
2010-06-01, 23:44
Helex, ever figure this one out? i'm struggling on QtMultimedia now :(

noobmonkey
2010-06-02, 07:28
Could be related too a QT bug here? - http://bugreports.qt.nokia.com/browse/QTBUG-10729

noobmonkey
2010-06-02, 07:38
Ok not sure it is related to this bug - http://bugreports.qt.nokia.com/browse/QTBUG-5723
But i'm still digging
aplay -L shows pulseaudio as our only method... But at least that shows something i spose!

helex
2010-06-03, 15:17
Helex, ever figure this one out? i'm struggling on QtMultimedia now :(

Sorry, no. I want to read more about and do some testing with gstreamer instead of QtMultimedia. But I have not enought time at the moment. :(

Personally I would prefer QtMultimedia. But without Audio source it's mostly useless. ;)
You can't find any Audio source with QtMultimedia, too?

Is your code similar to mine or have you done something totally different?

I thought it was only me. But if nobody at this forum can get QtMultimedia working... hmm... perhaps let's then creat a report at bugs.maemo.org to get a N900 specific reply. :confused:


Edit: Ah, and sorry for the late reply. Yesterday I enyoied the P!nk summer carneval tour! :D

noobmonkey
2010-06-03, 15:19
From what i can see so far.... Multimedia may not work, and qtmobility may be the way to go... will keep working on it :)

My code has ended up being very similar to yours! - started converting the C++....
It is new and i'll keep firing questions off. (I've also noted the pulse audio bug too) - so somewhere in there i'm looking for a workaround!

helex
2010-06-10, 22:31
I had not many time the last days... but today I found the babyphone application. It provides exactly what I need: http://maemo.org/packages/view/babyphone/

I looked into the source. And with my fundamental C++ knowledge I can't find any difference to my PyQt4 code posted several days ago in this thread. Am I correct or miss I some detail?

I guess something in PyQt4 is broken... :(

mikec
2010-06-27, 07:57
Just realised that you may need to have the qt mobility libs installed.
apt-get install libqtm*
going to have a look at this today.

kojacker
2010-07-01, 17:46
Just realised that you may need to have the qt mobility libs installed.
apt-get install libqtm*
going to have a look at this today.
Any luck with this, mike?

mikec
2010-07-02, 19:38
Any luck with this, mike?

nope, loaded the libs, and same problem. I get null for the device. I'm looking to learn C++ and Nokia SDK, as I'm stuck on NClock with PyQt and widgets, and I would really like to have this available.