Skip to content

Instantly share code, notes, and snippets.

@gcamfer

gcamfer/scale.py Secret

Created Apr 7, 2022
Embed
What would you like to do?
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