{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# listen to recordings of NBIRDS at the same time\n\nimport numpy as np\nfrom pathlib import Path\nfrom pydub import AudioSegment\nfrom IPython.display import FileLink\n\nPATH_TRAIN = \"/kaggle/input/birdsong-recognition/train_audio\"\nLENGTH = 60 #seconds\nSAMPLE_RATE = 2**15\n\ndef get_birds():\n    path = Path(PATH_TRAIN)\n    birds = [str(f).split(\"/\")[-1] \n             for f in path.rglob(\"*\") \n             if f.is_dir()]\n    birds.sort()\n    return birds\n\ndef get_audio(bird, length=LENGTH):\n    path = Path(PATH_TRAIN)/bird\n    files = [f for f in path.rglob(\"*.mp3\")]\n    np.random.shuffle(files)\n    all_audio = AudioSegment.empty()\n    while len(all_audio)/1e3<length:\n        f = files.pop()\n        audio = AudioSegment.from_file(f)\n        all_audio += audio\n    all_audio = all_audio[:length*1e3]\n    return all_audio\n\ndef overlay_birds(birds, length=LENGTH):\n    pans = [2*i/(len(birds)-1)-1 for i in range(len(birds))]\n    combined = AudioSegment.silent(length*1e3)\n    for b in birds:\n        sound_bird = get_audio(b, length)\n        #sound_bird.pan(pans.pop())\n        combined = combined.overlay(sound_bird)\n    return combined","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"birds = get_birds()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"NBIRDS = 10\nlisten_to = list(np.random.choice(birds, NBIRDS, replace=False))\nprint(listen_to)\naudification = overlay_birds(listen_to, length=30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"audification","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filename = \"_\".join(listen_to) + \".wav\"\naudification.export(filename)\nFileLink(filename)\n","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}