Skip to content

Repository files navigation

SoundIO

Github Status

A Swift wrapper for libsoundio, a cross-platform real-time audio input and output library.

Requirements

  • libsoundio

Installation

dependencies:[.package(url:"https://github.com/thara/SoundIO.git", from:"0.3.2"),]

Example

The following emits a sine wave over the default device using the best backend.
It means as same as libsoundio's synopsis.

import Foundation
import SoundIO
varsecondsOffset:Float=0.0func main()throws{letsoundio=trySoundIO()try soundio.connect()
soundio.flushEvents()letoutputDeviceIndex=try soundio.defaultOutputDeviceIndex()letdevice=try soundio.getOutputDevice(at: outputDeviceIndex)print("Output device: \(device.name)")letoutstream=tryOutStream(to: device)
outstream.format =.float32bitLittleEndian
outstream.writeCallback{(outstream, frameCountMin, frameCountMax)inletlayout= outstream.layout
letsecondsPerFrame=1.0/ Float(outstream.sampleRate)varframesLeft= frameCountMax
while0< framesLeft {varframeCount= framesLeft
letareas=try! outstream.beginWriting(theNumberOf:&frameCount)if frameCount ==0{break}letpitch:Float=440.0letradiansPerSecond= pitch *2.0*.pi
forframein0..<frameCount {letsample=sin((secondsOffset + Float(frame)* secondsPerFrame)* radiansPerSecond)forareain areas!.iterate(over: layout.channelCount){
area.write(sample, stepBy: frame)}}
secondsOffset =(secondsOffset + secondsPerFrame * Float(frameCount)).truncatingRemainder(dividingBy:1)try! outstream.endWrite()
framesLeft -= frameCountMax
}}try outstream.open()try outstream.start()whiletrue{
soundio.waitEvents()}}do{trymain()}catchlet error as SoundIOError{print("Error: \(error)")exit(EXIT_FAILURE)}

This code is located in Sources/SoundIODemo/main.swift. You can run it by using make demo.

Notes

You haven't need to call any **_destroy functions because the wrapper calls each of **_destroy functions in deinit and destroyed out of scopes properly.

TODO

  • Add documentation
  • Support error callbacks in OutStream
  • Add examples
  • Test other platforms except macOS

Author

Tomochika Hara - thara

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

  • libsoundio
  • rsoundio
    • This has gaven me an idea to keep callback functions in stream's userdata.

About

A Swift wrapper for libsoundio.

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages