Created
October 9, 2023 15:22
-
-
Save schollz/17b8874733466edeeb127e4dad4cd291 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# convert samples from http://leisureland.us/mellotron.htm | |
# unzip one of the zip files into a folder | |
# copy this file into that folder | |
# run | |
# python3 convert_leisureland.py | |
# a new folder "mx.samples" will have the converted samples for mx.samples | |
import os | |
import re | |
import mimetypes | |
import itertools as it | |
from pathlib import Path | |
from shutil import copyfile | |
MIDI_A4 = 69 # MIDI Pitch number | |
FREQ_A4 = 440.0 # Hz | |
SEMITONE_RATIO = 2.0 ** (1.0 / 12.0) # Ascending | |
def is_audio(path): | |
try: | |
if "audio" in mimetypes.guess_type(path)[0]: | |
return True | |
else: | |
return False | |
except TypeError: | |
# path is not a file | |
return False | |
def str2midi(note_string): | |
""" | |
Given a note string name (e.g. "Bb4"), returns its MIDI pitch number. | |
""" | |
if note_string == "?": | |
return nan | |
data = note_string.strip().lower() | |
name2delta = {"c": -9, "d": -7, "e": -5, "f": -4, "g": -2, "a": 0, "b": 2} | |
accident2delta = {"b": -1, "#": 1, "x": 2} | |
accidents = list(it.takewhile(lambda el: el in accident2delta, data[1:])) | |
octave_delta = int(data[len(accidents) + 1 :]) - 4 | |
return ( | |
MIDI_A4 | |
+ name2delta[data[0]] | |
+ sum(accident2delta[ac] for ac in accidents) # Name | |
+ 12 * octave_delta # Accident # Octave | |
) | |
folder_in = "." | |
folder_out = "mx.samples" | |
audio_files = [x for x in list(Path(folder_in).rglob("*")) if is_audio(x)] | |
try: | |
os.mkdir(folder_out) | |
except: | |
pass | |
for _, file in enumerate(audio_files): | |
# get the filename only | |
filename = os.path.basename(file) | |
# remove the extension | |
filename = os.path.splitext(filename)[0] | |
match = re.search(r"[a-g]{1}[#b]?[0-9]{1}", filename, flags=re.IGNORECASE) | |
if match is None: | |
print("could not find note name in {}".format(filename)) | |
pass | |
midival = str2midi(match.group(0)) | |
variation = 1 | |
dynamic = 1 | |
dynamics = 1 | |
variation = 1 | |
release = 0 | |
new_name = "{}.{}.{}.{}.{}.wav".format( | |
midival, dynamic, dynamics, variation, release | |
) | |
new_path = os.path.join(folder_out, new_name) | |
try: | |
copyfile(file, new_path) | |
print(f"{file} -> {new_path}") | |
except: | |
print(f"{file} -> {new_path} failed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment