{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":52324,"databundleVersionId":6229904,"sourceType":"competition"},{"sourceId":8337904,"sourceType":"datasetVersion","datasetId":4951833}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import warnings\n\nwarnings.filterwarnings('ignore')\n\nimport numpy as np  # linear algebra\nimport pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\nimport librosa\nimport librosa.display\n\nfrom IPython.display import Audio, IFrame, display\n\nfrom pydub import AudioSegment\nfrom pydub.silence import split_on_silence\nimport random\nfrom scipy import signal","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","ExecuteTime":{"end_time":"2024-05-01T14:59:01.013104Z","start_time":"2024-05-01T14:59:01.009980Z"},"execution":{"iopub.status.busy":"2024-06-08T16:55:58.017120Z","iopub.execute_input":"2024-06-08T16:55:58.017562Z","iopub.status.idle":"2024-06-08T16:56:00.566637Z","shell.execute_reply.started":"2024-06-08T16:55:58.017524Z","shell.execute_reply":"2024-06-08T16:56:00.565212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asr_dataset_1 = pd.read_csv('/kaggle/input/asr-dset-file/asr_dset_w_len.csv')\nprint(asr_dataset_1.columns)\nasr_dataset_1.head(5)","metadata":{"ExecuteTime":{"end_time":"2024-05-01T15:45:52.586031Z","start_time":"2024-05-01T15:45:49.585997Z"},"execution":{"iopub.status.busy":"2024-06-08T16:56:00.568976Z","iopub.execute_input":"2024-06-08T16:56:00.569493Z","iopub.status.idle":"2024-06-08T16:56:09.311998Z","shell.execute_reply.started":"2024-06-08T16:56:00.569459Z","shell.execute_reply":"2024-06-08T16:56:09.310716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asr_dataset_1.shape","metadata":{"ExecuteTime":{"end_time":"2024-05-01T15:46:03.174133Z","start_time":"2024-05-01T15:46:03.171538Z"},"execution":{"iopub.status.busy":"2024-05-27T10:16:19.314946Z","iopub.status.idle":"2024-05-27T10:16:19.315359Z","shell.execute_reply.started":"2024-05-27T10:16:19.315158Z","shell.execute_reply":"2024-05-27T10:16:19.315184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data = asr_dataset_1.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.317160Z","iopub.status.idle":"2024-05-27T10:16:19.317657Z","shell.execute_reply.started":"2024-05-27T10:16:19.317377Z","shell.execute_reply":"2024-05-27T10:16:19.317398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data.to_csv('sample_data.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.319043Z","iopub.status.idle":"2024-05-27T10:16:19.319427Z","shell.execute_reply.started":"2024-05-27T10:16:19.319245Z","shell.execute_reply":"2024-05-27T10:16:19.319261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.320423Z","iopub.status.idle":"2024-05-27T10:16:19.320805Z","shell.execute_reply.started":"2024-05-27T10:16:19.320628Z","shell.execute_reply":"2024-05-27T10:16:19.320644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from datasets import Dataset\n# import datasets\n# \n# train_dataset = Dataset.from_pandas(asr_dataset_1).cast_column('local_path', datasets.Audio(sampling_rate=2205))","metadata":{"ExecuteTime":{"end_time":"2024-05-01T15:50:23.100529Z","start_time":"2024-05-01T15:50:21.748716Z"},"execution":{"iopub.status.busy":"2024-05-27T10:16:19.322136Z","iopub.status.idle":"2024-05-27T10:16:19.322507Z","shell.execute_reply.started":"2024-05-27T10:16:19.322329Z","shell.execute_reply":"2024-05-27T10:16:19.322345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_audio(path):\n    fig, axs = plt.subplots(3, 1, figsize=(10, 10), sharex=True)\n    fig.subplots_adjust(hspace=0.5)\n    \n    try :\n        y, sr = librosa.load(path)\n        print(y)\n        \n        mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr)\n        mel_spectrogram_db = librosa.power_to_db(mel_spectrogram, ref=np.max)\n        mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=19)        \n\n        m_sp_img = librosa.display.specshow(mel_spectrogram_db, x_axis='time', ax=axs[0], y_axis='mel')\n        axs[0].set_title('Mel spectrogram')\n        fig.colorbar(m_sp_img, ax=axs[0], format=\"%+2.f dB\")\n\n        mfcc_img = librosa.display.specshow(mfccs, x_axis='time', ax=axs[1], y_axis='mel')\n        axs[1].set_title('MFCC')\n        fig.colorbar(mfcc_img, ax=axs[1], format=\"%+2.f dB\")\n       \n        librosa.display.waveshow(y, sr=sr, ax=axs[2], color='blue') \n\n    except Exception as e:\n        print(e)\n        \n    display(Audio(path))\n        ","metadata":{"ExecuteTime":{"end_time":"2024-05-01T16:36:39.495559Z","start_time":"2024-05-01T16:36:39.489008Z"},"execution":{"iopub.status.busy":"2024-05-27T10:16:19.323956Z","iopub.status.idle":"2024-05-27T10:16:19.324557Z","shell.execute_reply.started":"2024-05-27T10:16:19.324354Z","shell.execute_reply":"2024-05-27T10:16:19.324372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    asr_dataset_1.iloc[i]['path']","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2024-05-01T16:36:44.388326Z","start_time":"2024-05-01T16:36:40.688624Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-05-27T10:16:19.326789Z","iopub.status.idle":"2024-05-27T10:16:19.327193Z","shell.execute_reply.started":"2024-05-27T10:16:19.326990Z","shell.execute_reply":"2024-05-27T10:16:19.327007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asr_dataset_1['length'].describe()","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2024-05-01T15:53:53.675370Z","start_time":"2024-05-01T15:53:53.632132Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-05-27T10:16:19.328721Z","iopub.status.idle":"2024-05-27T10:16:19.329094Z","shell.execute_reply.started":"2024-05-27T10:16:19.328913Z","shell.execute_reply":"2024-05-27T10:16:19.328930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\nsns.histplot(asr_dataset_1['length'])","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2024-05-01T15:58:58.348246Z","start_time":"2024-05-01T15:58:57.573248Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-05-27T10:16:19.330290Z","iopub.status.idle":"2024-05-27T10:16:19.330995Z","shell.execute_reply.started":"2024-05-27T10:16:19.330743Z","shell.execute_reply":"2024-05-27T10:16:19.330767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len_greater_than_20 = asr_dataset_1[asr_dataset_1['length'] > 20]\nlen(len_greater_than_20)","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2024-05-01T15:58:59.168802Z","start_time":"2024-05-01T15:58:59.165769Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-05-27T10:16:19.332130Z","iopub.status.idle":"2024-05-27T10:16:19.332495Z","shell.execute_reply.started":"2024-05-27T10:16:19.332326Z","shell.execute_reply":"2024-05-27T10:16:19.332341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"S1 = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=64)\nD1 = librosa.power_to_db(S1, ref=np.max)\nlibrosa.display.specshow(D1, x_axis='time', y_axis='mel');","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.333674Z","iopub.status.idle":"2024-05-27T10:16:19.334027Z","shell.execute_reply.started":"2024-05-27T10:16:19.333857Z","shell.execute_reply":"2024-05-27T10:16:19.333872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for row in len_greater_than_20.iterrows():\n    print(row[1]['path'])\n    plot_audio(row[1]['path'])","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2024-05-01T16:37:26.760830Z","start_time":"2024-05-01T16:37:11.403983Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-05-27T10:16:19.336634Z","iopub.status.idle":"2024-05-27T10:16:19.336996Z","shell.execute_reply.started":"2024-05-27T10:16:19.336831Z","shell.execute_reply":"2024-05-27T10:16:19.336845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"less_than_one = asr_dataset_1[asr_dataset_1.length < 1]\nless_than_one.size","metadata":{"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-05-27T10:16:19.337704Z","iopub.status.idle":"2024-05-27T10:16:19.338053Z","shell.execute_reply.started":"2024-05-27T10:16:19.337883Z","shell.execute_reply":"2024-05-27T10:16:19.337899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy import signal\n\ndef f_high(y, sr):\n    b, a = signal.butter(10, 2000 / (sr / 2), btype='highpass')\n    yf = signal.lfilter(b, a, y)\n    return yf\n","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.339386Z","iopub.status.idle":"2024-05-27T10:16:19.339775Z","shell.execute_reply.started":"2024-05-27T10:16:19.339592Z","shell.execute_reply":"2024-05-27T10:16:19.339608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for row in less_than_one.head(5).iterrows():\n    print(row[1]['path'])\n    plot_audio(row[1]['path'])","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.341749Z","iopub.status.idle":"2024-05-27T10:16:19.342367Z","shell.execute_reply.started":"2024-05-27T10:16:19.342165Z","shell.execute_reply":"2024-05-27T10:16:19.342188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"less_than_two = asr_dataset_1[asr_dataset_1.length < 2]\nless_than_two.size","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.343414Z","iopub.status.idle":"2024-05-27T10:16:19.344030Z","shell.execute_reply.started":"2024-05-27T10:16:19.343832Z","shell.execute_reply":"2024-05-27T10:16:19.343855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_samples = less_than_two.sample(n=10)","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.345110Z","iopub.status.idle":"2024-05-27T10:16:19.346027Z","shell.execute_reply.started":"2024-05-27T10:16:19.345839Z","shell.execute_reply":"2024-05-27T10:16:19.345857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for _, row in random_samples.iterrows():\n    plot_audio(row['path'])","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.346923Z","iopub.status.idle":"2024-05-27T10:16:19.347622Z","shell.execute_reply.started":"2024-05-27T10:16:19.347395Z","shell.execute_reply":"2024-05-27T10:16:19.347418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for row in less_than_two.head(5).iterrows():\n    print(row)\n    plot_audio(row[1]['path'])","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.348973Z","iopub.status.idle":"2024-05-27T10:16:19.349344Z","shell.execute_reply.started":"2024-05-27T10:16:19.349165Z","shell.execute_reply":"2024-05-27T10:16:19.349188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asr_dataset_1 = pd.read_csv('/kaggle/input/bengaliai-speech/train.csv')\nprint(asr_dataset_1.columns)\nasr_dataset_1.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.350596Z","iopub.status.idle":"2024-05-27T10:16:19.350950Z","shell.execute_reply.started":"2024-05-27T10:16:19.350782Z","shell.execute_reply":"2024-05-27T10:16:19.350797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install noisereduce\n!pip install librosa noisereduce soundfile pandas","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.352729Z","iopub.status.idle":"2024-05-27T10:16:19.353112Z","shell.execute_reply.started":"2024-05-27T10:16:19.352937Z","shell.execute_reply":"2024-05-27T10:16:19.352953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport librosa\nimport soundfile as sf\nimport noisereduce as nr\n\n# Define the paths based on your provided locations\naudio_dir = '/kaggle/input/bengaliai-speech/train_mp3s'\ncsv_path = '/kaggle/input/bengaliai-speech/train.csv'\ncleaned_dir = '/kaggle/working/output_noise_free' #without noise free audio\nflagged_dir = '/kaggle/working/red_noise' #detect noise & reduce noise then store \n\n# Ensure output directories exist\nos.makedirs(cleaned_dir, exist_ok=True)\nos.makedirs(flagged_dir, exist_ok=True)\n\n# Read the CSV file to get mappings\naudio_info = pd.read_csv(csv_path)\n\n# Counter for processed files\nprocessed_count = 0\nmax_files = 20000  # Maximum number of files to process\n\n# Process each audio file\nfor index, row in audio_info.iterrows():\n    if processed_count >= max_files:\n        break  # Stop processing after reaching the limit\n\n    audio_path = os.path.join(audio_dir, f\"{row['id']}.mp3\")\n    if os.path.exists(audio_path):\n        # Load audio file with librosa\n        data, rate = librosa.load(audio_path, sr=None)\n        # Noise reduction\n        reduced_noise = nr.reduce_noise(y=data, sr=rate)\n\n        # Check if noise is sufficiently reduced\n        if reduced_noise.std() < data.std() * 0.5:\n            save_path = os.path.join(cleaned_dir, f\"{row['id']}.wav\")\n            sf.write(save_path, reduced_noise, rate)\n            print(f\"Saved cleaned audio to {save_path}\")\n        else:\n            flagged_path = os.path.join(flagged_dir, f\"{row['id']}.wav\")\n            sf.write(flagged_path, data, rate)\n            print(f\"Flagged audio for review at {flagged_path}\")\n        processed_count += 1\n    else:\n        print(f\"Audio file {row['id']}.mp3 does not exist in the audio directory\")\n\nprint(\"Processing completed.\")\n","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.354417Z","iopub.status.idle":"2024-05-27T10:16:19.354799Z","shell.execute_reply.started":"2024-05-27T10:16:19.354624Z","shell.execute_reply":"2024-05-27T10:16:19.354639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport librosa\nimport soundfile as sf\nimport noisereduce as nr\n\n# Path definitions\nflagged_dir = '/kaggle/working/red_noise'\nreprocessed_dir = '/kaggle/working/reprocessed' # again cheaking for noise detection\nfinal_review_dir = '/kaggle/working/reprocessed/final_review_reprocessed' \n\n# Ensure directories exist\nos.makedirs(reprocessed_dir, exist_ok=True)\nos.makedirs(final_review_dir, exist_ok=True)\n\n# Reprocess each flagged audio file\nfor file_name in os.listdir(flagged_dir):\n    if file_name.endswith('.wav'):\n        file_path = os.path.join(flagged_dir, file_name)\n        data, rate = librosa.load(file_path, sr=None)\n\n        # Noise reduction with adjusted parameters\n        reduced_noise = nr.reduce_noise(y=data, sr=rate, n_fft=1024, hop_length=512, n_std_thresh_stationary=1.5)\n\n        # Evaluate the noise reduction; using a simple placeholder evaluation\n        if reduced_noise.std() < data.std() * 0.5:\n            save_path = os.path.join(reprocessed_dir, file_name)\n            sf.write(save_path, reduced_noise, rate)\n            print(f\"Reprocessed and saved audio to {save_path}\")\n        else:\n            final_review_path = os.path.join(final_review_dir, file_name)\n            sf.write(final_review_path, reduced_noise, rate)\n            print(f\"Audio moved to final review at {final_review_path}\")\n\nprint(\"Reprocessing completed.\")\n","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.355847Z","iopub.status.idle":"2024-05-27T10:16:19.356202Z","shell.execute_reply.started":"2024-05-27T10:16:19.356027Z","shell.execute_reply":"2024-05-27T10:16:19.356042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport librosa\nimport librosa.display\nimport matplotlib.pyplot as plt\nfrom IPython.display import Audio, display\nimport numpy as np\n\n# Define your directories\ncleaned_dir = '/kaggle/working/output_msc'\nfinal_review_dir = '/kaggle/working/reprocessed'\nos.makedirs(final_review_dir, exist_ok=True)\n\ndef plot_audio(path, save_path=None):\n    fig, axs = plt.subplots(3, 1, figsize=(10, 10), sharex=True)\n    fig.subplots_adjust(hspace=0.5)\n    \n    try:\n        # Load audio file\n        y, sr = librosa.load(path)\n        print(y)  # Output the waveform array values\n        \n        # Generate Mel Spectrogram\n        mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr)\n        mel_spectrogram_db = librosa.power_to_db(mel_spectrogram, ref=np.max)\n        \n        # Generate MFCCs\n        mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=19)\n        \n        # Plot Mel Spectrogram\n        m_sp_img = librosa.display.specshow(mel_spectrogram_db, x_axis='time', ax=axs[0], y_axis='mel')\n        axs[0].set_title('Mel Spectrogram')\n        fig.colorbar(m_sp_img, ax=axs[0], format=\"%+2.f dB\")\n\n        # Plot MFCC\n        mfcc_img = librosa.display.specshow(mfccs, x_axis='time', ax=axs[1], y_axis='mel')\n        axs[1].set_title('MFCC')\n        fig.colorbar(mfcc_img, ax=axs[1], format=\"%+2.f dB\")\n        \n        # Waveform\n        librosa.display.waveshow(y, sr=sr, ax=axs[2], color='blue')\n        axs[2].set_title('Waveform')\n\n        # Optionally save the plot to a file\n        if save_path:\n            plt.savefig(save_path)\n            plt.close()  # Close the plot to free up memory\n        else:\n            plt.show()\n        \n    except Exception as e:\n        print(\"Error processing file:\", e)\n\n    # Display audio player\n    display(Audio(path))\n\n# Process each audio file in the cleaned directory\nfor filename in os.listdir(cleaned_dir):\n    if filename.endswith('.wav'):\n        file_path = os.path.join(cleaned_dir, filename)\n        save_image_path = os.path.join(final_review_dir, f\"{filename[:-4]}_features.png\")\n        plot_audio(file_path, save_image_path)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-27T10:16:19.358595Z","iopub.status.idle":"2024-05-27T10:16:19.359186Z","shell.execute_reply.started":"2024-05-27T10:16:19.358992Z","shell.execute_reply":"2024-05-27T10:16:19.359013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}