Skip to content

Commit 88f554b

Browse files
committed
Add opus support (#755)
1 parent 9c4b4f2 commit 88f554b

File tree

7 files changed

+64
-36
lines changed

7 files changed

+64
-36
lines changed

.circleci/config.yml

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,13 @@ commands:
3838
our_upload_channel=test
3939
fi
4040
echo "export UPLOAD_CHANNEL=${our_upload_channel}" >> ${BASH_ENV}
41-
install_cmake_macos:
42-
description: "installs cmake on macOS. Use binary distribution as brew is slow"
41+
install_build_tools_macos:
42+
description: "installs tools required to build torchaudio"
4343
steps:
4444
- run:
45-
name: Install cmake
46-
command: |
47-
curl -L -o cmake.tar.gz https://github.com/Kitware/CMake/releases/download/v3.16.5/cmake-3.16.5-Darwin-x86_64.tar.gz
48-
mkdir cmake
49-
tar -xf cmake.tar.gz --strip 3 -C cmake
50-
rm cmake.tar.gz
51-
echo 'export PATH='"${PWD}/cmake/bin"':${PATH}' >> ${BASH_ENV}
45+
name: Install cmake and pkg-config
46+
command: HOMEBREW_NO_AUTO_UPDATE=1 brew install cmake pkg-config
47+
# Disable brew auto update which is very slow
5248

5349
binary_common: &binary_common
5450
parameters:
@@ -158,7 +154,7 @@ jobs:
158154
xcode: "9.0"
159155
steps:
160156
- checkout
161-
- install_cmake_macos
157+
- install_build_tools_macos
162158
- attach_workspace:
163159
at: third_party
164160
- run:
@@ -183,7 +179,7 @@ jobs:
183179
xcode: "9.0"
184180
steps:
185181
- checkout
186-
- install_cmake_macos
182+
- install_build_tools_macos
187183
- attach_workspace:
188184
at: third_party
189185
- run:
@@ -398,14 +394,14 @@ jobs:
398394
- restore_cache:
399395

400396
keys:
401-
- env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum ".cachekey" }}
397+
- env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum "third_party/CMakeLists.txt" }}-{{ checksum ".cachekey" }}
402398

403399
- run:
404400
name: Setup
405401
command: .circleci/unittest/linux/scripts/setup_env.sh
406402
- save_cache:
407403

408-
key: env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum ".cachekey" }}
404+
key: env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum "third_party/CMakeLists.txt" }}-{{ checksum ".cachekey" }}
409405

410406
paths:
411407
- conda
@@ -440,14 +436,14 @@ jobs:
440436
- restore_cache:
441437

442438
keys:
443-
- env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum ".cachekey" }}
439+
- env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum "third_party/CMakeLists.txt" }}-{{ checksum ".cachekey" }}
444440

445441
- run:
446442
name: Setup
447443
command: docker run -t --gpus all -v $PWD:$PWD -w $PWD "${image_name}" .circleci/unittest/linux/scripts/setup_env.sh
448444
- save_cache:
449445

450-
key: env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum ".cachekey" }}
446+
key: env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum "third_party/CMakeLists.txt" }}-{{ checksum ".cachekey" }}
451447

452448
paths:
453449
- conda

.circleci/config.yml.in

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,13 @@ commands:
3838
our_upload_channel=test
3939
fi
4040
echo "export UPLOAD_CHANNEL=${our_upload_channel}" >> ${BASH_ENV}
41-
install_cmake_macos:
42-
description: "installs cmake on macOS. Use binary distribution as brew is slow"
41+
install_build_tools_macos:
42+
description: "installs tools required to build torchaudio"
4343
steps:
4444
- run:
45-
name: Install cmake
46-
command: |
47-
curl -L -o cmake.tar.gz https://github.com/Kitware/CMake/releases/download/v3.16.5/cmake-3.16.5-Darwin-x86_64.tar.gz
48-
mkdir cmake
49-
tar -xf cmake.tar.gz --strip 3 -C cmake
50-
rm cmake.tar.gz
51-
echo 'export PATH='"${PWD}/cmake/bin"':${PATH}' >> ${BASH_ENV}
45+
name: Install cmake and pkg-config
46+
command: HOMEBREW_NO_AUTO_UPDATE=1 brew install cmake pkg-config
47+
# Disable brew auto update which is very slow
5248

