#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
#BridgeLogger:true
#ApplicationLabel: Ringbuffer TEMP
Sub Process_Globals
Type SoundTyp(Sample() As Short )
Type MusicEventTyp(SoundNr, Intervall, NextTime As Int, Volume As Float)
Private Converter As ByteConverter
Public PlayTimer, AudioTimer As Timer
Private StartTime As Long
Private SoundTime As Int
Private Music As List 'MusicEventTyp
Private Sound(7) As SoundTyp
End Sub
Sub Globals
Private Button1 As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
AudioRingBuffer.FirstStart(20, 8, 24000) ' feed audio every 20msec, 8sec ring-buffer, 24kHz sampling rate
AudioTimer.Initialize("AudioTimer",100)
PlayTimer.Initialize("PlayTimer",50)
StartTime=DateTime.Now
DefineMusic
End Sub
Sub Activity_Resume
AudioRingBuffer.ReStartRingBuffer
PlayTimer.Enabled=True
AudioTimer.Enabled=True
End Sub
Sub Button1_Click
PlayTimer.Enabled=False
AudioTimer.Enabled=False
ExitApplication
End Sub
Sub Activity_Pause (UserClosed As Boolean)
PlayTimer.Enabled=False
AudioTimer.Enabled=False
End Sub
Sub AudioTimer_Tick
AudioRingBuffer.SendAudio
End Sub
Sub LoadAudioSample(FileName As String) As Short()
Dim WAV As RandomAccessFile
File.Copy(File.DirAssets,FileName,File.DirInternal,FileName)
Dim Size As Int= File.Size(File.DirInternal, FileName)-44
Dim ByteBuffer(Size) As Byte
WAV.Initialize(File.DirInternal, FileName,True)
WAV.ReadBytes(ByteBuffer,0,Size,44)
WAV.Close
Converter.LittleEndian=True
Dim ShortBuffer() As Short=Converter.ShortsFromBytes(ByteBuffer)
Return ShortBuffer
End Sub
Sub PlayTimer_Tick
Dim now As Long = DateTime.Now-StartTime ' latency
For Each loc As MusicEventTyp In Music
If loc.NextTime<now+1000 Then
AudioRingBuffer.JoinSample(Sound(loc.SoundNr).Sample, loc.NextTime, loc.volume )
loc.NextTime= loc.NextTime + loc.Intervall
End If
Next
End Sub
Sub DefineMusic
Sound(0).sample=LoadAudioSample("Piano0.wav")
Sound(1).sample=LoadAudioSample("Piano4.wav")
Sound(2).sample=LoadAudioSample("Piano7.wav")
Sound(3).sample=LoadAudioSample("BassDrum.wav")
Sound(4).sample=LoadAudioSample("SnareDrum.wav")
Sound(5).sample=LoadAudioSample("Applaus.wav")
Sound(6).sample=LoadAudioSample("Piano-7.wav")
Music.Initialize
'piano
Init(0,3000,2000,0.9)
Init(6,4000,2000,0.9)
Init(1,3500,1000,0.5)
Init(2,3500,1000,0.6)
Init(1,3833,1000,0.4)
Init(2,3833,1000,0.3)
'drums
Init(3,1000,1000,0.9)
Init(4,1500,1000,0.6)
Init(3,1833,2000,0.6)
Init(4,1830,2000,0.3)
'noise
Init(5,7000,3210,0.4)
End Sub
Sub Init(Nr As Int, Start As Int, Interval As Int, Volume As Float) As MusicEventTyp
Dim loc As MusicEventTyp
loc.Initialize
loc.SoundNr=Nr
loc.NextTime=Start
loc.Intervall=Interval
loc.volume=Volume
Music.Add(loc)
End Sub