"""Nature field recordings edited into planet ambience. Requires numpy + soundfile.""" from pathlib import Path import sys import json import wave import numpy as np ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0,str(ROOT/"tmp/surface_audio_tools")) import soundfile as sf from build_freighter_audio import eq, peak, pitch, fade, loop, RATE SOURCE = ROOT/"tmp/surface_audio_sources" OUT = ROOT/"assets/sounds/surface" manifest = {} def read(path, start=0, seconds=None): with sf.SoundFile(SOURCE/path) as stream: rate = stream.samplerate stream.seek(int(start*rate)) signal = stream.read(-1 if seconds is None else int(seconds*rate),always_2d=True) if signal.shape[1] == 1: signal = np.repeat(signal,2,axis=1) signal = signal[:,:2] if rate != RATE: at = np.arange(int(len(signal)*RATE/rate))*rate/RATE signal = np.stack([np.interp(at,np.arange(len(signal)),signal[:,c]) for c in range(2)],axis=1) signal -= signal.mean(axis=0) return signal def save(name, x, sources, processing, looping=False): x = peak(x,-3) assert np.isfinite(x).all() and np.max(np.abs(x)) < .9 with wave.open(str(OUT/(name+".wav")),"wb") as stream: stream.setparams((2,2,RATE,0,"NONE","not compressed")) stream.writeframes((x*32767).astype("