Skip to content

Instantly share code, notes, and snippets.

@thonerohh
Created April 20, 2024 15:53
Show Gist options
  • Save thonerohh/1d4399595feb8464ee9fcfe661be8afc to your computer and use it in GitHub Desktop.
Save thonerohh/1d4399595feb8464ee9fcfe661be8afc to your computer and use it in GitHub Desktop.
Another unsuccessful shot
import numpy as np
import soundfile as sf
# distribute the frequencies according patterns
# value in array means how much times the value will be duplicated
# every next value takes previous value and adds or removes 5 Hz from previous one
# Sample rate and duration of the sound
sample_rate = 44100 # Sample rate in Hz (e.g., CD quality)
duration = 10.0 # Duration of the sound in seconds
# Generate time array
t = np.linspace(0, duration, int(duration * sample_rate), endpoint=False)
# Initialize an empty waveform array
waveform = np.zeros(len(t))
# Generate a simple sine wave
duration = 5 # Duration in seconds
sample_rate = 44100 # Sampling rate (Hz)
# frequencies of the sine wave (Hz)
frequencies = [150]
index = 150
patterns = [
[2,1,1],
[2,2,1],
[2,2,2],
[2,1,2],
[1,2,1],
[1,2,2],
[1,1,2],
[1,1,1]
]
for pattern in patterns:
for _ in range(pattern[0]):
frequencies.append(frequencies[-1] + (5 if pattern[2] > 1 else -5) if index > 0 else 170)
for _ in range(pattern[1]):
frequencies.append(frequencies[-1] + (0 if pattern[2] > 1 else 5))
for _ in range(pattern[2]):
frequencies.append(frequencies[-1] + (-5 if pattern[2] > 1 else 0))
index += 1
print(frequencies)
frequencies = [
170, 175, 180, 180, 185, 185, 185, 190, 190, 190,
190, 195, 200, 200, 200, 200, 205, 210, 210, 210,
210, 215, 215, 215, 215, 220, 220, 225, 225, 225,
225, 230, 230, 230, 235
]
# Generate waveform by summing sinusoids of each frequencies
for freq in frequencies:
waveform += np.sin(2 * np.pi * freq * t)
# Normalize the waveform to be between -1 and 1 (optional but recommended)
waveform /= np.max(np.abs(waveform))
# Save the waveform as a WAV file
sf.write("output.wav", waveform, sample_rate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment