Platforms to show: All Mac Windows Linux Cross-Platform
/AVFoundation/AVAudioEngine/Show Samples
Required plugins for this example: MBS MacBase Plugin, MBS Main Plugin, MBS Picture Plugin, MBS AVFoundation Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /AVFoundation/AVAudioEngine/Show Samples
This example is the version from Mon, 20th Sep 2015.
Project "Show Samples.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control Listbox1 Inherits Listbox
ControlInstance Listbox1 Inherits Listbox
End Control
Control Canvas1 Inherits Canvas
ControlInstance Canvas1 Inherits Canvas
End Control
Control Canvas2 Inherits Canvas
ControlInstance Canvas2 Inherits Canvas
End Control
Control Canvas3 Inherits Canvas
ControlInstance Canvas3 Inherits Canvas
End Control
Control Canvas4 Inherits Canvas
ControlInstance Canvas4 Inherits Canvas
End Control
Control Canvas5 Inherits Canvas
ControlInstance Canvas5 Inherits Canvas
End Control
Control Canvas6 Inherits Canvas
ControlInstance Canvas6 Inherits Canvas
End Control
Control Canvas7 Inherits Canvas
ControlInstance Canvas7 Inherits Canvas
End Control
Control Canvas8 Inherits Canvas
ControlInstance Canvas8 Inherits Canvas
End Control
EventHandler Sub Open()
dim f as FolderItem = GetOpenFolderItem("")
if f = nil then Return
listbox1.AddRow "File", f.DisplayName
// Needs OS X 10.10 or newer
dim error as NSErrorMBS
dim a as new AVAudioFileMBS(f, error)
if error <> nil then
listbox1.AddRow "Open Error", error.LocalizedDescription
MsgBox error.LocalizedDescription
return
end if
dim ff as AVAudioFormatMBS = a.fileFormat
listbox1.AddRow "FileFormat SampleRate", format(ff.sampleRate, "0")+"Hz"
listbox1.AddRow "FileFormat Channels", format(ff.channelCount, "0")
if ff.Interleaved then
listbox1.AddRow "FileFormat Interleaved", "yes"
else
listbox1.AddRow "FileFormat Interleaved", "no"
end if
dim duration as Double = a.Length/a.fileFormat.sampleRate
listbox1.AddRow "Audio Duration", format(duration, "0.0")+" seconds"
listbox1.AddRow "Audio Length", format(a.Length, "0")+" frames"
dim pf as AVAudioFormatMBS = a.processingFormat
listbox1.AddRow "ProcessingFormat SampleRate", format(pf.sampleRate, "0")+"Hz"
listbox1.AddRow "ProcessingFormat Channels", format(pf.channelCount, "0")
if pf.Interleaved then
listbox1.AddRow "ProcessingFormat Interleaved", "yes"
else
listbox1.AddRow "ProcessingFormat Interleaved", "no"
end if
// new buffer to receive data
dim buf as new AVAudioPCMBufferMBS(a.processingFormat, a.Length)
// request data
if a.readIntoBuffer(buf, error) then
listbox1.AddRow "Read samples", str(buf.frameLength)+" frames"
canvas1.Backdrop = RenderChannel(pf, buf, 0)
canvas2.Backdrop = RenderChannel(pf, buf, 1)
canvas3.Backdrop = RenderChannel(pf, buf, 2)
canvas4.Backdrop = RenderChannel(pf, buf, 3)
canvas5.Backdrop = RenderChannel(pf, buf, 4)
canvas6.Backdrop = RenderChannel(pf, buf, 5)
canvas7.Backdrop = RenderChannel(pf, buf, 6)
canvas8.Backdrop = RenderChannel(pf, buf, 7)
// show only as many as we have
dim n as integer = pf.channelCount
if n < 8 then
n = 8 - n
me.Height = me.Height - n * 102
end if
else
listbox1.AddRow "Read Error", error.LocalizedDescription
end if
End EventHandler
Function RenderChannel(pf as AVAudioFormatMBS, buf as AVAudioPCMBufferMBS, Channel as integer) As Picture
// only one array will have data
if Channel >= pf.channelCount then Return nil // not available
dim memFloat as MemoryBlock = buf.floatChannelDataCopy(Channel)
if memFloat <> nil then
// RenderSamplesMBS in Picture plugin
return RenderSamplesMBS(memFloat, memFloat.Size / 4, 2, canvas1.Width, canvas1.Height, 1, &c88B5C4, &c274C5A, &c203F4E, -32)
end if
dim memInt16 as MemoryBlock = buf.int16ChannelDataCopy(Channel)
if memInt16 <> nil then
return RenderSamplesMBS(memInt16, memInt16.Size / 2, 2, canvas1.Width, canvas1.Height, 1, &c88B5C4, &c274C5A, &c203F4E, 15)
end if
Break // other format?
End Function
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
End Project
The items on this page are in the following plugins: MBS AVFoundation Plugin.