"""Rewards and pickups: sparkles, chimes, charges, beacons and items spilling.""" import math import numpy as np from .synth import Biquad, pulse_wave, rng_from, sampleRate, soften, square_wave def sparkle(): """Collecting something: quick rising twinkles and a ringing top note.""" parts = [] for frequency in (1318.5, 1568.0, 2093.0): times = np.arange(int(sampleRate * 0.07)) / sampleRate parts.append(np.sin(2.0 * math.pi * frequency * times) * np.exp(-times / 0.03)) tail = np.arange(int(sampleRate * 0.4)) / sampleRate parts.append(np.sin(2.0 * math.pi * 2637.0 * tail) * np.exp(-tail / 0.12)) return 0.4 * np.concatenate(parts) def bell_chime(count=1): """A high bell struck count times in quick succession, such as one to three for how hard a bend is.""" gap = int(sampleRate * 0.13) ringLength = int(sampleRate * 0.7) output = np.zeros(gap * (count - 1) + ringLength) times = np.arange(ringLength) / sampleRate ring = np.zeros(ringLength) for partial, level, decay in ((1.0, 1.0, 0.35), (2.76, 0.5, 0.2), (5.4, 0.25, 0.1)): ring += level * np.sin(2.0 * math.pi * 1046.5 * partial * times) * np.exp(-times / decay) ring *= np.minimum(1.0, times / 0.002) for index in range(count): output[index * gap:index * gap + ringLength] += ring return 0.3 * output def beacon_blip(): """One short square blip, for repeating quickly so a waiting item is easy to find by ear.""" blip = square_wave(500.0, 0.07) attack = int(sampleRate * 0.003) blip[:attack] *= np.linspace(0.0, 1.0, attack) release = int(sampleRate * 0.01) blip[-release:] *= np.linspace(1.0, 0.0, release) return 0.22 * soften(blip) def power_charge(): """A square wave charging up, rising and pulsing faster, then a bright ping.""" seconds = 0.9 times = np.arange(int(sampleRate * seconds)) / sampleRate progress = times / seconds # The pitch climbs from 250 to 1500 cycles, faster towards the end. frequency = 250.0 * 6.0 ** (progress ** 1.6) phaseStep = frequency / sampleRate phases = np.cumsum(phaseStep) % 1.0 charge = 2.0 * pulse_wave(phases, phaseStep, 0.5) # It pulses ever faster as it fills, from 8 to 40 pulses a second. pulseRate = 8.0 + 32.0 * progress pulses = 0.6 + 0.4 * np.sin(2.0 * math.pi * np.cumsum(pulseRate) / sampleRate) charge *= pulses * (0.5 + 0.5 * progress) pingTimes = np.arange(int(sampleRate * 0.5)) / sampleRate ping = square_wave(2000.0, 0.5) * np.exp(-pingTimes / 0.12) sound = np.concatenate((charge, ping)) attack = int(sampleRate * 0.005) sound[:attack] *= np.linspace(0.0, 1.0, attack) return 0.22 * soften(sound) def bonus_run(): """A quick rising run of square notes ending on a long high tone: a bigger reward than confirm_chime.""" parts = [] for frequency in (500.0, 630.0, 750.0, 1000.0): note = square_wave(frequency, 0.06) note[-int(sampleRate * 0.005):] *= np.linspace(1.0, 0.0, int(sampleRate * 0.005)) parts.append(note) tail = square_wave(1500.0, 0.5) * (1.0 - np.arange(int(sampleRate * 0.5)) / (sampleRate * 0.5)) ** 2.5 sound = np.concatenate(parts + [tail]) sound[:int(sampleRate * 0.003)] *= np.linspace(0.0, 1.0, int(sampleRate * 0.003)) return 0.22 * soften(sound) def rising_sweep(): """A quick bright sweep upwards, for repeating while an item waits to be picked up.""" length = int(sampleRate * 0.35) times = np.arange(length) / sampleRate frequency = 700.0 * 3.0 ** (times / times[-1]) phases = np.cumsum(frequency) / sampleRate sweep = np.sin(2.0 * math.pi * phases) + 0.3 * np.sin(4.0 * math.pi * phases) envelope = np.minimum(1.0, times / 0.01) * (1.0 - times / times[-1]) ** 1.5 return 0.35 * sweep * envelope def item_spill(count, seed=0): """Items spilling out: a quick run of falling plinks, one for each item lost (at most 10), then a light patter as the pieces bounce and settle.""" rng = rng_from(seed) plinks = max(1, min(10, count)) plinkSeconds = 0.09 gap = 0.05 patterStart = (plinks - 1) * gap + plinkSeconds output = np.zeros(int(sampleRate * (patterStart + 0.45))) times = np.arange(int(sampleRate * plinkSeconds)) / sampleRate for index in range(plinks): # Each plink drops in pitch as it falls, and each starts a little lower than the one before. start = 1900.0 * 2.0 ** (-index * 1.5 / 12.0) frequency = start * (1.0 - 0.35 * times / times[-1]) phases = np.cumsum(frequency) / sampleRate tone = np.sin(2.0 * math.pi * phases) + 0.25 * np.sin(6.0 * math.pi * phases) envelope = np.minimum(1.0, times / 0.003) * (1.0 - times / times[-1]) ** 2 offset = int(sampleRate * index * gap) output[offset:offset + len(tone)] += tone * envelope # The pieces landing: a few short ticks, quieter and further apart as they settle. tickFilter = Biquad("bandpass") tickLength = int(sampleRate * 0.012) moment = patterStart for index in range(min(6, 2 + plinks // 2)): tick = tickFilter.process(rng.standard_normal(tickLength), 3200.0, 4.0) tick *= np.linspace(1.0, 0.0, tickLength) * 0.9 ** index offset = int(sampleRate * moment) output[offset:offset + tickLength] += 0.8 * tick[:len(output) - offset] moment += rng.uniform(0.04, 0.08) return 0.3 * output / max(1e-9, np.abs(output).max())