Skip to content

Commit

Permalink
Merge pull request #14 from sensein/dev-rahul
Browse files Browse the repository at this point in the history
adding opensmile
  • Loading branch information
satra authored Mar 23, 2024
2 parents 0159abf + 6f14ee3 commit 9c18470
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/b2aiprep/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from speechbrain.augment.time_domain import Resample
from speechbrain.dataio.dataio import read_audio, read_audio_info
from speechbrain.inference.speaker import EncoderClassifier
import opensmile


class Audio:
Expand Down Expand Up @@ -131,6 +132,19 @@ def resample_iir(audio: Audio, lowcut: float, new_sample_rate: int, order: int =
return Audio(resampler(filtered.unsqueeze(0)).squeeze(0), new_sample_rate)


def extract_opensmile(
audio: Audio,
feature_set: opensmile.FeatureSet = opensmile.FeatureSet.eGeMAPSv02,
feature_level: opensmile.FeatureLevel = opensmile.FeatureLevel.Functionals
) -> torch.tensor: #feature_set: opensmile.FeatureSet = opensmile.FeatureSet.eGeMAPSv02

smile = opensmile.Smile(
feature_set=feature_set,
feature_level=feature_level,
verbose=True,
)
return smile.process_signal(audio.signal.squeeze(), audio.sample_rate)

def to_features(
filename: Path,
subject: str,
Expand All @@ -139,6 +153,8 @@ def to_features(
n_mels: int = 20,
n_coeff: int = 20,
compute_deltas: bool = True,
opensmile_feature_set: opensmile.FeatureSet = opensmile.FeatureSet.eGeMAPSv02,
opensmile_feature_level: opensmile.FeatureLevel = opensmile.FeatureLevel.Functionals
) -> ty.Tuple[dict, Path]:
"""Compute features from audio file
Expand All @@ -159,10 +175,12 @@ def to_features(
features = specgram(audio)
features_melfilterbank = melfilterbank(features, n_mels=n_mels)
features_mfcc = MFCC(features_melfilterbank, n_coeff=n_coeff, compute_deltas=compute_deltas)
features_opensmile = extract_opensmile(audio, opensmile_feature_set, opensmile_feature_level)
features = {
"specgram": features,
"melfilterbank": features_melfilterbank,
"mfcc": features_mfcc,
"opensmile": features_opensmile,
"sample_rate": audio.sample_rate,
"checksum": md5sum,
}
Expand Down

0 comments on commit 9c18470

Please sign in to comment.