{"metadata":{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import random\nimport librosa\nimport scipy\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\nimport IPython.display as ipd\nimport cv2\nimport torch\nimport torchaudio\nfrom torchaudio import transforms\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-25T04:35:59.154545Z","iopub.execute_input":"2023-09-25T04:35:59.155227Z","iopub.status.idle":"2023-09-25T04:36:04.102449Z","shell.execute_reply.started":"2023-09-25T04:35:59.15516Z","shell.execute_reply":"2023-09-25T04:36:04.101283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_path = '/kaggle/input/speech-audio/speech.wav'","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2023-09-25T04:40:36.523121Z","iopub.execute_input":"2023-09-25T04:40:36.523585Z","iopub.status.idle":"2023-09-25T04:40:36.528477Z","shell.execute_reply.started":"2023-09-25T04:40:36.523547Z","shell.execute_reply":"2023-09-25T04:40:36.527481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wav, sr = librosa.load(file_path, sr=None)\nprint(wav.shape, wav.max(), wav.min())\nipd.Audio(file_path)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T04:40:39.108441Z","iopub.execute_input":"2023-09-25T04:40:39.109037Z","iopub.status.idle":"2023-09-25T04:40:39.175373Z","shell.execute_reply.started":"2023-09-25T04:40:39.108996Z","shell.execute_reply":"2023-09-25T04:40:39.174117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_spectrogram(wav):\n    plotter = librosa.stft(wav, n_fft=480, hop_length=160,win_length=480, window='hamming')\n    spect, phase = librosa.magphase(plotter)\n    return spect","metadata":{"execution":{"iopub.status.busy":"2023-09-25T04:40:42.270704Z","iopub.execute_input":"2023-09-25T04:40:42.271519Z","iopub.status.idle":"2023-09-25T04:40:42.277563Z","shell.execute_reply.started":"2023-09-25T04:40:42.271467Z","shell.execute_reply":"2023-09-25T04:40:42.276582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio,sr = torchaudio.load(file_path)\nsample=(audio,sr)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-25T04:40:48.908988Z","iopub.execute_input":"2023-09-25T04:40:48.909667Z","iopub.status.idle":"2023-09-25T04:40:48.924181Z","shell.execute_reply.started":"2023-09-25T04:40:48.909568Z","shell.execute_reply":"2023-09-25T04:40:48.922724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tfm_spectro(ad, sr=16000, to_db_scale=False, n_fft=1024, \n                ws=None, hop=None, f_min=0.0, f_max=-80, pad=0, n_mels=128):\n    mel = transforms.MelSpectrogram(sample_rate=ad[1], n_mels=n_mels, n_fft=n_fft, hop_length=hop, \n                                    f_min=f_min, f_max=f_max, pad=pad,)(ad[0].reshape(1, -1))\n    mel = mel.permute(0,2,1)\n    if to_db_scale: mel = transforms.AmplitudeToDB(stype='magnitude', top_db=f_max)(mel)\n    return mel\nspectro = tfm_spectro(sample, ws=512, hop=256, n_mels=128, to_db_scale=True, f_max=8000, f_min=-80)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T04:40:53.463297Z","iopub.execute_input":"2023-09-25T04:40:53.463889Z","iopub.status.idle":"2023-09-25T04:40:53.70564Z","shell.execute_reply.started":"2023-09-25T04:40:53.463849Z","shell.execute_reply":"2023-09-25T04:40:53.704722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tensor_to_img(spectrogram, title): \n    plt.imshow(spectrogram[0],aspect='auto', origin='lower')\n    plt.xlabel(\"time\")\n    plt.ylabel(\"frequency\")\n    plt.title(title)\n    plt.show();\n    display(spectrogram.shape)\ntensor_to_img(spectro, \"Original Audio\")","metadata":{"execution":{"iopub.status.busy":"2023-09-25T05:01:01.171374Z","iopub.execute_input":"2023-09-25T05:01:01.171822Z","iopub.status.idle":"2023-09-25T05:01:01.452852Z","shell.execute_reply.started":"2023-09-25T05:01:01.171783Z","shell.execute_reply":"2023-09-25T05:01:01.452026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def freq_mask(spec, F=250, num_masks=1, start_freq=None, end_freq=None):\n    test = spec.clone()\n    num_mel_channels = test.shape[1]\n    \n    if start_freq is None:\n        start_freq = 0\n    if end_freq is None or end_freq > num_mel_channels:\n        end_freq = F\n\n    if start_freq > end_freq:\n        raise ValueError(\"start_freq should be less than or equal to end_freq\")\n\n    for i in range(0, num_masks):\n        if start_freq + F > num_mel_channels:\n            F = num_mel_channels - start_freq\n        \n        zero = start_freq\n        mask_end = min(zero + F, end_freq)\n        if mask_end <= zero:\n            return test\n\n        test[0][zero:mask_end] = test.mean()\n    return test","metadata":{"execution":{"iopub.status.busy":"2023-09-25T04:59:02.756497Z","iopub.execute_input":"2023-09-25T04:59:02.756951Z","iopub.status.idle":"2023-09-25T04:59:02.769453Z","shell.execute_reply.started":"2023-09-25T04:59:02.756916Z","shell.execute_reply":"2023-09-25T04:59:02.768209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_freq_mask():\n    print('Original')\n    tensor_to_img(spectro, \"Original Audio\")\n    print('5 masks')\n    tensor_to_img(freq_mask(spectro, num_masks=5, start_freq=0, F=750), \"Frequency masked\")\ntest_freq_mask()","metadata":{"execution":{"iopub.status.busy":"2023-09-25T05:04:33.544515Z","iopub.execute_input":"2023-09-25T05:04:33.544978Z","iopub.status.idle":"2023-09-25T05:04:34.072447Z","shell.execute_reply.started":"2023-09-25T05:04:33.544939Z","shell.execute_reply":"2023-09-25T05:04:34.071474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def time_mask(spec, T=60, num_masks=1, start_time=None):\n    test = spec.clone()\n    length = test.shape[2]\n    print(length)\n    \n    if start_time is None:\n        start_time = 0\n    if end_time is None or end_time > length:\n        end_time = T\n\n    if start_time > end_time:\n        raise ValueError(\"start_time should be less than or equal to end_time\")\n\n    for i in range(0, num_masks):\n        if start_time + T > length:\n            T = length - start_time\n        zero = start_time\n        mask_end = min(zero + T, end_time)\n        if mask_end <= zero:\n            return test\n        test[0][:,zero:mask_end] = test.mean()\n    return test","metadata":{"execution":{"iopub.status.busy":"2023-09-25T04:57:59.21852Z","iopub.execute_input":"2023-09-25T04:57:59.218968Z","iopub.status.idle":"2023-09-25T04:57:59.231902Z","shell.execute_reply.started":"2023-09-25T04:57:59.218934Z","shell.execute_reply":"2023-09-25T04:57:59.230724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_time_mask():\n    tensor_to_img(spectro, \"Original Audio\")\n    print('Two Mask')\n    tensor_to_img(time_mask(spectro, num_masks=1, start_time=0, T=60), \"Time masked\")\ntest_time_mask()","metadata":{"execution":{"iopub.status.busy":"2023-09-25T05:04:23.372441Z","iopub.execute_input":"2023-09-25T05:04:23.372899Z","iopub.status.idle":"2023-09-25T05:04:23.903349Z","shell.execute_reply.started":"2023-09-25T05:04:23.372848Z","shell.execute_reply":"2023-09-25T05:04:23.902068Z"},"trusted":true},"execution_count":null,"outputs":[]}]}