Sub Process_Globals
Public App As Application
Public NavControl As NavigationController
Private Page1 As Page
Private mp As MediaPlayer
End Sub
Private Sub Application_Start (Nav As NavigationController)
NavControl = Nav
Page1.Initialize("Page1")
Page1.Title = "Page 1"
Page1.RootPanel.Color = Colors.White
NavControl.ShowPage(Page1)
CreateBeepFile(File.DirLibrary, "1.wav", 1000, 500)
mp.Initialize(File.DirLibrary, "1.wav", "mp")
mp.Play
End Sub
Sub CreateBeepFile(Dir As String, FileName As String, Duration As Int, Frequency As Int)
Dim sampleRate As Int = 8000
Dim raf As RandomAccessFile = StartWaveFile(Dir, FileName, sampleRate, True, 16)
Dim dur As Double = Duration / 1000
Dim numSamples As Int = sampleRate * dur
Dim generateSnd(2 * numSamples) As Byte
Dim idx As Int = 0
For i = 0 To numSamples - 1
Dim d As Double = Sin(2 * cPI * i / (sampleRate / Frequency))
Dim val As Short = d * 32767
generateSnd(idx) = Bit.And(val, 0x00ff)
generateSnd(idx + 1) = Bit.ShiftRight(Bit.And(val, 0xff00), 8)
idx = idx + 2
Next
raf.WriteBytes(generateSnd, 0, generateSnd.Length, raf.CurrentPosition)
'close
raf.WriteInt(raf.Size - 8, 4)
raf.WriteInt(raf.Size - 44, 40)
raf.Close
End Sub
Sub StartWaveFile(Dir As String, FileName As String, SampleRate As Int, Mono As Boolean _
, BitsPerSample As Int) As RandomAccessFile
File.Delete(Dir, FileName)
Dim raf As RandomAccessFile
raf.Initialize2(Dir, FileName, False, True)
raf.WriteBytes("RIFF".GetBytes("ASCII"), 0, 4, raf.CurrentPosition)
raf.CurrentPosition = 8 'skip 4 bytes for the size
raf.WriteBytes("WAVE".GetBytes("ASCII"),0, 4, raf.CurrentPosition)
raf.WriteBytes("fmt ".GetBytes("ASCII"),0, 4, raf.CurrentPosition)
raf.WriteInt(16, raf.CurrentPosition)
raf.WriteShort(1, raf.CurrentPosition)
Dim numberOfChannels As Int
If Mono Then numberOfChannels = 1 Else numberOfChannels = 2
raf.WriteShort(numberOfChannels, raf.CurrentPosition)
raf.WriteInt(SampleRate, raf.CurrentPosition)
raf.WriteInt(SampleRate * numberOfChannels * BitsPerSample / 8, raf.CurrentPosition)
raf.WriteShort(numberOfChannels * BitsPerSample / 8, raf.CurrentPosition)
raf.WriteShort(BitsPerSample, raf.CurrentPosition)
raf.WriteBytes("data".GetBytes("ASCII"),0, 4, raf.CurrentPosition)
raf.WriteInt(0, raf.CurrentPosition)
Return raf
End Sub