User:Iii kaisei iii/31edo sound code

Revision as of 11:58, 28 July 2026 by Iii kaisei iii (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This code generates a synthesized sound whose harmonic series is rounded to the nearest steps of 31edo.

# Synthesizing a timbre for 31edo

import numpy as np
import soundfile as sf

sample_rate = 48_000
duration = 3.0
base_frequency = 440.0
k = 0.4

t = np.arange(int(sample_rate * duration)) / sample_rate

# Positions of the first 13 harmonics, rounded to the nearest 31edo steps
steps = [0, 31, 49, 62, 72, 80, 87, 93, 98, 103, 107, 111, 115]

# Assign an exponentially decreasing amplitude to each partial
partials = {}

for i, step in enumerate(steps):
    amplitude = np.exp(-k * i)
    partials[step] = amplitude

# Generate the waveform by summing the partials
wave = np.zeros_like(t)

for step, amplitude in partials.items():
    frequency = base_frequency * 2 ** (step / 31)
    wave += amplitude * np.sin(2 * np.pi * frequency * t)

# Apply short fade-in and fade-out ramps to prevent clicks
fade_length = int(sample_rate * 0.005)
fade = np.linspace(0, 1, fade_length)

wave[:fade_length] *= fade
wave[-fade_length:] *= fade[::-1]

# Peak-normalize the waveform and leave a small amount of headroom
wave /= np.max(np.abs(wave))
wave *= 0.9

sf.write("31edo_sound.wav", wave, sample_rate)

The MIT License (MIT)

Copyright (c) 2026 Kaisei

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.