Skip to content

Instantly share code, notes, and snippets.

@gcamfer

gcamfer/chord.py Secret

Last active April 7, 2022 13:06
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/9b1b142a6f837ba14322aad27f3823a7 to your computer and use it in GitHub Desktop.
Save gcamfer/9b1b142a6f837ba14322aad27f3823a7 to your computer and use it in GitHub Desktop.
Create plot and reproduce chord
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
}
t = np.linspace(0.0, duration, sampling_points)
chord_waves = 0.3*(np.sin(2 * np.pi * t * notas_freqs['do'])).astype(np.float32) + \
0.3*(np.sin(2 * np.pi * t * notas_freqs['mi'])).astype(np.float32) + \
0.3*(np.sin(2 * np.pi * t * notas_freqs['sol'])).astype(np.float32)
plt.figure(figsize=[15,5])
plt.plot(t[:2000],chord_waves[:2000],'-+', )
plt.legend(["Acorde [do,mi,sol]"])
plt.savefig('chord.svg')
sound_wave(chord_waves.tobytes(), fs)
save_wave(chord_waves, fs, "chord.wav")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment