Last active
June 23, 2016 20:58
-
-
Save 2xAA/4a400aaa181edbcd3c4c00037816ed28 to your computer and use it in GitHub Desktop.
Double the speed of LSDJ samples that are over 2.843 seconds, but less than double that for valid sample lengths with the LSD-Patcher for LSDJ.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wave | |
import os | |
import glob | |
import contextlib | |
Change_RATE = 2 | |
EXPORT_DIR = './LSDJprep/' | |
VALID_RESPONSES = ('yes', 'y') | |
# the maximum length file LSDJ can have in a kit | |
# based off of Audacity's export of a WAV Microsoft signed 16-bit PCM .wav file | |
# tested with LSD-Patcher v.19 | |
MAX_LENGTH = 2.84300453515 | |
if not os.path.exists(os.path.dirname(EXPORT_DIR)): | |
directoryResponse = raw_input('LSDJPrep export folder does not exist, create it? yes/no: ') | |
if directoryResponse.lower() in VALID_RESPONSES: | |
try: | |
os.makedirs(os.path.dirname(EXPORT_DIR)) | |
except OSError as exc: # Guard against race condition | |
if exc.errno != errno.EEXIST: | |
raise | |
else: | |
raise ValueError('User did not allow creation of export folder, quit.') | |
for filepath in glob.glob('./*.wav'): | |
filepathSplit = os.path.splitext(os.path.basename(filepath))[0] | |
with contextlib.closing(wave.open(filepath,'rb')) as f: | |
frames = f.getnframes() | |
rate = f.getframerate() | |
CHANNELS = f.getnchannels() | |
swidth = f.getsampwidth() | |
duration = frames / float(rate) | |
signal = f.readframes(-1) | |
if duration > MAX_LENGTH and duration <= MAX_LENGTH*2: | |
if os.path.exists(filepath): | |
overwriteResponse = raw_input('\n' + filepathSplit + '.wav already exists in export folder (' + EXPORT_DIR + '), overwrite? yes/(no): ') | |
if overwriteResponse.lower() not in VALID_RESPONSES: | |
print('Not overwriting ' + filepathSplit + '.wav, continuing...') | |
continue | |
print(filepathSplit + ' : ' + str(duration) +' : Creating new WAV at 2x speed') | |
wf = wave.open(EXPORT_DIR + filepathSplit + '.wav', 'wb') | |
wf.setnchannels(CHANNELS) | |
wf.setsampwidth(swidth) | |
wf.setframerate(rate * Change_RATE) | |
wf.writeframes(signal) | |
wf.close() | |
elif duration > MAX_LENGTH*2: | |
print(filepathSplit + ' : ' + str(duration) + ' : This is too long!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment