From c54db69513f1b1baaab57349a7361147b6616b9e Mon Sep 17 00:00:00 2001 From: Gabrielparizet Date: Mon, 3 Apr 2023 14:56:20 +0200 Subject: [PATCH] (feat) Tempo detector post route working in sending response to client --- api/app.py | 9 ++++---- api/sound_detector.py | 42 ++++++-------------------------------- api/sound_detector_test.py | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 40 deletions(-) create mode 100644 api/sound_detector_test.py diff --git a/api/app.py b/api/app.py index 9c494ae..0cc6e6a 100644 --- a/api/app.py +++ b/api/app.py @@ -3,12 +3,13 @@ # Instantiate flask app and assign it to app variable. app = Flask(__name__) -# @app.get("/") -# def test_route(): -# return "Your connected to your flask server" +from sound_detector import find_tempo @app.route("/upload", methods=["POST"]) def upload_file(): audio_file = request.files["audioFile"] audio_file.save("/Users/gabrielparizet/Desktop/Sound_Data/api/audio_files/" + audio_file.filename) - return "File uploaded successfully" \ No newline at end of file + new_path = "/Users/gabrielparizet/Desktop/Sound_Data/api/audio_files/" + audio_file.filename + find_tempo(new_path) + return find_tempo(new_path) + diff --git a/api/sound_detector.py b/api/sound_detector.py index 218c8ca..12dc0fb 100644 --- a/api/sound_detector.py +++ b/api/sound_detector.py @@ -1,43 +1,13 @@ # Importing librosa. import librosa +def find_tempo(filename): + y, sr = librosa.load(filename) + tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr) + file_tempo = format_tempo(tempo) + return f'Your audio file tempo is of {file_tempo} bpm.' -# Importing our audio file as filename. -filename = "/Users/gabrielparizet/Desktop/Sound_Data/api/audio_files/donato_dozzy_your_eyes.mp3" - -# Load our audiofile in librosa. -y, sr = librosa.load(filename) - - -# Find the tempo and the beat_frames from our audiofile with librosa beat_track method. -tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr) - - -# The format_temp function takes a float as argument and rounds it to two decimals. def format_tempo(float): float = "{:.2f}".format(float) - return float - - -# Get the duration of our audiofile in seconds with librosa get_duration method. -duration = librosa.get_duration(y=y, sr=sr) - - -# The seconds_to_minutes function takes time in float as an argument and converts it to string giving its time in minutes and seconds. -def seconds_to_minutes (time): - formated = "{:.2f}".format(time/60) - formated = str(formated) - x = formated.split('.') - minutes = x[0] - seconds = x[1] - return f'{minutes} minutes and {seconds} seconds' - - -# Function calls: -file_duration = seconds_to_minutes(duration) -file_tempo = format_tempo(tempo) - - -print(f'Your audio file tempo is of {file_tempo} bpm.') -print(f'Your audio file is {file_duration} long.') \ No newline at end of file + return float \ No newline at end of file diff --git a/api/sound_detector_test.py b/api/sound_detector_test.py new file mode 100644 index 0000000..b62c43b --- /dev/null +++ b/api/sound_detector_test.py @@ -0,0 +1,42 @@ +# Importing librosa. +import librosa + + +# Importing our audio file as filename. +filename = "/Users/gabrielparizet/Desktop/Sound_Data/api/audio_files/donato_dozzy_your_eyes.mp3" + + +# Load our audiofile in librosa. +y, sr = librosa.load(filename) + +# Find the tempo and the beat_frames from our audiofile with librosa beat_track method. +tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr) + + +# The format_temp function takes a float as argument and rounds it to two decimals. +def format_tempo(float): + float = "{:.2f}".format(float) + return float + + +# Get the duration of our audiofile in seconds with librosa get_duration method. +duration = librosa.get_duration(y=y, sr=sr) + + +# The seconds_to_minutes function takes time in float as an argument and converts it to string giving its time in minutes and seconds. +def seconds_to_minutes (time): + formated = "{:.2f}".format(time/60) + formated = str(formated) + x = formated.split('.') + minutes = x[0] + seconds = x[1] + return f'{minutes} minutes and {seconds} seconds' + + +# Function calls: +file_duration = seconds_to_minutes(duration) +file_tempo = format_tempo(tempo) + + +print(f'Your audio file tempo is of {file_tempo} bpm.') +print(f'Your audio file is {file_duration} long.') \ No newline at end of file