Skip to content

Instantly share code, notes, and snippets.

@gcamfer

gcamfer/scale.py Secret

Created April 7, 2022 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcamfer/8868acbe033bc3fa02028f35739188ce to your computer and use it in GitHub Desktop.
Save gcamfer/8868acbe033bc3fa02028f35739188ce to your computer and use it in GitHub Desktop.
import pyaudio
import matplotlib.pyplot as plt
import numpy as np
PyAudio = pyaudio.PyAudio
from scipy.io.wavfile import write
def sound_wave(wave, fs):
p = PyAudio()
stream = p.open(format = pyaudio.paFloat32, channels = 1, rate = fs, output = True)
stream.write(wave)
stream.stop_stream()
stream.close()
p.terminate()
def save_wave(wave, fs, name):
write(name, fs, wave)
fs = 44100 # sampling rate = 1/s
ts = 1/fs
duration = 2 # seconds
sampling_points = duration*fs
notas_freqs = {
'do': 261.63,
're': 293.66,
'mi': 329.63,
'fa': 349.23,
'sol': 392,
'la': 440,
'si': 493.88
}
plt.figure(figsize=[15,5])
t = np.linspace(0.0, duration, sampling_points)
samples_bytes = {}
scale_samples = []
for nota in notas_freqs:
samples = (np.sin(2 * np.pi * t * notas_freqs[nota])).astype(np.float32)
samples_bytes[nota] = samples.tobytes()
scale_samples.append(samples)
plt.plot(t[:200],samples[:200],'-+', )
plt.legend(notes.keys())
plt.savefig('scale.svg')
for nota in notas_freqs:
print(f"Escuchando: {nota}")
sound_wave(samples_bytes[nota], fs)
save_wave(np.concatenate(scale_samples), fs, "scale.wav")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment