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:
[lua]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 );[/lua] [import]uid: 123238 topic_id: 21642 reply_id: 321642[/import]

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 [import]uid: 7563 topic_id: 21642 reply_id: 85896[/import]

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. [import]uid: 123238 topic_id: 21642 reply_id: 86086[/import]

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 [import]uid: 123238 topic_id: 21642 reply_id: 86087[/import]

Just for the record (in case someone is struggling with similar issues), I was able to overcome this by being permanently recording.
When the volume drops, we stop the recording and seek the recorded file 1000ms back from where it started, and play it.
Works great. [import]uid: 123114 topic_id: 21642 reply_id: 90177[/import]

Great, glad you fixed it. [import]uid: 149863 topic_id: 21642 reply_id: 106976[/import]