Sha256: 66d7a63813a946cc2079f2ff3422c02f7dd2e5f9f597d85aa104f51224cbb5ad

Contents?: true

Size: 1.75 KB

Versions: 93

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require 'glimmer-dsl-libui'

class TinyMidiPlayer
  include Glimmer
  
  VERSION = '0.0.1'

  def initialize
    @pid = nil
    @music_directory = File.expand_path('../sounds', __dir__)
    @midi_files      = Dir.glob(File.join(@music_directory, '**/*.mid'))
                          .sort_by { |path| File.basename(path) }
    at_exit { stop_midi }
    create_gui
  end

  def stop_midi
    if @pid
      Process.kill(:SIGKILL, @pid) if @th.alive?
      @pid = nil
    end
  end

  def play_midi
    stop_midi
    if @pid.nil? && @selected_file
      begin
        @pid = spawn "timidity #{@selected_file}"
        @th = Process.detach @pid
      rescue Errno::ENOENT
        warn 'Timidty++ not found. Please install Timidity++.'
        warn 'https://sourceforge.net/projects/timidity/'
      end
    end
  end

  def show_version
    msg_box('Tiny Midi Player',
              "Written in Ruby\n" \
                "https://github.com/kojix2/libui\n" \
                "Version #{VERSION}")
  end

  def create_gui
    menu('Help') {
      menu_item('Version') {
        on_clicked do
          show_version
        end
      }
    }
    window('Tiny Midi Player', 200, 50) {
      horizontal_box {
        vertical_box {
          stretchy false
          
          button('▶') {
            on_clicked do
              play_midi
            end
          }
          button('■') {
            on_clicked do
              stop_midi
            end
          }
        }

        combobox { |c|
          items @midi_files.map { |path| File.basename(path) }
          
          on_selected do
            @selected_file = @midi_files[c.selected]
            play_midi if @th&.alive?
          end
        }
      }
    }.show
  end
end

TinyMidiPlayer.new

Version data entries

93 entries across 93 versions & 1 rubygems

Version Path
glimmer-dsl-libui-0.12.5 examples/midi_player3.rb
glimmer-dsl-libui-0.12.4 examples/midi_player3.rb
glimmer-dsl-libui-0.12.3 examples/midi_player3.rb
glimmer-dsl-libui-0.12.2 examples/midi_player3.rb
glimmer-dsl-libui-0.12.1 examples/midi_player3.rb
glimmer-dsl-libui-0.12.0 examples/midi_player3.rb
glimmer-dsl-libui-0.11.10 examples/midi_player3.rb
glimmer-dsl-libui-0.11.9 examples/midi_player3.rb
glimmer-dsl-libui-0.11.8 examples/midi_player3.rb
glimmer-dsl-libui-0.11.7 examples/midi_player3.rb
glimmer-dsl-libui-0.11.6 examples/midi_player3.rb
glimmer-dsl-libui-0.11.5 examples/midi_player3.rb
glimmer-dsl-libui-0.11.4 examples/midi_player3.rb
glimmer-dsl-libui-0.11.3 examples/midi_player3.rb
glimmer-dsl-libui-0.11.2 examples/midi_player3.rb
glimmer-dsl-libui-0.11.1 examples/midi_player3.rb
glimmer-dsl-libui-0.11.0 examples/midi_player3.rb
glimmer-dsl-libui-0.10.2 examples/midi_player3.rb
glimmer-dsl-libui-0.10.1 examples/midi_player3.rb
glimmer-dsl-libui-0.10.0 examples/midi_player3.rb