Swiftpack.co - thara/SoundIO as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by thara.
thara/SoundIO v0.3.3
A Swift wrapper for libsoundio.
⭐️ 4
🕓 4 years ago
.package(url: "https://github.com/thara/SoundIO.git", from: "v0.3.3")

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

var secondsOffset: Float = 0.0

func main() throws {
    let soundio = try SoundIO()
    try soundio.connect()
    soundio.flushEvents()

    let outputDeviceIndex = try soundio.defaultOutputDeviceIndex()
    let device = try soundio.getOutputDevice(at: outputDeviceIndex)
    print("Output device: \(device.name)")

    let outstream = try OutStream(to: device)
    outstream.format = .float32bitLittleEndian
    outstream.writeCallback { (outstream, frameCountMin, frameCountMax) in
        let layout = outstream.layout
        let secondsPerFrame = 1.0 / Float(outstream.sampleRate)

        var framesLeft = frameCountMax

        while 0 < framesLeft {
            var frameCount = framesLeft
            let areas = try! outstream.beginWriting(theNumberOf: &frameCount)

            if frameCount == 0 {
                break
            }

            let pitch: Float = 440.0
            let radiansPerSecond = pitch * 2.0 * .pi
            for frame in 0..<frameCount {
                let sample = sin((secondsOffset + Float(frame) * secondsPerFrame) * radiansPerSecond)
                for area in 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()

    while true {
        soundio.waitEvents()
    }
}

do {
    try main()
} catch let 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.

GitHub

link
Stars: 4
Last commit: 4 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Related Packages

Release Notes

4 years ago

Added

  • Support underflow callbacks in OutStream
  • Support soundio_instream_destroy
  • Support soundio_instream_create
  • Support soundio_instream_open
  • Support soundio_instream_start
  • Support soundio_instream_begin_write
  • Support soundio_instream_end_write
  • Support soundio_ring_buffer_create
  • Support soundio_ring_buffer_destroy
  • Support soundio_ring_buffer_capacity
  • Support soundio_ring_buffer_free_count
  • Support soundio_ring_buffer_fill_count
  • Support soundio_ring_buffer_write_ptr
  • Support soundio_ring_buffer_read_ptr
  • Support soundio_ring_buffer_advance_write_ptr
  • Support soundio_ring_buffer_advance_read_ptr

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics