Skip to content

Commit

Permalink
(feat) Tempo detector post route working in sending response to client
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabrielparizet committed Apr 3, 2023
1 parent 69d23a3 commit c54db69
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 40 deletions.
9 changes: 5 additions & 4 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
new_path = "/Users/gabrielparizet/Desktop/Sound_Data/api/audio_files/" + audio_file.filename
find_tempo(new_path)
return find_tempo(new_path)

42 changes: 6 additions & 36 deletions api/sound_detector.py
Original file line number Diff line number Diff line change
@@ -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.')
return float
42 changes: 42 additions & 0 deletions api/sound_detector_test.py
Original file line number Diff line number Diff line change
@@ -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.')

0 comments on commit c54db69

Please sign in to comment.