article 2/2026:Math to music with python

by Sande


Do you remember 1980's cool music tracks and those cool synth sounds? Well I remember and also those pretty amazing C64 SId s like in Giana sisters and commando and ... Now in 2026 I started to think that why not. Why not to try to make something similar and at same time practise litlle bit math and computing science from by retro angle. So hmm. I need formula like sine vawe and then if I littlebit modulate and then digitize this math formula to audio cd vaw format and listen what I can create? Sounds like we are back in 1980's

the basic math formula to put radius as time with sine to make sound

volume * sin(2 * pi * frequency * t) * exp(-20 * t)

And the python code

This script creates wav file from mathematical formula and can be used in linux and mac OS # libraries
import math
import wave
import struct
import os
import sys

# initial variables
filename = "mathsound.wav"
sample_rate = 44100
duration = 0.15
frequency = 1420
volume = 0.8
num_samples = int(sample_rate * duration)

# with opened file with parameters for wav
with wave.open(filename, 'w') as wav:
wav.setnchannels(1)
wav.setsampwidth(2)
wav.setframerate(sample_rate)

for i in range(num_samples):
t = i / sample_rate
sample = volume * math.sin(2 * math.pi * frequency * t ) * math.exp(-20 * t)
# some additional parameters to play with sounds
frequency = frequency + 5.1
volume = volume - 0.0001

# write data to wav file that is the container for digital sound
wav.writeframes(struct.pack('
# play the sound twice in mac an in linux
if sys.platform == "darwin":
os.system(f"afplay {filename}")
os.system(f"afplay {filename}")
else:
os.system(f"aplay {filename}")
os.system(f"aplay {filename}")

created sounds in wav format

laser sound:


zapper sound:


bongo sound: