[Q] how to get microphone level and detect peaks

376 views
Skip to first unread message

Pedro Infantilo

unread,
Oct 18, 2015, 5:56:34 AM10/18/15
to AndroidScript
HI,
could anyone help me getting the "realtime" microphone level and detect peaks?
Is this possible with the droidscript?

MANY thanks for reply

Steve Garman

unread,
Oct 18, 2015, 6:21:45 AM10/18/15
to AndroidScript
Does this give you enough information to work with?

var timer;
function OnStart()
{
    rec = app.CreateAudioRecorder();
    rec.Start(); 
    clearInterval( timer ); 
    timer = setInterval( GetLevel, 250 );
}

function GetLevel() 

    var level = rec.GetRMS().toFixed(8); 
    app.ShowPopup( "Level: " + level ); 

Pedro Infantilo

unread,
Oct 18, 2015, 8:14:42 AM10/18/15
to AndroidScript
THX for quick reply ;-)
But sadly level delivers value between 0.53 and 0.65 but not depending on the mic level.
(I'm running the coee on an motorola Moto G android 5.0.2)

As i'll need video, too, can i get the level from the camera in previewmode, too?

Steve Garman

unread,
Oct 18, 2015, 8:26:32 AM10/18/15
to AndroidScript
Strange, I wrote and tested that on an XT1068 running 5.0.2 and it goes up and down exactly with the mic. level.

Have you tried running the Audio Recorder sample?
The running display on that converts the calculation to something closer to a percentage.

As for the camera, it is hard to think why it would expose sound levels, so I doubt it.

Pedro Infantilo

unread,
Oct 18, 2015, 8:48:36 AM10/18/15
to AndroidScript
I've tested the Audio recorder example on my moto g and my archos 101 g9, and both show random values about 76-80%...nothing corresponding to the mic sound level :-(

Pedro Infantilo

unread,
Oct 18, 2015, 11:13:53 AM10/18/15
to AndroidScript
HI Steve,
i've tested both on 4 devices now, and never got an different result than the random 76-80%.... could it be an bug in the current droidscript version?.
Your give code isn't that long as there could be much wrong... 

once more, thanks for your quick and great support.... maybe you've an solution...

your's
Peter

Paul Norman

unread,
Oct 19, 2015, 2:41:56 AM10/19/15
to AndroidScript
Hi Pedro,

I'm also on a Motor G2, 5.02 sitting in a MacDonalds running Steve's code with level kept as text, and my results are on a much tighter band to yours, altering predictibly with the noise levels in the environment.

That's all I can tell you. Levels

Paul

var timer; 
var levels ="";
...

function GetLevel()  
{  
    var level = rec.GetRMS().toFixed(8);  
   // app.ShowPopup( "Level: " + level );  

levels += level + "\n";
app.SetClipboardText(levels);


0.55665195
0.54721826
0.57111270
0.59057720
0.58815150
0.57045823
0.59231720
0.57845294
0.56449986
0.58762085
0.58775680
0.58781123
0.56100450
0.56509876
0.56763660
0.60463500
0.55121404
0.58935100
0.58740515
0.56444560
0.57396110
0.56444310
0.56324540
0.57536423
0.59228116
0.59017390
0.57565060
0.57328000
0.57927180
0.57737430
0.56759570
0.55457590
0.58836967
0.60302400
0.56731796
0.57129970
0.57987106
0.58532524
0.55125225

Steve Garman

unread,
Oct 19, 2015, 2:53:33 AM10/19/15
to AndroidScript
I suspect the problem here is that I tested it with background noise too (turning TV volume up and down.)

RMS is basically a smoothing function and I suspect Pedro is more interested in peaks.

I'm afraid I don't think you will find anything built into DroidScript that will do this for you, Pedro.

Pedro Infantilo

unread,
Oct 19, 2015, 4:22:54 AM10/19/15
to AndroidScript
ok :-(
That's bad news....i'd need to measure delay between audio "burst" and video "burst"... :-(
video was simple in androidscript...
could audio peak be get by plugin in general?
I'd really love the  idea of droidscript and would like to keep coding with it.

Btw...is there a way to create/read phone's calendar events?

Dave Smart

unread,
Oct 19, 2015, 4:43:32 AM10/19/15
to AndroidScript
Hi Guys,

There is actually a GetPeak() method in DS but it has not been exposed to JavaScript yet. I have just exposed it now, so the next private beta release should allow you to get that information (Peak amplitude for the current sample buffer).

Also remember that most devices will have some sort of auto-gain circuit on the microphone which will boost the signal level when it goes quiet (it might be possible to turn this off in the settings or through code. I'm not sure)

Regards
David  

Pedro Infantilo

unread,
Oct 19, 2015, 5:16:33 AM10/19/15
to AndroidScript
Great!
The auto gain could be the reason for the strange behavior...even when it's absolute silent in the room, i get values about 76% and i tried a lot today, but i could not get higher value when it have loud music plaing, or its quiet in the room...an i'm currently on a XT1032 :-(

your's
Peter

Dave Smart

unread,
Oct 19, 2015, 6:10:14 AM10/19/15
to AndroidScript
The AGC issue may be a difficult one to solve :-



Note: I'm using the VOICE_RECOGNITION mode

Pedro Infantilo

unread,
Oct 19, 2015, 2:11:32 PM10/19/15
to AndroidScript
What will i have to, to get Access to the next public beta release?
 
Your's
Peter 

Chris Hopkin

unread,
Oct 20, 2015, 3:57:04 AM10/20/15
to AndroidScript
Hi Pedro

Please send an email to Dave if you wish to become a Beta Tester.

Thanks

Chris

Pedro Infantilo

unread,
Oct 20, 2015, 6:24:02 AM10/20/15
to AndroidScript
Hi Dave,
may i become beta tester,... MANY thanks

Pedro Infantilo

unread,
Oct 24, 2015, 4:19:39 AM10/24/15
to AndroidScript
Hi Steve,
sorry, i was a little bit busy....
The GetRMS from the CreateMediaRecorder does still deliver random values. Using GetRMS from the CreateSpeechRec delivers values that correspond to the mic Level.
It seems sto me, that the MediaRecorder records the system audio instead of the mic.
Is there a way to set the source for the mediarecorder (e.g. by optionflags?)
The SpeechRec for some reasons stops after a period of time, even when restarted.
 
When will the nex beta release available to use the GetPEAK() ??? ;-)
 
May i ask just an other question: why is the IntelliSense code-completion not available in the droidscript-app but only using the browser?  
 
 

Steve Garman

unread,
Oct 24, 2015, 4:49:10 AM10/24/15
to AndroidScript
If you are going to use the speechRec, you will need to restart it continuously.

I recommend taking a look at the "Voice Command" sample for a straightforward way to do that.

As for your other questions, I'm afraid they are outside my sphere of knowledge.

Pedro Infantilo

unread,
Oct 25, 2015, 9:31:41 AM10/25/15
to AndroidScript
I really dont want to be bothersome, but i swear, there's something wrong with the getRMS() function in the MediaRecorder!
 
 
Even when recorded to a file (and not at /dev/null), the values return are random.
GetRMS using the speechrec works, BUT thats not useable in my case cause when the recognition process runs, the GetRMS values hangs till the recognition Returns.
 
And shure i did restart the speechrec as in the sample (that i had a look at before for shure), but after a few recognitions, it hangs ant wont come to life again.
 
what audiosource is set for the MediaRecorder?
 
I tried with b4a to use the android mediarecorder with MediaRecorder.AudioSource.MIC and this works on my devices as expected. 
 
I'm sorry for being troublesome, but as i'm going to buy the apkbuilder, i Need to enshure, things are working as expected. 
Reply all
Reply to author
Forward
0 new messages