Close

Creating sound ambiences using exponential distribution

A project log for Protomatrix

First breadboard with a matrix router

vlad-shcherbakovVlad Shcherbakov 10/12/2025 at 20:060 Comments

In the previous log I made a sound amplifier that can be used to play a sound onto a speaker from line out of a controller or a similar device. Since the Halloween is approaching you could use it play some creepy sounds. Let say you have a crypt where you hear a scream of a tortured soul every minute. This is well-modeled by an exponential distribution with PDF (Probability Density Function): 

Integrating this PDF from 0 to a, gives CDF (Cumulative Distribution Function):

The meaning of it is "what is the probability that I will wait a minutes for event to happen". In our case, what is probability that you will hear a scream after a minutes of not hearing anybody scream in the crypt.

Now, how do you turn that into a program?

The process is as follows, first you generate a uniformly distributed number U:

U = random.random()

 Equate that to the CDF and solve for a

The intuition here is that we are sampling CDF using a uniform distribution, and converting from probability value to wait time by solving for a.

Here is just a wait time in seconds (units don't matter) between playing sounds of different tortured souls. Python implementation using pygame for playing sounds shows below how to create a dismal atmosphere using sounds from Dark Messiah Of Might and Magic.

import pygame
import time
import random
import math
import os

pygame.mixer.init()
pygame.mixer.music.load("crypt_main_01.wav")
pygame.mixer.music.play(-1)

sound_fx = os.listdir("sounds")

random.seed(18512158625243168)
lmda = 1/60 # average rate of sound effects

while pygame.mixer.music.get_busy(): 
    dt = -(1/lmda)*math.log(1-random.random()) 
    pygame.mixer.Sound(os.path.join("sounds", random.choice(sound_fx))).play() 
    print("Sleeping for", dt, "seconds\n") 
    time.sleep(dt)

The result of that is that the sound effects are playing "at random", but on average they are spaced exactly 1 minute apart (lmda=1/60).

Playing sound effect  talios_underground_drip_01.wav
Sleeping for 128.69566205426352 seconds

Playing sound effect  womb_pit-spiders2.wav
Sleeping for 77.31571960719558 seconds

Playing sound effect  crypt_main_01-voice02.wav
Sleeping for 118.22675017236637 seconds

Playing sound effect  a_windgust03.wav
Sleeping for 9.406140801162172 seconds

Playing sound effect  talios_underground_drip_05.wav
Sleeping for 143.527373362454 seconds

Playing sound effect  karthal_city_interior_rndc_03.wav
Sleeping for 233.31308460867473 seconds

Playing sound effect  boat_inside_rndb_03.wav
Sleeping for 34.25852255619906 seconds

Playing sound effect  menelag_inside-violins05.wav
Sleeping for 45.78918863544417 seconds

Playing sound effect  stonehelm_paokai_int_wind_02.wav
Sleeping for 26.795201242341257 seconds

Playing sound effect  kharthal_roofs-thunder03.wav
Sleeping for 40.608196333385564 seconds

Playing sound effect  boat_inside-creak01.wav
Sleeping for 79.57805612346229 seconds

Playing sound effect  karthal_city_cellar_rndc_03.wav
Sleeping for 7.086597576435293 seconds

Playing sound effect  stonehelm_paokai_ext_wind_02.wav
Sleeping for 74.11938743159087 seconds

Playing sound effect  goblin_main-bat03.wav
Sleeping for 19.494657143535072 seconds

Playing sound effect  talios_inside_no_wind.wav
Sleeping for 56.11492592704442 seconds

Playing sound effect  talios_inside-creaks01.wav
Sleeping for 65.92188091086814 seconds

Playing sound effect  orc_caves_02_creak_04.wav
Sleeping for 114.70192969525529 seconds

Playing sound effect  kharthal_roofs-thunder03.wav
Sleeping for 53.66492417273082 seconds

Playing sound effect  womb_03_3.wav
Sleeping for 1.850743553746255 seconds

Playing sound effect  crypt_catacombs.wav
Sleeping for 46.001320343673086 seconds

Playing sound effect  karthal_city_interior_rndb_02.wav
Sleeping for 30.43711004687171 seconds

Playing sound effect  upper_temple_main.wav
Sleeping for 6.600836081999313 seconds

Playing sound effect  karthal_gates-bird0204.wav
Sleeping for 53.59866821050122 seconds

Playing sound effect  crypt_goddess.wav
Sleeping for 101.2902363567874 seconds

Playing sound effect  orc_caves_02_wind_gust_04.wav
Sleeping for 48.05731083376595 seconds

Playing sound effect  orc_caves_01_small_river_01.wav
Sleeping for 42.116406568532575 seconds

Playing sound effect  karthal_gates-bird0101.wav
Sleeping for 141.33458066209457 seconds

Playing sound effect  boat_outside_rndd_01.wav
Sleeping for 23.410817717634007 seconds

Playing sound effect  boat_inside-creak02.wav
Sleeping for 105.39932936049989 seconds

Playing sound effect  boat_inside_rnde_02.wav
Sleeping for 15.517968358066575 seconds

Playing sound effect  crypt_main_03_02.wav
Sleeping for 34.4874963559264 seconds

Playing sound effect  womb_03_2.wav
Sleeping for 18.18784523134326 seconds

Playing sound effect  orc_caves_02_creak_02.wav
Sleeping for 47.969406439382674 seconds

Playing sound effect  crypt_main_05_01.wav
Sleeping for 49.53160356370553 seconds

Playing sound effect  temple_stairs_02-tone03.wav
Sleeping for 17.15574922570256 seconds

Playing sound effect  a_rockfalling02.wav
Sleeping for 8.342460896115393 seconds

Playing sound effect  karthal_gates-dog0202.wav
Sleeping for 2.5383012869378447 seconds

Playing sound effect  orc_cyclops.wav
Sleeping for 168.82321786938795 seconds

Playing sound effect  karthal_city_interior.wav
Sleeping for 29.775608361974413 seconds

Playing sound effect  crypt_catacombs-drips02.wav
Sleeping for 56.719310252147324 seconds

Playing sound effect  talios_inside-creaks02.wav
Sleeping for 53.343953975100156 seconds

Playing sound effect  boat_outside_rndd_03.wav
Sleeping for 1.9232440582360897 seconds

Playing sound effect  boat_inside_rnde_05.wav
Sleeping for 52.789288080099446 seconds

Playing sound effect  boat_inside_rnde_02.wav
Sleeping for 33.81348866316184 seconds

Playing sound effect  karthal_city_cellar_rnda_01.wav
Sleeping for 108.5107399666818 seconds

Playing sound effect  orc_caves_01_small_river.wav
Sleeping for 88.70394974369471 seconds

Playing sound effect  karthal_city_cellar_rndc_02.wav
Sleeping for 46.65599424100057 seconds

Playing sound effect  karthal_city_cellar_rnda_02.wav
Sleeping for 79.43392968844216 seconds

Playing sound effect  a_rockfalling02.wav
Sleeping for 36.106667120965014 seconds

Playing sound effect  orc_caves_01_small_river_02.wav
Sleeping for 15.475057283089406 seconds

Playing sound effect  temple_stairs_02-tone03.wav
Sleeping for 95.70672679528789 seconds

Playing sound effect  karthal_gates-dog0102.wav
Sleeping for 183.84268891105916 seconds

Playing sound effect  crypt_goddess.wav
Sleeping for 28.177534459415273 seconds

Playing sound effect  necropolis_goddess_voices3.wav
Sleeping for 7.271903475243708 seconds

Playing sound effect  stonehelm_fire_explowood_01.wav
Sleeping for 85.2453587303634 seconds

Playing sound effect  stonehelm_fire_explowood_04.wav
Sleeping for 7.432176949246249 seconds

Playing sound effect  temple_stairs_01.wav
Sleeping for 75.08470818533749 seconds

Playing sound effect  karthal_gates-dog0304.wav
Sleeping for 36.72286949890331 seconds

Exponential Distribution By Shandian Zhe

Discussions