{"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":"markdown","source":"Work in Progress, Pretty Rough Right Now","metadata":{}},{"cell_type":"code","source":"!pip install librosa","metadata":{"execution":{"iopub.status.busy":"2022-02-23T11:56:55.329586Z","iopub.execute_input":"2022-02-23T11:56:55.330305Z","iopub.status.idle":"2022-02-23T11:57:06.261826Z","shell.execute_reply.started":"2022-02-23T11:56:55.330205Z","shell.execute_reply":"2022-02-23T11:57:06.260661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-02-23T11:57:06.465700Z","iopub.execute_input":"2022-02-23T11:57:06.466028Z","iopub.status.idle":"2022-02-23T11:57:06.470199Z","shell.execute_reply.started":"2022-02-23T11:57:06.465992Z","shell.execute_reply":"2022-02-23T11:57:06.469420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load a single audio file, for anaylsis:","metadata":{}},{"cell_type":"code","source":"import librosa\naudio_data = '../input/birdclef-2022/train_audio/afrsil1/XC317039.ogg'\nx , sr = librosa.load(audio_data)\nprint(type(x), type(sr))\nprint(x.shape, sr)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T12:10:20.921838Z","iopub.execute_input":"2022-02-23T12:10:20.922164Z","iopub.status.idle":"2022-02-23T12:10:22.444804Z","shell.execute_reply.started":"2022-02-23T12:10:20.922130Z","shell.execute_reply":"2022-02-23T12:10:22.443413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This returns an audio time series as a numpy array with a default sampling rate(sr) of 22KHZ mono. We can change this behavior by resampling at 44.1KHz\n\n-> Do, tell me if I should sample it at a different frequency or if there are some patterns i can follow on this step","metadata":{}},{"cell_type":"code","source":"librosa.load(audio_data, sr=44100)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T12:10:24.952118Z","iopub.execute_input":"2022-02-23T12:10:24.952786Z","iopub.status.idle":"2022-02-23T12:10:26.941124Z","shell.execute_reply.started":"2022-02-23T12:10:24.952748Z","shell.execute_reply":"2022-02-23T12:10:26.940261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's listen to the audio...*Gotta know what we are working with","metadata":{}},{"cell_type":"code","source":"import IPython.display as ipd\nipd.Audio(audio_data)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T12:10:26.942836Z","iopub.execute_input":"2022-02-23T12:10:26.943095Z","iopub.status.idle":"2022-02-23T12:10:26.963919Z","shell.execute_reply.started":"2022-02-23T12:10:26.943065Z","shell.execute_reply":"2022-02-23T12:10:26.963015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Finally, Lets Visualize","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\nimport matplotlib.pyplot as plt\nimport librosa.display\nplt.figure(figsize=(14, 5))\nlibrosa.display.waveshow(x, sr=sr)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T12:10:26.965477Z","iopub.execute_input":"2022-02-23T12:10:26.965896Z","iopub.status.idle":"2022-02-23T12:10:27.579133Z","shell.execute_reply.started":"2022-02-23T12:10:26.965858Z","shell.execute_reply":"2022-02-23T12:10:27.577430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Spectogram:","metadata":{}},{"cell_type":"code","source":"X = librosa.stft(x)\nXdb = librosa.amplitude_to_db(abs(X))\nplt.figure(figsize=(14, 5))\nlibrosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='hz')\nplt.colorbar()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T12:10:38.925394Z","iopub.execute_input":"2022-02-23T12:10:38.925709Z","iopub.status.idle":"2022-02-23T12:10:41.238795Z","shell.execute_reply.started":"2022-02-23T12:10:38.925673Z","shell.execute_reply":"2022-02-23T12:10:41.237687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import soundfile as sf","metadata":{"execution":{"iopub.status.busy":"2022-02-23T12:10:47.114676Z","iopub.execute_input":"2022-02-23T12:10:47.114921Z","iopub.status.idle":"2022-02-23T12:10:47.118949Z","shell.execute_reply.started":"2022-02-23T12:10:47.114886Z","shell.execute_reply":"2022-02-23T12:10:47.117820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sr = 22050 # sample rate\nT = 5.0    # seconds\nt = np.linspace(0, T, int(T*sr), endpoint=False) # time variable\nx = 0.5*np.sin(2*np.pi*220*t) # pure sine wave at 220 Hz\n#Playing the audio\nipd.Audio(x, rate=sr) # load a NumPy array\n#Saving the audio\nsf.write('afrsil1_XC125458.wav', x, sr)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T12:10:52.110579Z","iopub.execute_input":"2022-02-23T12:10:52.110900Z","iopub.status.idle":"2022-02-23T12:10:52.130634Z","shell.execute_reply.started":"2022-02-23T12:10:52.110864Z","shell.execute_reply":"2022-02-23T12:10:52.129669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x, sr = librosa.load(audio_data)\n#Plot the signal:\nplt.figure(figsize=(14, 5))\nlibrosa.display.waveshow(x, sr=sr)\n# Zooming in\nn0 = 9000\nn1 = 9100\nplt.figure(figsize=(14, 5))\nplt.plot(x[n0:n1])\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T12:10:52.318980Z","iopub.execute_input":"2022-02-23T12:10:52.319384Z","iopub.status.idle":"2022-02-23T12:10:54.498434Z","shell.execute_reply.started":"2022-02-23T12:10:52.319327Z","shell.execute_reply":"2022-02-23T12:10:54.497334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zero_crossings = librosa.zero_crossings(x[n0:n1], pad=False)\nprint(sum(zero_crossings))","metadata":{"execution":{"iopub.status.busy":"2022-02-23T12:10:58.467243Z","iopub.execute_input":"2022-02-23T12:10:58.468183Z","iopub.status.idle":"2022-02-23T12:10:58.474263Z","shell.execute_reply.started":"2022-02-23T12:10:58.468121Z","shell.execute_reply":"2022-02-23T12:10:58.473401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}