{"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":"# An Introduction to Working with Audio Data\n- Live On Twitch!","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport IPython.display as ipd\n\nimport librosa\n\nfrom glob import glob\n\nimport matplotlib.pylab as plt\nplt.style.use('ggplot')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-11T03:11:33.450383Z","iopub.execute_input":"2022-03-11T03:11:33.451078Z","iopub.status.idle":"2022-03-11T03:11:33.458857Z","shell.execute_reply.started":"2022-03-11T03:11:33.451038Z","shell.execute_reply":"2022-03-11T03:11:33.457625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/birdclef-2022/train_metadata.csv')\ntrain_files = glob('../input/birdclef-2022/train_audio/*/*.ogg')\ntrain['filename'].nunique(), len(train_files)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:02:43.4178Z","iopub.execute_input":"2022-03-11T03:02:43.418321Z","iopub.status.idle":"2022-03-11T03:02:43.541562Z","shell.execute_reply.started":"2022-03-11T03:02:43.41828Z","shell.execute_reply":"2022-03-11T03:02:43.540371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files[0]","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:03:41.574517Z","iopub.execute_input":"2022-03-11T03:03:41.575442Z","iopub.status.idle":"2022-03-11T03:03:41.581676Z","shell.execute_reply.started":"2022-03-11T03:03:41.575386Z","shell.execute_reply":"2022-03-11T03:03:41.580713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.loc[train['filename'].str.contains('XC516224')]","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:04:11.612934Z","iopub.execute_input":"2022-03-11T03:04:11.613254Z","iopub.status.idle":"2022-03-11T03:04:11.644392Z","shell.execute_reply.started":"2022-03-11T03:04:11.613222Z","shell.execute_reply":"2022-03-11T03:04:11.643143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Listen to the audio\nipd.display(ipd.Audio(train_files[0]))\nipd.display(ipd.Audio(train_files[1]))","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:08:02.675484Z","iopub.execute_input":"2022-03-11T03:08:02.675751Z","iopub.status.idle":"2022-03-11T03:08:02.722411Z","shell.execute_reply.started":"2022-03-11T03:08:02.675724Z","shell.execute_reply":"2022-03-11T03:08:02.72168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Loading in audio file {train_files[0]}')\ny, sr = librosa.load(train_files[0])","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:10:00.141431Z","iopub.execute_input":"2022-03-11T03:10:00.141744Z","iopub.status.idle":"2022-03-11T03:10:03.19999Z","shell.execute_reply.started":"2022-03-11T03:10:00.141705Z","shell.execute_reply":"2022-03-11T03:10:03.198974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(y).plot(figsize=(15, 5), title='Entire Audio Example')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:13:59.119885Z","iopub.execute_input":"2022-03-11T03:13:59.120171Z","iopub.status.idle":"2022-03-11T03:13:59.568915Z","shell.execute_reply.started":"2022-03-11T03:13:59.12014Z","shell.execute_reply":"2022-03-11T03:13:59.567781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.Series(y).to_frame().reset_index().rename(columns={'index':'time'})\ndf['time'] /= sr\ndf[0].plot(figsize=(15, 5))","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:25:31.155362Z","iopub.execute_input":"2022-03-11T03:25:31.156075Z","iopub.status.idle":"2022-03-11T03:25:31.797883Z","shell.execute_reply.started":"2022-03-11T03:25:31.156016Z","shell.execute_reply":"2022-03-11T03:25:31.796974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.loc[20000:21000].set_index('time')[0].plot(figsize=(15, 5),\n          title='Entire Audio Example',\n          style='.-')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:27:08.99378Z","iopub.execute_input":"2022-03-11T03:27:08.994084Z","iopub.status.idle":"2022-03-11T03:27:09.220763Z","shell.execute_reply.started":"2022-03-11T03:27:08.994042Z","shell.execute_reply":"2022-03-11T03:27:09.219824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa.display\n","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:28:44.559518Z","iopub.execute_input":"2022-03-11T03:28:44.559809Z","iopub.status.idle":"2022-03-11T03:28:44.568298Z","shell.execute_reply.started":"2022-03-11T03:28:44.559777Z","shell.execute_reply":"2022-03-11T03:28:44.56721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = librosa.stft(y)\nS_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)\nfig, ax = plt.subplots(figsize=(10, 5))\nimg = librosa.display.specshow(S_db,\n                              x_axis='time',\n                              y_axis='log',\n                              ax=ax)\nax.set_title('Spectogram Example', fontsize=20)\nfig.colorbar(img, ax=ax, format=f'%0.2f')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:29:26.0172Z","iopub.execute_input":"2022-03-11T03:29:26.017599Z","iopub.status.idle":"2022-03-11T03:29:28.531045Z","shell.execute_reply.started":"2022-03-11T03:29:26.017568Z","shell.execute_reply":"2022-03-11T03:29:28.53024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"S = librosa.feature.melspectrogram(y=y[1000:50000],\n                                   sr=sr,\n#                                    n_fft=int(2048 / 2),\n#                                    hop_length=int(512 / 5)\n                                  )\nS_db_mel = librosa.amplitude_to_db(S, ref=np.max)\nfig, ax = plt.subplots(figsize=(10, 5))\n# Plot the mel spectogram\nimg = librosa.display.specshow(S_db_mel,\n                              x_axis='time',\n                              y_axis='log',\n                              ax=ax)\nax.set_title('Mel Spectogram Example', fontsize=20)\nfig.colorbar(img, ax=ax, format=f'%0.2f')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:35:43.449814Z","iopub.execute_input":"2022-03-11T03:35:43.450752Z","iopub.status.idle":"2022-03-11T03:35:43.875558Z","shell.execute_reply.started":"2022-03-11T03:35:43.450694Z","shell.execute_reply":"2022-03-11T03:35:43.874363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"S = librosa.feature.tempogram(y=y, sr=22050)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:42:00.065787Z","iopub.execute_input":"2022-03-11T03:42:00.066138Z","iopub.status.idle":"2022-03-11T03:42:00.546274Z","shell.execute_reply.started":"2022-03-11T03:42:00.066102Z","shell.execute_reply":"2022-03-11T03:42:00.545229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"S_db_mel = librosa.amplitude_to_db(S, ref=np.max)\nfig, ax = plt.subplots(figsize=(10, 5))\n\nimg = librosa.display.specshow(yfeatures,\n                              x_axis='time',\n                              y_axis='log',\n                              ax=ax)\n\nax.set_title('Tempogram Example', fontsize=20)\nfig.colorbar(img, ax=ax, format=f'%0.2f')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:42:37.915922Z","iopub.execute_input":"2022-03-11T03:42:37.916211Z","iopub.status.idle":"2022-03-11T03:42:38.980282Z","shell.execute_reply.started":"2022-03-11T03:42:37.91616Z","shell.execute_reply":"2022-03-11T03:42:38.979477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}