def get_bpm_and_key(self): """ """ if self.attrs.get("bpm", None) is None: # make temp file root fp_root = tempfile.mktemp(suffix='analysis-output') # cmd cmd = '{0} {1} "{2}"'.format(settings.FREESOUND_PATH, pipes.quote(self.file), fp_root) # exec p = util.sys_exec(cmd) if not p.ok: sys.stderr.write("Error running: {0}\n{1}\n".format(cmd, p.stderr)) return # grab output file try: data = json.loads(open(fp_root).read()) except Exception as e: sys.stderr.write("WARNING: Could not decode {0} because: {1}".format(self.file, e)) return # remove try: os.remove(fp_root) os.remove(fp_root) except: pass self.attrs["bpm"] = str(round(data.get("rhythm",{}).get("bpm", settings.DEFAULT_BPM), 1)) self.attrs["key"] = settings.KEY_LOOKUP.get((data.get("tonal", {}).get("key_temperley", {}).get("key", "") + data.get("tonal", {}).get("key_temperley", {}).get("scale", "")).upper(), settings.DEFAULT_KEY) self.attrs["chord"] = settings.KEY_LOOKUP.get((data.get("tonal", {}).get("chords_key", "") + data.get("tonal", {}).get("chords_scale", "")).upper(), settings.DEFAULT_KEY) self.attrs["harmonic_key"] = settings.KEY_TO_HARMONIC.get(self.attrs["key"], "")
def import_song(self, song): """ Import and Standardize a Song """ sys.stderr.write("INFO: Analyzing: {0}\n".format(song.file)) song.get_file() song.analyze() song.update() # standardize file naming truth_file = os.path.join(self.directory, song.truth["file"]).replace('//', '/') if song.file != truth_file: p = util.sys_exec('mkdir -p "{0}"'.format(self.directory + song.truth["directory"])) if not p.ok: sys.stderr.write( "ERROR: Could not copy file from {0} to {1} because {2}\n". format(song.file, truth_file, p.stdout)) sys.stderr.write("INFO: Copying {0} -> {1}\n".format( song.file, truth_file)) try: shutil.copy(song.file, truth_file) except Exception as e: sys.stderr.write( "ERROR: Could not copy file from {0} to {1} becauce {2}\n". format(song.file, truth_file, e)) # delete source file if self.cleanup: os.remove(song.file)
def get_fingerprint(self): """ Extract uid and fingerprint via """ if self.attrs.get("fingerprint", None) is None: cmd = '{0} {1} -json'.format(settings.FP_CALC_PATH, pipes.quote(self.file)) p = util.sys_exec(cmd) if not p.ok: sys.stderr.write("WARNING: Error running {0}: {1}\n".format(cmd, p.stderr)) try: data = json.loads(p.stdout) data["duration"] = str(int(round(data.get("duration", 0), 0))) data["uid"] = hashlib.md5(data.get("fingerprint", "").encode("utf-8")).hexdigest() self.attrs.update(data) except Exception as e: sys.stderr.write("WARNING: Could not compute fingerprint for {0}\n".format(self.file)) self.attrs.update({"fingerprint":"", "duration": "", "uid": hashlib.md5(self.file.encode("utf-8")).hexdigest()})
def setup(self): util.sys_exec('mkdir -p "{0}"'.format(self.directory))
def add_to_itunes(self): self.load() for s in self.songs: util.sys_exec("mv {0} '{1}'".format(pipes.quote(s.file), settings.ADD_TO_ITUNES_PATH))