/swift-for-wasm-examples

Primary LanguageSwiftApache License 2.0Apache-2.0

Swift for WebAssembly Examples

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.

Requirements

WebAssembly support in Swift is available for preview in latest Trunk Development (main) snapshots at swift.org/download.

macOS

  1. Install Xcode.
  2. 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".
  3. Download latest main development snapshot, you can use DEVELOPMENT-SNAPSHOT-2024-04-01-a or a later version.
  4. Run the downloaded installer. Select "Install for me only" option during installation.
  5. Select the newly installed snapshot in terminal:
export TOOLCHAINS=$(plutil -extract CFBundleIdentifier raw \
  ~/Library/Developer/Toolchains/swift-latest.xctoolchain/Info.plist)

Linux

Follow Linux-specific instructions provided on swift.org/install to install the latest development toolchain for your specific distribution.

Docker

  1. 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
  1. Navigate to the package directory within the container:
cd /root/build

How to Build and Run

Assuming you're within the cloned repository and have the latest development snapshots selected per the instructions above, first build the package:

swift build --triple wasm32-unknown-none-wasm -c release --product swift-audio

Then start the HTTP server:

python3 -m http.server

Open http://localhost:8000 in your browser to see the project running.

License

Copyright 2024 Apple Inc. and the Swift project authors. Licensed under Apache License v2.0 with Runtime Library Exception.

See https://swift.org/LICENSE.txt for license information.

See https://swift.org/CONTRIBUTORS.txt for Swift project authors.

See LICENSE-vendored.md for exact licenses of code vendored in this repository. Specifically:

wasi-libc as a whole is multi-licensed under the Apache License v2.0 with LLVM Exceptions, the Apache License v2.0, and the MIT License. See the LICENSE-APACHE-LLVM, LICENSE-APACHE and LICENSE-MIT files, respectively, for details.

Portions of this software are derived from third-party works covered by their own licenses:

dlmalloc/ - CC0; see the notice in malloc.c for details emmalloc/ - MIT; see the notice in emmalloc.c for details libc-bottom-half/cloudlibc/ - BSD-2-Clause; see the LICENSE file for details libc-top-half/musl/ - MIT; see the COPYRIGHT file for details

wasi-libc's changes to these files are multi-licensed under the Apache License v2.0 with LLVM Exceptions, the Apache License v2.0, the MIT License, and the original licenses of the third-party works.

The MIT License (MIT)

Copyright (c) 2015 Yuji Miyane

MIT License

Copyright (c) 2017 Leonardo Laguna Ruiz