title: how to debug midi output device using pygames
body: <p>I'm using a <a href="http://www.behringer.com/EN/Products/BCF2000.aspx" rel="nofollow">BCF2000 Behringer fader</a> as input to control a robot using ROS. Everything was working fine, but suddenly it stopped working. By suddenly I mean after I got to word after a week off. I need to know if it's a hardware or software problem (maybe somebody drop it off while I was gone? I don't think so but I cannot find a bug). I'm running only a python module that reads the data from the fader and publishes it in ROS to find the problem, which is that I cannot make the sliders of the fader move.</p>
  
  <p>Basically, the python module reads two integers from the argument list. If a third one is given, all values of the fader should be put to 0 (sliders and buttons). The first integer is the input device id, and the second is the output device id. This is:</p>
  
  <pre><code>   if len(sys.argv) &gt; 1:
        input_dev = int(sys.argv[1])
     else:
        input_dev = pygame.midi.get_default_input_id()
        print "Using DEFAULT input device %d" % input_dev
        if input_dev == -1:
           print "No default MIDI input device"
           exit(-1)
     print "Using input device %d" % input_dev
  
     if len(sys.argv) &gt; 2:
        output_dev = int(sys.argv[2])
     else:
        output_dev = pygame.midi.get_default_output_id()
        print "Using DEFAULT output device %d" % input_dev
        if output_dev == -1:
           print "No default MIDI output device"
           exit(-1)
     print "Using output device %d" % output_dev
  
     controller = pygame.midi.Input(input_dev)
     controller_input = pygame.midi.Output(output_dev)
  </code></pre>
  
  <p>The thing is that I don't know how to find the correct input device number. For the last month I was calling it using '3' and '2', meaning:</p>
  
  <p><code>me@mycpu:ros$ rosrun bcf2000 bcf2000_driver.py 3 2</code></p>
  
  <p>if I make an echo of the published data, I get all the data published when I move any slider or press a button. For example:</p>
  
  <pre><code>me@mycpu:ros$ rostopic echo /bcf2000/joy
  header: 
    seq: 1
    stamp: 
      secs: 1441969279
      nsecs: 677656888
    frame_id: ''
  axes: [21.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 69.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  buttons: [127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  </code></pre>
  
  <p>but if I write to the fader, the sliders won't move. Again, this code was working a few days ago. I'm 100% sure is the same code according to the GIT repository log. For example, putting everything to 0:</p>
  
  <pre><code>for i in range(1,93):
   controller_input.write([[[176, i, 0, 0], 0]])
  </code></pre>
  
  <p>where the fader has 93 channels.</p>
  
  <p>It may be that I'm using the wrong output device number. I've changed the number between 0 and 10 and nothing. How do I find out what is the correct device number? How do I debug that is actually a software issue (maybe a OS issue, I don't know) or a hardware issue? The last is very unlikely, since it would be a very selective hardware issue. But I may be mistake.</p>
  
  <p>UPDATE: This only happens on my computer, so it's not a hardware issue.</p>
  
  <p>UPDATE 2: This is the output of  aseqdump -l</p>
  
  <pre><code>me@mycpu:ros$ aseqdump -l
   Port    Client name                      Port name
    0:0    System                           Timer
    0:1    System                           Announce
   14:0    Midi Through                     Midi Through Port-0
   24:0    BCF2000                          BCF2000 MIDI 1
   24:1    BCF2000                          BCF2000 MIDI 2
  </code></pre>
  
  <p>I can listen to the input using aseqdump -p 24:0.</p>
  
  <p>Listing the info from all devices using get_device_info gives:</p>
  
  <pre><code>('ALSA', 'Midi Through Port-0', 0, 1, 0)
  ('ALSA', 'Midi Through Port-0', 1, 0, 0)
  ('ALSA', 'BCF2000 MIDI 1', 0, 1, 0)
  ('ALSA', 'BCF2000 MIDI 1', 1, 0, 0)
  ('ALSA', 'BCF2000 MIDI 2', 0, 1, 0)
  ('ALSA', 'BCF2000 MIDI 2', 1, 0, 0)
  ('ALSA', 'BCF2000 MIDI 3', 0, 1, 0)
  ('ALSA', 'aseqdump', 0, 1, 0)
  </code></pre>
  
  <p>For devices 2, 4 and 6 I cannot write to the device. I'm trying to write to each output device using:</p>
  
  <pre><code>#!/usr/bin/env python
  import pygame
  import pygame.midi
  import sys
  import time
  
  def main():
      pygame.midi.init()
      devices = pygame.midi.get_count()
  
      if devices &lt; 1:
          print "No MIDI devices detected"
          exit(-1)
  
      print "Found %d MIDI devices" % devices
      print("id -- interface -- name -- input -- ouput -- opened")
  
      for ii in range(devices):
          ll = pygame.midi.get_device_info(ii)
          bool1 = bool(ll[2])
          bool2 = bool(ll[3])
          bool3 = bool(ll[4])
          print(str(ii) + " -- " + str(ll[0]) + " -- " + str(ll[1]) + " -- " + str(bool1) + " -- " + str(bool2) + " -- " + str(bool3))
  
          if (bool2):
              value = 0
              if ii%2 == 0:
                  value = 127
  
              controller = pygame.midi.Output(ii)
              controller.write([[[176, 48, 0, 0], value]])
              time.sleep(2)
  </code></pre>
  
  <p>which should print the device info for each device, and if its an output device, it should move the first slider. It doesn't move for any device.</p>