A repository with a "Swift Audio Workstation" example built with Swift for WebAssembly running in the browser.
This example demonstrates support for WebAssembly in latest development snapshots of the Swift toolchain, in combination with the Embedded Swift mode. With foundational building blocks written in Swift, it utilizes C++ interop for calling into a DSP library for synthesizing simple musical sequences. It is written with a multi-platform approach, which makes it easy to integrate into Wasm-based serverless environment or native applications and libraries.
The repository is split into three packages: Guest
with Wasm modules built with Embedded Swift, ServerHost
that embeds these modules, and WATExample
that demonstrates compilation of WebAssembly Text Format to binary Wasm modules using Swift.
WebAssembly support in Swift is available for preview in latest Trunk Development (main) snapshots at swift.org/download.
- Install Xcode.
- Verify selected Xcode path by running
xcode-select -p
in the terminal. If the incorrect Xcode is selected, follow the steps provided in "How do I select the default version of Xcode" section of "Building from the Command Line with Xcode FAQ". - Download latest
main
development snapshot, you can useDEVELOPMENT-SNAPSHOT-2024-04-01-a
or a later version. - Run the downloaded installer:
installer -target CurrentUserHomeDirectory -pkg ~/Downloads/swift-DEVELOPMENT-SNAPSHOT-2024-04-01-a-osx.pkg
- Select the newly installed snapshot:
export TOOLCHAINS=$(plutil -extract CFBundleIdentifier raw \
~/Library/Developer/Toolchains/swift-latest.xctoolchain/Info.plist)
Follow Linux-specific instructions provided on swift.org/install to install the latest development toolchain for your specific distribution.
- Start a docker container in a clone of this repository using the nightly swiftlang Ubuntu image, with a
/root/build
mount to the current directory:
docker run --rm -it -v $(pwd):/root/build swiftlang/swift:nightly-jammy /bin/bash
- Navigate to the package directory within the container:
cd /root/build
Assuming you're within the cloned repository and have the latest development snapshots selected per the instructions
above, build modules from the Guest
package (this will copy .wasm
modules to the home directory of the current user):
cd Guest; ./build.sh
Then build and start the HTTP server:
cd ../ServerHost; swift run Server
Open http://localhost:8080 in your browser to see the project running. Use the web interface to upload previously built
Guest
modules from the home directory.
Contributions to Swift are welcomed and encouraged! Please see the Contributing to Swift guide.
Before submitting the pull request, please make sure you have tested your changes and that they follow the Swift project guidelines for contributing code.
To be a truly great community, Swift.org needs to welcome developers from all walks of life, with different backgrounds, and with a wide range of experience. A diverse and friendly community will have more great ideas, more unique perspectives, and produce more great code. We will work diligently to make the Swift community welcoming to everyone.
To give clarity of what is expected of our members, Swift has adopted the code of conduct defined by the Contributor Covenant. This document is used across many open source communities, and we think it articulates our values well. For more, see the Code of Conduct.
See https://swift.org/LICENSE.txt for license information.
See LICENSE-vendored.md
for exact licenses of code vendored in this repository.