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)

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

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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

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?

hi Peach,

Please help... .

Regards,

erwin

Hey again,

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

Peach

Thank You!!
it works ok on the simulator but breaks down on device :(

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

1
2
3
4
5
6
-- convert to scale
    local vdb = 20*math.log10( v ); 
 
        local vdb = v;
        
    youAre:setText(vdb)

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

Hi Snarla,

I've edited the code to:

1
2
3
4
5
6
7
8
function soundDetector:enterFrame( event )
    local v = r:getTunerVolume()
 
    if v == 0 then
        return
    end
 
    v = 20 * 0.434 * math.log(v) 

Hi Peach, Hi Snarla,

any updates on this issue?

Regards,

erwin

views:1686 update:2011/9/27 8:54:05
corona forums © 2003-2011