{"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":"%%sh\npip install audiomentations","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:34:47.051737Z","iopub.execute_input":"2023-10-04T02:34:47.052135Z","iopub.status.idle":"2023-10-04T02:34:58.577529Z","shell.execute_reply.started":"2023-10-04T02:34:47.052101Z","shell.execute_reply":"2023-10-04T02:34:58.576312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# audio stuff\nimport librosa\n\n# normal DS stuff\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns; sns.set()\n\n# stdlib stuff\nfrom glob import glob\nimport random\nimport os\nfrom pathlib import Path\n\n# display stuff\nfrom IPython.display import Audio","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-04T02:34:58.579438Z","iopub.execute_input":"2023-10-04T02:34:58.579794Z","iopub.status.idle":"2023-10-04T02:34:59.706405Z","shell.execute_reply.started":"2023-10-04T02:34:58.579766Z","shell.execute_reply":"2023-10-04T02:34:59.705618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SR = 32_000","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:50:06.837751Z","iopub.execute_input":"2023-10-04T02:50:06.838120Z","iopub.status.idle":"2023-10-04T02:50:06.843618Z","shell.execute_reply.started":"2023-10-04T02:50:06.838091Z","shell.execute_reply":"2023-10-04T02:50:06.842306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = Path(\"/kaggle/input/bengaliai-speech/train_mp3s\")","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:50:07.924700Z","iopub.execute_input":"2023-10-04T02:50:07.925326Z","iopub.status.idle":"2023-10-04T02:50:07.930121Z","shell.execute_reply.started":"2023-10-04T02:50:07.925284Z","shell.execute_reply":"2023-10-04T02:50:07.929268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files = glob(os.path.join(train_dir, \"*.mp3\"))","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:50:08.548921Z","iopub.execute_input":"2023-10-04T02:50:08.549318Z","iopub.status.idle":"2023-10-04T02:50:33.113320Z","shell.execute_reply.started":"2023-10-04T02:50:08.549289Z","shell.execute_reply":"2023-10-04T02:50:33.112105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_files = random.sample(train_files, 5)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:50:33.115174Z","iopub.execute_input":"2023-10-04T02:50:33.115508Z","iopub.status.idle":"2023-10-04T02:50:33.120984Z","shell.execute_reply.started":"2023-10-04T02:50:33.115478Z","shell.execute_reply":"2023-10-04T02:50:33.120096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wav, sr = librosa.load(random_files[0], sr=SR)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:51:13.535015Z","iopub.execute_input":"2023-10-04T02:51:13.535394Z","iopub.status.idle":"2023-10-04T02:51:13.554161Z","shell.execute_reply.started":"2023-10-04T02:51:13.535365Z","shell.execute_reply":"2023-10-04T02:51:13.552930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wav.shape, sr","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:51:14.277031Z","iopub.execute_input":"2023-10-04T02:51:14.277415Z","iopub.status.idle":"2023-10-04T02:51:14.285051Z","shell.execute_reply.started":"2023-10-04T02:51:14.277384Z","shell.execute_reply":"2023-10-04T02:51:14.283736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(data=wav, rate=SR)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:51:14.794804Z","iopub.execute_input":"2023-10-04T02:51:14.795191Z","iopub.status.idle":"2023-10-04T02:51:14.810409Z","shell.execute_reply.started":"2023-10-04T02:51:14.795161Z","shell.execute_reply":"2023-10-04T02:51:14.809344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# via here https://github.com/musikalkemist/audioDataAugmentationTutorial/blob/main/3/dataaugmentation.py\n\ndef add_white_noise(signal, noise_percentage_factor):\n    noise = np.random.normal(0, signal.std(), signal.size)\n    augmented_signal = signal + noise * noise_percentage_factor\n    return augmented_signal\n\n\ndef time_stretch(signal, time_stretch_rate):\n    \"\"\"Time stretching implemented with librosa:\n    https://librosa.org/doc/main/generated/librosa.effects.pitch_shift.html?highlight=pitch%20shift#librosa.effects.pitch_shift\n    \"\"\"\n    return librosa.effects.time_stretch(y=signal, rate=time_stretch_rate)\n\ndef pitch_scale(signal, sr, num_semitones):\n    \"\"\"Pitch scaling implemented with librosa:\n    https://librosa.org/doc/main/generated/librosa.effects.pitch_shift.html?highlight=pitch%20shift#librosa.effects.pitch_shift\n    \"\"\"\n    return librosa.effects.pitch_shift(y=signal, sr=sr, n_steps=num_semitones)\n\ndef random_gain(signal, min_factor=0.1, max_factor=0.12):\n    gain_rate = random.uniform(min_factor, max_factor)\n    augmented_signal = signal * gain_rate\n    return augmented_signal\n\n\ndef invert_polarity(signal):\n    return signal * -1","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:51:21.106412Z","iopub.execute_input":"2023-10-04T02:51:21.106820Z","iopub.status.idle":"2023-10-04T02:51:21.114327Z","shell.execute_reply.started":"2023-10-04T02:51:21.106786Z","shell.execute_reply":"2023-10-04T02:51:21.113032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(add_white_noise(wav, 0.2), rate=SR)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:51:24.691310Z","iopub.execute_input":"2023-10-04T02:51:24.691674Z","iopub.status.idle":"2023-10-04T02:51:24.711505Z","shell.execute_reply.started":"2023-10-04T02:51:24.691647Z","shell.execute_reply":"2023-10-04T02:51:24.710305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(time_stretch(wav, 0.8), rate=SR)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:51:29.238138Z","iopub.execute_input":"2023-10-04T02:51:29.238547Z","iopub.status.idle":"2023-10-04T02:51:29.342142Z","shell.execute_reply.started":"2023-10-04T02:51:29.238519Z","shell.execute_reply":"2023-10-04T02:51:29.341308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(pitch_scale(wav, SR, 3), rate=SR)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T03:00:15.003367Z","iopub.execute_input":"2023-10-04T03:00:15.003750Z","iopub.status.idle":"2023-10-04T03:00:15.423890Z","shell.execute_reply.started":"2023-10-04T03:00:15.003723Z","shell.execute_reply":"2023-10-04T03:00:15.422866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(pitch_scale(wav, SR, -3), rate=SR)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T03:00:15.425840Z","iopub.execute_input":"2023-10-04T03:00:15.426274Z","iopub.status.idle":"2023-10-04T03:00:15.512932Z","shell.execute_reply.started":"2023-10-04T03:00:15.426238Z","shell.execute_reply":"2023-10-04T03:00:15.511829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(random_gain(wav), rate=SR)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:52:13.363075Z","iopub.execute_input":"2023-10-04T02:52:13.363468Z","iopub.status.idle":"2023-10-04T02:52:13.378030Z","shell.execute_reply.started":"2023-10-04T02:52:13.363437Z","shell.execute_reply":"2023-10-04T02:52:13.376866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(invert_polarity(wav), rate=SR)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T03:00:37.553372Z","iopub.execute_input":"2023-10-04T03:00:37.553762Z","iopub.status.idle":"2023-10-04T03:00:37.567093Z","shell.execute_reply.started":"2023-10-04T03:00:37.553730Z","shell.execute_reply":"2023-10-04T03:00:37.566314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Using Audiomentations","metadata":{}},{"cell_type":"code","source":"# similar to CV library\nfrom audiomentations import (Compose, \n                             AddGaussianNoise, \n                             PitchShift,\n                             HighPassFilter, \n                             TimeStretch,\n                             Trim,\n                             Shift)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:52:20.169871Z","iopub.execute_input":"2023-10-04T02:52:20.170852Z","iopub.status.idle":"2023-10-04T02:52:20.175856Z","shell.execute_reply.started":"2023-10-04T02:52:20.170804Z","shell.execute_reply":"2023-10-04T02:52:20.174832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"augment = Compose([\n    AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.015, p=0.2),\n    TimeStretch(min_rate=0.8, max_rate=1.25, p=0.2),\n    PitchShift(min_semitones=-4, max_semitones=4, p=0.2),\n    Shift(p=0.2),\n    Trim(top_db=30.0, p=1.0)\n])","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:52:49.856384Z","iopub.execute_input":"2023-10-04T02:52:49.856783Z","iopub.status.idle":"2023-10-04T02:52:49.862542Z","shell.execute_reply.started":"2023-10-04T02:52:49.856751Z","shell.execute_reply":"2023-10-04T02:52:49.861540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(augment(wav, sample_rate=SR), rate=SR)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:52:50.023651Z","iopub.execute_input":"2023-10-04T02:52:50.024059Z","iopub.status.idle":"2023-10-04T02:52:50.137213Z","shell.execute_reply.started":"2023-10-04T02:52:50.024026Z","shell.execute_reply":"2023-10-04T02:52:50.136019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Try With English to See if These Examples Make Sense","metadata":{}},{"cell_type":"code","source":"librosa.util.list_examples()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:53:37.979065Z","iopub.execute_input":"2023-10-04T02:53:37.980273Z","iopub.status.idle":"2023-10-04T02:53:38.073777Z","shell.execute_reply.started":"2023-10-04T02:53:37.980225Z","shell.execute_reply":"2023-10-04T02:53:38.072967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eng_wav, sr = librosa.load(librosa.example(\"libri1\"))","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:54:43.417853Z","iopub.execute_input":"2023-10-04T02:54:43.418293Z","iopub.status.idle":"2023-10-04T02:54:43.438661Z","shell.execute_reply.started":"2023-10-04T02:54:43.418259Z","shell.execute_reply":"2023-10-04T02:54:43.437285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(eng_wav, rate=sr)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:54:59.502343Z","iopub.execute_input":"2023-10-04T02:54:59.502849Z","iopub.status.idle":"2023-10-04T02:54:59.527036Z","shell.execute_reply.started":"2023-10-04T02:54:59.502805Z","shell.execute_reply":"2023-10-04T02:54:59.525861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(add_white_noise(eng_wav, 0.2), rate=sr)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:56:04.628065Z","iopub.execute_input":"2023-10-04T02:56:04.628455Z","iopub.status.idle":"2023-10-04T02:56:04.664890Z","shell.execute_reply.started":"2023-10-04T02:56:04.628423Z","shell.execute_reply":"2023-10-04T02:56:04.663516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(time_stretch(eng_wav, 0.8), rate=sr)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:56:28.091972Z","iopub.execute_input":"2023-10-04T02:56:28.092370Z","iopub.status.idle":"2023-10-04T02:56:28.311903Z","shell.execute_reply.started":"2023-10-04T02:56:28.092340Z","shell.execute_reply":"2023-10-04T02:56:28.311082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(time_stretch(eng_wav, 1.2), rate=sr)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:57:26.634979Z","iopub.execute_input":"2023-10-04T02:57:26.635429Z","iopub.status.idle":"2023-10-04T02:57:26.812772Z","shell.execute_reply.started":"2023-10-04T02:57:26.635398Z","shell.execute_reply":"2023-10-04T02:57:26.811804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(pitch_scale(eng_wav, sr, 1), rate=sr)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:59:21.083541Z","iopub.execute_input":"2023-10-04T02:59:21.083923Z","iopub.status.idle":"2023-10-04T02:59:21.282873Z","shell.execute_reply.started":"2023-10-04T02:59:21.083892Z","shell.execute_reply":"2023-10-04T02:59:21.281672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(pitch_scale(eng_wav, sr, -1), rate=sr)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:59:21.285060Z","iopub.execute_input":"2023-10-04T02:59:21.285512Z","iopub.status.idle":"2023-10-04T02:59:21.472429Z","shell.execute_reply.started":"2023-10-04T02:59:21.285468Z","shell.execute_reply":"2023-10-04T02:59:21.471368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(pitch_scale(eng_wav, sr, 4), rate=sr)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:59:21.608388Z","iopub.execute_input":"2023-10-04T02:59:21.608769Z","iopub.status.idle":"2023-10-04T02:59:21.837873Z","shell.execute_reply.started":"2023-10-04T02:59:21.608737Z","shell.execute_reply":"2023-10-04T02:59:21.836887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(pitch_scale(eng_wav, sr, -4), rate=sr)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:59:22.779698Z","iopub.execute_input":"2023-10-04T02:59:22.780115Z","iopub.status.idle":"2023-10-04T02:59:22.941459Z","shell.execute_reply.started":"2023-10-04T02:59:22.780081Z","shell.execute_reply":"2023-10-04T02:59:22.940688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(random_gain(eng_wav), rate=sr)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T02:58:03.828352Z","iopub.execute_input":"2023-10-04T02:58:03.828726Z","iopub.status.idle":"2023-10-04T02:58:03.852354Z","shell.execute_reply.started":"2023-10-04T02:58:03.828698Z","shell.execute_reply":"2023-10-04T02:58:03.851362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Audio(invert_polarity(eng_wav), rate=sr)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T03:01:01.320854Z","iopub.execute_input":"2023-10-04T03:01:01.321525Z","iopub.status.idle":"2023-10-04T03:01:01.343888Z","shell.execute_reply.started":"2023-10-04T03:01:01.321486Z","shell.execute_reply":"2023-10-04T03:01:01.343083Z"},"trusted":true},"execution_count":null,"outputs":[]}]}