"""Ambient loops: bubbling, dripping, gurgling, bouncing, a ratchet, and repeating patterns. These are live voices with nothing for a game to set while they play, so they can be recorded once into a loop: loop = make_loop(render_voice(BubblingVoice(), 4.0, fadeSeconds=0)) A game with its own live mixer can also play them directly through render(frameCount). """ import math import numpy as np from .materials import squelch from .synth import Biquad, OneShotPlayer, Oscillator, rng_from, sampleRate class PatternVoice: """Plays short sounds over and over in a fixed rhythm: each entry is (sound, seconds until the next). Good for an item waiting to be found by ear, such as a beacon blip or two finger snaps and a rest. """ def __init__(self, pattern): self.pattern = pattern self.player = OneShotPlayer() self.time = 0.0 self.nextSound = 0.0 self.index = 0 def render(self, frameCount): if self.time >= self.nextSound: sound, gap = self.pattern[self.index % len(self.pattern)] self.player.add(sound) self.nextSound += gap self.index += 1 self.time += frameCount / sampleRate return self.player.render(frameCount) class BubblingVoice: """A bubbling pot or cauldron: quick rising blips over a low simmer.""" def __init__(self, seed=0): self.rng = rng_from(seed) self.bubbles = OneShotPlayer() self.simmer = Biquad("lowpass") def render(self, frameCount): # A new bubble about eight times a second, each a quick rising blip. if self.rng.random() < 8.0 * frameCount / sampleRate: length = int(sampleRate * self.rng.uniform(0.05, 0.09)) times = np.arange(length) / sampleRate start = self.rng.uniform(180.0, 320.0) phase = 2.0 * math.pi * np.cumsum(start * (1.0 + 4.0 * times / times[-1])) / sampleRate self.bubbles.add(0.5 * np.sin(phase) * np.sin(math.pi * times / times[-1])) simmer = self.simmer.process(self.rng.standard_normal(frameCount), 300.0, 0.7) return self.bubbles.render(frameCount) + 0.15 * simmer class DrippingVoice: """Thick, slow drips into a puddle over a dark, sloshing swell. Each drip is a low plop whose pitch rises as it lands, slower and lower than water, so it sounds heavy and sticky, like blood or mud. """ def __init__(self, seed=0): self.rng = rng_from(seed) self.drips = OneShotPlayer() self.sloshFilter = Biquad("lowpass") self.tickFilter = Biquad("lowpass") self.time = 0.0 self.nextDrip = 0.0 def make_drip(self): length = int(sampleRate * 0.16) times = np.arange(length) / sampleRate start = self.rng.uniform(170.0, 230.0) # The plop: the pitch climbs fast, then settles, like a heavy drop closing over. pitch = start * (1.0 + 2.2 * (1.0 - np.exp(-times / 0.03))) plop = np.sin(2.0 * math.pi * np.cumsum(pitch) / sampleRate) envelope = np.minimum(1.0, times / 0.004) * np.exp(-times / 0.045) # A tiny wet tick as the drop lands. tick = self.tickFilter.process(self.rng.standard_normal(length), 2500.0, 0.7) * np.exp(-times / 0.004) return 0.75 * plop * envelope + 0.15 * tick def render(self, frameCount): if self.time >= self.nextDrip: self.drips.add(self.make_drip()) # Drips come unevenly, sometimes two close together. self.nextDrip += self.rng.choice([0.18, 0.55, 0.7, 0.9]) times = self.time + np.arange(frameCount) / sampleRate self.time += frameCount / sampleRate # The slosh rocks back and forth a little under once a second. swell = 0.5 + 0.5 * np.sin(2.0 * math.pi * 0.7 * times) * np.sin(2.0 * math.pi * 0.31 * times + 1.0) slosh = self.sloshFilter.process(self.rng.standard_normal(frameCount), 260.0 + 140.0 * swell[0], 1.2) return self.drips.render(frameCount) + 0.35 * slosh * swell class GurglingVoice: """Something wet and alive: squelches now and then, over a low, bubbling gurgle like a stomach.""" def __init__(self, seed=0): self.rng = rng_from(seed) self.squelches = OneShotPlayer() self.gurgle = Oscillator() self.gurgleFilter = Biquad("lowpass") self.time = 0.0 self.nextSquelch = 0.0 self.wobble = 0.0 def render(self, frameCount): if self.time >= self.nextSquelch: seconds = self.rng.uniform(0.14, 0.26) low = self.rng.uniform(220.0, 320.0) high = self.rng.uniform(700.0, 1000.0) self.squelches.add(1.6 * squelch(seconds, low, high, seed=self.rng)) self.nextSquelch += self.rng.uniform(0.45, 1.1) self.time += frameCount / sampleRate # The gurgle's pitch wanders and bubbles, from about 60 to 120 cycles a second. self.wobble = min(1.0, max(-1.0, self.wobble + self.rng.uniform(-0.3, 0.3))) times = np.arange(frameCount) / sampleRate bubble = np.sin(2.0 * math.pi * 9.0 * (self.time + times)) * np.sin(2.0 * math.pi * 2.3 * (self.time + times)) phases, _ = self.gurgle.advance(90.0 + 30.0 * self.wobble + 20.0 * bubble) gurgle = np.sin(2.0 * math.pi * phases) * (0.5 + 0.5 * np.abs(bubble)) murk = self.gurgleFilter.process(self.rng.standard_normal(frameCount), 180.0, 0.9) return self.squelches.render(frameCount) + 0.35 * gurgle + 0.25 * murk class BouncingSpringVoice: """Something hollow bouncing on a spring: a springy boing over a hollow thump. It bounces three times, each lower and quicker, then gets kicked back up again. gaps and pitches can be changed for a different bounce. """ gaps = [0.36, 0.3, 0.25, 0.55] pitches = [262.0, 233.0, 208.0, 185.0] def __init__(self, seed=0): self.rng = rng_from(seed) self.bounces = OneShotPlayer() self.time = 0.0 self.nextBounce = 0.0 self.index = 0 def make_bounce(self, frequency, size): length = int(sampleRate * 0.22) times = np.arange(length) / sampleRate # The boing: a swoop down onto the note, then a wobble like a spring. wobble = 1.0 + 0.1 * np.sin(2.0 * math.pi * 16.0 * times) * np.exp(-times / 0.08) pitch = frequency * (1.0 + 0.6 * np.exp(-times / 0.025)) * wobble boing = np.sin(2.0 * math.pi * np.cumsum(pitch) / sampleRate) * np.exp(-times / 0.09) # The thump of something hollow landing. thumpPitch = 110.0 * (1.0 + np.exp(-times / 0.01)) thump = np.sin(2.0 * math.pi * np.cumsum(thumpPitch) / sampleRate) * np.exp(-times / 0.03) attack = np.minimum(1.0, times / 0.002) return 1.3 * size * attack * (0.45 * boing + 0.5 * thump) def render(self, frameCount): if self.time >= self.nextBounce: step = self.index % len(self.gaps) size = 1.0 - 0.15 * step self.bounces.add(self.make_bounce(self.pitches[step], size)) self.nextBounce += self.gaps[step] self.index += 1 self.time += frameCount / sampleRate return self.bounces.render(frameCount) class RatchetVoice: """A wooden ratchet winding back, click by click, then a rest, over and over. The knock of each click rises a little as it winds tighter. """ clicksPerWind = 8 clickSeconds = 0.11 restSeconds = 0.6 def __init__(self, seed=0): self.rng = rng_from(seed) self.player = OneShotPlayer() self.clickFilter = Biquad("bandpass") self.time = 0.0 self.nextClick = 0.0 self.index = 0 def make_click(self, tension): length = int(sampleRate * 0.06) times = np.arange(length) / sampleRate snap = self.clickFilter.process(self.rng.standard_normal(length), 1500.0 + 600.0 * tension, 5.0) snap *= np.exp(-times / 0.006) knock = np.sin(2.0 * math.pi * (240.0 + 80.0 * tension) * times) * np.exp(-times / 0.02) return 0.6 * snap + 0.5 * knock def render(self, frameCount): if self.time >= self.nextClick: step = self.index % self.clicksPerWind self.player.add(self.make_click(step / (self.clicksPerWind - 1))) self.index += 1 last = step == self.clicksPerWind - 1 self.nextClick += self.clickSeconds + (self.restSeconds if last else 0.0) self.time += frameCount / sampleRate return self.player.render(frameCount)