5349
binary_common: &binary_common
5450
parameters:
@@ -158,7 +154,7 @@ jobs:
158154
xcode: "9.0"
159155
steps:
160156
- checkout
161-
- install_cmake_macos
157+
- install_build_tools_macos
162158
- attach_workspace:
163159
at: third_party
164160
- run:
@@ -183,7 +179,7 @@ jobs:
183179
xcode: "9.0"
184180
steps:
185181
- checkout
186-
- install_cmake_macos
182+
- install_build_tools_macos
187183
- attach_workspace:
188184
at: third_party
189185
- run:
@@ -398,14 +394,14 @@ jobs:
398394
- restore_cache:
399395
{% raw %}
400396
keys:
401-
- env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum ".cachekey" }}
397+
- env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum "third_party/CMakeLists.txt" }}-{{ checksum ".cachekey" }}
402398
{% endraw %}
403399
- run:
404400
name: Setup
405401
command: .circleci/unittest/linux/scripts/setup_env.sh
406402
- save_cache:
407403
{% raw %}
408-
key: env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum ".cachekey" }}
404+
key: env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum "third_party/CMakeLists.txt" }}-{{ checksum ".cachekey" }}
409405
{% endraw %}
410406
paths:
411407
- conda
@@ -440,14 +436,14 @@ jobs:
440436
- restore_cache:
441437
{% raw %}
442438
keys:
443-
- env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum ".cachekey" }}
439+
- env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum "third_party/CMakeLists.txt" }}-{{ checksum ".cachekey" }}
444440
{% endraw %}
445441
- run:
446442
name: Setup
447443
command: docker run -t --gpus all -v $PWD:$PWD -w $PWD "${image_name}" .circleci/unittest/linux/scripts/setup_env.sh
448444
- save_cache:
449445
{% raw %}
450-
key: env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum ".cachekey" }}
446+
key: env-v3-linux-{{ arch }}-py<< parameters.python_version >>-{{ checksum ".circleci/unittest/linux/scripts/environment.yml" }}-{{ checksum "third_party/CMakeLists.txt" }}-{{ checksum ".cachekey" }}
451447
{% endraw %}
452448
paths:
453449
- conda

