import librosa 
import numpy as np
from scipy.io import wavfile as wav
fname = '../input/urbansound8k/fold2/100652-3-0-3.wav'
librosa_audio , librosa_sample_rate = librosa.load(fname)
scipy_sample_rate , scipy_audio = wav.read(fname)
print('Librosa Sample Rate',librosa_sample_rate)
print('Scipy sample Rate',scipy_sample_rate)
librosa_audio
scipy_audio
"""
Librosa has also normalised the audio data
"""
"""
Librosa also converts Audio into Mono Channel
"""
import matplotlib.pyplot as plt
plt.figure(figsize=(12,4))
plt.plot(scipy_audio)
"""
Dual Stereo Scipy Audio
"""
plt.figure(figsize=(12,4))
plt.plot(librosa_audio)
"""
Single Channel Librosa Audio
"""
"""
# Feature Extraction
"""
"""
we will extract Mel-Frequency Cepstral Coefficients (MFCC) from the the audio samples.

The MFCC summarises the frequency distribution across the window size, so it is possible to analyse both the frequency and time characteristics of the sound. These audio representations will allow us to identify features for classification
"""
mfcc = librosa.feature.mfcc(y=librosa_audio,sr=librosa_sample_rate,n_mfcc=40)
mfcc
mfcc.shape
def extract_features(file_name):
    try:
        audio,sample_rate = librosa.load(file_name,res_type='kaiser_fast')
        mfccs = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
        mfccsscaled = np.mean(mfccs.T,axis=0)
    
    except Exception as e:
        print("Error encountered while parsing file: ", file)
        return None 
     
    return mfccsscaled
        
    
# Load various imports 
import pandas as pd
import os
import librosa

# Set the path to the full UrbanSound dataset 
#fulldatasetpath = '../input/urbansound8k'

#metadata = pd.read_csv('../input/urbansound8k/UrbanSound8K.csv')

#features = []

# Iterate through each sound file and extract the features 
#for index, row in metadata.iterrows():
    
    #file_name = os.path.join(os.path.abspath(fulldatasetpath),'fold'+str(row["fold"])+'/',str(row["slice_file_name"]))
    
    #class_label = row["class"]
    #data = extract_features(file_name)
    
    #features.append([data, class_label])

# Convert into a Panda dataframe 
#featuresdf = pd.DataFrame(features, columns=['feature','class_label'])

#print('Finished feature extraction from ', len(featuresdf), ' files')
featuresdf
#featuresdf.to_csv('urban_sound_data.csv')