{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"pip install praat-parselmouth\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pip install --upgrade praat-parselmouth","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport os\nimport librosa\nimport parselmouth\nimport soundfile","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"acafly_xc109605 = '../input/birdclef-2021/train_short_audio/acafly/XC109605.ogg'\nacowoo_xc110258 = '../input/birdclef-2021/train_short_audio/acowoo/XC110258.ogg'\nyacafly,sry = librosa.load(acafly_xc109605)\nacowoo,sro = librosa.load(acowoo_xc110258)\nprint(\"yacafly[:30]:{}\\nyacfly.sr:{}\".format(yacafly[:30],sry))\nprint(\"acowoo[:30]:{}\\nyacfly.sr:{}\".format(acowoo[:30],sro))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def normalize(data):\n    max = np.max(data)\n    min = np.min(data)\n    return (data-min)/(max-min)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_5s_features(wavdata, sr):\n    iters = int(np.ceil(len(wavdata) / (sr*5)))\n    \n    for i in range(iters):\n        datas = wavdata[i * sr*5 : (i+1) * sr*5]\n        print(len(datas))\n        if not os.path.exists('../output/tmp/'):\n             os.makedirs('../output/tmp/')\n        soundfile.write('../output/tmp/tmp.wav', datas, sr)\n        sound = parselmouth.Sound('../output/tmp/tmp.wav')\n        frame = librosa.util.frame(datas, frame_length=1024, hop_length=512)\n        energy = np.sum(frame**2, axis=0)\n        n_energy = normalize(energy)\n        mean_energy = np.mean(n_energy)\n        std_energy = np.std(n_energy)\n        \n        zcr = librosa.feature.zero_crossing_rate(datas, frame_length=1024, hop_length=512, center=False)\n        n_zcr = normalize(zcr)\n        mean_zcr = np.mean(n_zcr)\n        std_zcr = np.std(n_zcr)\n\n        pitch = sound.to_pitch()  \n        #print('pitch:{}'.format(pitch))\n        q1 = parselmouth.praat.call(pitch, \"Get quantile\", 0.0, 0.0, 0.25, \"Hertz\")\n\n        q2 = parselmouth.praat.call(pitch, \"Get quantile\", 0.0, 0.0, 0.5, \"Hertz\")\n        q3 = parselmouth.praat.call(pitch, \"Get quantile\", 0.0, 0.0, 0.75, \"Hertz\")\n        \n        pitch_max = parselmouth.praat.call(pitch, \"Get maximum\", 0, 0, \"Hertz\", \"Parabolic\")\n        pitch_min = parselmouth.praat.call(pitch, \"Get minimum\", 0, 0, \"Hertz\", \"Parabolic\")\n        pitch_sd = parselmouth.praat.call(pitch, \"Get standard deviation\", 0, 0, \"Hertz\")\n        \n        mean_pitch = parselmouth.praat.call(pitch, \"Get mean\", 0.0, 0.0, \"Hertz\")\n        \n        formant = sound.to_formant_burg()\n        mean_formant = []\n        for idx in range(1, 6):\n            mean_formant_i = parselmouth.praat.call(formant, \"Get mean\", float(idx), 0.0, 0.0, \"Hertz\")\n            mean_formant.append(str(float(mean_formant_i)/1000))\n            \n        print(str(i*5)+'-'+ str((i+1)*5) +'\\t'+ str(mean_energy)+ '\\t'+ str(std_energy)+ '\\t'+ str(mean_zcr)+ '\\t'+ str(std_zcr)+ '\\t'+ \n                str(float(q1)/1000)+ '\\t'+ str(float(q2)/1000)+ '\\t'+ str(float(q3)/1000)+ '\\t'+ str(float(pitch_max)/1000)+ '\\t'+ \n                str(float(pitch_min)/1000)+ '\\t'+ str(float(pitch_sd)/1000)+ '\\t' + str(float(mean_pitch)/1000)+ '\\t'+ '\\t'.join(mean_formant)\n             )\nprint(\"-\"*100)\nget_5s_features(yacafly,sry)\nprint(\"\\n\\n\"+\"-\"*100)\nget_5s_features(yacafly,sry)\nprint(\"-\"*100)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}