{"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":"# 🔊 Working with Audio in Python\n\n<img src=\"https://miro.medium.com/max/1100/1*Zx9QAMPzxhama9O4q9xWXg.jpeg\" width=\"600\"/>\n\nThis notebook is intended to be an introduction for anyone interested in using python to interperate audio data.\n\nPlease watch the youtube video that discusses the contents of this notebook if you want to learn more!\n- [Video Link](https://www.youtube.com/watch?v=ZqpSb5p1xQo)\n- [Youtube Channel](https://www.youtube.com/channel/UCxladMszXan-jfgzyeIMyvw)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pylab as plt\nimport seaborn as sns\n\nfrom glob import glob\n\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\n\nfrom itertools import cycle\n\nsns.set_theme(style=\"white\", palette=None)\ncolor_pal = plt.rcParams[\"axes.prop_cycle\"].by_key()[\"color\"]\ncolor_cycle = cycle(plt.rcParams[\"axes.prop_cycle\"].by_key()[\"color\"])","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:11:07.501806Z","iopub.execute_input":"2022-02-19T21:11:07.50238Z","iopub.status.idle":"2022-02-19T21:11:09.890749Z","shell.execute_reply.started":"2022-02-19T21:11:07.502291Z","shell.execute_reply":"2022-02-19T21:11:09.889786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Terms to know for Audio in Digital Form:\n\n## Frequency (Hz)\n- Frequency describes the differences of wave lengths.\n- We interperate frequency has high and low pitches.\n\n<img src=\"https://uploads-cdn.omnicalculator.com/images/britannica-wave-frequency.jpg\" width=\"400\"/>\n\n## Intensity (db / power)\n- Intensity describes the amplitude (height) of the wave.\n\n<img src=\"https://ars.els-cdn.com/content/image/3-s2.0-B9780124722804500162-f13-15-9780124722804.gif\" width=\"400\"/>\n\n## Sample Rate\n- Sample rate is specific to how the computer reads in the audio file.\n- Think of it as the \"resolution\" of the audio.\n\n<img src=\"https://www.headphonesty.com/wp-content/uploads/2019/07/Sample-Rate-Bit-Depth-and-Bit-Rate.jpeg\" width=\"400\"/>\n","metadata":{}},{"cell_type":"markdown","source":"# Reading in Audio Files\nThere are many types of audio files: `mp3`, `wav`, `m4a`, `flac`, `ogg`","metadata":{}},{"cell_type":"code","source":"audio_files = glob('../input/ravdess-emotional-speech-audio/*/*.wav')","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:17:05.560814Z","iopub.execute_input":"2022-02-19T21:17:05.56168Z","iopub.status.idle":"2022-02-19T21:17:05.592357Z","shell.execute_reply.started":"2022-02-19T21:17:05.561631Z","shell.execute_reply":"2022-02-19T21:17:05.591717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Play audio file\nipd.Audio(audio_files[0])","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:17:31.83982Z","iopub.execute_input":"2022-02-19T21:17:31.840115Z","iopub.status.idle":"2022-02-19T21:17:31.869235Z","shell.execute_reply.started":"2022-02-19T21:17:31.840083Z","shell.execute_reply":"2022-02-19T21:17:31.868431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y, sr = librosa.load(audio_files[0])\nprint(f'y: {y[:10]}')\nprint(f'shape y: {y.shape}')\nprint(f'sr: {sr}')","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:19:29.501419Z","iopub.execute_input":"2022-02-19T21:19:29.501743Z","iopub.status.idle":"2022-02-19T21:19:29.678071Z","shell.execute_reply.started":"2022-02-19T21:19:29.501714Z","shell.execute_reply":"2022-02-19T21:19:29.677344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(y).plot(figsize=(10, 5),\n                  lw=1,\n                  title='Raw Audio Example',\n                 color=color_pal[0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:21:03.374562Z","iopub.execute_input":"2022-02-19T21:21:03.374852Z","iopub.status.idle":"2022-02-19T21:21:03.642317Z","shell.execute_reply.started":"2022-02-19T21:21:03.374821Z","shell.execute_reply":"2022-02-19T21:21:03.641635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Trimming leading/lagging silence\ny_trimmed, _ = librosa.effects.trim(y, top_db=20)\npd.Series(y_trimmed).plot(figsize=(10, 5),\n                  lw=1,\n                  title='Raw Audio Trimmed Example',\n                 color=color_pal[1])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:23:54.389489Z","iopub.execute_input":"2022-02-19T21:23:54.390065Z","iopub.status.idle":"2022-02-19T21:23:54.639935Z","shell.execute_reply.started":"2022-02-19T21:23:54.390029Z","shell.execute_reply":"2022-02-19T21:23:54.63938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(y[30000:30500]).plot(figsize=(10, 5),\n                  lw=1,\n                  title='Raw Audio Zoomed In Example',\n                 color=color_pal[2])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:25:40.965005Z","iopub.execute_input":"2022-02-19T21:25:40.965344Z","iopub.status.idle":"2022-02-19T21:25:41.1966Z","shell.execute_reply.started":"2022-02-19T21:25:40.965307Z","shell.execute_reply":"2022-02-19T21:25:41.195712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Spectogram","metadata":{}},{"cell_type":"code","source":"D = librosa.stft(y)\nS_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)\nS_db.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:28:11.620131Z","iopub.execute_input":"2022-02-19T21:28:11.620901Z","iopub.status.idle":"2022-02-19T21:28:11.63607Z","shell.execute_reply.started":"2022-02-19T21:28:11.620858Z","shell.execute_reply":"2022-02-19T21:28:11.635291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the transformed audio data\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-02-19T21:30:40.190957Z","iopub.execute_input":"2022-02-19T21:30:40.191221Z","iopub.status.idle":"2022-02-19T21:30:40.622933Z","shell.execute_reply.started":"2022-02-19T21:30:40.191193Z","shell.execute_reply":"2022-02-19T21:30:40.622047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Mel Spectogram","metadata":{}},{"cell_type":"code","source":"S = librosa.feature.melspectrogram(y=y,\n                                   sr=sr,\n                                   n_mels=128 * 2,)\nS_db_mel = librosa.amplitude_to_db(S, ref=np.max)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:37:18.363181Z","iopub.execute_input":"2022-02-19T21:37:18.363461Z","iopub.status.idle":"2022-02-19T21:37:18.387745Z","shell.execute_reply.started":"2022-02-19T21:37:18.363427Z","shell.execute_reply":"2022-02-19T21:37:18.38681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, 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-02-19T21:37:18.726294Z","iopub.execute_input":"2022-02-19T21:37:18.726563Z","iopub.status.idle":"2022-02-19T21:37:19.040674Z","shell.execute_reply.started":"2022-02-19T21:37:18.726533Z","shell.execute_reply":"2022-02-19T21:37:19.039786Z"},"trusted":true},"execution_count":null,"outputs":[]}]}