<jupyter_start><jupyter_text>Audio InspectionWith this notebook you can:1. Inspect speech recordings,2. Clean the recordings,3. Extract various spectrograms4. Compress and export the spectrograms.<jupyter_code>import os
import time

import tarfile

import numpy as np
import pandas as pd

import librosa
import librosa.display

import IPython.display as ipd

import matplotlib.pyplot as plt
def listen_clip(idx):
    
    audio_path = os.path.join(clips_dir,paths[idx]) 
    print(sentences[idx])
    return ipd.Audio(audio_path)

def clean_audio(audio):
    
    intervals = librosa.effects.split(audio, top_db=18, frame_length=4096, hop_length=2048)
    no_intervals = intervals.shape[0]    
        
    if no_intervals == 1:       
        cut_start_idx = intervals[0,0]
        cut_end_idx = intervals[0,1]
        
    else:               
        energies = np.zeros((no_intervals,))
        for j in range(no_intervals):
            
            energies[j] = np.sum(np.square(audio[intervals[j,0]:intervals[j,1]]))
        
        max_energy = np.max(energies)
        
        relative_energies = 100*energies/max_energy
                
        high_energy_indices = []
        for j,relative_energy in enumerate(relative_energies):
            
            if relative_energy> 10: # 10 percent found heuristicaly
                high_energy_indices.append(j) # find which intervals have high energy
        
        first_interval_idx = high_energy_indices[0] # the first interval with high energy
        last_interval_idx = high_energy_indices[-1] # last interval with high energy
        
        cut_start_idx = intervals[first_interval_idx,0] 
        cut_end_idx = intervals[last_interval_idx,1]
        
        
    clean_audio = audio[cut_start_idx:cut_end_idx]
        
    return clean_audio
# Mozilla Turkish Dataset
dataset_name = "cv-corpus-5.1-2020-06-22"

dataset_dir = os.path.join("..","data","Datasets",dataset_name,"tr")

clips_dir = os.path.join(dataset_dir,"clips")

pickle_dir = os.path.join(dataset_dir,"cv-corpus-5.1-2020-06-22_validated_simple_ordered.pkl") 
df = pd.read_pickle(pickle_dir)

#annotation_df = pd.read_csv(tsv_dir,sep='\t')
#annotation_df = pd.read_csv(csv_dir)

IDs = df["client_id"]
paths = df["path"]
sentences = df["sentence"]
codes = df['encoded']
#METUbet
dataset_dir = os.path.join('..','..',"Datasets","METUbet","data")

dataset_name = "METUbet"

clip_dir = os.path.join(dataset_dir,'speech-text')
pickle_dir = os.path.join(dataset_dir,'METUbet_encoded.pkl') # Original annotations

df = pd.read_pickle(pickle_dir)

#paths = df["path"]
sentences = df["turkish_sentence"]
codes = df['encoded']<jupyter_output><empty_output><jupyter_text>**Spectrogram Output Directories**<jupyter_code>amp_spectrogram_dir = os.path.join(dataset_dir,"spectrograms",'amplitude')
dB_spectrogram_dir = os.path.join(dataset_dir,"spectrograms",'dB')
power_spectrogram_dir = os.path.join(dataset_dir,"spectrograms",'power')

spectrogram_dirs = [amp_spectrogram_dir,dB_spectrogram_dir,power_spectrogram_dir]

for d in spectrogram_dirs:
    if not os.path.isdir(d):
        os.makedirs(d)<jupyter_output><empty_output><jupyter_text>Single File Read<jupyter_code>idx = 100
listen_clip(idx)
#clip_dir = os.path.join(dataset_dir,'speech-text','s1000','s1000-000.wav')
idx = 100
clip_dir = os.path.join(clips_dir,paths[idx])
audio,sr = librosa.load(clip_dir,sr=16000)

fig, ax = plt.subplots(figsize=(20,8))
librosa.display.waveplot(audio, sr=sr, ax=ax) 
ax.set(title="{} Waveform".format(paths[idx]))
#plt.savefig("{} Waveform.png".format(paths[idx]))
def plot_waveform_with_cuts(audio,sr,start_cut_time,end_cut_time,idx): 
    
    fig, ax = plt.subplots(figsize=(10,5))
    librosa.display.waveplot(audio, sr=sr, ax=ax) 
    ax.set(title="{} Waveform".format(paths[idx]))

    #plt.hlines(amplitude_threshold,0,len(audio),colors='r')
    #plt.hlines(-amplitude_threshold,0,len(audio),colors='r')

    bound = np.max(np.abs(audio))
    
    #plt.vlines(len(audio)/sr/3,-bound,bound,colors='b')
    #plt.vlines(2*len(audio)/sr/3,-bound,bound,colors='b')

    plt.vlines(start_cut_time,-bound,bound,colors='g')
    plt.vlines(end_cut_time,-bound,bound,colors='g')
    #plt.savefig("{} Cut.png".format(paths[idx]))<jupyter_output><empty_output><jupyter_text>Compute Spectrogram Variations for a Single Recording<jupyter_code>audio_path = os.path.join(clips_dir,paths[1])
audio,sr = librosa.load(audio_path,sr=16000)
 
audio_clean = clean_audio(audio)

amplitude_spectrogram = np.abs(librosa.stft(audio_clean,n_fft=512,hop_length=int(512/3),win_length=512))

dB_spectrogram = librosa.amplitude_to_db(amplitude_spectrogram,np.max(amplitude_spectrogram))

power_spectrogram = librosa.db_to_power(dB_spectrogram, ref=1.0)
fig, ax = plt.subplots(figsize=(20,8))
img = librosa.display.specshow(amplitude_spectrogram,
                               y_axis='log', x_axis='time', ax=ax)
ax.set_title('Amplitude spectrogram')
#fig.colorbar(img, ax=ax, format="%+2.0f dB")
#plt.text(18, 8000, sentences[1], fontsize=15,bbox=dict(alpha=1))

#plt.savefig("Amplitude Spectrogram.png")
fig, ax = plt.subplots(figsize=(20,8))
img = librosa.display.specshow(dB_spectrogram,
                               y_axis='log', x_axis='time', ax=ax)
ax.set_title('dB spectrogram')
fig.colorbar(img, ax=ax, format="%+2.0f dB")
#plt.text(18, 9000, sentences[1], fontsize=14,bbox=dict(alpha=1))

#plt.savefig("dB Spectrogram.png")
fig, ax = plt.subplots(figsize=(20,8))
img = librosa.display.specshow(power_spectrogram,
                               y_axis='log', x_axis='time', ax=ax)
ax.set_title('Power spectrogram')
#plt.text(18, 8000, sentences[1], fontsize=13,bbox=dict(alpha=1))
fig.colorbar(img, ax=ax, format="%+2.0f dB")

#plt.savefig("Power Spectrogram.png")<jupyter_output><empty_output><jupyter_text>Multiple Spectrogram Extraction  Create spectrograms and Write them to csvRead audio, clean it, calculate spectrograms and write to csv files<jupyter_code>counter = 0
start_time = time.time()
audio_lengths = []

for root,_,files in os.walk(clips_dir):
    
    for file in files:
        
        if file in paths and file.split('.')[-1] in ['wav','mp3']:

            audio_path = os.path.join(root,file)

            audio,sr = librosa.load(audio_path,sr=16000)
            
            audio_clean = clean_audio(audio)
            
            audio_lengths.append(len(audio_clean))

            amplitude_spectrogram = np.abs(librosa.stft(audio_clean,n_fft=512,hop_length=int(512/3),win_length=512))
            dB_spectrogram = librosa.amplitude_to_db(amplitude_spectrogram,np.max(amplitude_spectrogram))
            power_spectrogram = librosa.db_to_power(dB_spectrogram, ref=1.0)           

            file_name = file.split(".")[0]+".csv"
            np.savetxt(os.path.join(amp_spectrogram_dir,file_name),amplitude_spectrogram,delimiter=',')
            np.savetxt(os.path.join(dB_spectrogram_dir,file_name),dB_spectrogram,delimiter=',')
            np.savetxt(os.path.join(power_spectrogram_dir,file_name),power_spectrogram,delimiter=',')

            counter += 1        
            if counter == 1000:
                print("Time passed: {:.2f}s".format(time.time()-start_time))
                counter = 0
                start_time = time.time()
                
print('Total audio length {:.2f} hours'.format(sum(audio_lengths)/sr/60/60))<jupyter_output><empty_output><jupyter_text>Compress the folders<jupyter_code>dB_spectrogram_dir
spectrogram_dirs = [dB_spectrogram_dir]
for d in spectrogram_dirs:

    start_time = time.time()

    output_filename = '{}_{}_spectrograms.tar.gz'.format(dataset_name,d.split("\\")[-1])   
    
    output_dir = os.path.join(dataset_dir,"spectrograms",output_filename)

    with tarfile.open(output_dir, "w:gz") as tar:
        tar.add(d, arcname=os.path.basename(d))

    print("Total time: {:.2f} mins".format((time.time()-start_time)/60))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13855649326111602, 'fraction_numerical': 0.023428643405970524, 'mean_word_length': 4.099550417469493, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '625582', 'license': 'MIT', 'n_tokens_mistral': 3067, 'n_tokens_neox': 2877, 'n_words': 404, 'path': 'data_preparation/Audio Inspection.ipynb', 'repo_name': 'raraz15/DeepTurkish', 'size': '7939'}