Skip to content

Instantly share code, notes, and snippets.

@ninadpchaudhari
Created November 14, 2024 04:01
Show Gist options
  • Save ninadpchaudhari/b99a110f25ce6fc86904f4738a0f1257 to your computer and use it in GitHub Desktop.
Save ninadpchaudhari/b99a110f25ce6fc86904f4738a0f1257 to your computer and use it in GitHub Desktop.
Temporarily hosted for Ahyaan
# Ahyaan Malik - Submitted 11/10/24
# Resubmitted 11/11/24 due to extra time - Added two new sounds for less awkwardness, improved rick roll by adding a new bass then blended that bass
# Sound Collage - Creates an audio collage with various effects
# Professor Chaudhari
# There are 2 original segments, a modification for each of those 2 segments (2 for the 2nd one), 1 segment that isn't modified, and the rick roll created.
# Each segment, original or modified, is placed in an empty sound file with just the right amount of samples
# Each segment is separated by 0.5 seconds
# Segments with alterations went through increased speed, double frequency, halved frequency, and volume changed for random portions. Specifics are described by the function
# The Rick Roll is an additional thing, described in the extra points section.
# EXTRA POINTS - I've attempted to use more difficult alteration functions, like one that ramps up the speed, and one that randomizes the modified "blocks" of the audio
# EXTRA POINTS - Additionally, I researched and manually created all the notes from g4 to f5 needed to create my grand piece, Never Gonna Give You Up
# EXTRA POINTS - After creating those note audios, I had to manually create the song itself, by precisely placing the audios at the correct positions to create the song
# EXTRA POINTS - I also created a bass for the rick roll, and had to blend it into the melody section. (Incorrect terminology may be used for these music related stuff, I'm a cs major, not a music major)
from JES import *
from random import *
from math import *
def noteMaker(): # Creates all the notes needed for the rickroll
bpm114quarternote = int(0.526316*22050) # int since it might be a decimal
c5note = makeEmptySound(bpm114quarternote)
samplesPerSecond = 22050//523.25 # Divides sample rate by desired frequency to get the samples per second
for i in range(0, bpm114quarternote):
if (i//(samplesPerSecond//2))%2 == 0: # This took alot of research and looks very weird, but many many square waves are created based on the frequency. The samplesPerSecond divided by 2 gives the amount of samples per high (or low) of each wave. i//(samplesPerSecond//2) tells us how many half cycles we've passed, and the %2 checks if we're in a high cycle or a low cycle.
amplitude = 32767 # Max amplitude
else:
amplitude = -32767 # Min amplitude
setSampleValueAt(c5note, i, amplitude)
writeSoundTo(c5note, "c5note.wav")
g4note = makeEmptySound(bpm114quarternote)
samplesPerSecond = 22050//329.63 # Divides sample rate by desired frequency to get the samples per second
for i in range(0, bpm114quarternote):
if (i//(samplesPerSecond//2))%2 == 0:
amplitude = 32767 # Max amplitude
else:
amplitude = -32767 # Min amplitude
setSampleValueAt(g4note, i, amplitude)
writeSoundTo(g4note, "g4note.wav")
a4note = makeEmptySound(bpm114quarternote)
samplesPerSecond = 22050//440.00 # Divides sample rate by desired frequency to get the samples per second
for i in range(0, bpm114quarternote):
if (i//(samplesPerSecond//2))%2 == 0:
amplitude = 32767 # Max amplitude
else:
amplitude = -32767 # Min amplitude
setSampleValueAt(a4note, i, amplitude)
writeSoundTo(a4note, "a4note.wav")
b4note = makeEmptySound(bpm114quarternote)
samplesPerSecond = 22050//493.88 # Divides sample rate by desired frequency to get the samples per second
for i in range(0, bpm114quarternote):
if (i//(samplesPerSecond//2))%2 == 0:
amplitude = 32767 # Max amplitude
else:
amplitude = -32767 # Min amplitude
setSampleValueAt(b4note, i, amplitude)
writeSoundTo(b4note, "b4note.wav")
d5note = makeEmptySound(bpm114quarternote)
samplesPerSecond = 22050//587.33 # Divides sample rate by desired frequency to get the samples per second
for i in range(0, bpm114quarternote):
if (i//(samplesPerSecond//2))%2 == 0:
amplitude = 32767 # Max amplitude
else:
amplitude = -32767 # Min amplitude
setSampleValueAt(d5note, i, amplitude)
writeSoundTo(d5note, "d5note.wav")
e5note = makeEmptySound(bpm114quarternote)
samplesPerSecond = 22050//659.26 # Divides sample rate by desired frequency to get the samples per second
for i in range(0, bpm114quarternote):
if (i//(samplesPerSecond//2))%2 == 0:
amplitude = 32767 # Max amplitude
else:
amplitude = -32767 # Min amplitude
setSampleValueAt(e5note, i, amplitude)
writeSoundTo(e5note, "e5note.wav")
f5note = makeEmptySound(bpm114quarternote)
samplesPerSecond = 22050//698.46 # Divides sample rate by desired frequency to get the samples per second
for i in range(0, bpm114quarternote):
if (i//(samplesPerSecond//2))%2 == 0:
amplitude = 32767 # Max amplitude
else:
amplitude = -32767 # Min amplitude
setSampleValueAt(f5note, i, amplitude)
writeSoundTo(f5note, "f5note.wav")
e2note = makeEmptySound(bpm114quarternote) # For bass clef section
samplesPerSecond = 22050//82.41 # Divides sample rate by desired frequency to get the samples per second
for i in range(0, bpm114quarternote):
if (i//(samplesPerSecond//2))%2 == 0:
amplitude = 32767 # Max amplitude
else:
amplitude = -32767 # Min amplitude
setSampleValueAt(e2note, i, amplitude)
writeSoundTo(e2note, "e2note.wav")
f2note = makeEmptySound(bpm114quarternote)
samplesPerSecond = 22050//87.31 # Divides sample rate by desired frequency to get the samples per second
for i in range(0, bpm114quarternote):
if (i//(samplesPerSecond//2))%2 == 0:
amplitude = 32767 # Max amplitude
else:
amplitude = -32767 # Min amplitude
setSampleValueAt(f2note, i, amplitude)
writeSoundTo(f2note, "f2note.wav")
g2note = makeEmptySound(bpm114quarternote)
samplesPerSecond = 22050//98.00 # Divides sample rate by desired frequency to get the samples per second
for i in range(0, bpm114quarternote):
if (i//(samplesPerSecond//2))%2 == 0:
amplitude = 32767 # Max amplitude
else:
amplitude = -32767 # Min amplitude
setSampleValueAt(g2note, i, amplitude)
writeSoundTo(g2note, "g2note.wav")
a2note = makeEmptySound(bpm114quarternote)
samplesPerSecond = 22050//110.00 # Divides sample rate by desired frequency to get the samples per second
for i in range(0, bpm114quarternote):
if (i//(samplesPerSecond//2))%2 == 0:
amplitude = 32767 # Max amplitude
else:
amplitude = -32767 # Min amplitude
setSampleValueAt(a2note, i, amplitude)
writeSoundTo(a2note, "a2note.wav")
def rickRoll(): # Creates a rick roll by modifying frequencies at specific sample indexes
noteMaker() # Creates the notes
notelength16 = int(0.131579*22050) # 16th notes which are used alot
empty16interval = makeEmptySound(notelength16//4) # Creates a 64th empty sound for repeating notes
empty16length = getLength(empty16interval)
rickrollsong = makeEmptySound(128*notelength16 + 5*empty16length)
g4 = makeSound("g4note.wav") # Imports all the created notes in
a4 = makeSound("a4note.wav")
b4 = makeSound("b4note.wav")
c5 = makeSound("c5note.wav")
d5 = makeSound("d5note.wav")
e5 = makeSound("e5note.wav")
f5 = makeSound("f5note.wav")
e2 = makeSound("e2note.wav") # Bass Clef Notes
f2 = makeSound("f2note.wav")
g2 = makeSound("g2note.wav")
a2 = makeSound("a2note.wav")
copy(g4, rickrollsong, 0)
copy(a4, rickrollsong, notelength16)
copy(c5, rickrollsong, 2*notelength16)
copy(a4, rickrollsong, 3*notelength16)
copy(e5, rickrollsong, 4*notelength16)
copy(empty16interval, rickrollsong, 7*notelength16)
copy(e5, rickrollsong, 7*notelength16 + empty16length)
copy(d5, rickrollsong, 10*notelength16 + empty16length) # Quarter rest after
copy(g4, rickrollsong, 16*notelength16 + empty16length)
copy(a4, rickrollsong, 17*notelength16 + empty16length)
copy(c5, rickrollsong, 18*notelength16 + empty16length)
copy(a4, rickrollsong, 19*notelength16 + empty16length)
copy(d5, rickrollsong, 20*notelength16 + empty16length)
copy(empty16interval, rickrollsong, 23*notelength16 + empty16length)
copy(d5, rickrollsong, 23*notelength16 + 2*empty16length)
copy(c5, rickrollsong, 26*notelength16 + 2*empty16length)
copy(b4, rickrollsong, 29*notelength16 + 2*empty16length)
copy(a4, rickrollsong, 30*notelength16 + 2*empty16length)
copy(g4, rickrollsong, 32*notelength16 + 2*empty16length)
copy(a4, rickrollsong, 33*notelength16 + 2*empty16length)
copy(c5, rickrollsong, 34*notelength16 + 2*empty16length)
copy(a4, rickrollsong, 35*notelength16 + 2*empty16length)
copy(c5, rickrollsong, 36*notelength16 + 2*empty16length)
copy(d5, rickrollsong, 40*notelength16 + 2*empty16length)
copy(b4, rickrollsong, 42*notelength16 + 2*empty16length)
copy(a4, rickrollsong, 45*notelength16 + 2*empty16length)
copy(g4, rickrollsong, 46*notelength16 + 2*empty16length)
copy(empty16interval, rickrollsong, 50*notelength16 + 2*empty16length)
copy(g4, rickrollsong, 50*notelength16 + 3*empty16length)
copy(d5, rickrollsong, 52*notelength16 + 3*empty16length)
copy(c5, rickrollsong, 56*notelength16 + 3*empty16length)
copy(g4, rickrollsong, 64*notelength16 + 3*empty16length)
copy(a4, rickrollsong, 65*notelength16 + 3*empty16length)
copy(c5, rickrollsong, 66*notelength16 + 3*empty16length)
copy(a4, rickrollsong, 67*notelength16 + 3*empty16length)
copy(e5, rickrollsong, 68*notelength16 + 3*empty16length)
copy(empty16interval, rickrollsong, 71*notelength16 + 3*empty16length)
copy(e5, rickrollsong, 71*notelength16 + 4*empty16length)
copy(d5, rickrollsong, 74*notelength16 + 4*empty16length) # Quarter rest after again
copy(g4, rickrollsong, 80*notelength16 + 4*empty16length)
copy(a4, rickrollsong, 81*notelength16 + 4*empty16length)
copy(c5, rickrollsong, 82*notelength16 + 4*empty16length)
copy(a4, rickrollsong, 83*notelength16 + 4*empty16length)
copy(f5, rickrollsong, 84*notelength16 + 4*empty16length)
copy(b4, rickrollsong, 88*notelength16 + 4*empty16length)
copy(c5, rickrollsong, 90*notelength16 + 4*empty16length)
copy(b4, rickrollsong, 93*notelength16 + 4*empty16length)
copy(a4, rickrollsong, 94*notelength16 + 4*empty16length)
copy(g4, rickrollsong, 96*notelength16 + 4*empty16length)
copy(a4, rickrollsong, 97*notelength16 + 4*empty16length)
copy(c5, rickrollsong, 98*notelength16 + 4*empty16length)
copy(a4, rickrollsong, 99*notelength16 + 4*empty16length)
copy(c5, rickrollsong, 100*notelength16 + 4*empty16length)
copy(d5, rickrollsong, 104*notelength16 + 4*empty16length)
copy(b4, rickrollsong, 106*notelength16 + 4*empty16length)
copy(a4, rickrollsong, 109*notelength16 + 4*empty16length)
copy(g4, rickrollsong, 110*notelength16 + 4*empty16length)
copy(empty16interval, rickrollsong, 114*notelength16 + 4*empty16length)
copy(g4, rickrollsong, 114*notelength16 + 5*empty16length)
copy(d5, rickrollsong, 116*notelength16 + 5*empty16length)
copy(c5, rickrollsong, 120*notelength16 + 5*empty16length)
rickrollbass = makeEmptySound(getLength(rickrollsong)) # rick roll song melody is slightly longer
copy(f2, rickrollbass, 4*notelength16) # Starts with a quarter rest
copy(empty16interval, rickrollbass, 7*notelength16)
copy(f2, rickrollbass, 7*notelength16 + empty16length)
copy(g2, rickrollbass, 10*notelength16 + empty16length) # Half rest after
copy(e2, rickrollbass, 20*notelength16 + empty16length)
copy(empty16interval, rickrollbass, 23*notelength16 + empty16length)
copy(e2, rickrollbass, 23*notelength16 + 2*empty16length)
copy(a2, rickrollbass, 26*notelength16 + 2*empty16length) # Half rest after
copy(f2, rickrollbass, 36*notelength16 + 2*empty16length)
copy(g2, rickrollbass, 42*notelength16 + 2*empty16length)
copy(e2, rickrollbass, 52*notelength16 + 2*empty16length)
copy(a2, rickrollbass, 58*notelength16 + 2*empty16length)
copy(f2, rickrollbass, 68*notelength16 + 2*empty16length)
copy(empty16interval, rickrollbass, 65*notelength16 + 2*empty16length)
copy(f2, rickrollbass, 71*notelength16 + 3*empty16length)
copy(g2, rickrollbass, 74*notelength16 + 3*empty16length) # Half rest after
copy(e2, rickrollbass, 84*notelength16 + 3*empty16length)
copy(a2, rickrollbass, 90*notelength16 + 3*empty16length) # Half rest after
copy(f2, rickrollbass, 100*notelength16 + 3*empty16length)
copy(g2, rickrollbass, 106*notelength16 + 3*empty16length)
copy(e2, rickrollbass, 116*notelength16 + 3*empty16length)
copy(a2, rickrollbass, 120*notelength16 + 3*empty16length)
rickrollfull = makeEmptySound(getLength(rickrollsong))
for i in range(0, getLength(rickrollsong)):
val1 = getSampleValueAt(rickrollsong, i)
val2 = getSampleValueAt(rickrollbass, i)
setSampleValueAt(rickrollfull, i, val1*0.7 + val2*0.3) # Weighted to not let bass take over
writeSoundTo(rickrollfull, "NeverGonna4.wav") # Finally outputs the file
def speedUpExp(sound): # This function speeds up an audio exponentially for half its original length
len = getLength(sound)//2
spedup = makeEmptySound(len)
i = 1
for k in range(0, len):
val = getSampleValueAt(sound, int(k+i)) # int is used since i is mostly a decimal
setSampleValueAt(spedup, k, val)
i = i + sqrt(k)*0.004 # This may seem very random, but it's a good exponential curve
return spedup # outputs a return statement to be used later
def makeDoubleFreq(sound): # Doubles the frequency of any sound, part of alteration 1 requirement
lenDoubleFreq = getLength(sound)//2
doubleFreq = makeEmptySound(lenDoubleFreq)
for k in range(0, lenDoubleFreq):
val = getSampleValueAt(sound, k*2)
setSampleValueAt(doubleFreq, k, val)
return doubleFreq # outputs a return statement to be used later
def makeHalfFreq(sound): # Halves the frequency of any sound, part of alteration 1 requirement again
lenHalfFreq = getLength(sound)*2
halfFreq = makeEmptySound(lenHalfFreq)
for k in range(0, lenHalfFreq):
val = getSampleValueAt(sound, k//2)
setSampleValueAt(halfFreq, k, val)
return halfFreq # outputs a return statement to be used later
def volumeRandomness(sound): # Changes the volume to either -32000 or 32000 depending on if the amplitude is negative for random 1/10 portions of the sound, part of alteration 2 requirement
volumerandom = makeEmptySound(getLength(sound))
segmentLength = getLength(sound)//10
for i in range(0,10):
choice = randint(0,1) # 50/50 chance for the 1/10th segment to be changed
for k in range(0, segmentLength):
val = getSampleValueAt(sound, i*segmentLength + k)
if choice == 1:
if val > 0:
setSampleValueAt(volumerandom, i*segmentLength + k, 32000)
elif val < 0:
setSampleValueAt(volumerandom, i*segmentLength + k, -32000)
else:
setSampleValueAt(volumerandom, i*segmentLength + k, val)
return volumerandom # outputs a return statement to be used later
# main function
def soundCollage(): # Creates the sound collage itself
s1og = makeSound("DonPollo1.wav") # s1 original
s1 = clip(s1og, 0, 22050*5) # The audio itself is too long, so I will only use the first 5 seconds
s2 = makeSound("OhMyGod2.wav")
s3 = makeSound("HelloMoto3.wav")
emptysound = makeEmptySound(11025) # Creates a 0.5 second long empty sound
commentbelow = makeSound("CommentBelow.wav")
hoodclassic = makeSound("HoodClassic.wav") # Added after
rickRoll() # Calls the Rick Roll function to create the audio file
s4 = makeSound("NeverGonna4.wav") # The Rick Roll function itself
s5 = makeSound("FriesBag5.wav") # Added before resubmittion
s6 = makeSound("XIAHONSHU6.wav") # Added before resubmittion
modifieds1 = speedUpExp(s1) # All modifications done here
modifieds2 = makeDoubleFreq(s2)
modifieds3 = makeHalfFreq(s2)
modifieds4 = volumeRandomness(s3)
canvasLen = getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + getLength(modifieds4) + getLength(modifieds2) + getLength(commentbelow) + getLength(s4) + getLength(s5) + getLength(hoodclassic) + getLength(s6) + 9*getLength(emptysound) # Gets the length of the canvas
canvas = makeEmptySound(canvasLen)
copy(s1, canvas, 0)
copy(emptysound, canvas, getLength(s1))
copy(modifieds3, canvas, getLength(s1) + getLength(emptysound))
copy(emptysound, canvas, getLength(s1) + getLength(emptysound) + getLength(modifieds3))
copy(modifieds1, canvas, getLength(s1) + getLength(modifieds3) + 2*getLength(emptysound))
copy(emptysound, canvas, getLength(s1) + getLength(modifieds1) + 2*getLength(emptysound) + getLength(modifieds3))
copy(s2, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + 3*getLength(emptysound))
copy(emptysound, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + 3*getLength(emptysound))
copy(modifieds4, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + 4*getLength(emptysound))
copy(emptysound, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + getLength(modifieds4) + 4*getLength(emptysound))
copy(modifieds2, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + getLength(modifieds4) + 5*getLength(emptysound))
copy(emptysound, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + getLength(modifieds4) + getLength(modifieds2) + 5*getLength(emptysound))
copy(commentbelow, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + getLength(modifieds4) + getLength(modifieds2) + 6*getLength(emptysound))
copy(emptysound, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + getLength(modifieds4) + getLength(modifieds2) + getLength(commentbelow) + 6*getLength(emptysound))
copy(hoodclassic, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + getLength(modifieds4) + getLength(modifieds2) + getLength(commentbelow) + 7*getLength(emptysound))
copy(emptysound, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + getLength(modifieds4) + getLength(modifieds2) + getLength(commentbelow) + getLength(hoodclassic) + 7*getLength(emptysound))
copy(s4, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + getLength(modifieds4) + getLength(modifieds2) + getLength(commentbelow) + getLength(hoodclassic) + 8*getLength(emptysound))
copy(emptysound, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + getLength(modifieds4) + getLength(modifieds2) + getLength(commentbelow) + getLength(s4) + getLength(hoodclassic) + 8*getLength(emptysound))
copy(s5, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + getLength(modifieds4) + getLength(modifieds2) + getLength(commentbelow) + getLength(s4) + getLength(hoodclassic) + 9*getLength(emptysound))
copy(s6, canvas, getLength(s1) + getLength(modifieds1) + getLength(modifieds3) + getLength(s2) + getLength(modifieds4) + getLength(modifieds2) + getLength(commentbelow) + getLength(s4) + getLength(s5) + getLength(hoodclassic) + 9*getLength(emptysound)) # Intentionally no gap
#write out the final sound
writeSoundTo(canvas, "collage.wav") # Writes the sound
return canvas # Returns the sound
# functions above
################
# test code below
canvas = soundCollage() # Gets the canvas from the sound collage for final printing
print("Finished Running!")
print("")
print("Canvas Information:")
print("Length:", getDuration(canvas), "Seconds", "|", getLength(canvas), "Samples", "|", "Sampling Rate:", getSamplingRate(canvas))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment