{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import Libraries & Load Dataset","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pylab as plt\nimport seaborn as sns\n\nfrom glob import glob\n\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\n\nfrom itertools import cycle\n\nsns.set_theme(style=\"white\", palette=None)\ncolor_pal = plt.rcParams[\"axes.prop_cycle\"].by_key()[\"color\"]\ncolor_cycle = cycle(plt.rcParams[\"axes.prop_cycle\"].by_key()[\"color\"])","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:18:02.874542Z","iopub.execute_input":"2023-09-01T05:18:02.875104Z","iopub.status.idle":"2023-09-01T05:18:02.882306Z","shell.execute_reply.started":"2023-09-01T05:18:02.875070Z","shell.execute_reply":"2023-09-01T05:18:02.881426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_files = glob(\"/kaggle/input/bengaliai-speech/train_mp3s/*.mp3\")","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:30:14.887211Z","iopub.execute_input":"2023-09-01T05:30:14.887657Z","iopub.status.idle":"2023-09-01T05:30:24.638908Z","shell.execute_reply.started":"2023-09-01T05:30:14.887596Z","shell.execute_reply":"2023-09-01T05:30:24.637914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Play audio file\nipd.Audio(audio_files[0])","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:30:42.510267Z","iopub.execute_input":"2023-09-01T05:30:42.510670Z","iopub.status.idle":"2023-09-01T05:30:42.549527Z","shell.execute_reply.started":"2023-09-01T05:30:42.510639Z","shell.execute_reply":"2023-09-01T05:30:42.548453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y, sr = librosa.load(audio_files[0])\nprint(f'y: {y[:10]}')\nprint(f'shape y: {y.shape}')\nprint(f'sr: {sr}')","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:31:07.922252Z","iopub.execute_input":"2023-09-01T05:31:07.922664Z","iopub.status.idle":"2023-09-01T05:31:20.099069Z","shell.execute_reply.started":"2023-09-01T05:31:07.922603Z","shell.execute_reply":"2023-09-01T05:31:20.096938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(y).plot(figsize=(10, 5),\n                  lw=1,\n                  title='Raw Audio Example',\n                 color=color_pal[0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:31:31.166410Z","iopub.execute_input":"2023-09-01T05:31:31.167085Z","iopub.status.idle":"2023-09-01T05:31:31.626667Z","shell.execute_reply.started":"2023-09-01T05:31:31.167049Z","shell.execute_reply":"2023-09-01T05:31:31.625817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Trimming leading/lagging silence\ny_trimmed, _ = librosa.effects.trim(y, top_db=20)\npd.Series(y_trimmed).plot(figsize=(10, 5),\n                  lw=1,\n                  title='Raw Audio Trimmed Example',\n                 color=color_pal[1])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:31:45.113765Z","iopub.execute_input":"2023-09-01T05:31:45.114148Z","iopub.status.idle":"2023-09-01T05:31:47.647184Z","shell.execute_reply.started":"2023-09-01T05:31:45.114119Z","shell.execute_reply":"2023-09-01T05:31:47.646078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(y[30000:30500]).plot(figsize=(10, 5),\n                  lw=1,\n                  title='Raw Audio Zoomed In Example',\n                 color=color_pal[2])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:32:02.654353Z","iopub.execute_input":"2023-09-01T05:32:02.654751Z","iopub.status.idle":"2023-09-01T05:32:02.987532Z","shell.execute_reply.started":"2023-09-01T05:32:02.654721Z","shell.execute_reply":"2023-09-01T05:32:02.986356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Spectogram","metadata":{}},{"cell_type":"code","source":"D = librosa.stft(y)\nS_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)\nS_db.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:32:17.091785Z","iopub.execute_input":"2023-09-01T05:32:17.092218Z","iopub.status.idle":"2023-09-01T05:32:17.112751Z","shell.execute_reply.started":"2023-09-01T05:32:17.092182Z","shell.execute_reply":"2023-09-01T05:32:17.111574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the transformed audio data\nfig, ax = plt.subplots(figsize=(10, 5))\nimg = librosa.display.specshow(S_db,\n                              x_axis='time',\n                              y_axis='log',\n                              ax=ax)\nax.set_title('Spectogram Example', fontsize=20)\nfig.colorbar(img, ax=ax, format=f'%0.2f')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:32:53.365235Z","iopub.execute_input":"2023-09-01T05:32:53.365649Z","iopub.status.idle":"2023-09-01T05:32:53.976679Z","shell.execute_reply.started":"2023-09-01T05:32:53.365603Z","shell.execute_reply":"2023-09-01T05:32:53.975736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Mel Spectogram","metadata":{}},{"cell_type":"code","source":"S = librosa.feature.melspectrogram(y=y,\n                                   sr=sr,\n                                   n_mels=128 * 2,)\nS_db_mel = librosa.amplitude_to_db(S, ref=np.max)","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:33:21.053600Z","iopub.execute_input":"2023-09-01T05:33:21.054158Z","iopub.status.idle":"2023-09-01T05:33:21.075445Z","shell.execute_reply.started":"2023-09-01T05:33:21.054119Z","shell.execute_reply":"2023-09-01T05:33:21.074672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(10, 5))\n# Plot the mel spectogram\nimg = librosa.display.specshow(S_db_mel,\n                              x_axis='time',\n                              y_axis='log',\n                              ax=ax)\nax.set_title('Mel Spectogram Example', fontsize=20)\nfig.colorbar(img, ax=ax, format=f'%0.2f')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:33:30.895373Z","iopub.execute_input":"2023-09-01T05:33:30.896135Z","iopub.status.idle":"2023-09-01T05:33:31.420332Z","shell.execute_reply.started":"2023-09-01T05:33:30.896099Z","shell.execute_reply":"2023-09-01T05:33:31.419253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Mel-Frequency Cepstral Coefficients","metadata":{}},{"cell_type":"markdown","source":"## Extracting MFCCs","metadata":{}},{"cell_type":"code","source":"mfccs = librosa.feature.mfcc(y=y, n_mfcc=13, sr=sr)","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:53:04.473974Z","iopub.execute_input":"2023-09-01T05:53:04.474384Z","iopub.status.idle":"2023-09-01T05:53:04.509603Z","shell.execute_reply.started":"2023-09-01T05:53:04.474354Z","shell.execute_reply":"2023-09-01T05:53:04.508425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfccs.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:53:24.142585Z","iopub.execute_input":"2023-09-01T05:53:24.143786Z","iopub.status.idle":"2023-09-01T05:53:24.150711Z","shell.execute_reply.started":"2023-09-01T05:53:24.143744Z","shell.execute_reply":"2023-09-01T05:53:24.149588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualising MFCCs","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(25, 10))\nlibrosa.display.specshow(mfccs, \n                         x_axis=\"time\", \n                         sr=sr)\nplt.colorbar(format=\"%+2.f\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-01T05:54:11.618184Z","iopub.execute_input":"2023-09-01T05:54:11.618578Z","iopub.status.idle":"2023-09-01T05:54:12.033749Z","shell.execute_reply.started":"2023-09-01T05:54:11.618548Z","shell.execute_reply":"2023-09-01T05:54:12.032630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}}