sound recording delay

I've been trying to build a "Talking Tom" clone App, but it fails to point that it does not record the first second of what we speak, causing it to become impracticable.
I dont know if thats a bug in Corona's media API, or simply bad code design from me :\

Any tips or corrections are most welcome.

Here is the code:

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
display.setStatusBar( display.HiddenStatusBar )
 
-- iOS 5 -only workaround for known Apple bug (Bug ID:9948362 and 10508829)
local platVersion = system.getInfo( "platformVersion" )
platVersion = string.sub( platVersion, 1, 1 )
if system.getInfo( "platformName" ) == "iPhone OS" and platVersion == "5" then
        if audio.supportsSessionProperty then
            audio.setSessionProperty(audio.MixMode, audio.PlayAndRecordMixMode)
        end
end
 
 
local dataFileName = "testfile"
if "simulator" == system.getInfo("environment") then
    dataFileName = dataFileName .. ".aif"
else
        local platformName = system.getInfo( "platformName" )
    if "iPhone OS" == platformName then
        dataFileName = dataFileName .. ".aif"
    elseif "Android" == platformName then
        dataFileName = dataFileName .. ".pcm"
    else
        print("Unknown OS " .. platformName )
    end
end
 
 
local fSoundPlaying = false   -- sound playback state
local filePath = system.pathForFile( dataFileName, system.DocumentsDirectory )
local recPlay = media.newRecording(filePath) -- object for recording and playing the audio
local recVol = media.newRecording() -- object for volume measurement
recVol:startRecording(); recVol:startTuner() -- init the Volume measurement
 
-- Create an object to hold display items
local g = display.newGroup()
 
-- status label
local volume = display.newText( "INIT", 0, 0, nil, 22 )
volume:setReferencePoint( display.TopLeftReferencePoint)
volume:setTextColor( 255,0,0, 200 )
volume.x = display.contentWidth/2 - 20
volume.y = display.contentHeight/2
g:insert(volume)
 
 
-- quando acaba de tocar
local function onCompleteSound (event)
    fSoundPlaying = false
    -- Free the audio memory and close the file now that we are done playing it.
        audio.dispose(event.handle)
end
 
 
local function startRecording ()
        if recPlay:isRecording () then
                --do nothing
        else
                fSoundPlaying = false
                recPlay:startRecording()
        end
end
 
 
local function startPlaying ()
   if recPlay:isRecording () then   
                recPlay:stopRecording()
                -- Play back the recording
                local file = io.open( filePath, "recPlay" )
                if file then
                        io.close( file )
                        fSoundPlaying = true
                        playbackSoundHandle = audio.loadStream( dataFileName, system.DocumentsDirectory )
                        echoChannel, echoSource = audio.play( playbackSoundHandle, { onComplete=onCompleteSound } )
                        al.Source(echoSource, al.PITCH, 1.5 );
                end
        else
                -- do nothing
        end
end
 
 
-- volume measurement
function volume:enterFrame( event )
        local v = 20*math.log(recVol:getTunerVolume())
        
        if fSoundPlaying then
            volume.text = "PLAYING"
    else                
                if (v > -20) then
                        volume.text = "RECORDING @ " .. string.format("%4.2f", v )
                        startRecording ()
                else 
                        volume.text = "IDLE @ " .. string.format("%4.2f", v )
                        startPlaying()
                end
        end
end
Runtime:addEventListener( "enterFrame", volume );

Are you running iOS 5? There is a bug that causes a delay. However, that code at the top is supposed to work around things.

Does the sample recording app have similar problems?

Have you tried the ALexplorer app? It tries to do some Talking Tom like stuff. Does it have the same problem? Remember to add the iOS workaround to the top of the code.

http://developer.anscamobile.com/code/alexplorer

Yes im running the App on iOS5.
Im not sure about the code, thought, because from what I've understood the problem resides in a delay in the Corona "Tunner API" not the "Recording API"...
I state this because the recording works great if I use a record botton, its instant! We can check this with the SimpleAudioRecorder demo provider with the SDK.
But I want the recording to start automatically when the volume reaches a certain decibel volume.
And because of the delay in the "Tunner API", my recording function awakes too late.

I've tried to tun the ALexplorer app both on the simulator and on the iphone/iPod, but it does not work in either places... on the screen its just a white "record" button that apparently does nothing.

Silva.

further digging, and found out that another user (Bajazz) has already found out about this huge delay in the tunner, and report it as a bug:
http://developer.anscamobile.com/forum/2012/02/07/tuner-delay-issue

views:2829 update:2012/2/12 11:34:30
corona forums © 2003-2011