import numpy as np samplerate = 36*44100 #Frequecy in Hz def get_wave(freq, duration=0.5): ''' Function takes the "frequecy" and "time_duration" for a wave as the input and returns a "numpy array" of values at all points in time ''' amplitude = 4096 intervalo=0.03 t = np.linspace(0, duration, int(samplerate * duration)) wave = amplitude * np.sin(2 * np.pi * freq * t) mudulacion=np.ones(len(wave)) for i in range(0,len(t)): if(t[i] < intervalo): wave[i]*=(t[i])/intervalo if(t[i] < (duration*(1-intervalo))): wave[i]*=-(t[i]-duration)/intervalo return wave # To get a 1 second long wave of frequency 440Hz a_wave = get_wave(440, 1) #wave features print(len(a_wave)) # 44100 print(np.max(a_wave)) # 4096 print(np.min(a_wave)) # -4096 from pprint import pprint n_oct=9 #OCTAVA NÚMERO print("octava:",n_oct) def get_piano_notes(): ''' Returns a dict object for all the piano note's frequencies ''' # White keys are in Uppercase and black keys (sharps) are in lowercase octave = ['D', 'D#', 'R', 'R#', 'M', 'F', 'F#', 'So', 'So#', 'L', 'L#', 'Si'] base_freq = pow(2,-9/12.)*440 #261.63 #Frequency of Note Do_3 #n_oct=3 fac_oc=pow(2,(n_oct-3)) note_freqs = {octave[i]: fac_oc*base_freq * pow(2,(i/12)) for i in range(len(octave))} note_freqs[''] = 0.0 # silent note return note_freqs # To get the piano note's frequencies note_freqs = get_piano_notes() pprint(note_freqs) ''' {'': 0.0, 'A': 440.00745824565865, 'B': 493.8916728538229, 'C': 261.63, 'D': 293.66974569918125, 'E': 329.63314428399565, 'F': 349.2341510465061, 'G': 392.0020805232462, 'a': 466.1716632541139, 'c': 277.18732937722245, 'd': 311.1322574981619, 'f': 370.00069432367286, 'g': 415.31173722644} ''' def get_song_data(music_notes): ''' Function to concatenate all the waves (notes) ''' note_freqs = get_piano_notes() # Function that we made earlier song = [get_wave(note_freqs[note]) for note in music_notes.split('-')] song = np.concatenate(song) return song #music_notes = 'C-C-G-G-A-A-G--F-F-E-E-D-D-C--G-G-F-F-E-E-D--G-G-F-F-E-E-D--C-C-G-G-A-A-G--F-F-E-E-D-D-C' #music_notes = 'C-C-G-G-A-A-G-F-F-E-E-D-D-C-G-G-F-F-E-E-D-G-G-F-F-E-E-D-C-C-G-G-A-A-G-F-F-E-E-D-D-C' #music_notes = 'M-R#-M-R#-M-Si-R-D-L-D-M-L-Si-M-So#-Si-D-M-M-R#-M-R#-M-Si-R-D-L-D-M-L-Si-M-D-Si-L-M-R#-M-R#-M-Si-R-D-L-Si-D-R-M-M-F-M-R' music_notes = 'M-M-F-So-So-F-M-R-D-D-R-M-M-R-R--M-M-F-So-So-F-M-R-D-D-R-M-R-D-D--R-R-M-D-R' data = get_song_data(music_notes) data = data * (16300/np.max(data)) # Adjusting the Amplitude (Optional) from scipy.io.wavfile import write nombre='FHABn_o' nombre+=str(n_oct)+str('.wav') write(nombre, samplerate, data.astype(np.int16))