<commit_before># A single MIDI device
# Wraps javax.sound.midi.MidiDevice objects

module JSound
  
  module Midi
    
    class Device      
      include_package 'javax.sound.midi'      
      include Util
      
      attr_reader :type
      
      def initialize(device)
        @device = device
        case device
        when Sequencer then @type = :sequencer
        when Synthesizer then @type = :synth
        else
          # This assumes a single device cannot be both an input and an output:
          if device.maxTransmitters != 0
            @type = :input
          elsif device.maxReceivers != 0
            @type = :output
          else
            @type = :unknown
          end
        end
      end

      def info
        @device.deviceInfo
      end

      def method_missing(sym, *args, &block)
        if @device.respond_to? sym
          @device.send(sym, *args, &block)
        else
          info.send(sym, *args, &block)
        end
      end 

      def >>(receiver)
        if receiver.kind_of? Device
          receiver.open if not receiver.open?     
          receiver = receiver.receiver
        end
        @device.open if not @device.open?    
        @device.transmitter.receiver = receiver
      end   

      def [](field)
        send field
      end

      def to_s(indent='')
        fields = []
        fields << "#{indent}  type: '#{type}'"
        fields << "#{indent}  description: '#{escape info.description}'" if info.description !~ unknown?
        fields << "#{indent}  name: '#{escape info.name}'" if info.name !~ unknown?
        fields << "#{indent}  vendor: '#{escape info.vendor}'" if info.vendor !~ unknown?
        fields << "#{indent}  version: '#{escape info.version}'" if info.version !~ unknown?      
        "#{indent}{\n" + fields.join(",\n") + "\n#{indent}}"
      end    
    end
    
  end
  
end<commit_msg>Device now prints feedback when opening and closing a device. Each opened device is now tracked and closed down automatically at_exit. This should exit cleanly and prevent jirb from hanging.<commit_after># A single MIDI device
# Wraps javax.sound.midi.MidiDevice objects

module JSound
  
  module Midi
    
    class Device      
      include_package 'javax.sound.midi'      
      include Util
      
      attr_reader :type      
      
      def self.open_devices
        @@open_devices ||= []
      end
      at_exit do
        # Close all open devices so we don't hang the program at shutdown time
        for device in Device.open_devices
          puts "Closing #{device.short_s}"
          device.close
        end
      end         
      
      def initialize(device)
        @device = device
        case device
        when Sequencer then @type = :sequencer
        when Synthesizer then @type = :synth
        else
          # This assumes a single device cannot be both an input and an output:
          if device.maxTransmitters != 0
            @type = :input
          elsif device.maxReceivers != 0
            @type = :output
          else
            @type = :unknown
          end
        end
      end

      def info
        @device.deviceInfo
      end
      
      def open
        if not @device.open?
          puts "Opening #{short_s}"
          @device.open
          Device.open_devices << self
        end
      end          

      def method_missing(sym, *args, &block)
        if @device.respond_to? sym
          @device.send(sym, *args, &block)
        else
          info.send(sym, *args, &block)
        end
      end 

      def >>(receiver)
        if receiver.kind_of? Device
          receiver.open     
          receiver = receiver.receiver
        end
        self.open
        @device.transmitter.receiver = receiver
      end   

      def [](field)
        send field
      end

      def to_s(indent='')
        fields = []
        fields << "#{indent}  type: '#{type}'"
        fields << "#{indent}  description: '#{escape info.description}'" if info.description !~ unknown?
        fields << "#{indent}  name: '#{escape info.name}'" if info.name !~ unknown?
        fields << "#{indent}  vendor: '#{escape info.vendor}'" if info.vendor !~ unknown?
        fields << "#{indent}  version: '#{escape info.version}'" if info.version !~ unknown?      
        "#{indent}{\n" + fields.join(",\n") + "\n#{indent}}"
      end 
      
      # A more compact String representation that should uniquely identify the device to a human.
      def short_s
        "MIDI #{type} device: #{info.description}"
      end
      
    end
    
  end
  
end
{'fraction_non_alphanumeric': 0.09897087803809941, 'fraction_numerical': 0.0008758484782132691, 'mean_word_length': 1.7029585798816569, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '630979', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1288, 'n_words': 399}