Android Question Generate sound frequency

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yes. You can use AudioStreamer from the Audio library:
B4X:
Sub Process_Globals
   Dim streamer As AudioStreamer
   Dim timer1 As Timer
   Dim freq As Int = 500
End Sub

Sub Globals
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     streamer.Initialize("streamer", 11025, True, 16, streamer.VOLUME_MUSIC)
     streamer.StartPlaying
     timer1.Initialize("timer1", 1000)
   End If
   timer1.Enabled = True
End Sub

Sub Timer1_Tick
   Log(freq)
   Beep(1000, freq)
   freq = freq + 500
End Sub

Sub Beep(Duration As Int, Frequency As Int)
   Dim dur As Double = Duration / 10000
   Dim sampleRate As Int = 11025
   Dim numSamples As Int = sampleRate * dur
   Dim snd(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
     snd(i * 2) = Bit.AND(0xff, val)
     snd(i * 2 + 1) = Bit.ShiftRight(Bit.AND(0xff00, val), 8)
   Next
   streamer.Write(snd)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   timer1.Enabled = False
End Sub
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
Wow! Thank you! it's possible to record and save the generated sound in real time?
or better store the informations somewhere like an array so the sound can be reproduced in any device which has the same library (app)?
 
Upvote 0

giacomo-italy

Member
Licensed User
Longtime User
Hi,
I would like to know if there is a way to change the frequency (and volume) of the sound while the sound is generated? (To get an effect like theremin).
In the example I can change the frequency only after it has passed the duration of the sound, so that the frequency can not change continuously ..
thank you.
 
Upvote 0

giacomo-italy

Member
Licensed User
Longtime User
Change Frequency before each Duration.

thanks for the reply,
but is exactly what I wrote that i can do but i do not want to do:
I need that frequency and amplitude can be changed while the sound is generated.

I tried to reduce the duration of the sound and insert the frequency change in a loop of the same duration,
but the resulting sound is choppy and full of disturbs as 'click' and noise...

There is an alternative way to change frequency and amplitude while the sound is generated?
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
In this case, you may need two passes.. one to generate, record and concatenate the frequencies, and the other to play or save it.
 
Upvote 0
Top