Python으로 특정 주파수 소리 내기

 
import pyaudio import numpy as np p = pyaudio.PyAudio() volume = 0.5 # range [0.0, 1.0] fs = 44100 # sampling rate, Hz, must be integer duration = 5.0 # in seconds, may be float f = 440.0 # sine frequency, Hz, may be float # generate samples, note conversion to float32 array samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32) # for paFloat32 sample values must be in range [-1.0, 1.0] stream = p.open(format=pyaudio.paFloat32, channels=1, rate=fs, output=True) # play. May repeat with different volume values (if done interactively) stream.start_stream() stream.write(volume*samples) stream.stop_stream() stream.close() p.terminate()
 
import pyaudio import numpy as np import time p = pyaudio.PyAudio() volume = 0.5 # range [0.0, 1.0] fs = 44100 # sampling rate, Hz, must be integer duration = 1.0 # in seconds, may be float # Define the frequencies for each note C4 = 261.63 D4 = 293.66 E4 = 329.63 F4 = 349.23 G4 = 392.00 A4 = 440.00 B4 = 493.88 notes = [C4, D4, E4, F4, G4, A4, B4] for f in notes: samples = (np.sin(2 * np.pi * np.arange(fs * duration) * f / fs)).astype(np.float32) # for paFloat32 sample values must be in range [-1.0, 1.0] stream = p.open(format=pyaudio.paFloat32, channels=1, rate=fs, output=True) # play. May repeat with different volume values (if done interactively) stream.start_stream() stream.write(volume * samples) stream.stop_stream() stream.close() time.sleep(1) p.terminate()
 
import pyaudio import numpy as np import time p = pyaudio.PyAudio() volume = 0.5 # range [0.0, 1.0] fs = 44100 # sampling rate, Hz, must be integer duration = 1.0 # in seconds, may be float # Define the frequencies for each note C4 = 261.63 D4 = 293.66 E4 = 329.63 F4 = 349.23 G4 = 392.00 A4 = 440.00 B4 = 493.88 notes = {'C4': C4, 'D4': D4, 'E4': E4, 'F4': F4, 'G4': G4, 'A4': A4, 'B4': B4} note_sequence = input("Enter the sequence of notes to play (e.g. C4 D4 E4 F4 G4 A4 B4): ").split() for note in note_sequence: f = notes[note] samples = (np.sin(2 * np.pi * np.arange(fs * duration) * f / fs)).astype(np.float32) # for paFloat32 sample values must be in range [-1.0, 1.0] stream = p.open(format=pyaudio.paFloat32, channels=1, rate=fs, output=True) # play. May repeat with different volume values (if done interactively) stream.start_stream() stream.write(volume * samples) stream.stop_stream() stream.close() time.sleep(1) p.terminate()
 
import pyaudio import numpy as np import time p = pyaudio.PyAudio() volume = 0.5 # range [0.0, 1.0] fs = 44100 # sampling rate, Hz, must be integer duration = 1.0 # in seconds, may be float # Define the frequencies for each note C4 = 261.63 D4 = 293.66 E4 = 329.63 F4 = 349.23 G4 = 392.00 A4 = 440.00 B4 = 493.88 notes = {'C4': C4, 'D4': D4, 'E4': E4, 'F4': F4, 'G4': G4, 'A4': A4, 'B4': B4} note_sequence = input("Enter the sequence of notes to play (e.g. C4 D4 E4 F4 G4 A4 B4): ").split() for note in note_sequence: f = notes[note] samples = (np.sin(2 * np.pi * np.arange(fs * duration) * f / fs)).astype(np.float32) # for paFloat32 sample values must be in range [-1.0, 1.0] stream = p.open(format=pyaudio.paFloat32, channels=1, rate=fs, output=True) # play. May repeat with different volume values (if done interactively) stream.start_stream() stream.write(volume * samples) stream.stop_stream() stream.close() time.sleep(1) p.terminate()
사용자가 지정한 노트 시퀀스를 연주하기 위해서는 위 코드에 입력한 노트 시퀀스를 변수로 입력하면 됩니다. 예를 들어 사용자가 C4 D4 E4 F4 G4 A4 B4라는 노트 시퀀스를 입력한다면 다음과 같이 입력하면 됩니다.
note_sequence = 'C4 D4 E4 F4 G4 A4 B4'.split()
이후에는 노트 시퀀스를 반복하여 연주하기 위해 반복문을 사용합니다. 각 노트의 높이는 미리 정의한 notes 딕셔너리에서 받아올 수 있습니다. 이를 이용하여 pyaudio 모듈을 사용하여 소리를 재생할 수 있습니다. 그리고 마지막으로 반복문 사이에 sleep 함수를 사용하여 노트를 구분할 수 있습니다.
 
