Android Question Audio Library - Beeper - Duration

Horst Leistner

Member
Licensed User
Hello,

I want to create short pulses of 19kHz and with a duration shorter than 1ms.
Currently I do that by making a wav-file, save it and play it by MP3.
Is there a possibility to modify the Beeper function to generate pulses shorter than 1ms?
Maybe it would be helpfull for me to get the source code of the Audio Library.

Best regards
Horst
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should use AudioStreamer for that.

Put this code in the Starter service:
B4X:
Sub Process_Globals
   Private streamer As AudioStreamer
End Sub

Sub Service_Create
   streamer.Initialize("streamer", 8000, True, 16, streamer.VOLUME_MUSIC)
   streamer.StartPlaying
End Sub

Public Sub Beep (DurationMs As Double, Frequency As Int)
   Dim sampleRate As Int = 8000
   Dim numSamples As Int = sampleRate * DurationMs / 1000
   Dim gsnd(2 * numSamples) As Byte
   For i = 0 To numSamples - 1
     Dim d As Double = Sin(2 * cPI * i / (sampleRate / Frequency))
     Dim val As Short = d * 32767
     gsnd(2 * i) = Bit.And(val, 0x00ff)
     gsnd(2 * i + 1) = Bit.UnsignedShiftRight(Bit.And(val, 0xff00), 8)
   Next
   streamer.Write(gsnd)
End Sub

You can then call it with:
B4X:
CallSub3(Starter, "Beep", 1000, 400)
 
Upvote 0

Horst Leistner

Member
Licensed User
Thank you very much, Erel. Each time I use it I am exited with this development tool! Due to my Seagrave book I am sure to understand your piece of code within the next hours.
Regards
Horst.
 
Upvote 0
Top