Platforms to show: All Mac Windows Linux Cross-Platform
/MacOSX/CoreAudio/CoreAudio Player
Required plugins for this example: MBS MacOSX Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacOSX/CoreAudio/CoreAudio Player
This example is the version from Sun, 17th Mar 2012.
Project "CoreAudio Player.xojo_binary_project"
Class Window1 Inherits Window
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
dim timeIndex,totaltime,n,p as integer
dim rate as Single
dim frequency as single
dim audioBuffer as MemoryBlock
dim c as CoreAudioPlayerMBS
dim b as Boolean
dim t,lastp as integer
c=new CoreAudioPlayerMBS
timeIndex = 0
rate = 8000.0
totalTime = rate * 10 // 10 seconds
frequency = 440.0
const BUFSIZE=4000
audioBuffer=NewMemoryBlock(BUFSIZE*4)
c.NoUnderflow=true
c.SampleRate=rate
c.NumberOfChannels=1
while (timeIndex < totalTime)
// Output sound until our ten seconds are up
p=300.0*c.OutputPosition/totaltime
if p<>lastp then
lastp=p
ProgressBar1.Value=p
ProgressBar1.Refresh
end if
if (timeIndex < totalTime) and (c.FreeSpace>0) then
p=0
for n = 1 to BUFSIZE
audioBuffer.SingleValue(p) = 0.25 * sin(timeIndex * 2.0 * 3.1415926 * frequency / rate)
p=p+4
timeIndex=timeIndex+1
next
if not c.PlayFloatAudio(audioBuffer) then
MsgBox "Failed to play audio!"
Return
end if
end if
wend
t=ticks
while abs(ticks-t)<600
app.DoEvents
wend
timeIndex = 0
rate = 8000.0
totalTime = rate * 10 // 10 seconds
frequency = 440.0
audioBuffer=NewMemoryBlock(BUFSIZE*4)
c.SampleRate=rate
c.NumberOfChannels=1
while (timeIndex < totalTime) or c.isRunning
// Output sound until our ten seconds are up
p=300.0*c.OutputPosition/totaltime
if p<>lastp then
lastp=p
ProgressBar1.Value=p
ProgressBar1.Refresh
end if
if (timeIndex < totalTime) and (c.FreeSpace>0) then
p=0
for n = 1 to BUFSIZE
audioBuffer.SingleValue(p) = 0.25 * sin(timeIndex * 2.0 * 3.1415926 * frequency / rate)
p=p+4
timeIndex=timeIndex+1
next
if not c.PlayFloatAudio(audioBuffer) then
MsgBox "Failed to play audio!"
Return
end if
end if
wend
c.NoUnderflow=False
while c.IsRunning
app.DoEvents
wend
End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action()
PlayWithTimer
End EventHandler
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action()
PlayNextBytes
End EventHandler
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action()
dim timeIndex,totaltime,n,p as integer
dim rate as Single
dim f as single
dim audioBuffer as MemoryBlock
'dim c as CoreAudioPlayerMBS // local and it stops too early
dim b as Boolean
c=new myCoreAudioPlayer
c.NumberOfChannels=2
timeIndex = 0
rate = 8000.0
totalTime = rate * 20 // 20 seconds
const Fleft = 440.0
const Fright = 640.0
const BUFSIZE=8000
audioBuffer=NewMemoryBlock(BUFSIZE*8)
c.SampleRate=rate
while timeIndex < totalTime
// Output sound until our ten seconds are up
if c.FreeSpace>0 then
p=0
for n = 1 to BUFSIZE
'left
audioBuffer.SingleValue(p) = 0.25 * sin( (timeIndex) * 2.0 * 3.1415926 * Fleft / rate)
p=p+4
'right
audioBuffer.SingleValue(p) = 0.25 * cos( (timeIndex) * 2.0 * 3.1415926 * Fright / rate)
p=p+4
timeIndex=timeIndex+1
next
if not c.PlayFloatAudio(audioBuffer) then
MsgBox "Failed to play audio!"
Return
end if
end if
wend
End EventHandler
End Control
Control ProgressBar1 Inherits ProgressBar
ControlInstance ProgressBar1 Inherits ProgressBar
End Control
Sub PlayNextBytes()
if c<>nil then
c.ContinuePlaying
else
Timer1.Mode=0
end if
End Sub
Sub PlayWithTimer()
c=new myCoreAudioPlayer
c.Start
Timer1.Mode=2
End Sub
Property Protected c As myCoreAudioPlayer
End Class
MenuBar Menu
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class myCoreAudioPlayer Inherits CoreAudioPlayerMBS
Sub ContinuePlaying()
const BUFSIZE = 8000
dim p,n as integer
dim m as MemoryBlock
if timeIndex<totalTime then
if FreeSpace>0 then
p=0
m=NewMemoryBlock(BUFSIZE*4)
for n=1 to BUFSIZE
m.Long(p) = 65536.0 * 32767.0 * 0.25 * sin((timeIndex) * 2.0 * 3.1415926 * frequency / SampleRate)
timeIndex=timeIndex+1
p=p+4
next
if not me.PlayAudio(m,0,m.Size,31) then
MsgBox "Failed to add audio!"
end if
end if
end if
End Sub
Sub Start()
timeIndex=0 // how much is already played
SampleRate = 8000.0
NumberOfChannels=1
totalTime = SampleRate * 10.0 // 10 seconds
frequency = 920.0
End Sub
Property Protected frequency As single
Property Protected timeIndex As integer
Property Protected totalTime As Integer
End Class
Class App Inherits Application
End Class
End Project
See also:
The items on this page are in the following plugins: MBS MacOSX Plugin.