'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim MediaPlayer1 As MediaPlayer
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim AudioFile(88200) As Byte ' one second of sound
Dim AudioFileCounter As Long
Dim BytePacket(13) As Byte
End Sub
Sub Activity_Create(FirstTime As Boolean)
Dim i As Int
Msgbox("Play a wav file", "First program")
MediaPlayer1.Initialize( )
MediaPlayer1.Load("/sdcard", "1200.wav")
'Msgbox("Hit the home key to exit", "Finish")
CreateWavHeader ' create the header
For i=1 To 10
addbytetowav(0)
addbytetowav(1)
addbytetowav(2)
addbytetowav(85)
addbytetowav(255)
Next
savewavfile
playcreatedwav
activity.finish ' close the program
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub SaveWavFile
Dim raf As RandomAccessFile
Msgbox("Saving to sd card","")
raf.Initialize(File.DirDefaultExternal, "packet.wav", False)' open the file which will end up in I:\Android\data\test.test.test\files (test.test.test is my program)
raf.WriteBytes(audiofile,0,88199, 0) ' write bytes. Is 32768 the max size as this is an integer variable
raf.close
End Sub
Sub PlayCreatedWav
MediaPlayer1.Initialize( )
MediaPlayer1.Load(File.DirDefaultExternal , "packet.wav")
End Sub
Sub CreateWavHeader
Dim i As Int
audiofile(0)=0x52 ' R
audiofile(1)=0x49 ' I
audiofile(2)=0x46 ' F
audiofile(3)=0x46 ' F
audiofile(4)=0x34
audiofile(5)=0xB1
audiofile(6)=0x02
audiofile(7)=0x00
audiofile(8)=0x57
audiofile(9)=0x41
audiofile(10)=0x56
audiofile(11)=0x45
audiofile(12)=0x66
audiofile(13)=0x6D
audiofile(14)=0x74
audiofile(15)=0x20
audiofile(16)=0x10
audiofile(17)=0x00
audiofile(18)=0x00
audiofile(19)=0x00
audiofile(20)=0x01
audiofile(21)=0x00
audiofile(22)=0x01
audiofile(23)=0x00
audiofile(24)=0x44
audiofile(25)=0xAC
audiofile(26)=0x00
audiofile(27)=0x00
audiofile(28)=0x88
audiofile(29)=0x58
audiofile(30)=0x01
audiofile(31)=0x00
audiofile(32)=0x02
audiofile(33)=0x00
audiofile(34)=0x10
audiofile(35)=0x00
audiofile(36)=0x64
audiofile(37)=0x61
audiofile(38)=0x74
audiofile(39)=0x61
audiofile(40)=0x5C ' size in bytes (same as size of array = 88200 = 0x15888 minus the 44 bytes at the beginning 1585C)
audiofile(41)=0x58
audiofile(42)=0x01
audiofile(43)=0x00
' data starts at 44
For i=44 To 99
audiofile(i)=0 ' some zero values at the beginning of the file
Next
audiofilecounter=100' start of data
End Sub
Sub FillBytePacket
Dim i As Int
For i=0 To 13
bytepacket(i)=65+i ' dummy values starting at A
Next
End Sub
Sub AddByteToWav(ByteValue As Int) ' uses audiofilecounter
' wav file format is 0 for silence. values from -7FFF to 8000. LSB first. So FFFF is -1 which is just less than 0 for silence
' data starts at byte 43 https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
' so on hexedit, header is first two lines and 12 bytes of the third line
' the previous 4 bytes are the size of the data NumSamples * NumChannels * BitsPerSample/8
' this is a 1 channel sample.
' bytes 34 and 35 are the bits per sample = 0x0010 which is 16
' so size is numsamples * 1 * 16/ 8 which is numsamples *2
' the number of samples I have for 1 second of 1200 baud is 0x015876 which is 88182 or 44091 x 2.
' for a 1200 baud square wave this is high for 1/2400th of a second and low for 1/2400th second
' sample rate is 44100 per second.
' for a 4410hz tone this will be high for 5 samples and low for 5 samples
' 1200 is 36.75 samples. 18.375 high and 18.375 low.
' BUT - 1200 baud means 1200 bits per second. So a character U will be 18.375 low then 18.375 high etc
' the start bit is 18.375 samples.
' There are some aliasing issues here as well due to the remainder. This is 147/8
' so need to round to the nearest integer
' 18.375 = 18
' 36.75 = 37
' 55.125 = 55
' 73.5 = 74
' 91.875 = 91
' 110.25 = 110
' 128.625 = 129
' 147 = 147
' 165.375 = 165
' 183.75 = 184
' 202.125 = 202
' 220.5 = 221 rounded up as this is the idle bit then start at zero again
' then restart on whole integer values
' RS232 idle is -12V. Logical 1 is -12V. Logical 0 is +12V. Start bit is +12V. Stop bit is -12V
' So we use the inverse of this ready to go into a max232
' idle = 5V, logical 1 is 5V, logical 0 is 0, Start bit is 0V, Stop bit is 5V
' so there is the start bit, 8 data bits, stop bit and an idle bit making 11 bits.
' set up a counter and start at 0
' 0-17 = start bit
' 18-36 = bit 0
' 37-54 = bit 1
' 55-73 = bit 2
' 74-90 = bit 3
' 91-109 = bit 4
' 110-128= bit 5
' 129-146= bit 6
' 147-164= bit 7
' 165-183= stop bit
' 184-202= idle
Dim i As Long
Dim testvalue As Int
Dim msb As Byte
Dim lsb As Byte
msb=0x7F
lsb=0xFF
For i=0 To 17 ' start bit
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
testvalue=Bit.And(bytevalue,0x01)' test bit 0
If testvalue=0 Then
msb=0x7F ' hex 7FFF
lsb=0xFF
Else
msb=0x80 ' hex 8000
lsb=0x00
End If
For i=18 To 36
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
testvalue=Bit.And(bytevalue,0x02)' test bit 1
If testvalue=0 Then
msb=0x7F ' hex 7FFF
lsb=0xFF
Else
msb=0x80 ' hex 8000
lsb=0x00
End If
For i=37 To 54
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
testvalue=Bit.And(bytevalue,0x04)' test bit 2
If testvalue=0 Then
msb=0x7F ' hex 7FFF
lsb=0xFF
Else
msb=0x80 ' hex 8000
lsb=0x00
End If
For i=55 To 73
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
testvalue=Bit.And(bytevalue,0x08)' test bit 3
If testvalue=0 Then
msb=0x7F ' hex 7FFF
lsb=0xFF
Else
msb=0x80 ' hex 8000
lsb=0x00
End If
For i=74 To 90
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
testvalue=Bit.And(bytevalue,0x10)' test bit 4
If testvalue=0 Then
msb=0x7F ' hex 7FFF
lsb=0xFF
Else
msb=0x80 ' hex 8000
lsb=0x00
End If
For i=91 To 109
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
testvalue=Bit.And(bytevalue,0x20)' test bit 5
If testvalue=0 Then
msb=0x7F ' hex 7FFF
lsb=0xFF
Else
msb=0x80 ' hex 8000
lsb=0x00
End If
For i=110 To 128
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
testvalue=Bit.And(bytevalue,0x40)' test bit 6
If testvalue=0 Then
msb=0x7F ' hex 7FFF
lsb=0xFF
Else
msb=0x80 ' hex 8000
lsb=0x00
End If
For i=129 To 146
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
testvalue=Bit.And(bytevalue,0x80)' test bit 7
If testvalue=0 Then
msb=0x7F ' hex 7FFF
lsb=0xFF
Else
msb=0x80 ' hex 8000
lsb=0x00
End If
For i=147 To 164
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
' stop bit = high
msb=0x80
lsb=0x00
For i=165 To 183
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
' idle bit = high and leave the rest high as well
msb=0x80
lsb=0x00
For i=184 To 202
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
End Sub