Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
alisomay committed Oct 17, 2023
1 parent 1d086b3 commit 835ed4b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
62 changes: 29 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,44 +51,43 @@ WASAPI including access to large numbers of channels and lower-latency audio
processing.

CPAL allows for using the ASIO SDK as the audio host on Windows instead of
WASAPI. To do so, follow these steps:

1. **Download the ASIO SDK** `.zip` from [this
link](https://www.steinberg.net/en/company/developers.html). The version as
of writing this is 2.3.1.
2. Extract the files and place the directory somewhere you are happy for it to stay
(e.g. `~/.asio`).
3. Assign the full path of the directory (that contains the `readme`, `changes`,
`ASIO SDK 2.3` pdf, etc) to the `CPAL_ASIO_DIR` environment variable. This is
necessary for the `asio-sys` build script to build and bind to the SDK.
4. `bindgen`, the library used to generate bindings to the C++ SDK, requires
WASAPI.

### Locating the ASIO SDK

The location of ASIO SDK is exposed to CPAL by setting the `CPAL_ASIO_DIR` environment variable.

The build script will try to find the ASIO SDK by following these steps in order:
1. Check if `CPAL_ASIO_DIR` is set and if so use the path to point to the SDK.
2. Check if the ASIO SDK is already installed in the temporary directory, if so use that and set the path of `CPAL_ASIO_DIR` to the output of `std::env::temp_dir().join("asio_sdk")`.
3. If the ASIO SDK is not already installed, download it from <https://www.steinberg.net/asiosdk> and install it in the temporary directory. The path of `CPAL_ASIO_DIR` will be set to the output of `std::env::temp_dir().join("asio_sdk")`.

In an ideal situation you don't need to worry about this step.

### Preparing the build environment

1. `bindgen`, the library used to generate bindings to the C++ SDK, requires
clang. **Download and install LLVM** from
[here](http://releases.llvm.org/download.html) under the "Pre-Built Binaries"
section. The version as of writing this is 7.0.0.
5. Add the LLVM `bin` directory to a `LIBCLANG_PATH` environment variable. If
section. The version as of writing this is 17.0.1.
2. Add the LLVM `bin` directory to a `LIBCLANG_PATH` environment variable. If
you installed LLVM to the default directory, this should work in the command
prompt:
```
setx LIBCLANG_PATH "C:\Program Files\LLVM\bin"
```
6. If you don't have any ASIO devices or drivers available, you can [**download
3. If you don't have any ASIO devices or drivers available, you can [**download
and install ASIO4ALL**](http://www.asio4all.org/). Be sure to enable the
"offline" feature during installation despite what the installer says about
it being useless.
7. **Loading VCVARS**. `rust-bindgen` uses the C++ tool-chain when generating
bindings to the ASIO SDK. As a result, it is necessary to load some
environment variables in the command prompt that we used to build our project.
On 64-bit machines run:
```
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
```
On 32-bit machines run:
```
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86
```
Note that, depending on your version of Visual Studio, this script might be
in a slightly different location.
8. Select the ASIO host at the start of our program with the following code:
4. Our build script assumes that Microsoft Visual Studio is installed. The script will try to find `vcvarsall.bat` and execute it with the right machine architecture regardless of the Microsoft Visual Studio version. If there are any errors encountered in this process which is unlikely, you may find the `vcvarsall.bat` manually and execute it with your machine architecture as an argument. The script will detect this and skip the step.

Manual command example for 64 bit machines:
```
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
```

5. Select the ASIO host at the start of our program with the following code:

```rust
let host;
Expand All @@ -100,7 +99,7 @@ WASAPI. To do so, follow these steps:

If you run into compilations errors produced by `asio-sys` or `bindgen`, make
sure that `CPAL_ASIO_DIR` is set correctly and try `cargo clean`.
9. Make sure to enable the `asio` feature when building CPAL:
6. Make sure to enable the `asio` feature when building CPAL:

```
cargo build --features "asio"
Expand All @@ -112,8 +111,5 @@ WASAPI. To do so, follow these steps:
```toml
cpal = { version = "*", features = ["asio"] }
```

In the future we would like to work on automating this process to make it
easier, but we are not familiar enough with the ASIO license to do so yet.


*Updated as of ASIO version 2.3.3.*
27 changes: 16 additions & 11 deletions asio-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ fn main() {
// Directory where bindings and library are created
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("bad path"));

let mut vc_vars_invoked = false;

// Check if library exists,
// if it doesn't create it
let mut lib_path = out_dir.clone();
lib_path.push("libasio.a");
if !lib_path.exists() {
// Set env vars from vcvarsall.bat before attempting to build
invoke_vcvars();
vc_vars_invoked = true;
if !vcvars_set() {
println!("VCINSTALLDIR is not set. Attempting to invoke vcvarsall.bat..");
invoke_vcvars();
}
create_lib(&cpal_asio_dir);
}

Expand All @@ -49,8 +48,8 @@ fn main() {
let mut binding_path = out_dir.clone();
binding_path.push("asio_bindings.rs");
if !binding_path.exists() {
// Set env vars from vcvarsall.bat before attempting to build if not already done
if !vc_vars_invoked {
if !vcvars_set() {
println!("VCINSTALLDIR is not set. Attempting to invoke vcvarsall.bat..");
invoke_vcvars();
}
create_bindings(&cpal_asio_dir);
Expand Down Expand Up @@ -229,7 +228,10 @@ fn get_asio_dir() -> PathBuf {
}

// If not found, download ASIO SDK using PowerShell's Invoke-WebRequest
println!("CPAL_ASIO_DIR is not set or contents are cached downloading from {}", ASIO_SDK_URL);
println!(
"CPAL_ASIO_DIR is not set or contents are cached downloading from {}",
ASIO_SDK_URL
);

let asio_zip_path = temp_dir.join("asio_sdk.zip");
let status = Command::new("powershell")
Expand Down Expand Up @@ -343,7 +345,10 @@ fn invoke_vcvars() {
}
}

panic!(
"Could not find vcvarsall.bat. Please install the latest version of Visual Studio."
);
panic!("Could not find vcvarsall.bat. Please install the latest version of Visual Studio.");
}
// Checks if vcvarsall.bat has been invoked
// Assumes that it is very unlikely that the user would set VCINSTALLDIR manually
fn vcvars_set() -> bool {
env::var("VCINSTALLDIR").is_ok()
}

0 comments on commit 835ed4b

Please sign in to comment.