'Class module
Sub Class_Globals
   Dim BufferSize As Int
   Dim SampleRate As Int
   Dim ChannelConfig As Int
   Dim AudioFormat As Int
   Dim AudioSource As Int
   Dim NoChnls,BitsPerSample,DataSize As Int
   
   Dim AR As AudioRecord
   Dim Record As Thread
   Dim StartTime As Long
   
   Dim OutFile As RandomAccessFile
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
Record.Initialise("Rec")
   
   'Set up recording parameters Not all parameters will be supported on all
   'devices
   AudioSource=AR.A_SRC_MIC
   SampleRate=44100
   ChannelConfig=AR.Ch_Conf_Mono
   NoChnls=1
   AudioFormat=AR.Af_PCM_16
   BitsPerSample=16
   
   
   'Determine the required min buffer size
   BufferSize=AR.GetMinBufferSize(SampleRate,ChannelConfig,AudioFormat)
   
   If BufferSize < 0 Then
      Msgbox("Buffer error, Hardware does not support recording with the given parameters","Buffer error")
   End If
   
   AR.Initialize(AudioSource,SampleRate,ChannelConfig,AudioFormat,BufferSize)
   'Test settings
'   AR.SetNotificationMarkerPosition(3000)
'   AR.SetPositionNotificationPeriod(2000)
   'delete the file if it exists to avoid problems with Random access files
   If File.Exists(File.DirRootExternal,"test.wav") Then
      File.Delete(File.DirRootExternal,"test.wav")
   End If
   
   'Initialize the output file
   OutFile.Initialize2(File.DirRootExternal,"test.wav",False,True)
   
   'Write the Wave file header
   WriteWavHeader
   AR.StartRecording
   
   'enable restriction of recording time for testing
   StartTime=DateTime.Now
   
   'Start the thread to record on
   Record.Start(Null,"Recording",Null)
      
End Sub
Sub WriteWavHeader
   Dim Pos,IntLen,ShLen,StrLen As Int
   
   Pos=0
   IntLen=4
   ShLen=2
   StrLen=4
   OutFile.WriteBytes(Array As Byte(Asc("R"),Asc("I"),Asc("F"),Asc("F")),0,StrLen,Pos)
   Pos=Pos+IntLen
   OutFile.WriteInt(0,Pos)            'Final size not yet known
   Pos=Pos+IntLen
   OutFile.WriteBytes(Array As Byte(Asc("W"),Asc("A"),Asc("V"),Asc("E")),0,StrLen,Pos)
   Pos=Pos+IntLen
   OutFile.WriteBytes(Array As Byte(Asc("f"),Asc("m"),Asc("t"),Asc(" ")),0,StrLen,Pos)
   Pos=Pos+IntLen
   OutFile.WriteInt(16,Pos)            'Sub chunk size 16 for PCM
   Pos=Pos+IntLen
   OutFile.WriteShort(1,Pos)            'Audio Format, 1 for PCM
   Pos=Pos+ShLen
   OutFile.WriteShort(1,Pos)            'No of Channels
   Pos=Pos+ShLen
   OutFile.WriteInt(SampleRate,Pos)
   Pos=Pos+IntLen
   OutFile.WriteInt(SampleRate*BitsPerSample*NoChnls/8,Pos)   'Byte Rate
   Pos=Pos+IntLen
   OutFile.WriteShort(NoChnls*BitsPerSample/8,Pos)   'Block align, NumberOfChannels*BitsPerSample/8
   Pos=Pos+ShLen
   OutFile.WriteShort(BitsPerSample,Pos)   'BitsPerSample
   Pos=Pos+ShLen
   OutFile.WriteBytes(Array As Byte(Asc("d"),Asc("a"),Asc("t"),Asc("a")),0,StrLen,Pos)
   Pos=Pos+IntLen
   OutFile.WriteInt(0,Pos)         'Data chunk size (Not yet known)
   Log("Pos "&Pos)
   
End Sub
Sub UpdateHeader
   Log("DataSize "&DataSize)
   OutFile.WriteInt(36+DataSize,4)
   OutFile.WriteInt(DataSize,40)
   OutFile.Flush
   OutFile.Close
End Sub
Sub Rec_Ended(endedOK As Boolean,Error As String)
   'Stop recording and release resources
   AR.stop
   AR.release
      UpdateHeader
   If endedOK Then
      'Finish writing the WAVE Header
      UpdateHeader
      
      Log("Thread Rec EndedOK "&endedOK&" "&Error)
      
      'Load the recorded file into Media player as a test
   End If
End Sub
Sub Recording
   'ReSet the data size
   DataSize=0
   
   Log("Recording...")
   'Do the recording
   'I've read that the read methods are blocking and won't return until
   ' the buffer is full.  Which appears to be the case in my testing.
   ' data has to be read pretty much immediately or it will get overwritten
   
   Do While True
      Dim RecData() As Byte
      Dim Sum As Int
      RecData=AR.ReadBytes(0,BufferSize)
      OutFile.WriteBytes(RecData,0,RecData.Length,44+DataSize)
      DataSize=DataSize+RecData.Length
      For i = 0 To 480 Step 2         'Approx 5ms worth of data
         Sum=Sum+(RecData(i)*256)+RecData(i+1)
      Next
      Record.RunOnGuiThread("Showvolume",Array As Object(Sum))
      'Check if interupt requested
      If Record.IsInterrupted Then Exit
      'Check if recording time is up
      If DateTime.Now > StartTime+"10000" Then Exit
   Loop
End Sub
Sub AudioRecord_PeriodPassed
   'Just for testing
   Log("PPCalled")
End Sub
Sub Showvolume(Sum As Int)
   Sum=Sum/240+(32767/2)
   Log("Ampl:" & Sum)
   'Convert To dB
   Sum=20*Logarithm(Sum/32767,10)
   'DbLbl.Text=Sum&" dB"
   Log("dB: " & Sum)
End Sub
Sub stop
AR.stop
   AR.release
      UpdateHeader
   If Record.Running Then Record.Interrupt
   Log("Stopped")
End Sub