Format code with Black

This commit is contained in:
deepsource-autofix[bot] 2020-07-07 14:55:32 +00:00 committed by GitHub
parent 6624f8fb1a
commit 52694cd7ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 311 additions and 251 deletions

124
main.py
View File

@ -16,15 +16,19 @@ import time
version = 2.0 version = 2.0
style = False style = False
text = '| V A P O R W A V E || G E N E R A T O R |' text = "| V A P O R W A V E || G E N E R A T O R |"
parser = argparse.ArgumentParser(description = text) parser = argparse.ArgumentParser(description=text)
parser.add_argument("-M", "--music", help="generate v a p o r w a v e music", action="store_true") parser.add_argument(
parser.add_argument("-P", "--picture", help="generate VHS Style image", action="store_true") "-M", "--music", help="generate v a p o r w a v e music", action="store_true"
parser.add_argument("-V","--video", help="VHS Style Video", action="store_true") )
parser.add_argument(
"-P", "--picture", help="generate VHS Style image", action="store_true"
)
parser.add_argument("-V", "--video", help="VHS Style Video", action="store_true")
parser.add_argument("-v", "--version", help="show program version", action="store_true") parser.add_argument("-v", "--version", help="show program version", action="store_true")
parser.add_argument("-i", "--input") parser.add_argument("-i", "--input")
parser.add_argument("-o","--output", help="Output for specifying output video") parser.add_argument("-o", "--output", help="Output for specifying output video")
args = parser.parse_args() args = parser.parse_args()
@ -34,26 +38,34 @@ picture = False
video = False video = False
if args.version: if args.version:
print("vaporwave generator 旺育栄", version) print("vaporwave generator 旺育栄", version)
exit exit
if args.music: if args.music:
music = True music = True
elif args.picture: elif args.picture:
picture = True picture = True
elif args.video: elif args.video:
video = True video = True
if args.input: if args.input:
query = args.input query = args.input
if args.output: if args.output:
outfile = args.output outfile = args.output
else: else:
parser.print_help() parser.print_help()
exit exit
MAX_DURATION = 600 # In-case the program finds a compilation MAX_DURATION = 600 # In-case the program finds a compilation
youtube_urls = ('youtube.com', 'https://www.youtube.com/', 'http://www.youtube.com/', 'http://youtu.be/', 'https://youtu.be/', 'youtu.be') youtube_urls = (
"youtube.com",
"https://www.youtube.com/",
"http://www.youtube.com/",
"http://youtu.be/",
"https://youtu.be/",
"youtu.be",
)
def download_file(query,request_id=1):
def download_file(query, request_id=1):
"""Returns audio to the vapor command handler """Returns audio to the vapor command handler
Searches YouTube for 'query', finds first match that has Searches YouTube for 'query', finds first match that has
@ -63,16 +75,18 @@ def download_file(query,request_id=1):
Query can be YouTube link. Query can be YouTube link.
""" """
ydl_opts = { ydl_opts = {
'quiet': 'True', "quiet": "True",
'format': 'bestaudio/best', "format": "bestaudio/best",
'outtmpl': str(request_id) +'.%(ext)s', "outtmpl": str(request_id) + ".%(ext)s",
'prefer_ffmpeg': 'True', "prefer_ffmpeg": "True",
'noplaylist': 'True', "noplaylist": "True",
'postprocessors': [{ "postprocessors": [
'key': 'FFmpegExtractAudio', {
'preferredcodec': 'wav', "key": "FFmpegExtractAudio",
'preferredquality': '192', "preferredcodec": "wav",
}], "preferredquality": "192",
}
],
} }
original_path = str(request_id) + ".wav" original_path = str(request_id) + ".wav"
@ -81,9 +95,13 @@ def download_file(query,request_id=1):
# check if query is youtube url # check if query is youtube url
if not query.lower().startswith((youtube_urls)): if not query.lower().startswith((youtube_urls)):
# search for youtube videos matching query # search for youtube videos matching query
query_string = urllib.parse.urlencode({"search_query" : query}) query_string = urllib.parse.urlencode({"search_query": query})
html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string) html_content = urllib.request.urlopen(
search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode()) "http://www.youtube.com/results?" + query_string
)
search_results = re.findall(
r"href=\"\/watch\?v=(.{11})", html_content.read().decode()
)
info = False info = False
# find video that fits max duration # find video that fits max duration
@ -91,30 +109,30 @@ def download_file(query,request_id=1):
for url in search_results: for url in search_results:
# check for video duration # check for video duration
try: try:
info = youtube_dl.YoutubeDL(ydl_opts).extract_info(url,download = False) info = youtube_dl.YoutubeDL(ydl_opts).extract_info(url, download=False)
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)
raise ValueError('Could not get information about video.') raise ValueError("Could not get information about video.")
full_title = info['title'] full_title = info["title"]
if (info['duration'] < MAX_DURATION and info['duration'] >= 5): if info["duration"] < MAX_DURATION and info["duration"] >= 5:
# get first video that fits the limit duration # get first video that fits the limit duration
logger.info("Got video: " + str(full_title)) logger.info("Got video: " + str(full_title))
file_title = info['title'] file_title = info["title"]
break break
# if we ran out of urls, return error # if we ran out of urls, return error
if (not info): if not info:
raise ValueError('Could not find a video.') raise ValueError("Could not find a video.")
# query was a youtube link # query was a youtube link
else: else:
logger.info("Query was a YouTube URL.") logger.info("Query was a YouTube URL.")
url = query url = query
info = youtube_dl.YoutubeDL(ydl_opts).extract_info(url,download = False) info = youtube_dl.YoutubeDL(ydl_opts).extract_info(url, download=False)
file_title = info['title'] file_title = info["title"]
# check if video fits limit duration # check if video fits limit duration
if (info['duration'] < 5 or info['duration'] > MAX_DURATION): if info["duration"] < 5 or info["duration"] > MAX_DURATION:
raise ValueError('Video is too short. Need 5 seconds or more.') raise ValueError("Video is too short. Need 5 seconds or more.")
# download video and extract audio # download video and extract audio
logger.info("Downloading video...") logger.info("Downloading video...")
@ -123,36 +141,35 @@ def download_file(query,request_id=1):
ydl.download([url]) ydl.download([url])
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)
raise ValueError('Could not download ' + str(full_title) + '.') raise ValueError("Could not download " + str(full_title) + ".")
return original_path, file_title return original_path, file_title
def gen_vapor(filePath, title): def gen_vapor(filePath, title):
# Delete stuff if there is anything left over. # Delete stuff if there is anything left over.
os.system("rm -r download/") os.system("rm -r download/")
os.system("rm -r beats/") os.system("rm -r beats/")
# Make the proper folders for intermediate steps # Make the proper folders for intermediate steps
os.system("mkdir download/") os.system("mkdir download/")
os.system("mkdir beats/") os.system("mkdir beats/")
# Download the youtube query's first result. Might be wrong but YOLO
# YTDownloader.download_wav_to_samp2(query)
# Download the youtube query's first result. Might be wrong but YOLO # For every song in download folder(just one for now)
#YTDownloader.download_wav_to_samp2(query) """
# For every song in download folder(just one for now)
"""
for fs in os.listdir("download/"): for fs in os.listdir("download/"):
# Slow down the song. # Slow down the song.
VaporSong.vaporize_song(query,"download/"+fs) VaporSong.vaporize_song(query,"download/"+fs)
pass pass
# When we are finished, delete the old folders. # When we are finished, delete the old folders.
""" """
VaporSong.vaporize_song(filePath, title) VaporSong.vaporize_song(filePath, title)
os.system("rm -r download/") os.system("rm -r download/")
os.system("rm -r beats/") os.system("rm -r beats/")
""" """
@ -167,7 +184,6 @@ if music:
name, title = download_file(query) name, title = download_file(query)
gen_vapor(name, title) gen_vapor(name, title)
elif picture: elif picture:
generateVHSStyle(query,"out.jpg") generateVHSStyle(query, "out.jpg")
elif video: elif video:
VHS_Vid(query, outfile) VHS_Vid(query, outfile)

View File

@ -2,7 +2,7 @@ import requests
import datetime import datetime
from PIL import Image from PIL import Image
from PIL import ImageFont from PIL import ImageFont
from PIL import ImageDraw from PIL import ImageDraw
import colorsys import colorsys
import json import json
import numpy as np import numpy as np
@ -21,7 +21,7 @@ def generate_offsets(array_size, max_offset):
periodicity = random.random() * periodicity periodicity = random.random() * periodicity
offsets = [] offsets = []
for i in range(array_size): for i in range(array_size):
offsets.append(floor(max_offset*np.sin(periodicity*(i*np.pi/180)))) offsets.append(floor(max_offset * np.sin(periodicity * (i * np.pi / 180))))
return offsets return offsets
@ -35,11 +35,11 @@ def hueChange(img, offset):
g_data = [] g_data = []
b_data = [] b_data = []
offset = offset/100. offset = offset / 100.0
for rd, gr, bl in zip(r.getdata(), g.getdata(), b.getdata()): for rd, gr, bl in zip(r.getdata(), g.getdata(), b.getdata()):
h, s, v = colorsys.rgb_to_hsv(rd/255.0, bl/255.0, gr/255.0) h, s, v = colorsys.rgb_to_hsv(rd / 255.0, bl / 255.0, gr / 255.0)
rgb = colorsys.hsv_to_rgb(h, s+offset, v) rgb = colorsys.hsv_to_rgb(h, s + offset, v)
rd, bl, gr = [int(x*255.) for x in rgb] rd, bl, gr = [int(x * 255.0) for x in rgb]
r_data.append(rd) r_data.append(rd)
g_data.append(gr) g_data.append(gr)
b_data.append(bl) b_data.append(bl)
@ -47,12 +47,21 @@ def hueChange(img, offset):
r.putdata(r_data) r.putdata(r_data)
g.putdata(g_data) g.putdata(g_data)
b.putdata(b_data) b.putdata(b_data)
return Image.merge('RGB',(r,g,b)) return Image.merge("RGB", (r, g, b))
def decision(probability): def decision(probability):
return random.random() < probability return random.random() < probability
def mod_image_repeat_rows(imgname, chance_of_row_repeat=0, max_row_repeats=0, min_row_repeats=0, save=True, out_name="image.jpg"):
def mod_image_repeat_rows(
imgname,
chance_of_row_repeat=0,
max_row_repeats=0,
min_row_repeats=0,
save=True,
out_name="image.jpg",
):
img = Image.open(imgname) img = Image.open(imgname)
pixels = img.load() pixels = img.load()
width, height = img.size width, height = img.size
@ -80,7 +89,7 @@ def mod_image_repeat_rows(imgname, chance_of_row_repeat=0, max_row_repeats=0, mi
pixels[x, y] = row_to_repeat[x - offsets[num_repeats]] pixels[x, y] = row_to_repeat[x - offsets[num_repeats]]
else: else:
pixels[x, y] = (r, g, b) pixels[x, y] = (r, g, b)
if repeat: if repeat:
num_repeats += 1 num_repeats += 1
if num_repeats >= times_to_repeat: if num_repeats >= times_to_repeat:
@ -102,53 +111,68 @@ def add_date(img_path, out_name="image.jpg", bottom_offset=0):
width, height = img.size width, height = img.size
draw = ImageDraw.Draw(img) draw = ImageDraw.Draw(img)
font = ImageFont.truetype("src/VCR_OSD_MONO_1.001.ttf", 64) font = ImageFont.truetype("src/VCR_OSD_MONO_1.001.ttf", 64)
draw.text((corner_offset, (height-150-bottom_offset)), date_str_1, (255, 255, 255), font=font) draw.text(
draw.text((corner_offset, (height-75)-bottom_offset), date_str_2, (255, 255, 255), font=font) (corner_offset, (height - 150 - bottom_offset)),
date_str_1,
(255, 255, 255),
font=font,
)
draw.text(
(corner_offset, (height - 75) - bottom_offset),
date_str_2,
(255, 255, 255),
font=font,
)
draw.text((corner_offset, 25), "|| PAUSE", (255, 255, 255), font=font) draw.text((corner_offset, 25), "|| PAUSE", (255, 255, 255), font=font)
img.save(out_name) img.save(out_name)
def add_img_noise(imgpath, intensity=1, out_name="image.jpg"): def add_img_noise(imgpath, intensity=1, out_name="image.jpg"):
img = imageio.imread(imgpath, pilmode='RGB') img = imageio.imread(imgpath, pilmode="RGB")
noise1 = img + intensity * img.std() * np.random.random(img.shape) noise1 = img + intensity * img.std() * np.random.random(img.shape)
imageio.imwrite(out_name, noise1) imageio.imwrite(out_name, noise1)
def offset_hue(image, out_name="image.jpg"): def offset_hue(image, out_name="image.jpg"):
if isinstance(image, str): if isinstance(image, str):
image = Image.open(image) image = Image.open(image)
image = hueChange(image, 25) image = hueChange(image, 25)
image.save(out_name) image.save(out_name)
def build_background(out_name, taskbar_offset): def build_background(out_name, taskbar_offset):
#getImage(out_name="start.jpg") # getImage(out_name="start.jpg")
offset_hue("start.jpg", out_name="saturated.jpg") offset_hue("start.jpg", out_name="saturated.jpg")
mod_image_repeat_rows("saturated.jpg", 0.012, 50, 10, out_name="shifted.jpg") mod_image_repeat_rows("saturated.jpg", 0.012, 50, 10, out_name="shifted.jpg")
add_img_noise("shifted.jpg", out_name="noisy.jpg") add_img_noise("shifted.jpg", out_name="noisy.jpg")
add_date("noisy.jpg", out_name=out_name, bottom_offset=taskbar_offset) add_date("noisy.jpg", out_name=out_name, bottom_offset=taskbar_offset)
""" """
if __name__ == "__main__": if __name__ == "__main__":
build_background("bkg.jpg", 25) build_background("bkg.jpg", 25)
""" """
def generateVHSStyle(infile, outfile, silence=False): def generateVHSStyle(infile, outfile, silence=False):
if silence: if silence:
cut_rows = bool(random.getrandbits(1)) cut_rows = bool(random.getrandbits(1))
offset = random.choice([0,5,10]) offset = random.choice([0, 5, 10])
offset_hue(infile,"saturated.jpg") offset_hue(infile, "saturated.jpg")
if cut_rows: if cut_rows:
mod_image_repeat_rows("saturated.jpg", 0.012, 50, 10, True, "shifted.jpg") mod_image_repeat_rows("saturated.jpg", 0.012, 50, 10, True, "shifted.jpg")
else: else:
mod_image_repeat_rows("saturated.jpg", 0, 0, 0, True, "shifted.jpg") mod_image_repeat_rows("saturated.jpg", 0, 0, 0, True, "shifted.jpg")
add_date("shifted.jpg","noisy.jpg") add_date("shifted.jpg", "noisy.jpg")
add_date("noisy.jpg",outfile, bottom_offset=offset) add_date("noisy.jpg", outfile, bottom_offset=offset)
os.remove("shifted.jpg") os.remove("shifted.jpg")
os.remove("saturated.jpg") os.remove("saturated.jpg")
os.remove("noisy.jpg") os.remove("noisy.jpg")
else: else:
cut_rows = bool(random.getrandbits(1)) cut_rows = bool(random.getrandbits(1))
offset = random.choice([0,5,10,15,20,25]) offset = random.choice([0, 5, 10, 15, 20, 25])
logger.info("Saturating the image") logger.info("Saturating the image")
offset_hue(infile,"saturated.jpg") offset_hue(infile, "saturated.jpg")
if cut_rows: if cut_rows:
logger.info("Shifting the image") logger.info("Shifting the image")
mod_image_repeat_rows("saturated.jpg", 0.012, 50, 10, True, "shifted.jpg") mod_image_repeat_rows("saturated.jpg", 0.012, 50, 10, True, "shifted.jpg")
@ -156,13 +180,14 @@ def generateVHSStyle(infile, outfile, silence=False):
logger.info("Not applying lines effect") logger.info("Not applying lines effect")
mod_image_repeat_rows("saturated.jpg", 0, 0, 0, True, "shifted.jpg") mod_image_repeat_rows("saturated.jpg", 0, 0, 0, True, "shifted.jpg")
logger.info("Adding noise") logger.info("Adding noise")
add_date("shifted.jpg","noisy.jpg") add_date("shifted.jpg", "noisy.jpg")
logger.info("Adding text") logger.info("Adding text")
add_date("noisy.jpg",outfile, bottom_offset=offset) add_date("noisy.jpg", outfile, bottom_offset=offset)
logger.info("Generated Image: out.jpg") logger.info("Generated Image: out.jpg")
logger.info("Removing residual files") logger.info("Removing residual files")
os.remove("shifted.jpg") os.remove("shifted.jpg")
os.remove("saturated.jpg") os.remove("saturated.jpg")
os.remove("noisy.jpg") os.remove("noisy.jpg")
#generateVHSStyle("s.jpg","o.jpg")
# generateVHSStyle("s.jpg","o.jpg")

View File

@ -8,16 +8,18 @@ import logzero
from logzero import logger from logzero import logger
from logzero import setup_logger from logzero import setup_logger
def SaveVid(path):
vidObj = cv2.VideoCapture(path) def SaveVid(path):
count = 0 vidObj = cv2.VideoCapture(path)
success = 1 count = 0
while success: success = 1
success, image = vidObj.read() while success:
cv2.imwrite("frames/"+str(count)+".jpg", image) success, image = vidObj.read()
#os.rename("frames/"+str(count)+".jpg", os.path.splitext("frames/"+str(count)+".jpg")[0]) cv2.imwrite("frames/" + str(count) + ".jpg", image)
count += 1 # os.rename("frames/"+str(count)+".jpg", os.path.splitext("frames/"+str(count)+".jpg")[0])
count += 1
def Style(pathToFrames): def Style(pathToFrames):
files = [f for f in os.listdir(pathToFrames) if isfile(join(pathToFrames, f))] files = [f for f in os.listdir(pathToFrames) if isfile(join(pathToFrames, f))]
count = 0 count = 0
@ -26,7 +28,7 @@ def Style(pathToFrames):
f = str(i) f = str(i)
fi = pathToFrames + f fi = pathToFrames + f
out = fi + ".jpg" out = fi + ".jpg"
generateVHSStyle(fi, out, silence=True) generateVHSStyle(fi, out, silence=True)
os.rename(out, fi) os.rename(out, fi)
print("--------") print("--------")
@ -41,29 +43,42 @@ def Style(pathToFrames):
os.system(c) os.system(c)
os.chdir(cwd) os.chdir(cwd)
def generateVideo(outfile, path, infile): def generateVideo(outfile, path, infile):
frame_array = [] frame_array = []
files = [int(f) for f in os.listdir(path) if isfile(join(path, f))] files = [int(f) for f in os.listdir(path) if isfile(join(path, f))]
files.sort() files.sort()
duration = subprocess.check_output(['ffprobe', '-i', infile, '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv=%s' % ("p=0")]) duration = subprocess.check_output(
fps = len(files)/float(duration) [
"ffprobe",
"-i",
infile,
"-show_entries",
"format=duration",
"-v",
"quiet",
"-of",
"csv=%s" % ("p=0"),
]
)
fps = len(files) / float(duration)
print("FPS", fps) print("FPS", fps)
for i in range(len(files)): for i in range(len(files)):
filename=path + str(files[i]) filename = path + str(files[i])
img = cv2.imread(filename) img = cv2.imread(filename)
height, width, layers = img.shape height, width, layers = img.shape
size = (width,height) size = (width, height)
frame_array.append(img) frame_array.append(img)
out = cv2.VideoWriter(outfile,cv2.VideoWriter_fourcc(*'MP4V'), fps, size) out = cv2.VideoWriter(outfile, cv2.VideoWriter_fourcc(*"MP4V"), fps, size)
for i in range(len(frame_array)): for i in range(len(frame_array)):
out.write(frame_array[i]) out.write(frame_array[i])
out.release() out.release()
def VHS_Vid(infile, outfile): def VHS_Vid(infile, outfile):
path = './frames/' path = "./frames/"
os.system("rm frames/*") os.system("rm frames/*")
os.system("mkdir frames") os.system("mkdir frames")
logger.info("Exctracting Frames") logger.info("Exctracting Frames")
@ -84,4 +99,4 @@ def VHS_Vid(infile, outfile):
os.remove("output-audio.aac") os.remove("output-audio.aac")
#VHS_Vid("video.mp4","video2.mp4") # VHS_Vid("video.mp4","video2.mp4")

View File

@ -11,174 +11,178 @@ from logzero import setup_logger
CONFIDENCE_THRESH = 0.02 CONFIDENCE_THRESH = 0.02
class VaporSong: class VaporSong:
# Slows down Track # Slows down Track
def slow_down(src, rate, dest):
cmd = "sox -G -D " + src + " " + dest + " speed " + str(rate)
os.system(cmd)
return dest
# Adds Reverb def slow_down(src, rate, dest):
cmd = "sox -G -D " + src + " " + dest + " speed " + str(rate)
def reverbize(src, dest): os.system(cmd)
cmd = "sox -G -D " + src + " " + dest + " reverb 100 fade 5 -0 7" # idk what this does tbh, https://stackoverflow.com/a/57767238/8386344 return dest
os.system(cmd)
return dest
# Adds Reverb
# Crops "src" from "start" plus "start + dur" and return it in "dest"
def crop(src,dest,start,dur):
cmd = "sox " + src + " " + dest + " trim " + " " + str(start) + " " + str(dur)
os.system(cmd)
def reverbize(src, dest):
# Randomly crops a part of the song of at most max_sec_len. cmd = (
def random_crop(src, max_sec_len, dest): "sox -G -D " + src + " " + dest + " reverb 100 fade 5 -0 7"
out = subprocess.check_output(["soxi","-D",src]).rstrip() ) # idk what this does tbh, https://stackoverflow.com/a/57767238/8386344
f_len = int(float(out)) os.system(cmd)
if (f_len <= max_sec_len): return dest
os.system("cp " + src + " " + dest)
return
else:
start_region = f_len - max_sec_len
start = randint(0,start_region)
VaporSong.crop(src,dest,start,max_sec_len)
# Crops "src" from "start" plus "start + dur" and return it in "dest"
# Given a file, returns a list of [beats, confidence], executable based on audibo's test-beattracking.c def crop(src, dest, start, dur):
# TODO: Move away from executable and use aubio's Python module cmd = "sox " + src + " " + dest + " trim " + " " + str(start) + " " + str(dur)
def fetchbeats(src): os.system(cmd)
beat_matrix = []
if platform == 'darwin':
beats = subprocess.check_output(["noah", "get-beats",src]).rstrip()
else:
beats = subprocess.check_output(["./get-beats",src]).rstrip()
beats_ary = beats.splitlines()
for i in beats_ary:
record = i.split()
record[0] = float(record[0])/1000.0
record[1] = float(record[1])
beat_matrix.append(record)
return beat_matrix
# Splits an audio file into beats according to beat_matrix list # Randomly crops a part of the song of at most max_sec_len.
def random_crop(src, max_sec_len, dest):
def split_beat(src,beat_matrix): out = subprocess.check_output(["soxi", "-D", src]).rstrip()
split_files = [] f_len = int(float(out))
for i in range(0,len(beat_matrix)-1): if f_len <= max_sec_len:
os.system("cp " + src + " " + dest)
return
else:
start_region = f_len - max_sec_len
start = randint(0, start_region)
VaporSong.crop(src, dest, start, max_sec_len)
if(beat_matrix[i][1] > CONFIDENCE_THRESH): # Given a file, returns a list of [beats, confidence], executable based on audibo's test-beattracking.c
dur = (beat_matrix[i+1][0] - beat_matrix[i][0]) # TODO: Move away from executable and use aubio's Python module
out = src.split(".")[0]+str(i)+".wav" def fetchbeats(src):
VaporSong.crop(src,out,beat_matrix[i][0],dur) beat_matrix = []
split_files.append(out) if platform == "darwin":
return split_files beats = subprocess.check_output(["noah", "get-beats", src]).rstrip()
else:
beats = subprocess.check_output(["./get-beats", src]).rstrip()
beats_ary = beats.splitlines()
for i in beats_ary:
record = i.split()
record[0] = float(record[0]) / 1000.0
record[1] = float(record[1])
beat_matrix.append(record)
return beat_matrix
# Combines a list of sections # Splits an audio file into beats according to beat_matrix list
def combine(sections,dest):
tocomb = []
tocomb.append("sox")
tocomb.append("-G")
for section in sections:
for sample in section:
tocomb.append(sample)
tocomb.append(dest)
tmpFileLimit = len(tocomb) + 256 # in case the program messes up, it does not actually frick up your system
n = str(tmpFileLimit)
#logger.info("Setting file limit to ", n)
os.system("ulimit -n " + n)
subprocess.check_output(tocomb)
return dest
# Arbitrarily groups beats into lists of 4, 6, 8, or 9, perfect for looping. def split_beat(src, beat_matrix):
split_files = []
def generate_sections(ary): for i in range(0, len(beat_matrix) - 1):
sections = []
beats = [4,6,8,9]
index = 0
while(index != len(ary)):
current_beat = beats[randint(0,len(beats)-1)]
new_section = []
while((current_beat != 0) and (index != len(ary))):
new_section.append(ary[index])
current_beat -= 1
index += 1
sections.append(new_section)
return sections
if beat_matrix[i][1] > CONFIDENCE_THRESH:
# given a list of sections, selects some of them and duplicates them, perfect for that vaporwave looping effect dur = beat_matrix[i + 1][0] - beat_matrix[i][0]
def dup_sections(sections): out = src.split(".")[0] + str(i) + ".wav"
new_section = [] VaporSong.crop(src, out, beat_matrix[i][0], dur)
for section in sections: split_files.append(out)
new_section.append(section) return split_files
if(randint(0,1) == 0):
new_section.append(section)
return new_section
# a passage is a list of sections. This takes some sections and groups them into passages. # Combines a list of sections
def make_passages(sections):
passages = []
index = 0
while(index != len(sections)):
passage_len = randint(1,4)
passage = []
while(index != len(sections) and passage_len > 0):
passage.append(sections[index])
index += 1
passage_len -= 1
passages.append(passage)
return passages
# Given all of our passages, picks some of them and inserts them into a list some number of times. def combine(sections, dest):
tocomb = []
def reorder_passages(passages): tocomb.append("sox")
new_passages = [] tocomb.append("-G")
passage_count = randint(5,12) for section in sections:
while(passage_count != 0): for sample in section:
passage = passages[randint(0,len(passages)-1)] tocomb.append(sample)
passage_count -= 1 tocomb.append(dest)
dup = randint(1,4) tmpFileLimit = (
while(dup != 0): len(tocomb) + 256
dup -= 1 ) # in case the program messes up, it does not actually frick up your system
new_passages.append(passage) n = str(tmpFileLimit)
return new_passages # logger.info("Setting file limit to ", n)
os.system("ulimit -n " + n)
subprocess.check_output(tocomb)
return dest
# converts a list of passages to a list of sections. # Arbitrarily groups beats into lists of 4, 6, 8, or 9, perfect for looping.
def flatten(passages):
sections = []
for passage in passages:
for section in passage:
sections.append(section)
return sections
# It's all coming together def generate_sections(ary):
sections = []
def vaporize_song(fname, title): beats = [4, 6, 8, 9]
logger.info("Slowing down the music") index = 0
VaporSong.slow_down(fname, 0.7, "beats/out.wav") while index != len(ary):
#logger.info("Cropping") current_beat = beats[randint(0, len(beats) - 1)]
#VaporSong.random_crop("beats/out.wav",150,"beats/outcrop.wav") new_section = []
logger.info("Doing Beat Analysis") while (current_beat != 0) and (index != len(ary)):
bm = VaporSong.fetchbeats("beats/out.wav") new_section.append(ary[index])
logger.info("Split into beats") current_beat -= 1
splitd = VaporSong.split_beat("beats/out.wav",bm) index += 1
#group beats to sections sections.append(new_section)
logger.info("Divide into sections") return sections
sections = VaporSong.generate_sections(splitd)
logger.info("Duping Sections") # given a list of sections, selects some of them and duplicates them, perfect for that vaporwave looping effect
sdup = VaporSong.dup_sections(sections) def dup_sections(sections):
# group sections into passages new_section = []
paslist = VaporSong.make_passages(sdup) for section in sections:
# reorder packages new_section.append(section)
pasloop = VaporSong.reorder_passages(paslist) if randint(0, 1) == 0:
sectionflat = VaporSong.flatten(pasloop) new_section.append(section)
logger.info("Mastering & Reverbing") return new_section
VaporSong.combine(sectionflat,"beats/out_norev.wav")
VaporSong.reverbize("beats/out_norev.wav","./" + (re.sub(r"\W+|_", " ", title)).replace(" ","_") + ".wav") # a passage is a list of sections. This takes some sections and groups them into passages.
logger.info("Generated V A P O R W A V E")
def make_passages(sections):
passages = []
index = 0
while index != len(sections):
passage_len = randint(1, 4)
passage = []
while index != len(sections) and passage_len > 0:
passage.append(sections[index])
index += 1
passage_len -= 1
passages.append(passage)
return passages
# Given all of our passages, picks some of them and inserts them into a list some number of times.
def reorder_passages(passages):
new_passages = []
passage_count = randint(5, 12)
while passage_count != 0:
passage = passages[randint(0, len(passages) - 1)]
passage_count -= 1
dup = randint(1, 4)
while dup != 0:
dup -= 1
new_passages.append(passage)
return new_passages
# converts a list of passages to a list of sections.
def flatten(passages):
sections = []
for passage in passages:
for section in passage:
sections.append(section)
return sections
# It's all coming together
def vaporize_song(fname, title):
logger.info("Slowing down the music")
VaporSong.slow_down(fname, 0.7, "beats/out.wav")
# logger.info("Cropping")
# VaporSong.random_crop("beats/out.wav",150,"beats/outcrop.wav")
logger.info("Doing Beat Analysis")
bm = VaporSong.fetchbeats("beats/out.wav")
logger.info("Split into beats")
splitd = VaporSong.split_beat("beats/out.wav", bm)
# group beats to sections
logger.info("Divide into sections")
sections = VaporSong.generate_sections(splitd)
logger.info("Duping Sections")
sdup = VaporSong.dup_sections(sections)
# group sections into passages
paslist = VaporSong.make_passages(sdup)
# reorder packages
pasloop = VaporSong.reorder_passages(paslist)
sectionflat = VaporSong.flatten(pasloop)
logger.info("Mastering & Reverbing")
VaporSong.combine(sectionflat, "beats/out_norev.wav")
VaporSong.reverbize(
"beats/out_norev.wav",
"./" + (re.sub(r"\W+|_", " ", title)).replace(" ", "_") + ".wav",
)
logger.info("Generated V A P O R W A V E")