Microphone API does not work - getTunerVolume()

Hi,

We are creating a blow game for iOS. We are using getTunerVolume() but it seems that the value is always at around .56 to .57. The change happens even with just the background sound. Even if I shout or blow directly to the mic, the value still remains at .56 to .57. My mic is working fine because I can record sounds using the Sound Recorder built in app.

We used the code below.

Regards,

erwin

– Create an object to access audio input features
local r = media.newRecording()
r:startRecording()
r:startTuner()

– Create an object to hold display items
local g = display.newGroup()

– Simple sound detector
– This just changes the color of a text label in response to a detected sound.
– There are two threshold quantities here that could be used to adjust the sensitivity,
– one is the amount of change that detects sound onset, the other is for sound offset.
– You could connect these to a UI element to allow the user to adjust the sensitivity.
local soundDetector = display.newText( “…”, 0, 0, nil, 22 )
soundDetector:setReferencePoint( display.TopLeftReferencePoint)
soundDetector:setTextColor( 255,255,255, 150 )
soundDetector.x = display.contentWidth/2
soundDetector.y = 0.8*display.contentHeight
local lastV = 0
local threshold = {}
threshold.pos = 2.5 – adjust these
threshold.neg = 1 – to change sensitivity

function soundDetector:enterFrame( event )
local v = r:getTunerVolume()
if v == 0 then
return
end
– Convert RMS power to dB scale
v = 20 * 0.301 * math.log(v)
soundDetector.text = string.format("%4.2f", v - lastV )
if v > lastV + threshold.pos then
– Detected a level increase
soundDetector:setTextColor( 255,255,100, 255 )
lastV = v
else
if v < lastV - threshold.neg then
– Detected a level drop
soundDetector:setTextColor( 255,255,255, 150 )
lastV = v
else
– Adapt the background level
– Simple running average
lastV = 0.5 * v + 0.5 * lastV
end
end
end
Runtime:addEventListener( “enterFrame”, soundDetector );
g:insert(soundDetector) [import]uid: 75358 topic_id: 13589 reply_id: 313589[/import]

Hey there,

Please post your code in < lua > tags, it makes it much easier to read.

Take a look here; http://developer.anscamobile.com/forum/2010/11/28/gettunervolume-values that thread discusses some of the more finicky parts of using getTunerVolume :slight_smile: [import]uid: 52491 topic_id: 13589 reply_id: 49882[/import]

Hi Peach,

Base on the thread you gave, we changed the code to 20*log10(v). Nothing changed in the result.

This is my code. The value is always .56 to .58.
How is it possible to detect a blow if the result is like that?

[lua]module(…, package.seeall)

function new()
local localGroup = display.newGroup()

– YOUR CODE WILL GO HERE

local youAre = ui.newLabel{
bounds = { 20, 50, 300, 40 },
text = "Sound Meter ",
textColor = { 43, 89, 157, 255 },
size = 20,
align = “center”
}
localGroup:insert(youAre)

local function fadeIn (event)
– stop recording
r:stopRecording()
r:stopTuner()
Runtime:removeEventListener( “enterFrame”, soundDetector )
end

– detect the sound of the microphone
r = media.newRecording()
r:startRecording()
r:startTuner()

function soundDetector( event )
local v = r:getTunerVolume()

if v == 0 then
return
end

– convert to scale
local vdb = 20*math.log10( v );

local vdb = v;

youAre:setText(vdb)

– ignore weak sounds / background noise
if vdb >= 5.5 then
– call function
return
end

end
Runtime:addEventListener( “enterFrame”, soundDetector );


return localGroup
end[/lua] [import]uid: 75358 topic_id: 13589 reply_id: 49907[/import]

I read from the same forum that it is taking the average, so maybe that is why the value is always around 0.57 to 0.58.

The question is how can I get peak values? [import]uid: 75358 topic_id: 13589 reply_id: 49910[/import]

hi Peach,

Please help… .

Regards,

erwin [import]uid: 75358 topic_id: 13589 reply_id: 50041[/import]

Hey again,

Let me take a look and this and I, or someone a little cleverer with this API :wink: will get back to you :slight_smile:

Peach [import]uid: 52491 topic_id: 13589 reply_id: 50144[/import]

Thank You!!
it works ok on the simulator but breaks down on device :frowning: [import]uid: 75358 topic_id: 13589 reply_id: 50148[/import]

What device? The API is only for iOS. Also, in your code, you have

-- convert to scale  
 local vdb = 20\*math.log10( v );   
   
 local vdb = v;  
  
 youAre:setText(vdb)  

which also won’t work… vdb stays the same [import]uid: 6787 topic_id: 13589 reply_id: 50247[/import]

Hi Snarla,

we’re using ipod touch 4th gen. Why is that 20*math.log10(v) will have the same results? What should be the right formula then?

Regards,

erwin [import]uid: 75358 topic_id: 13589 reply_id: 50327[/import]

Hi Snarla,

I’ve edited the code to:

[lua]function soundDetector:enterFrame( event )
local v = r:getTunerVolume()

if v == 0 then
return
end

v = 20 * 0.434 * math.log(v) [/lua]

yes, the device is iOS - I’m using a 4th gen Ipod Touch for testing.

I’ve tried different sets of code posted in different threads such as…
v = 20 * 0.434 * math.log(v)
v = 20 * 0.301 * math.log(v) ( from your example )

The result on all these: I get some values jumping in a certain range. ie 0.70-0.75

On low background noise, detecting a blow or speech works. The number moves up and back down a considerable amount.
Covering the microphone with my finger makes the number jump too ( Don’t know why )

But when the background noise is up ( music playing in the background or strong wind ), it has trouble detecting anything. The number stays at 0.70-0.75 no matter how loud I might shout or if I stick it infront of loud music.

Am I missing something? [import]uid: 75358 topic_id: 13589 reply_id: 50331[/import]

Hi Peach, Hi Snarla,

any updates on this issue?

Regards,

erwin [import]uid: 75358 topic_id: 13589 reply_id: 51851[/import]