{"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":"!pip install soundfile","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-16T15:05:38.777426Z","iopub.execute_input":"2022-02-16T15:05:38.777734Z","iopub.status.idle":"2022-02-16T15:05:49.514342Z","shell.execute_reply.started":"2022-02-16T15:05:38.777648Z","shell.execute_reply":"2022-02-16T15:05:49.513781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Objective \n\nLoading and preprocessing data to spectogram","metadata":{}},{"cell_type":"markdown","source":"# Convert Audio to Frequency","metadata":{}},{"cell_type":"code","source":"import soundfile as sf\nfreq,rate=sf.read('../input/birdclef-2022/train_audio/afrsil1/XC125458.ogg')\nimport plotly.express as px\nimport numpy as np\npx.line(x=np.array(list(range(len(freq)))),y=freq)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T15:05:49.515575Z","iopub.execute_input":"2022-02-16T15:05:49.515743Z","iopub.status.idle":"2022-02-16T15:05:55.685464Z","shell.execute_reply.started":"2022-02-16T15:05:49.515723Z","shell.execute_reply":"2022-02-16T15:05:55.684446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sound","metadata":{}},{"cell_type":"code","source":"import IPython\nIPython.display.Audio(\"../input/birdclef-2022/train_audio/afrsil1/XC125458.ogg\")","metadata":{"execution":{"iopub.status.busy":"2022-02-16T15:05:55.686786Z","iopub.execute_input":"2022-02-16T15:05:55.687018Z","iopub.status.idle":"2022-02-16T15:05:55.699606Z","shell.execute_reply.started":"2022-02-16T15:05:55.686988Z","shell.execute_reply":"2022-02-16T15:05:55.698462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Remove the noise from the sound","metadata":{}},{"cell_type":"code","source":"!pip install noisereduce","metadata":{"execution":{"iopub.status.busy":"2022-02-16T15:05:55.701440Z","iopub.execute_input":"2022-02-16T15:05:55.702207Z","iopub.status.idle":"2022-02-16T15:06:05.266944Z","shell.execute_reply.started":"2022-02-16T15:05:55.702122Z","shell.execute_reply":"2022-02-16T15:06:05.265585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import noisereduce as nr\nreduced_noise=nr.reduce_noise(y=freq,sr=rate)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T15:06:05.270515Z","iopub.execute_input":"2022-02-16T15:06:05.270746Z","iopub.status.idle":"2022-02-16T15:06:07.490435Z","shell.execute_reply.started":"2022-02-16T15:06:05.270724Z","shell.execute_reply":"2022-02-16T15:06:07.489769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.line(x=np.array(list(range(len(reduced_noise)))),y=reduced_noise)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T15:06:07.491599Z","iopub.execute_input":"2022-02-16T15:06:07.491794Z","iopub.status.idle":"2022-02-16T15:06:10.491202Z","shell.execute_reply.started":"2022-02-16T15:06:07.491765Z","shell.execute_reply":"2022-02-16T15:06:10.490465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wavio\nwavio.write(\"myfile.wav\",reduced_noise,rate,sampwidth=2)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T15:06:10.492366Z","iopub.execute_input":"2022-02-16T15:06:10.492679Z","iopub.status.idle":"2022-02-16T15:06:10.509386Z","shell.execute_reply.started":"2022-02-16T15:06:10.492649Z","shell.execute_reply":"2022-02-16T15:06:10.508862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IPython.display.Audio(\"myfile.wav\")","metadata":{"execution":{"iopub.status.busy":"2022-02-16T15:06:10.510450Z","iopub.execute_input":"2022-02-16T15:06:10.510742Z","iopub.status.idle":"2022-02-16T15:06:10.527741Z","shell.execute_reply.started":"2022-02-16T15:06:10.510717Z","shell.execute_reply":"2022-02-16T15:06:10.527162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convert to spectogram using tfio [chanage params here to reduce memory usage]","metadata":{}},{"cell_type":"code","source":"!pip install tensorflow-io","metadata":{"execution":{"iopub.status.busy":"2022-02-16T15:06:10.528707Z","iopub.execute_input":"2022-02-16T15:06:10.528967Z","iopub.status.idle":"2022-02-16T15:06:20.065839Z","shell.execute_reply.started":"2022-02-16T15:06:10.528944Z","shell.execute_reply":"2022-02-16T15:06:20.064918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_io as tfio\nimport matplotlib.pyplot as plt\n# Convert to spectrogram\nspectrogram = tfio.audio.spectrogram(\n    reduced_noise, nfft=3600, window=256, stride=256)\nimport tensorflow as tf\nplt.figure()\nplt.imshow(tf.math.log(spectrogram).numpy())","metadata":{"execution":{"iopub.status.busy":"2022-02-16T15:07:21.077814Z","iopub.execute_input":"2022-02-16T15:07:21.078110Z","iopub.status.idle":"2022-02-16T15:07:22.884960Z","shell.execute_reply.started":"2022-02-16T15:07:21.078079Z","shell.execute_reply":"2022-02-16T15:07:22.884448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Finally a function to convert an ogg file to spectogram","metadata":{}},{"cell_type":"code","source":"def read(pth,reduce_noise):\n    freq,rate=sf.read(pth)\n    if reduce_noise:\n        freq=nr.reduce_noise(y=freq,sr=rate)\n    # Convert to spectrogram\n    spectrogram = tfio.audio.spectrogram(\n    reduced_noise, nfft=3600, window=256, stride=256)\n    return tf.math.log(spectrogram).numpy()\nplt.imshow(read('../input/birdclef-2022/train_audio/afrsil1/XC175522.ogg',True))","metadata":{"execution":{"iopub.status.busy":"2022-02-16T15:07:26.275972Z","iopub.execute_input":"2022-02-16T15:07:26.276929Z","iopub.status.idle":"2022-02-16T15:07:28.942511Z","shell.execute_reply.started":"2022-02-16T15:07:26.276881Z","shell.execute_reply":"2022-02-16T15:07:28.941455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Thank you upvote if u like work in progress","metadata":{}}]}