require 'qml' require 'audioinfo' require './models' module BassPlayer VERSION = '0.1' class SongController include QML::Access register_to_qml property(:title) { '' } property(:duration) { '' } property(:current_song) { 0 } property(:model) { QML::ArrayModel.new(:title, :duration) } property(:part_model) { QML::ArrayModel.new(:name, :from, :to) } def initialize super() Song.all.each do |song| model << { title: song.title, duration: ms_to_time(song.duration) } end end def add(file_url, duration, metadata) path = file_url.split('file://').last AudioInfo.open(path) do |info| title = if info.title.empty? File.basename(path) else info.title end song = Song.create( title: title, duration: info.length * 1000, path: file_url ) item = { title: song.title, duration: ms_to_time(song.duration) } model << item 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}" end part_model.clear parts = Song.first(title: self.current_song).parts.map do |part| { name: part.name, from: ms_to_time(part.from), to: ms_to_time(part.to) } end for part in parts part_model << part end end def add_segment(name, from, to) song = Song.first(title: current_song) Part.create( name: name, from: from, to: to, song_id: song.id ) song_selected(nil) end def ms_to_time(ms) retval = Time.at(ms/1000).utc.strftime("%H:%M:%S") return retval end end end QML.run do |app| app.load_path Pathname(__FILE__) + '../main.qml' end