From 86310cfddbff7091f143c10f642109d9834854d6 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 12 Mar 2024 18:34:02 +0000 Subject: [PATCH 1/6] Add `README.md` --- README.md | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ce71f63..fde2508 100644 --- a/README.md +++ b/README.md @@ -1 +1,88 @@ -# swift-for-wasm-examples \ No newline at end of file +# Swift for WebAssembly Examples + +A repository with a "Swift Audio Workstation" example built with Swift for WebAssembly running in the browser. + +## Requirements + +WebAssembly support in Swift is available for preview in latest Trunk Development (main) snapshots at [swift.org/download](https://www.swift.org/download). + +### macOS + +1. Make sure you have Xcode installed. +2. Verify selected Xcode path by running `xcode-select -p` in the terminal. If that's not the case, follow the steps provided in ["How do I select the default version of Xcode"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html#//apple_ref/doc/uid/DTS40014588-CH1-HOW_DO_I_SELECT_THE_DEFAULT_VERSION_OF_XCODE_TO_USE_FOR_MY_COMMAND_LINE_TOOLS_) section of ["Building from the Command Line with Xcode FAQ"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html). +3. Download latest development snapshot, for example [`DEVELOPMENT-SNAPSHOT-2024-03-07-a`](https://download.swift.org/development/xcode/swift-DEVELOPMENT-SNAPSHOT-2024-03-07-a/swift-DEVELOPMENT-SNAPSHOT-2024-03-07-a-osx.pkg) or later. +4. Run the downloaded installer. Select "Install for me only" option during installation. +5. Select the newly installed snapshot in terminal: + +```sh +export TOOLCHAINS=$(plutil -extract CFBundleIdentifier raw \ + ~/Library/Developer/Toolchains/swift-latest.xctoolchain/Info.plist) +``` +6. Install `wasm-ld` linker with [Homebrew](https://brew.sh/): +```sh +brew install llvm +``` + +### Docker + +1. Run this command in a clone of this repository: + +```sh +docker run --rm -it -v $(pwd):/root/build swiftlang/swift:nightly-jammy /bin/bash +``` + +This will mount the clone directory as a volume in the Docker container and also make final build products available to your host system. + +2. Navigate to the package directory within the container: + +```sh +cd /root/build +``` + +## How to Build and Run + +Assuming you have the latest development snapshots selected per the instructions above, build the package with this command: + +```sh +swift build --triple wasm32-unknown-none-wasm -c release --product swift-audio +``` + +Start an HTTP server in the cloned directory of the package: + +``` +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](https://swift.org/LICENSE.txt) for license information. + +See [https://swift.org/CONTRIBUTORS.txt](https://swift.org/CONTRIBUTORS.txt) for Swift project authors. + +See [`LICENSE-vendored.md`](https://github.com/apple/swift-for-wasm-examples/blob/main/LICENSE-vendored.md) for exact licenses of code vendored in this repository. Specifically: + +1. Code in `Sources/dlmalloc` directory is derived from wasi-libc: https://github.com/WebAssembly/wasi-libc + +> 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. + +2. .wav format encoding implementation is derived from WavAudioEncoder.js library https://github.com/higuma/wav-audio-encoder-js and is licensed as following: + +> The MIT License (MIT) +> +> Copyright (c) 2015 Yuji Miyane + +3. Code in `Sources/VultDSP` directory is derived from https://github.com/vult-dsp/vult and is licensed as following: + +> MIT License +> +> Copyright (c) 2017 Leonardo Laguna Ruiz From 55f848a23a4b43d858e1efa82a36760600aa1862 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 12 Mar 2024 18:37:50 +0000 Subject: [PATCH 2/6] Enable highlighting missing in a code snippet in `README.md` --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fde2508..d8a6fe2 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ swift build --triple wasm32-unknown-none-wasm -c release --product swift-audio Start an HTTP server in the cloned directory of the package: -``` +```sh python3 -m http.server ``` From ff11e3ee6dcd8864e1a3bd66f0eae5edb5d9f02a Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 12 Mar 2024 21:00:47 +0000 Subject: [PATCH 3/6] Apply suggestions from code review Co-authored-by: Ben Barham --- README.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d8a6fe2..465066d 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ WebAssembly support in Swift is available for preview in latest Trunk Developmen ### macOS -1. Make sure you have Xcode installed. -2. Verify selected Xcode path by running `xcode-select -p` in the terminal. If that's not the case, follow the steps provided in ["How do I select the default version of Xcode"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html#//apple_ref/doc/uid/DTS40014588-CH1-HOW_DO_I_SELECT_THE_DEFAULT_VERSION_OF_XCODE_TO_USE_FOR_MY_COMMAND_LINE_TOOLS_) section of ["Building from the Command Line with Xcode FAQ"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html). +1. Install [Xcode](https://apps.apple.com/us/app/xcode/id497799835?mt=12). +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"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html#//apple_ref/doc/uid/DTS40014588-CH1-HOW_DO_I_SELECT_THE_DEFAULT_VERSION_OF_XCODE_TO_USE_FOR_MY_COMMAND_LINE_TOOLS_) section of ["Building from the Command Line with Xcode FAQ"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html). 3. Download latest development snapshot, for example [`DEVELOPMENT-SNAPSHOT-2024-03-07-a`](https://download.swift.org/development/xcode/swift-DEVELOPMENT-SNAPSHOT-2024-03-07-a/swift-DEVELOPMENT-SNAPSHOT-2024-03-07-a-osx.pkg) or later. 4. Run the downloaded installer. Select "Install for me only" option during installation. 5. Select the newly installed snapshot in terminal: @@ -25,13 +25,12 @@ brew install llvm ### Docker -1. Run this command in a clone of this repository: +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: ```sh docker run --rm -it -v $(pwd):/root/build swiftlang/swift:nightly-jammy /bin/bash ``` -This will mount the clone directory as a volume in the Docker container and also make final build products available to your host system. 2. Navigate to the package directory within the container: @@ -41,13 +40,13 @@ cd /root/build ## How to Build and Run -Assuming you have the latest development snapshots selected per the instructions above, build the package with this command: +Assuming you're within the cloned repository and have the latest development snapshots selected per the instructions above, first build the package: ```sh swift build --triple wasm32-unknown-none-wasm -c release --product swift-audio ``` -Start an HTTP server in the cloned directory of the package: +Then start the HTTP server: ```sh python3 -m http.server From 254a7e460769f3d36deccb1bb9535a95b4ffee84 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 12 Mar 2024 21:03:11 +0000 Subject: [PATCH 4/6] Apply suggestions from code review --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 465066d..a88d068 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ See [https://swift.org/CONTRIBUTORS.txt](https://swift.org/CONTRIBUTORS.txt) for See [`LICENSE-vendored.md`](https://github.com/apple/swift-for-wasm-examples/blob/main/LICENSE-vendored.md) for exact licenses of code vendored in this repository. Specifically: -1. Code in `Sources/dlmalloc` directory is derived from wasi-libc: https://github.com/WebAssembly/wasi-libc +* Code in `Sources/dlmalloc` directory is derived from wasi-libc: https://github.com/WebAssembly/wasi-libc > 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. > @@ -74,13 +74,13 @@ See [`LICENSE-vendored.md`](https://github.com/apple/swift-for-wasm-examples/blo > > 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. -2. .wav format encoding implementation is derived from WavAudioEncoder.js library https://github.com/higuma/wav-audio-encoder-js and is licensed as following: +* .wav format encoding implementation is derived from WavAudioEncoder.js library https://github.com/higuma/wav-audio-encoder-js and is licensed as following: > The MIT License (MIT) > > Copyright (c) 2015 Yuji Miyane -3. Code in `Sources/VultDSP` directory is derived from https://github.com/vult-dsp/vult and is licensed as following: +* Code in `Sources/VultDSP` directory is derived from https://github.com/vult-dsp/vult and is licensed as following: > MIT License > From e468a04a5ec851b8d96dcee81007de2701704cfe Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 14 Mar 2024 00:17:33 +0100 Subject: [PATCH 5/6] Add a short description of example code and a link to Linux instructions --- README.md | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a88d068..f6cc6f0 100644 --- a/README.md +++ b/README.md @@ -2,17 +2,29 @@ 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](https://github.com/apple/swift/blob/main/docs/EmbeddedSwift/UserManual.md). +With foundational building blocks written in Swift, it utilizes C++ interop for calling into a +[DSP](https://en.wikipedia.org/wiki/Digital_signal_processing) 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](https://www.swift.org/download). +WebAssembly support in Swift is available for preview in latest Trunk Development (main) snapshots at +[swift.org/download](https://www.swift.org/download). ### macOS 1. Install [Xcode](https://apps.apple.com/us/app/xcode/id497799835?mt=12). -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"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html#//apple_ref/doc/uid/DTS40014588-CH1-HOW_DO_I_SELECT_THE_DEFAULT_VERSION_OF_XCODE_TO_USE_FOR_MY_COMMAND_LINE_TOOLS_) section of ["Building from the Command Line with Xcode FAQ"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html). -3. Download latest development snapshot, for example [`DEVELOPMENT-SNAPSHOT-2024-03-07-a`](https://download.swift.org/development/xcode/swift-DEVELOPMENT-SNAPSHOT-2024-03-07-a/swift-DEVELOPMENT-SNAPSHOT-2024-03-07-a-osx.pkg) or later. -4. Run the downloaded installer. Select "Install for me only" option during installation. -5. Select the newly installed snapshot in terminal: +2. Verify selected Xcode path by running `xcode-select -p` in the terminal. If the incorrect Xcode is selected, follow +3. the steps provided in ["How do I select the default version of Xcode"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html#//apple_ref/doc/uid/DTS40014588-CH1-HOW_DO_I_SELECT_THE_DEFAULT_VERSION_OF_XCODE_TO_USE_FOR_MY_COMMAND_LINE_TOOLS_) section of +4. ["Building from the Command Line with Xcode FAQ"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html). +5. Download latest development snapshot, for example +6. [`DEVELOPMENT-SNAPSHOT-2024-03-07-a`](https://download.swift.org/development/xcode/swift-DEVELOPMENT-SNAPSHOT-2024-03-07-a/swift-DEVELOPMENT-SNAPSHOT-2024-03-07-a-osx.pkg) +7. or later. +8. Run the downloaded installer. Select "Install for me only" option during installation. +9. Select the newly installed snapshot in terminal: ```sh export TOOLCHAINS=$(plutil -extract CFBundleIdentifier raw \ @@ -23,9 +35,15 @@ export TOOLCHAINS=$(plutil -extract CFBundleIdentifier raw \ brew install llvm ``` +### Linux + +Follow Linux-specific instructions provided on [swift.org/install](https://www.swift.org/install/#linux) 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: +1. Start a docker container in a clone of this repository using the nightly swiftlang Ubuntu image, with a `/root/build` +2. mount to the current directory: ```sh docker run --rm -it -v $(pwd):/root/build swiftlang/swift:nightly-jammy /bin/bash @@ -40,7 +58,8 @@ 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: +Assuming you're within the cloned repository and have the latest development snapshots selected per the instructions +above, first build the package: ```sh swift build --triple wasm32-unknown-none-wasm -c release --product swift-audio From 09f3bae073e7d0df81dc2eb9a3c4b8bf744840b8 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 14 Mar 2024 00:24:48 +0100 Subject: [PATCH 6/6] Fix number list --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f6cc6f0..8874f67 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,13 @@ WebAssembly support in Swift is available for preview in latest Trunk Developmen 1. Install [Xcode](https://apps.apple.com/us/app/xcode/id497799835?mt=12). 2. Verify selected Xcode path by running `xcode-select -p` in the terminal. If the incorrect Xcode is selected, follow -3. the steps provided in ["How do I select the default version of Xcode"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html#//apple_ref/doc/uid/DTS40014588-CH1-HOW_DO_I_SELECT_THE_DEFAULT_VERSION_OF_XCODE_TO_USE_FOR_MY_COMMAND_LINE_TOOLS_) section of -4. ["Building from the Command Line with Xcode FAQ"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html). -5. Download latest development snapshot, for example -6. [`DEVELOPMENT-SNAPSHOT-2024-03-07-a`](https://download.swift.org/development/xcode/swift-DEVELOPMENT-SNAPSHOT-2024-03-07-a/swift-DEVELOPMENT-SNAPSHOT-2024-03-07-a-osx.pkg) -7. or later. -8. Run the downloaded installer. Select "Install for me only" option during installation. -9. Select the newly installed snapshot in terminal: +the steps provided in ["How do I select the default version of Xcode"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html#//apple_ref/doc/uid/DTS40014588-CH1-HOW_DO_I_SELECT_THE_DEFAULT_VERSION_OF_XCODE_TO_USE_FOR_MY_COMMAND_LINE_TOOLS_) section of +["Building from the Command Line with Xcode FAQ"](https://developer.apple.com/library/archive/technotes/tn2339/_index.html). +3. Download latest development snapshot, for example +[`DEVELOPMENT-SNAPSHOT-2024-03-07-a`](https://download.swift.org/development/xcode/swift-DEVELOPMENT-SNAPSHOT-2024-03-07-a/swift-DEVELOPMENT-SNAPSHOT-2024-03-07-a-osx.pkg) +or later. +4. Run the downloaded installer. Select "Install for me only" option during installation. +5. Select the newly installed snapshot in terminal: ```sh export TOOLCHAINS=$(plutil -extract CFBundleIdentifier raw \