.circleci/unittest/linux/docker/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ RUN apt update && apt install -y \
5959
libsox-dev \
6060
libsox-fmt-all \
6161
cmake \
62+
pkg-config \
6263
&& rm -rf /var/lib/apt/lists/*
6364
COPY --from=builder /kaldi /kaldi
6465
COPY --from=builder /third_party /third_party

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ python setup.py install
113113
MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py install
114114
```
115115

116-
Alternatively, the build process can build SoX (and codecs such as libmad, lame and flac) statically and torchaudio can link them, by setting environment variable `BUILD_SOX=1`.
117-
The build process will fetch and build SoX, liblame, libmad, flac before building extension.
116+
Alternatively, the build process can build libsox and some optional codecs statically and torchaudio can link them, by setting environment variable `BUILD_SOX=1`.
117+
The build process will fetch and build libmad, lame, flac, vorbis, opus, and libsox before building extension. This process requires `cmake` and `pkg-config`.
118118

119119
```bash
120120
# Linux

build_tools/setup_helpers/extension.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ def _get_extra_objects():
8383
'libmad.a',
8484
'libFLAC.a',
8585
'libmp3lame.a',
86+
'libopusfile.a',
87+
'libopus.a',
8688
'libvorbisenc.a',
8789
'libvorbisfile.a',
8890
'libvorbis.a',

third_party/CMakeLists.txt

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ ExternalProject_Add(libflac
3838
DOWNLOAD_DIR ${ARCHIVE_DIR}
3939
URL https://ftp.osuosl.org/pub/xiph/releases/flac/flac-1.3.2.tar.xz
4040
URL_HASH SHA256=91cfc3ed61dc40f47f050a109b08610667d73477af6ef36dcad31c31a4a8d53f
41-
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/src/libflac/configure ${COMMON_ARGS} --with-ogg=${INSTALL_DIR}
41+
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build_codec_helper.sh ${CMAKE_CURRENT_SOURCE_DIR}/src/libflac/configure ${COMMON_ARGS} --with-ogg
4242
)
4343

4444
ExternalProject_Add(libvorbis
@@ -47,14 +47,34 @@ ExternalProject_Add(libvorbis
4747
DOWNLOAD_DIR ${ARCHIVE_DIR}
4848
URL https://ftp.osuosl.org/pub/xiph/releases/vorbis/libvorbis-1.3.6.tar.gz
4949
URL_HASH SHA256=6ed40e0241089a42c48604dc00e362beee00036af2d8b3f46338031c9e0351cb
50-
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/src/libvorbis/configure ${COMMON_ARGS} --with-ogg=${INSTALL_DIR}
50+
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build_codec_helper.sh ${CMAKE_CURRENT_SOURCE_DIR}/src/libvorbis/configure ${COMMON_ARGS} --with-ogg
51+
)
52+
53+
ExternalProject_Add(libopus
54+
PREFIX ${CMAKE_CURRENT_SOURCE_DIR}
55+
DEPENDS libogg
56+
DOWNLOAD_DIR ${ARCHIVE_DIR}
57+
URL https://ftp.osuosl.org/pub/xiph/releases/opus/opus-1.3.1.tar.gz
58+
URL_HASH SHA256=65b58e1e25b2a114157014736a3d9dfeaad8d41be1c8179866f144a2fb44ff9d
59+
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build_codec_helper.sh ${CMAKE_CURRENT_SOURCE_DIR}/src/libopus/configure ${COMMON_ARGS} --with-ogg
60+
)
61+
62+
ExternalProject_Add(opusfile
63+
PREFIX ${CMAKE_CURRENT_SOURCE_DIR}
64+
DEPENDS libopus
65+
DOWNLOAD_DIR ${ARCHIVE_DIR}
66+
STAMP_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/opusfile-stamp
67+
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/opusfile
68+
URL https://ftp.osuosl.org/pub/xiph/releases/opus/opusfile-0.12.tar.gz
69+
URL_HASH SHA256=118d8601c12dd6a44f52423e68ca9083cc9f2bfe72da7a8c1acb22a80ae3550b
70+
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build_codec_helper.sh ${CMAKE_CURRENT_SOURCE_DIR}/src/opusfile/configure ${COMMON_ARGS} --disable-http
5171
)
5272

5373
ExternalProject_Add(libsox
5474
PREFIX ${CMAKE_CURRENT_SOURCE_DIR}
55-
DEPENDS libogg libflac libvorbis libmp3lame libmad
75+
DEPENDS libogg libflac libvorbis opusfile libmp3lame libmad
5676
DOWNLOAD_DIR ${ARCHIVE_DIR}
5777
URL https://downloads.sourceforge.net/project/sox/sox/14.4.2/sox-14.4.2.tar.bz2
5878
URL_HASH SHA256=81a6956d4330e75b5827316e44ae381e6f1e8928003c6aa45896da9041ea149c
59-
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/src/libsox/configure ${COMMON_ARGS} LDFLAGS=-L${INSTALL_DIR}/lib CPPFLAGS=-I${INSTALL_DIR}/include --with-lame --with-flac --with-mad --with-oggvorbis --without-alsa --without-coreaudio --without-png --without-oss --without-sndfile
79+
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build_codec_helper.sh ${CMAKE_CURRENT_SOURCE_DIR}/src/libsox/configure ${COMMON_ARGS} --with-lame --with-flac --with-mad --with-oggvorbis --without-alsa --without-coreaudio --without-png --without-oss --without-sndfile --with-opus
6080
)

third_party/build_codec_helper.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
# Helper script for building codecs depending on libogg, such as libopus and opus.
3+
# It is difficult to set environment variable inside of ExternalProject_Add,
4+
# so this script sets necessary environment variables before running the given command
5+
6+
this_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
7+
install_dir="${this_dir}/install"
8+
9+
export PKG_CONFIG_PATH="${install_dir}/lib/pkgconfig"
10+
export LDFLAGS="-L${install_dir}/lib ${LDFLAGS}"
11+
export CPPFLAGS="-I${install_dir}/include ${CPPFLAGS}"
12+
13+
$@

0 commit comments

Comments
 (0)