93 lines
2.8 KiB
Python
93 lines
2.8 KiB
Python
"""
|
|
file_name: hnr
|
|
project_name: DBM
|
|
created: 2020-20-07
|
|
"""
|
|
|
|
import pandas as pd
|
|
import numpy as np
|
|
import os
|
|
import glob
|
|
import parselmouth
|
|
import librosa
|
|
from os.path import join
|
|
import logging
|
|
|
|
from dbm_lib.dbm_features.raw_features.util import util as ut
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger=logging.getLogger()
|
|
|
|
hnr_dir = 'audio/harmonic_noise'
|
|
csv_ext = '_hnr_frame.csv'
|
|
error_txt = 'error: length less than 0.064'
|
|
|
|
def hnr_ratio(filepath):
|
|
"""
|
|
Using parselmouth library fetching harmonic noise ratio ratio
|
|
Args:
|
|
path: (.wav) audio file location
|
|
Returns:
|
|
(list) list of hnr ratio for each voice frame, min,max and mean hnr
|
|
"""
|
|
sound = parselmouth.Sound(filepath)
|
|
harmonicity = sound.to_harmonicity_ac(time_step=.001)
|
|
|
|
hnr_all_frames = harmonicity.values#[harmonicity.values != -200] nan it (****)
|
|
hnr_all_frames = np.where(hnr_all_frames==-200, np.NaN, hnr_all_frames)
|
|
return hnr_all_frames.transpose()
|
|
|
|
def calc_hnr(video_uri, audio_file, out_loc, fl_name, r_config):
|
|
"""
|
|
Preparing harmonic noise matrix
|
|
Args:
|
|
audio_file: (.wav) parsed audio file
|
|
out_loc: (str) Output directory for csv's
|
|
"""
|
|
|
|
hnr_all_frames = hnr_ratio(audio_file)
|
|
df_hnr = pd.DataFrame(hnr_all_frames, columns=[r_config.aco_hnr])
|
|
|
|
df_hnr['Frames'] = df_hnr.index
|
|
df_hnr['dbm_master_url'] = video_uri
|
|
df_hnr[r_config.err_reason] = 'Pass'# will replace with threshold in future release
|
|
|
|
logger.info('Saving Output file {} '.format(out_loc))
|
|
ut.save_output(df_hnr, out_loc, fl_name, hnr_dir, csv_ext)
|
|
|
|
def empty_hnr(video_uri, out_loc, fl_name, r_config):
|
|
"""
|
|
Preparing empty HNR matrix if something fails
|
|
"""
|
|
cols = ['Frames', r_config.aco_hnr, r_config.err_reason]
|
|
out_val = [[np.nan, np.nan, error_txt]]
|
|
df_hnr = pd.DataFrame(out_val, columns = cols)
|
|
df_hnr['dbm_master_url'] = video_uri
|
|
|
|
logger.info('Saving Output file {} '.format(out_loc))
|
|
ut.save_output(df_hnr, out_loc, fl_name, hnr_dir, csv_ext)
|
|
|
|
def run_hnr(video_uri, out_dir, r_config):
|
|
"""
|
|
Processing all patient's for fetching harmonic noise ratio
|
|
-------------------
|
|
-------------------
|
|
Args:
|
|
video_uri: video path; r_config: raw variable config object
|
|
out_dir: (str) Output directory for processed output
|
|
"""
|
|
input_loc, out_loc, fl_name = ut.filter_path(video_uri, out_dir)
|
|
aud_filter = glob.glob(join(input_loc, fl_name + '.wav'))
|
|
if len(aud_filter)>0:
|
|
|
|
audio_file = aud_filter[0]
|
|
aud_dur = librosa.get_duration(filename=audio_file)
|
|
|
|
if float(aud_dur) < 0.064:
|
|
logger.info('Output file {} size is less than 0.064sec'.format(audio_file))
|
|
|
|
empty_hnr(video_uri, out_loc, fl_name, r_config)
|
|
return
|
|
|
|
calc_hnr(video_uri, audio_file, out_loc, fl_name, r_config)
|