I am not going to give up yet
public int setPlaybackRate (int sampleRateInHz)
Since: API Level 3
Sets the playback sample rate for this track. This sets the sampling rate at which the audio data will be consumed and played back, not the original sampling rate of the content. Setting it to half the sample rate of the content will cause the playback to last twice as long, but will also result in a negative pitch shift. The valid sample rate range if from 1Hz to twice the value returned by getNativeOutputSampleRate(int).
ParameterssampleRateInHz the sample rate expressed in Hz
So I wonder if you could set that for 1200 or 31250 or 9600 or whatever? Basic4Android seems to have some methods for bit manipulation so converting a byte/integer to bits and storing them in an array ought to be possible.
The idea I have is to send data out of the audio socket and emulate the waveforms of a serial data stream.
But remember that the audio output is likely to be band-limited to between 50Hz and15KHz or thereabouts.The hardware would be able to run to hundreds of kilohertz.
That red-eye device looks interesting and shows that this is possible to do.
I got some of the code working last night. For the hardware, I am thinking a 1uF blocking capacitor, two 10ks in series to bias the volts to half the rail, then run that into an op amp comparator. Bias the other input of the comparator just below half the rail so the output is low when there is no audio. Then use another op amp so there is normal and inverted output. Also a max232 so there is normal, inverted and RS232 as the output. And maybe a transistor so the output is also midi compatible. That should cover a number of options. The hardware would be able to run to hundreds of kilohertz.
That is the theory anyway!
I got some of the code working last night. For the hardware, I am thinking a 1uF blocking capacitor, two 10ks in series to bias the volts to half the rail, then run that into an op amp comparator. Bias the other input of the comparator just below half the rail so the output is low when there is no audio. Then use another op amp so there is normal and inverted output. Also a max232 so there is normal, inverted and RS232 as the output. And maybe a transistor so the output is also midi compatible. That should cover a number of options. The hardware would be able to run to hundreds of kilohertz.
That is the theory anyway!
I wonder if it is possible to program a chip like the propeller to emulate an sd card?
'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
'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)
' see C:\Program Files\Android\android-sdk-windows\platform-tools
' run the small batch file adb connect 192.168.2.6 where the IP address is the device address
' to get the IP address, Wifi settings, 3rd one down just under WiFi networks is the name of the network connected to
' Click this and it gives the signal strength and also the IP address
Dim i As Int
Dim j As Int
Dim strings As String
'Msgbox("Play a wav file", "First program")
'MediaPlayer1.Initialize( )
'MediaPlayer1.Load("/sdcard", "1200.wav")
CreateWavHeader ' create the header
addbytetowav(32) ' space
addbytetowav(13) ' carriage return
addbytetowav(10) ' linefeed
addbytetowav(72) ' H
addbytetowav(101) ' e
addbytetowav(108) ' l
addbytetowav(108) ' l
addbytetowav(111) ' 0
addbytetowav(32) ' space
addbytetowav(87) ' W
addbytetowav(111) ' o
addbytetowav(114) ' r
addbytetowav(108) ' l
addbytetowav(100) ' d
addbytetowav(13) ' CR
addbytetowav(10) ' LF
'For i=33 To 126
' addbytetowav(i) ' add ascii characters
'Next
CreateWavFooter
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 ' see bottom corner of this program "Libraries" tab and check 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 CreateWavFooter
' fill with zero (RS232 ends on 7FFF which is negative. So need to bring bias back to zero
Dim i As Int
For i=audiofilecounter To 88199
audiofile(i)=0 ' change to zero
Next
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.
' ** addit - all the above is WRONG and it turns out this is a 2400 baud output ***
' 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
' translate this to 0/5 "true" logic levels where resting is 0V and the bits are inverted
' resting is 0V. Start bit is 5V. High is 0V. Low is 5V. Stop bit is 0V
' or "space", mark or space x8, "mark"
' "mark" is 7FFF, "space" Is 8000
' 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=0x80
lsb=0x00
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=0x80 ' hex 8000 = space
lsb=0x00
Else
msb=0x7F ' hex 7FFF = mark
lsb=0xFF
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=0x80 ' hex 8000
lsb=0x00
Else
msb=0x7F ' hex 7FFF
lsb=0xFF
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=0x80 ' hex 8000
lsb=0x00
Else
msb=0x7F ' hex 7FFF
lsb=0xFF
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=0x80 ' hex 8000
lsb=0x00
Else
msb=0x7F ' hex 7FFF
lsb=0xFF
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=0x80 ' hex 8000
lsb=0x00
Else
msb=0x7F ' hex 7FFF
lsb=0xFF
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=0x80 ' hex 8000
lsb=0x00
Else
msb=0x7F ' hex 7FFF
lsb=0xFF
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=0x80 ' hex 8000
lsb=0x00
Else
msb=0x7F ' hex 7FFF
lsb=0xFF
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=0x80 ' hex 8000
lsb=0x00
Else
msb=0x7F ' hex 7FFF
lsb=0xFF
End If
For i=147 To 164
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
' stop bit = "mark"
msb=0x7F
lsb=0xFF
For i=165 To 183
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
' idle bit = mark and leave in this state
msb=0x7F
lsb=0xFF
For i=184 To 202 ' time to resynch
audiofile(audiofilecounter)=lsb ' LSB
audiofilecounter=audiofilecounter+1
audiofile(audiofilecounter)=msb ' MSB
audiofilecounter=audiofilecounter+1
Next
End Sub
The JetPlayer is a midi player. It doesn't support it.Is it possible with the new audio library to generate a sound (such as: Sound (frequency, duration))?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?