# Define a function to get the frequency of an octave's note name def get_note_freq(note_name): # Define the frequencies for each note C4 = 261.63 D4 = 293.66 E4 = 329.63 F4 = 349.23 G4 = 392.00 A4 = 440.00 B4 = 493.88 notes = {'C4': C4, 'D4': D4, 'E4': E4, 'F4': F4, 'G4': G4, 'A4': A4, 'B4': B4} return notes[note_name]
위 함수는 사용자가 입력한 계이름의 주파수를 구합니다. 계이름의 주파수는 미리 정의한 notes 딕셔너리에서 읽어옵니다. 함수는 인자로 계이름을 받아서 딕셔너리에서 해당하는 계이름의 주파수를 반환합니다. 예를 들어 사용자가 C4라는 계이름을 입력하면 함수는 261.63을 반환합니다.
import pyaudio import numpy as np import time p = pyaudio.PyAudio() volume = 0.5 # range [0.0, 1.0] fs = 44100 # sampling rate, Hz, must be integer duration = 1.0 # in seconds, may be float # Define the frequencies for each note C4 = 261.63 D4 = 293.66 E4 = 329.63 F4 = 349.23 G4 = 392.00 A4 = 440.00 B4 = 493.88 notes = {'C4': C4, 'D4': D4, 'E4': E4, 'F4': F4, 'G4': G4, 'A4': A4, 'B4': B4} # Define a function to get the frequency of an octave's note name def get_note_freq(note_name): notes = {'C4': C4, 'D4': D4, 'E4': E4, 'F4': F4, 'G4': G4, 'A4': A4, 'B4': B4} return notes[note_name] chord_sequence = input("Enter the sequence of chords to play (e.g. C4maj G4min A4maj): ").split() for chord in chord_sequence: chord_notes = chord.split('maj')[0].split('min')[0].split('aug')[0].split('dim')[0].split('+') # Get the frequencies of each note in the chord chord_freqs = [get_note_freq(note) for note in chord_notes] # Generate samples for the chord chord_samples = np.array([]) for freq in chord_freqs: samples = (np.sin(2 * np.pi * np.arange(fs * duration) * freq / fs)).astype(np.float32) chord_samples = np.append(chord_samples, samples) # for paFloat32 sample values must be in range [-1.0, 1.0] stream = p.open(format=pyaudio.paFloat32, channels=1, rate=fs, output=True) # play. May repeat with different volume values (if done interactively) stream.start_stream() stream.write(volume * chord_samples) stream.stop_stream() stream.close() time.sleep(1) p.terminate()
사용자가 지정한 화음 시퀀스를 연주하기 위해서는 위 코드에 입력한 화음 시퀀스를 변수로 입력하면 됩니다. 예를 들어 사용자가 C4maj G4min A4maj라는 화음 시퀀스를 입력한다면 다음과 같이 입력하면 됩니다.
chord_sequence = 'C4maj G4min A4maj'.split()
이후에는 화음 시퀀스를 반복하여 연주하기 위해 반복문을 사용합니다. 각 화음의 노트의 높이는 미리 정의한 함수에서 받아올 수 있습니다. 이를 이용하여 pyaudio 모듈을 사용하여 소리를 재생할 수 있습니다. 그리고 마지막으로 반복문 사이