Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audio support to DataPack #585

Merged
merged 7 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
rm -rf texar-pytorch
- name: Install Forte
run: |
pip install --use-feature=in-tree-build --progress-bar off .[ner,test,example,wikipedia,augment,stave,audio]
pip install --use-feature=in-tree-build --progress-bar off .[ner,test,example,wikipedia,augment,stave,audio_ext]
- name: Build ontology
run: |
./scripts/build_ontology_specs.sh
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Welcome to Forte's documentation!

examples.md
ontology_generation.md
audio_processing.md

API
====
Expand Down
1 change: 1 addition & 0 deletions forte/data/readers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
from forte.data.readers.ag_news_reader import *
from forte.data.readers.largemovie_reader import *
from forte.data.readers.misc_readers import *
from forte.data.readers.audio_reader import *
18 changes: 16 additions & 2 deletions forte/data/readers/audio_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"""
import os
from typing import Any, Iterator
import soundfile

from forte.data.data_pack import DataPack
from forte.data.data_utils_io import dataset_path_iterator
Expand All @@ -30,6 +29,21 @@
class AudioReader(PackReader):
r""":class:`AudioReader` is designed to read in audio files."""

def initialize(self, resources, configs):
# pylint: disable=attribute-defined-outside-init
super().initialize(resources, configs)
try:
import soundfile # pylint: disable=import-outside-toplevel
hunterhector marked this conversation as resolved.
Show resolved Hide resolved
except ModuleNotFoundError as e:
raise ModuleNotFoundError(
"AudioReader requires 'soundfile' package to be installed."
" You can run 'pip install soundfile' or 'pip install forte"
"[audio_ext]'. Note that additional steps might apply to Linux"
" users (refer to "
"https://pysoundfile.readthedocs.io/en/latest/#installation)."
) from e
self._soundfile = soundfile

def _collect(self, audio_directory) -> Iterator[Any]: # type: ignore
r"""Should be called with param ``audio_directory`` which is a path to a
folder containing audio files.
Expand All @@ -48,7 +62,7 @@ def _parse_pack(self, file_path: str) -> Iterator[DataPack]:
pack: DataPack = DataPack()

# Read in audio data and store in DataPack
audio, sample_rate = soundfile.read(
audio, sample_rate = self._soundfile.read(
file=file_path, **(self.configs.read_kwargs or {})
)
pack.set_audio(audio=audio, sample_rate=sample_rate)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
# transformers 4.10.0 will break the translation model we used here
"augment": ["transformers>=3.1, <=4.9.2", "nltk"],
"stave": ["stave>=0.0.1.dev12"],
"audio": ["soundfile>=0.10.3"],
"audio_ext": ["soundfile>=0.10.3"],
hunterhector marked this conversation as resolved.
Show resolved Hide resolved
},
entry_points={
"console_scripts": [
Expand Down
2 changes: 1 addition & 1 deletion tests/forte/data/readers/audio_reader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from forte.common.resources import Resources
from forte.common.exception import ProcessFlowException
from forte.data.data_pack import DataPack
from forte.data.readers.audio_reader import AudioReader
from forte.data.readers import AudioReader
from forte.pipeline import Pipeline
from forte.processors.base.pack_processor import PackProcessor

Expand Down