{"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":"!ls ../input/birdsong-recognition/train_audio","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-18T11:18:40.764140Z","iopub.execute_input":"2021-07-18T11:18:40.764779Z","iopub.status.idle":"2021-07-18T11:18:41.570027Z","shell.execute_reply.started":"2021-07-18T11:18:40.764678Z","shell.execute_reply":"2021-07-18T11:18:41.568802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\nimport numpy as np\nimport librosa\nimport IPython.display as ipd\nfrom joblib import Parallel, delayed\nfrom functools import partial\n# import random\nimport tqdm\nimport tqdm.notebook\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-07-18T11:18:41.571993Z","iopub.execute_input":"2021-07-18T11:18:41.572461Z","iopub.status.idle":"2021-07-18T11:18:44.144168Z","shell.execute_reply.started":"2021-07-18T11:18:41.572418Z","shell.execute_reply":"2021-07-18T11:18:44.143060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths = [p for p in Path('../input/birdsong-recognition/train_audio').iterdir()]","metadata":{"execution":{"iopub.status.busy":"2021-07-18T11:18:44.146286Z","iopub.execute_input":"2021-07-18T11:18:44.146610Z","iopub.status.idle":"2021-07-18T11:18:44.153436Z","shell.execute_reply.started":"2021-07-18T11:18:44.146581Z","shell.execute_reply":"2021-07-18T11:18:44.152227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Select All birds","metadata":{}},{"cell_type":"code","source":"# random.seed(42)","metadata":{"execution":{"iopub.status.busy":"2021-07-18T11:18:44.157704Z","iopub.execute_input":"2021-07-18T11:18:44.158088Z","iopub.status.idle":"2021-07-18T11:18:44.165821Z","shell.execute_reply.started":"2021-07-18T11:18:44.158051Z","shell.execute_reply":"2021-07-18T11:18:44.164955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selected_birds = paths#random.sample(paths, 50)","metadata":{"execution":{"iopub.status.busy":"2021-07-18T11:18:44.168160Z","iopub.execute_input":"2021-07-18T11:18:44.168508Z","iopub.status.idle":"2021-07-18T11:18:44.180378Z","shell.execute_reply.started":"2021-07-18T11:18:44.168474Z","shell.execute_reply":"2021-07-18T11:18:44.179076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(selected_birds)","metadata":{"execution":{"iopub.status.busy":"2021-07-18T11:18:44.181879Z","iopub.execute_input":"2021-07-18T11:18:44.182227Z","iopub.status.idle":"2021-07-18T11:18:44.197691Z","shell.execute_reply.started":"2021-07-18T11:18:44.182183Z","shell.execute_reply":"2021-07-18T11:18:44.196537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convert audio to melspectogram and save","metadata":{}},{"cell_type":"code","source":"sampling_rate = 16000\nduration = 10","metadata":{"execution":{"iopub.status.busy":"2021-07-18T11:18:44.199153Z","iopub.execute_input":"2021-07-18T11:18:44.199482Z","iopub.status.idle":"2021-07-18T11:18:44.210922Z","shell.execute_reply.started":"2021-07-18T11:18:44.199450Z","shell.execute_reply":"2021-07-18T11:18:44.209920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_spectrum(sampling_rate, duration, bird_name, audio_path):\n    try:\n        data, _ = librosa.load(audio_path, sr=sampling_rate, duration=duration)\n        data = np.pad(data, (0, sampling_rate * duration - data.shape[0]), mode='symmetric')\n        spectogram = librosa.feature.melspectrogram(data.astype(np.float32), sr=sampling_rate, n_mels=128)\n        name = bird_name + audio_path.name\n        return (name, spectogram)\n    except ZeroDivisionError:\n        pass","metadata":{"execution":{"iopub.status.busy":"2021-07-18T11:18:44.213336Z","iopub.execute_input":"2021-07-18T11:18:44.213923Z","iopub.status.idle":"2021-07-18T11:18:44.224451Z","shell.execute_reply.started":"2021-07-18T11:18:44.213870Z","shell.execute_reply":"2021-07-18T11:18:44.223323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spectograms = []\nfor bird_path in tqdm.notebook.tqdm(selected_birds):\n    bird_name = bird_path.name\n    spectograms.extend(Parallel(n_jobs=4)(delayed(partial(get_spectrum, sampling_rate, duration, bird_name))(path) for path in bird_path.iterdir()))","metadata":{"execution":{"iopub.status.busy":"2021-07-18T11:18:44.226087Z","iopub.execute_input":"2021-07-18T11:18:44.226635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spectograms = dict(filter(bool, spectograms))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.savez_compressed('spectograms.npz', **spectograms)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -lh","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}