Skip to content

Commit

Permalink
[#67] Add get x path functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sindre0830 committed Mar 6, 2022
1 parent 81eec46 commit df28b24
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
10 changes: 5 additions & 5 deletions NN/API/beat_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
# Get beats and BPM from Librosa's beat tracker.
def librosaBeatAnalysis(id):
# loads audio file and gets bpm and beat timestamps
y, sr = librosa.load(dict.NATIVE_DIR + id + dict.WAV_FORMAT, sr=None)
y, sr = librosa.load(dict.getNativeAudioPath(id), sr=None)
return librosa.beat.beat_track(y=y, sr=sr, units="time")


# Handler for aubio analysis.
def aubioBeatAnalysis(id):
# read metadata of audio file to get the samplerate
info = pydub.utils.mediainfo(dict.NATIVE_DIR + id + dict.WAV_FORMAT)
info = pydub.utils.mediainfo(dict.getNativeAudioPath(id))
# gets timestamps and bpm
beats = extractBeats(dict.NATIVE_DIR + id + dict.WAV_FORMAT, samplerate=int(info['sample_rate']))
beats = extractBeats(dict.getNativeAudioPath(id), samplerate=int(info['sample_rate']))
bpm = getBPM(beats)
return bpm, beats

Expand Down Expand Up @@ -61,7 +61,7 @@ def extractBeats(path, samplerate, win_s=512, hop_s=256):
# Plots beat timestamps.
def plotBeats(id, manual_beats=None, aubio_beats=None, librosa_beats=None, start=None, end=None):
# load audio file
y, _ = librosa.load(dict.NATIVE_DIR + id + dict.WAV_FORMAT)
y, _ = librosa.load(dict.getNativeAudioPath(id))
# plot waveform
librosa.display.waveshow(y, alpha=0.6)
# plot beat timestamps
Expand All @@ -87,5 +87,5 @@ def plotBeats(id, manual_beats=None, aubio_beats=None, librosa_beats=None, start
if not os.path.exists(dict.PLOTS_DIR):
os.makedirs(dict.PLOTS_DIR)
# save plot as PNG and show results
plt.savefig("Data/Plots/" + id + ".png", dpi=300, transparent=True, bbox_inches="tight")
plt.savefig(dict.getPlotPath(id), dpi=300, transparent=True, bbox_inches="tight")
plt.show()
10 changes: 10 additions & 0 deletions NN/API/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@
MODIFIED_DIR = "Data/Audio/Modified/"
# extensions for conversion to wav
EXTENSIONS = [".m4v", ".webm", ".mp3", ".mp4"]
# formats
WAV_FORMAT = ".wav"
PNG_FORMAT = ".png"
# status messages
DONE = 'DONE'
SUCCESS = 'SUCCESS'
FAILED = 'FAILED'


def getNativeAudioPath(id):
return NATIVE_DIR + id + WAV_FORMAT


def getPlotPath(id):
return PLOTS_DIR + id + PNG_FORMAT


# Print operation that allows status message on the same line.
def printOperation(message):
print('{:<60s}'.format(message), end="", flush=True)
Expand Down
9 changes: 4 additions & 5 deletions NN/API/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def downloadAudio(id):
if not os.path.exists(dict.NATIVE_DIR):
os.makedirs(dict.NATIVE_DIR)
# branch if audio file doesn't exist
filename = id + dict.WAV_FORMAT
if not os.path.isfile(dict.NATIVE_DIR + filename):
if not os.path.isfile(dict.getNativeAudioPath(id)):
url = "https://www.youtube.com/watch?v=" + id
audiostreams = pafy.new(url).audiostreams
# get audio format with best quality
Expand All @@ -29,14 +28,14 @@ def downloadAudio(id):
if os.path.exists(dict.NATIVE_DIR + tempFilename) is False:
audiostreams[best].download(filepath=dict.NATIVE_DIR + tempFilename)
# convert file to wav format and remove temporary file
convertToWav(tempFilename)
convertToWav(id, tempFilename)
os.remove(dict.NATIVE_DIR + tempFilename)


# Attempts to convert a file into wav format.
def convertToWav(file):
def convertToWav(id, file):
path = dict.NATIVE_DIR + file
newPath = dict.NATIVE_DIR + Path(file).stem + dict.WAV_FORMAT
newPath = dict.getNativeAudioPath(id)
if os.path.exists(newPath) is False and testExt(file):
sound = AudioSegment.from_file(path)
sound.export(newPath, format="wav")
Expand Down

0 comments on commit df28b24

Please sign in to comment.