-
Notifications
You must be signed in to change notification settings - Fork 0
/
helperFunctions.py
37 lines (31 loc) · 1.2 KB
/
helperFunctions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
import mutagen
import mutagen.wave
import pandas as pd
def create_dataset_df(csv_file):
dataset_df = pd.read_csv(csv_file)
filepaths = []
for i, row in dataset_df.iterrows():
filepaths.append(os.path.join('UrbanSound8K/audio', 'fold'+str(row['fold']), row['slice_file_name']))
dataset_df['filepath'] = filepaths
return dataset_df
def compute_audio_statistics(dataset_df):
metadata_dict = {'length': [], 'bitrate': [], 'channels': [], 'sample_rate': [], 'bits_per_sample': []}
# Extract metadata
for filepath in dataset_df['filepath']:
metadata = get_audio_metadata_mutagen(filepath)
for key in metadata_dict.keys():
metadata_dict[key].append(metadata[key])
# Add new columns to dataframe
for key in metadata_dict.keys():
dataset_df[key] = metadata_dict[key]
return dataset_df
def get_audio_metadata_mutagen(filepath):
metadata = {}
f = mutagen.wave.WAVE(filepath)
metadata['length'] = f.info.length
metadata['bitrate'] = f.info.bitrate
metadata['channels'] = f.info.channels
metadata['sample_rate'] = f.info.sample_rate
metadata['bits_per_sample'] = f.info.bits_per_sample
return metadata