-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4fc5277
commit 6ba2e9e
Showing
13 changed files
with
190 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "BeatTrack.hpp" | ||
|
||
namespace ta | ||
{ | ||
auto beatTrack(juce::File const& audioFile) -> BeatTrackResult | ||
{ | ||
auto const* pythonPath = "/home/tobante/Developer/tobanteAudio/StiggiDJ/venv/bin/python3"; | ||
auto const scriptDir = juce::File{"/home/tobante/Developer/tobanteAudio/StiggiDJ/src/Scripts"}; | ||
auto const scriptPath = scriptDir.getChildFile("bpm_track.py"); | ||
|
||
juce::ChildProcess process{}; | ||
auto processArgs = juce::StringArray{pythonPath, scriptPath.getFullPathName(), audioFile.getFullPathName()}; | ||
if (!process.start(processArgs)) { return BeatTrackResult{"Failed to start"}; } | ||
if (!process.waitForProcessToFinish(10'000)) { return BeatTrackResult{"Timeout"}; } | ||
|
||
auto exitCode = process.getExitCode(); | ||
auto output = process.readAllProcessOutput(); | ||
if (exitCode != 0) { return BeatTrackResult{output}; } | ||
|
||
auto outputLines = juce::StringArray::fromLines(output); | ||
if (outputLines.size() < 2) { return BeatTrackResult{"Invalid output"}; } | ||
|
||
auto bpm = outputLines[0].getDoubleValue(); | ||
auto beatPositions = std::vector<double>{}; | ||
beatPositions.reserve(outputLines.size() - 1); | ||
|
||
auto toDouble = [](auto const& line) { return line.getDoubleValue(); }; | ||
std::transform(outputLines.begin() + 1, outputLines.end(), std::back_inserter(beatPositions), toDouble); | ||
|
||
return BeatTrackResult{bpm, std::move(beatPositions)}; | ||
} | ||
} // namespace ta |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <juce_core/juce_core.h> | ||
|
||
namespace ta | ||
{ | ||
|
||
struct BeatTrackResult | ||
{ | ||
explicit BeatTrackResult(juce::String error) : _errorMessage{std::move(error)} {} | ||
|
||
BeatTrackResult(double estimatedBPM, std::vector<double>&& onsetPositions) | ||
: _bpm{estimatedBPM}, onsets{std::move(onsetPositions)} | ||
{ | ||
} | ||
|
||
auto ok() const noexcept -> bool { return _errorMessage.isEmpty(); } | ||
auto error() const noexcept -> bool { return !ok(); } | ||
auto errorMessage() const -> juce::String const& { return _errorMessage; } | ||
|
||
auto estimatedBPM() const -> double { return _bpm; } | ||
auto beatPositions() const -> std::vector<double> const& { return onsets; } | ||
|
||
private: | ||
double _bpm{}; | ||
std::vector<double> onsets{}; | ||
juce::String _errorMessage{}; | ||
}; | ||
|
||
auto beatTrack(juce::File const& audioFile) -> BeatTrackResult; | ||
|
||
} // namespace ta |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import sys | ||
|
||
import numpy as np | ||
import librosa | ||
|
||
|
||
def main(): | ||
try: | ||
file = sys.argv[1] | ||
y, sr = librosa.load(file, sr=None, mono=True) | ||
tempo, beats = librosa.beat.beat_track(y=y, sr=sr) | ||
beats = librosa.frames_to_time(beats, sr=sr) | ||
print(f'{tempo}') | ||
for b in beats: | ||
print(f'{b}') | ||
sys.exit(0) | ||
except Exception as e: | ||
print(e) | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains 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
This file contains 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
This file contains 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
This file contains 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