From 66fb186681ba955c32e4ec954344a781ad52362b Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Thu, 23 Jun 2022 20:51:14 -0300 Subject: Add segment formatting and handling --- bass_player.rb | 36 ++++++++++++++++++++++++++++-------- main.qml | 24 ++++++++++++++++++------ 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/bass_player.rb b/bass_player.rb index 6cb73c4..9d8b00c 100644 --- a/bass_player.rb +++ b/bass_player.rb @@ -10,7 +10,8 @@ module BassPlayer property(:title) { '' } property(:duration) { '' } - property(:current_song) { 0 } + property(:current_song) { '' } + property(:current_segment) { '' } property(:model) { QML::ArrayModel.new(:title, :duration) } property(:part_model) { QML::ArrayModel.new(:name, :from, :to) @@ -48,14 +49,18 @@ module BassPlayer end end - def song_selected(song) - puts "Selected song: #{song}" - unless song.nil? - self.current_song = model[song][:title] - puts "Setting current song to #{self.current_song}" + def song_selected(row) + unless row.nil? + self.current_song = model[row][:title] end + + # Clear current segment + self.current_segment = nil + + # Update model + song = Song.first(title: self.current_song) part_model.clear - parts = Song.first(title: self.current_song).parts.map do |part| + parts = song.parts.map do |part| { name: part.name, from: ms_to_time(part.from), @@ -66,12 +71,21 @@ module BassPlayer for part in parts part_model << part end + return { + path: song.path, + duration: song.duration + } + end + + def segment_selected(row) + song = Song.first(title: self.current_song) + self.current_segment = song.parts[row.to_i].to_hash end def add_segment(name, from, to) song = Song.first(title: current_song) Part.create( - name: name, from: from, to: to, + name: name, from: parse_time(from), to: parse_time(to), song_id: song.id ) song_selected(nil) @@ -81,6 +95,12 @@ module BassPlayer retval = Time.at(ms/1000).utc.strftime("%H:%M:%S") return retval end + + def parse_time(string) + # Parses a string of the form '3:12' to milliseconds + min, sec = string.split(':').map(&:to_i) + return ((min * 60) + sec) * 1000 + end end end diff --git a/main.qml b/main.qml index d0dda5b..a4bb28a 100644 --- a/main.qml +++ b/main.qml @@ -9,6 +9,8 @@ import BassPlayer 0.1 ApplicationWindow { visible: true + height: 400 + width: 600 title: "Bass Player!" @@ -35,9 +37,9 @@ ApplicationWindow { title: "Duration" } onClicked: { - console.log(song.current_song) - song.song_selected(row) - console.log(song.current_song) + var tmp = song.song_selected(row) + audioInterface.source = tmp.path + durationText.text = song.ms_to_time(tmp.duration) } } @@ -62,6 +64,9 @@ ApplicationWindow { role: "to" title: "To" } + onClicked: { + song.segment_selected(row) + } } } FileDialog { @@ -101,7 +106,6 @@ ApplicationWindow { onClicked: { audioInterface.play() timer.start() - durationText.text = song.ms_to_time(audioInterface.duration) } } Button { @@ -143,6 +147,14 @@ ApplicationWindow { repeat: true onTriggered: { slider.value = audioInterface.position / audioInterface.duration + + // Cool segment + if (song.current_segment) { + if(audioInterface.position < song.current_segment.from || + audioInterface.position > song.current_segment.to) { + audioInterface.seek(song.current_segment.from) + } + } } } @@ -166,11 +178,11 @@ ApplicationWindow { } TextField { id: textviewFrom - placeholderText: "From" + placeholderText: "00:00" } TextField { id: textviewTo - placeholderText: "To" + placeholderText: "00:00" } } } -- cgit v1.2.3-70-g09d2