-
Notifications
You must be signed in to change notification settings - Fork 633
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor libtorchaudio example (#1486)
- Loading branch information
Showing
7 changed files
with
77 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,27 @@ | ||
# Example usage: libtorchaudio | ||
# Libtorchaudio Examples | ||
|
||
This example demonstrates how you can use torchaudio's I/O features in C++ application, in addition to PyTorch's operations. | ||
* [Augmentation](./augmentation) | ||
|
||
To try this example, simply run `./run.sh`. This script will | ||
## Build | ||
|
||
1. Create an audio preprocessing pipeline with TorchScript and dump it to a file. | ||
2. Build the application using `libtorch` and `libtorchaudio`. | ||
3. Execute the preprocessing pipeline on an example audio. | ||
The example applications in this directory depend on `libtorch` and `libtorchaudio`. | ||
If you have a working `PyTorch`, you already have `libtorch`. | ||
Please refer to [this tutorial](https://pytorch.org/tutorials/advanced/torch_script_custom_classes.html) for the use of `libtorch` and TorchScript. | ||
|
||
The detail of the preprocessing pipeline can be found in [`create_jittable_pipeline.py`](./create_jittable_pipeline.py). | ||
`libtorchaudio` is the library of torchaudio's C++ components without Python component. | ||
It is currently not distributed, and it will be built alongside with the applications. | ||
|
||
The following commands will build `libtorchaudio` and applications. | ||
|
||
```bash | ||
mkdir build | ||
cd build | ||
cmake -GNinja \ | ||
-DCMAKE_PREFIX_PATH="$(python -c 'import torch;print(torch.utils.cmake_prefix_path)')" \ | ||
-DBUILD_SOX=ON \ | ||
-DBUILD_KALDI=OFF \ | ||
.. | ||
cmake --build . | ||
``` | ||
|
||
For the usages of each application, refer to the corresponding application directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
add_executable(augment main.cpp) | ||
target_link_libraries(augment "${TORCH_LIBRARIES}" "${TORCHAUDIO_LIBRARY}") | ||
set_property(TARGET augment PROPERTY CXX_STANDARD 14) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Augmentation | ||
|
||
This example demonstrates how you can use torchaudio's I/O features and augmentations in C++ application. | ||
|
||
**NOTE** | ||
This example uses `"sox_io"` backend, thus does not work on Windows. | ||
|
||
## Steps | ||
### 1. Create augmentation pipeline TorchScript file. | ||
|
||
First, we implement our data process pipeline as a regular Python, and save it as a TorchScript object. | ||
We will load and execute it in our C++ application. The C++ code is found in [`main.cpp`](./main.cpp). | ||
|
||
```python | ||
python create_jittable_pipeline.py \ | ||
--rir-path "../data/rir.wav" \ | ||
--output-path "./pipeline.zip" | ||
``` | ||
|
||
### 2. Build the application | ||
|
||
Please refer to [the top level README.md](../README.md) | ||
|
||
### 3. Run the application | ||
|
||
Now we run the C++ application `augment`, with the TorchScript object we created in Step.1 and an input audio file. | ||
|
||
In [the top level directory](../) | ||
|
||
```bash | ||
input_audio_file="./data/input.wav" | ||
./build/augmentation/augment ./augmentation/pipeline.zip "${input_audio_file}" "output.wav" | ||
``` | ||
|
||
When you give a clean speech file, the output audio sounds like it's a phone conversation. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters