# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="QHGCcZwPP85b"
import numpy as np
import random
from google.colab import drive
import json
import time
import pandas as pd
import os
import sys
import librosa
import soundfile as sf
import xgboost
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from matplotlib import pyplot

# + colab={"base_uri": "https://localhost:8080/"} id="XroM1Bc6QYFF" outputId="3dbda8de-ae38-4c4f-c925-55a7534b1455"
drive.mount('/content/gdrive')

# + id="XnCZr76OUQ3f"
speaker_list = ['KENN',
 'MCUR',
 'JTAN',
 'MPOL',
 'MPUR',
 'ADIZ',
 'NJAT',
 'PMAR',
 'JLEE',
 'SAMF',
 'ZHIY']

# + id="PAYeW9O-WaJ3"
test_speaker = ['VKOW']

# + id="pu5lAtpgXJU8"
audio_array = []
phoneme_array = []

# + colab={"base_uri": "https://localhost:8080/"} id="_fYkJgIGDW8u" outputId="8fd43e71-60d0-4937-d069-9ccb805976f6"
# !cp -r /content/gdrive/MyDrive/nus-smc-corpus_48/ /content/

# + id="0Pq1J8qPWcZd"
for sp in speaker_list:
  for sig in os.listdir("/content/nus-smc-corpus_48/"+str(sp)+"/read/"):
    if '.wav' in sig:
      song = sig.replace('.wav','')
      df = pd.read_csv("/content/nus-smc-corpus_48/"+str(sp)+"/read/"+str(song)+".txt", delimiter=" ", header=None, names=['start', 'end', 'phoneme'])
      audio, sr = librosa.load("/content/nus-smc-corpus_48/"+str(sp)+"/read/"+str(sig), sr=None)
      audio = audio[:int(df['end'].iloc[-1]*sr)]

      for idx, row in df.iterrows():
        for i in range(int(row['start']*100),int(row['end']*100)):
          reqd_audio = audio[int(i/100*sr):int((i+1)/100*sr)]
          spectrogram = librosa.stft(reqd_audio, n_fft=256, hop_length=256, center=False, win_length=256)
          M = abs(spectrogram)
          audio_array.append(M)
          phoneme_array.append(row['phoneme'])

# + id="xoK_YRZKewOi"
# one_hot = np.eye(40)

# + id="WM6eNxb6e7lS"
# one_hot_dict = {}
# for idx, ph in enumerate(set(phoneme_array)):
#   one_hot_dict[ph] = one_hot[idx,:]

# + id="z3hJRr7DLajZ"
params = {'learning_rate':0.1, 'verbose':True}

# + id="s3FXM9sgoGnE"
final_audio = np.array(audio_array)
final_audio = final_audio.reshape(final_audio.shape[0], final_audio.shape[1])

# + id="Kyl6hsbkL1tg"
label_encoder = LabelEncoder()
label_encoder = label_encoder.fit(phoneme_array)
label_encoded_y = label_encoder.transform(phoneme_array)

# + id="M-bjrOTlrecF"
train_X, test_X, train_Y, test_Y = train_test_split(final_audio, label_encoded_y, test_size=0.25, random_state=11755)

# + colab={"base_uri": "https://localhost:8080/"} id="8g_xkrBKr0q6" outputId="886d4f23-07af-40ed-a905-f5db55441ad9"
eval_set = [(train_X, train_Y), (test_X, test_Y)]
model = xgboost.XGBClassifier(**params)
model.fit(train_X, train_Y, early_stopping_rounds=2, eval_metric=["merror"], eval_set=eval_set, verbose=True)

# + id="MszYC2DYfpTs"
# fit model no training data
model = xgboost.XGBClassifier(**params)
model.fit(final_audio, label_encoded_y, verbose=True)

# + id="x3YQkmuUw3qz"
model.save_model("/content/gdrive/MyDrive/xgboost_model.json")

# + id="anRJZbfppOE5"
# new_phoneme = []
# for ph in phoneme_array:
#   new_phoneme.append(int(np.where(ph==1.)[0]))

# + id="WbYO980yqSkJ"
preds = model.predict(final_audio[:1000])

# + colab={"base_uri": "https://localhost:8080/"} id="SdEaEcc0__8w" outputId="11ded708-ec23-40da-b492-ef2673935b0c"
sum(label_encoded_y[:1000] == preds)

# + colab={"base_uri": "https://localhost:8080/"} id="pChUcscHBKy4" outputId="b00fb7ae-2806-4f44-85df-f705f0f9a1ec"
525/1000

# + id="_lG7GGwUBRAG"
audio_array_test = []
phoneme_array_test = []

# + id="UnKGhXcxQLaR"
for sp in test_speaker:
  for sig in os.listdir("/content/nus-smc-corpus_48/"+str(sp)+"/read/"):
    if '.wav' in sig:
      song = sig.replace('.wav','')
      df = pd.read_csv("/content/nus-smc-corpus_48/"+str(sp)+"/read/"+str(song)+".txt", delimiter=" ", header=None, names=['start', 'end', 'phoneme'])
      audio, sr = librosa.load("/content/nus-smc-corpus_48/"+str(sp)+"/read/"+str(sig), sr=None)
      audio = audio[:int(df['end'].iloc[-1]*sr)]

      for idx, row in df.iterrows():
        for i in range(int(row['start']*100),int(row['end']*100)):
          reqd_audio = audio[int(i/100*sr):int((i+1)/100*sr)]
          spectrogram = librosa.stft(reqd_audio, n_fft=256, hop_length=256, center=False, win_length=256)
          M = abs(spectrogram)
          audio_array_test.append(M)
          phoneme_array_test.append(row['phoneme'])

# + id="L5el2Az1QV1_"
final_audio_test = np.array(audio_array_test)
final_audio_test = final_audio_test.reshape(final_audio_test.shape[0], final_audio_test.shape[1])
label_encoded_y_test = label_encoder.transform(phoneme_array_test)

# + id="ZE0dE1V6Qj8v"
preds_test = model.predict(final_audio_test)

# + colab={"base_uri": "https://localhost:8080/"} id="2PhWwP-HQvTy" outputId="ba5d46b1-e7eb-4a3c-9d51-6fd3ab5f9786"
sum(preds_test==label_encoded_y_test)/len(label_encoded_y_test)

# + colab={"base_uri": "https://localhost:8080/"} id="ifIVeZmtQ2xG" outputId="6d262b53-fa5f-4ba4-c208-968ac97acfe5"
preds

# + id="_HyvwbeSa_gb"


StarcoderdataJupyterScriptsDedupFiltered