Preview Music playback that kind of works

This commit is contained in:
Stepland 2022-11-14 22:27:53 +01:00
parent 07d409adf6
commit c81ef0112a
4 changed files with 113 additions and 17 deletions

View File

@ -122,6 +122,68 @@ void EditorState::play_music_preview() {
preview_audio->play(); preview_audio->play();
} }
} }
} else {
set_playback_position(sf::seconds(std::stod(song.metadata.preview_loop.start.format("f"))));
play();
is_playing_preview_music_from_sss = true;
}
}
void EditorState::stop_music_preview() {
if (song.metadata.use_preview_file) {
if (preview_audio) {
preview_audio->stop();
}
} else {
stop();
is_playing_preview_music_from_sss = false;
}
}
bool EditorState::music_preview_is_playing() const {
if (song.metadata.use_preview_file) {
if (preview_audio) {
return preview_audio->getStatus() == sf::Music::Playing;
} else {
return false;
}
} else {
return is_playing_preview_music_from_sss;
}
}
sf::Time EditorState::music_preview_position() const {
if (not music_preview_is_playing()) {
return sf::Time::Zero;
}
if (song.metadata.use_preview_file) {
if (preview_audio) {
return preview_audio->getPlayingOffset();
}
} else if (is_playing_preview_music_from_sss) {
return audio.getPlayingOffset() - sf::seconds(std::stod(song.metadata.preview_loop.start.format("f")));
}
return sf::Time::Zero;
}
sf::Time EditorState::music_preview_duration() const {
if (song.metadata.use_preview_file) {
if (preview_audio) {
return preview_audio->getDuration();
}
} else {
return sf::seconds(std::stod(song.metadata.preview_loop.duration.format("f")));
}
return sf::Time::Zero;
}
void EditorState::update_music_preview_status() {
if (is_playing_preview_music_from_sss) {
if (music_preview_position() > music_preview_duration()) {
stop_music_preview();
}
} }
} }
@ -545,11 +607,8 @@ void EditorState::display_file_properties() {
ImGui::Separator(); ImGui::Separator();
ImGui::Text("Preview"); ImGui::Text("Preview");
if (ImGui::ArrowButton("Play", ImGuiDir_Right)) {
play_music_preview();
}
ImGui::SameLine();
if (ImGui::Checkbox("Use separate preview file", &song.metadata.use_preview_file)) { if (ImGui::Checkbox("Use separate preview file", &song.metadata.use_preview_file)) {
stop_music_preview();
if (song.metadata.use_preview_file) { if (song.metadata.use_preview_file) {
history.push(std::make_shared<ChangePreview>(song.metadata.preview_loop, song.metadata.preview_file)); history.push(std::make_shared<ChangePreview>(song.metadata.preview_loop, song.metadata.preview_file));
} else { } else {
@ -622,8 +681,42 @@ void EditorState::display_file_properties() {
song.metadata.preview_loop.duration = edited_loop_duration; song.metadata.preview_loop.duration = edited_loop_duration;
} }
} }
if (music_preview_is_playing()) {
if (ImGui::Button("Stop##Mucic Preview")) {
stop_music_preview();
}
} else {
if (ImGui::Button("Play##Music Preview")) {
play_music_preview();
}
}
ImGui::SameLine();
const auto position = music_preview_position();
const auto duration = music_preview_duration();
float progress = [&](){
if (duration == sf::Time::Zero) {
return 0.f;
} else {
return position / duration;
}
}();
ImGui::PushItemWidth(ImGui::CalcItemWidth() - ImGui::GetCursorPosX() + ImGui::GetStyle().WindowPadding.x);
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::SliderFloat(
"##Progress",
&progress,
0.f,
1.f,
fmt::format(
"{:02}:{:02} / {:02}:{:02}",
static_cast<unsigned int>(position / sf::seconds(60)),
static_cast<unsigned int>(position.asSeconds()) % 60,
static_cast<unsigned int>(duration / sf::seconds(60)),
static_cast<unsigned int>(duration.asSeconds()) % 60
).c_str()
);
ImGui::PopItemFlag();
ImGui::PopItemWidth();
} }
ImGui::End(); ImGui::End();
}; };

View File

@ -58,6 +58,7 @@ public:
std::shared_ptr<ChordClaps> chord_claps; std::shared_ptr<ChordClaps> chord_claps;
std::shared_ptr<BeatTicks> beat_ticks; std::shared_ptr<BeatTicks> beat_ticks;
std::optional<std::shared_ptr<OpenMusic>> music = {}; std::optional<std::shared_ptr<OpenMusic>> music = {};
bool is_playing_preview_music_from_sss = false;
int get_volume() const; int get_volume() const;
void set_volume(int newMusicVolume); void set_volume(int newMusicVolume);
@ -73,6 +74,11 @@ public:
std::optional<sf::Music> preview_audio; std::optional<sf::Music> preview_audio;
void play_music_preview(); void play_music_preview();
void stop_music_preview();
bool music_preview_is_playing() const;
sf::Time music_preview_position() const;
sf::Time music_preview_duration() const;
void update_music_preview_status();
Playfield playfield; Playfield playfield;
LinearView linear_view; LinearView linear_view;

View File

@ -394,6 +394,7 @@ int main() {
// Audio playback management // Audio playback management
if (editor_state) { if (editor_state) {
editor_state->update_visible_notes(); editor_state->update_visible_notes();
editor_state->update_music_preview_status();
if (editor_state->get_status() == sf::SoundSource::Playing) { if (editor_state->get_status() == sf::SoundSource::Playing) {
editor_state->previous_playback_position = editor_state->playback_position; editor_state->previous_playback_position = editor_state->playback_position;
if (editor_state->has_any_audio()) { if (editor_state->has_any_audio()) {

View File

@ -57,20 +57,16 @@ std::vector<std::string> Toolbox::getRecentFiles(std::filesystem::path settings)
* return an sf::Time as ±MM:SS.mmm in a string * return an sf::Time as ±MM:SS.mmm in a string
*/ */
std::string Toolbox::to_string(sf::Time time) { std::string Toolbox::to_string(sf::Time time) {
std::ostringstream stringStream;
int minutes = static_cast<int>(std::abs(time.asSeconds())) / 60; int minutes = static_cast<int>(std::abs(time.asSeconds())) / 60;
int seconds = static_cast<int>(std::abs(time.asSeconds())) % 60; int seconds = static_cast<int>(std::abs(time.asSeconds())) % 60;
int miliseconds = static_cast<int>(std::abs(time.asMilliseconds())) % 1000; int miliseconds = static_cast<int>(std::abs(time.asMilliseconds())) % 1000;
if (time.asSeconds() < 0) { return fmt::format(
stringStream << "-"; "{}{:02}:{:02}.{:03}",
} else { time.asSeconds() < 0 ? "-" : "+",
stringStream << "+"; minutes,
} seconds,
stringStream.fill('0'); miliseconds
stringStream << std::setw(2) << minutes << ":" << std::setw(2) << seconds );
<< "." << std::setw(3) << miliseconds;
//("-%02d:%02d.%03d",minutes,seconds,miliseconds);
return stringStream.str();
} }
float Toolbox::convertVolumeToNormalizedDB(int input) { float Toolbox::convertVolumeToNormalizedDB(int input) {