#Sine wave music generator by Iykury #Copy this text into a .py file and run with Python 3 (Download at https://www.python.org/downloads/) #Note: Audio sounds a bit noisy because it's only an 8-bit LPCM #Notes to play (characters for notes in ASCII/Unicode order; ! is A0 and x is C8) #The uncommented line will be used for notes notes = list("TOTRPRTOTVTVXT[YXVXT[YXVTOTRPRTOTVTVXT[YXVXT[YXV") #Melody #notes = list("CHLAFJCHLAFJCHLAFJCHLAFJCHLAFJCHLAFJCHLAFJCHLAFJ") #Chords rate = 44100 #Sample rate amp = 127.5 #Amplitude (Maximum 127.5) lng = rate*.25 #Length of one unit of time in samples (rate*seconds) import math #Used to calculate sine fil = open("notes.raw", "w") #Open file fil.write("") #Delete all data in file fil.close() #Close file fil = open("notes.raw", "ab") #Open file again in binary and append mode nts = notes.__len__() #Number of notes to write trt = 2**(1/12) #Twelfth root of two a0 = 27.5 #Frequency of the lowest key on an 88-key piano for q in range(0, nts): notes[q] = a0*trt**(ord(notes[q])-33) #Replace characters with frequencies of notes wav = 0 #How far into the wave (0 = beginning, 1 = end) for i in range(0, nts): #If note character is a space i.e. the note is below A0, write silence if notes[i] < 27.5: smp = 0 byte = [128] while(smp < lng): fil.write(bytes(byte)) smp += 1 #Otherwise, write the note else: frq = notes[i] #Get note frequency smp = 0 #How far into the note (samples) while(smp < lng): if wav > 1: #If over 100% far into wave, subtract 100% wav = wav-1 #i.e. 105% becomes 5% byte = [round(math.sin(wav*2*math.pi)*amp+128)] #Calculate which byte to write based on how far into wave fil.write(bytes(byte)) #Write byte smp += 1 #Increase how far into note wav += frq/rate #Increase how far into wave