{"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":"import numpy as np\nimport pandas as pd \nimport seaborn as sns\n\nimport IPython.display as idp\n\nimport librosa\nfrom glob import glob\n\nimport librosa.display\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-13T16:16:38.655579Z","iopub.execute_input":"2022-03-13T16:16:38.655863Z","iopub.status.idle":"2022-03-13T16:16:38.662544Z","shell.execute_reply.started":"2022-03-13T16:16:38.655832Z","shell.execute_reply":"2022-03-13T16:16:38.661635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Working with Audio in Python\n\n<font size=4> I referred to Rob's notebook and youtube video first to learn how to deal with audio data.(\n https://www.youtube.com/watch?v=ZqpSb5p1xQo | https://www.kaggle.com/robikscube/working-with-audio-data-overview )\n</font>","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv('../input/kaggle-pog-series-s01e02/train.csv')\ntest_df = pd.read_csv('../input/kaggle-pog-series-s01e02/test.csv')\n\ngenre = pd.read_csv('../input/kaggle-pog-series-s01e02/genres.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-13T16:16:38.664691Z","iopub.execute_input":"2022-03-13T16:16:38.665296Z","iopub.status.idle":"2022-03-13T16:16:38.725147Z","shell.execute_reply.started":"2022-03-13T16:16:38.665252Z","shell.execute_reply":"2022-03-13T16:16:38.724441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nax = sns.countplot(train_df.genre)\nplt.xticks(rotation=45);\ntotal=len(train_df)\nfor p in ax.patches:\n    percentage = f'{100 * p.get_height() / total:.1f}%\\n'\n    x = p.get_x() + p.get_width() / 2\n    y = p.get_height()\n    ax.annotate(percentage, (x, y), ha='center', va='center')","metadata":{"execution":{"iopub.status.busy":"2022-03-13T16:16:38.726228Z","iopub.execute_input":"2022-03-13T16:16:38.726493Z","iopub.status.idle":"2022-03-13T16:16:39.187485Z","shell.execute_reply.started":"2022-03-13T16:16:38.726463Z","shell.execute_reply":"2022-03-13T16:16:39.186355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files = glob('../input/kaggle-pog-series-s01e02/train/*.ogg')\nlen(train_df),len(train_files)","metadata":{"execution":{"iopub.status.busy":"2022-03-13T16:16:39.189475Z","iopub.execute_input":"2022-03-13T16:16:39.189728Z","iopub.status.idle":"2022-03-13T16:16:39.273161Z","shell.execute_reply.started":"2022-03-13T16:16:39.189698Z","shell.execute_reply":"2022-03-13T16:16:39.272482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files[0],train_files[1]","metadata":{"execution":{"iopub.status.busy":"2022-03-13T16:16:39.274533Z","iopub.execute_input":"2022-03-13T16:16:39.275129Z","iopub.status.idle":"2022-03-13T16:16:39.282925Z","shell.execute_reply.started":"2022-03-13T16:16:39.275066Z","shell.execute_reply":"2022-03-13T16:16:39.281745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[train_df['filename'].str.contains('011395')]","metadata":{"execution":{"iopub.status.busy":"2022-03-13T16:16:39.284933Z","iopub.execute_input":"2022-03-13T16:16:39.285622Z","iopub.status.idle":"2022-03-13T16:16:39.328764Z","shell.execute_reply.started":"2022-03-13T16:16:39.285554Z","shell.execute_reply":"2022-03-13T16:16:39.327521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[train_df['filename'].str.contains('012509')]","metadata":{"execution":{"iopub.status.busy":"2022-03-13T16:16:39.332146Z","iopub.execute_input":"2022-03-13T16:16:39.332575Z","iopub.status.idle":"2022-03-13T16:16:39.365623Z","shell.execute_reply.started":"2022-03-13T16:16:39.332525Z","shell.execute_reply":"2022-03-13T16:16:39.364461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idp.display(idp.Audio(train_files[0]))\n#idp.display(idp.Audio(train_files[1]))","metadata":{"execution":{"iopub.status.busy":"2022-03-13T16:16:39.367511Z","iopub.execute_input":"2022-03-13T16:16:39.368044Z","iopub.status.idle":"2022-03-13T16:16:39.391188Z","shell.execute_reply.started":"2022-03-13T16:16:39.367998Z","shell.execute_reply":"2022-03-13T16:16:39.389465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y, sr = librosa.load(train_files[0])","metadata":{"execution":{"iopub.status.busy":"2022-03-13T16:16:39.392753Z","iopub.execute_input":"2022-03-13T16:16:39.393640Z","iopub.status.idle":"2022-03-13T16:16:40.750281Z","shell.execute_reply.started":"2022-03-13T16:16:39.393596Z","shell.execute_reply":"2022-03-13T16:16:40.749372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(y).plot(figsize=(15,5),title='Entire Audio Example');\n","metadata":{"execution":{"iopub.status.busy":"2022-03-13T16:16:40.753285Z","iopub.execute_input":"2022-03-13T16:16:40.753561Z","iopub.status.idle":"2022-03-13T16:16:41.550160Z","shell.execute_reply.started":"2022-03-13T16:16:40.753528Z","shell.execute_reply":"2022-03-13T16:16:41.549215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<font size=4> Around about 14 seconds, they get into the chorus.Let's see the part. ","metadata":{}},{"cell_type":"code","source":"pd.Series(y[290000:310000]).plot(figsize=(10,5),\n                         lw=1,\n                         title='Raw Audio Zoomed Exampole',\n                         );","metadata":{"execution":{"iopub.status.busy":"2022-03-13T16:16:41.551403Z","iopub.execute_input":"2022-03-13T16:16:41.551639Z","iopub.status.idle":"2022-03-13T16:16:41.841283Z","shell.execute_reply.started":"2022-03-13T16:16:41.551610Z","shell.execute_reply":"2022-03-13T16:16:41.840451Z"},"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,x_axis='time',y_axis='log',ax=ax)\nax.set_title('Spectogram Example',fontsize=20);\n\nfig.colorbar(img,ax=ax, format=f'%0.2f')\nplt.show()\n             ","metadata":{"execution":{"iopub.status.busy":"2022-03-13T16:16:41.842427Z","iopub.execute_input":"2022-03-13T16:16:41.842645Z","iopub.status.idle":"2022-03-13T16:16:43.053794Z","shell.execute_reply.started":"2022-03-13T16:16:41.842616Z","shell.execute_reply":"2022-03-13T16:16:43.052553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(20,32))\nfig.subplots_adjust(hspace=0.25)\nfor i,g in enumerate(genre.genre.unique()):\n    df = train_df.query('genre==@g')\n    ax = fig.add_subplot(7,3,i+1)\n    \n    y,sr = librosa.load('../input/kaggle-pog-series-s01e02/' + str(df.iloc[0].filepath))\n    \n    D = librosa.stft(y)\n    S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)\n    img = librosa.display.specshow(S_db,x_axis='time',y_axis='log',ax=ax)\n    ax.set_title(f'Spectogram {g}',fontsize=10);\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T16:34:10.820797Z","iopub.execute_input":"2022-03-13T16:34:10.821091Z","iopub.status.idle":"2022-03-13T16:34:55.268893Z","shell.execute_reply.started":"2022-03-13T16:34:10.821052Z","shell.execute_reply":"2022-03-13T16:34:55.267785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"genre","metadata":{"execution":{"iopub.status.busy":"2022-03-13T16:49:39.627899Z","iopub.execute_input":"2022-03-13T16:49:39.628225Z","iopub.status.idle":"2022-03-13T16:49:39.643063Z","shell.execute_reply.started":"2022-03-13T16:49:39.628186Z","shell.execute_reply":"2022-03-13T16:49:39.641140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<font size =4> Soul-Rnb's lower Hz part looks unique, probably it's showing the bass drum's sound. ","metadata":{}},{"cell_type":"code","source":"df = train_df.query('genre==\"Soul-RnB\"')\nfile = '../input/kaggle-pog-series-s01e02/' + str(df.iloc[0].filepath)\n\nidp.display(idp.Audio(file))","metadata":{"execution":{"iopub.status.busy":"2022-03-13T17:04:38.714688Z","iopub.execute_input":"2022-03-13T17:04:38.715054Z","iopub.status.idle":"2022-03-13T17:04:38.740022Z","shell.execute_reply.started":"2022-03-13T17:04:38.715016Z","shell.execute_reply":"2022-03-13T17:04:38.739055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# My music \n<font size=4> My hobby is making music.  I just wanted to see what does my music look like. \n    </font>","metadata":{}},{"cell_type":"code","source":"y,sr = librosa.load('../input/music-file/Orchestra_Jam.wav')\nD = 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,x_axis='time',y_axis='log',ax=ax)\nax.set_title('Spectogram Example',fontsize=20);\n\nfig.colorbar(img,ax=ax, format=f'%0.2f')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T18:09:49.626752Z","iopub.execute_input":"2022-03-13T18:09:49.627100Z","iopub.status.idle":"2022-03-13T18:09:55.567753Z","shell.execute_reply.started":"2022-03-13T18:09:49.627062Z","shell.execute_reply":"2022-03-13T18:09:55.566853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idp.display(idp.Audio('../input/music-file/Orchestra_Jam.wav'))","metadata":{"execution":{"iopub.status.busy":"2022-03-13T18:10:49.328518Z","iopub.execute_input":"2022-03-13T18:10:49.329607Z","iopub.status.idle":"2022-03-13T18:10:49.626157Z","shell.execute_reply.started":"2022-03-13T18:10:49.329554Z","shell.execute_reply":"2022-03-13T18:10:49.625361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<font size=4> I categorized this song as folk by the way. I think I tried to sound like Chinese music. Thank you for reading my notebook and listening if you did. ","metadata":{"execution":{"iopub.status.busy":"2022-03-13T18:23:58.105942Z","iopub.execute_input":"2022-03-13T18:23:58.106386Z","iopub.status.idle":"2022-03-13T18:23:58.113846Z","shell.execute_reply.started":"2022-03-13T18:23:58.106339Z","shell.execute_reply":"2022-03-13T18:23:58.112706Z"}}},{"cell_type":"markdown","source":"<font size=6> To be continued... \n    </font>","metadata":{}}]}