How do I use a Keyboard Sustain Pedal as input for my computer?

9,109

Solution 1

Well it's been 7 months or so and I totally forgot about this... came back and tried it again today and I managed to get it working.

Here's a simple Java program that works for my pedal. It converts the presses into keyboard "V"s (it's for me to activate +voice_record in cS GO)

    package pedal2keyboard;
    
    import java.io.ByteArrayOutputStream;
    import java.nio.ByteBuffer;
    import java.nio.ByteOrder;
    
    import javax.sound.sampled.*;
    
    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    
    /***
     * Author: Dois Koh
     * Date: 27th October 2015
     * 
     * Gets your microphone signal and you can go do whatever you want with it.
     * Right now, it takes signals from my Cherub WTB-004 Keyboard Sustain Pedal, plugged into
     * my microphone jack, and converts it into key presses (holds down V when depressed,
     * releases V when released)
     */
    public class PedalToKeyboard {
    
        // Robot for performing keyboard actions (pressing V)
        public static Robot robot = null;
        
        // Currently 8KHz, 16 bit signal (2 bytes), single channel, signed (+ and -) and BIG ENDIAN format
        public static AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
        
        public static TargetDataLine microphone = null;
        public static boolean pedalPressed = false;
    
        public static void main(String[] args) {
            
            try {
                // Initialize robot for later use
                robot = new Robot();
                
                // Retrieve the line to from which to read in the audio signal
                microphone = AudioSystem.getTargetDataLine(format);
                
                // Open the line in the specified format -
                // Currently 8KHz, 16 bit signal (2 bytes), single channel, signed (+ and -) and BIG ENDIAN format      
                microphone.open(new AudioFormat(8000.0f, 16, 1, true, true));
    
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] data = new byte[microphone.getBufferSize()/8];
                
                // Begin audio capture.
                microphone.start();
    
                int numBytesRead = 0;
                short previousShort = 0;
                
                // Continue until program is manually terminated
                while (true) {
                    
                    // Read the next chunk of data from the TargetDataLine.
                    numBytesRead = microphone.read(data, 0, data.length);
                    
                    // Reset the buffer (get rid of previous data)
                    out.reset();
                    
                    // Save this chunk of data.
                    out.write(data, 0, numBytesRead);
                    
                    byte[] bytes = out.toByteArray();
                    short[] shorts = new short[bytes.length/2];
                    
                    // to turn bytes to shorts as either big endian or little endian. 
                    ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asShortBuffer().get(shorts);
                    
                    // Iterate through retrieved 16 bit data (shorts)
                    for (short s : shorts) {
                        
                        // Check if descending or ascending (pedal press is descending, release is ascending)
                        if (s < 0) { // descending                  
                            // make sure drop is large instantaneous drop
                            if (Math.abs(previousShort - s) > 200 && s < -32700) {
                                if (!pedalPressed) {
                                    PedalPressedAction();
                                    previousShort = s;
                                    break;
                                }
                            }
                        } else if (s > 0) { // ascending
                            // make sure increase is large instantaneous increase
                            if (Math.abs(previousShort - s) > 200 && s > 32700) {
                                if (pedalPressed) {
                                    PedalReleasedAction();
                                    previousShort = s;
                                    break;
                                }
                            }
                        }
                        
                        previousShort = s;
                    }
                }    
                
            } catch (LineUnavailableException | AWTException e) {
                e.printStackTrace();
            } finally {
                if (microphone != null)
                    microphone.close();
            }
        }
        
        /***
         * The action to perform when the pedal is depressed
         */
        public static void PedalPressedAction() {
            pedalPressed = true;
            robot.keyPress(KeyEvent.VK_V);
        }
        
        /***
         * The action to perform when the pedal is released
         */
        public static void PedalReleasedAction(){
            pedalPressed = false;
            robot.keyRelease(KeyEvent.VK_V);        
        }
    }

Solution 2

Why go through all the trouble, when you can just buy exactly what you are looking for? A USB Foot Pedal made for PCs, like this:

enter image description here

They are cheap and does exactly what you are looking to do.

Solution 3

To actually do this. All you really need is keyboard. Something small like this. Gut the pedal and the keyboard. You might even be able to fit the keyboard inside the pedal with the USB cable to replace the audio wire. The pedal probably has 3 leads, + & - and the resistor lead. Just find the + and - leads through trial and error (only 6 possible choices). Wire one of the keys to the leads in the pedal. When you press the pedal and close the circuit... keypress!

Share:
9,109

Related videos on Youtube

Dois
Author by

Dois

Updated on September 18, 2022

Comments

  • Dois
    Dois over 1 year

    I was hoping some super electronics expert dude could give me some advice. How do I get this http://www.amazon.co.uk/Cherub-WTB-004-Keyboard-Sustain-Pedal/dp/B000UDVV6E to work with my computer? I basically want to use it to replace/emulate a keystroke or something in my games. Anyone with some experience with something like this got any advice? I'm willing to try simple software related hacks... or more advanced software stuff if there are resources online for it.

    Edit:

    Pedal Waveform

    I tried plugging it into my microphone jack and lo and behold, whenever I activate (step on) the pedal, I get a signal - this is the recording (on audacity).

    The first "thick" one is from holding down on the pedal, the rest were just taps.

    • LPChip
      LPChip about 9 years
      I don't think this exists, as the pedal talks in the language MIDI. You'd need a device that translates this into MIDI signals and maybe then you can somehow do something with this. This, to my knowledge, does not exist.
    • slhck
      slhck about 9 years
      @LPChip The pedal does not talk MIDI. It only has an analog jack connector.
    • Yorik
      Yorik about 9 years
      @slhck: the pedal is a midi trigger ( midi trigger interface )
    • Dois
      Dois about 9 years
      So there's no way to process this without additional hardware?
    • user3359503
      user3359503 about 9 years
      Technically you could process it w/o additional hardware, but unless someone wrote software to do this, you'd have to write your own code to translate the microphone input into a usable keystroke.
  • Yorik
    Yorik about 9 years
    This isn't really an answer, since the OP already owns the device and wants to know how to make use of it as-is. I agree that this is easier, but he will still need suitable midi-aware software to make use of it (DAW, amp simulator, midi-pass-through etc all of which can be gotten as freeware)
  • Keltari
    Keltari about 9 years
    @Yorik this is an answer, as it is an alternative that will work
  • Yorik
    Yorik about 9 years
    He said "how to get this thing that I already own to work" You said "use something else." So in this sense, it isn't an answer. I think you know that.
  • Dois
    Dois about 9 years
    I want to go through the trouble because I think it'll be fun trying to repurpose this thing... and also like the people above have said, I don't wanna buy something else.
  • Dois
    Dois about 9 years
    Sorry, what do you mean "generate voltage"? There's no battery on this thing and it's is pulling current from my PC so I don't get that part (just clarifying). Also, is there some other way to process the signal that I can get with just software? (I can try writing the software if you can point me in the right direction) I don't understand how the signal is generated or what format it's in but surely I can do something with this... Does it HAVE to be converted into "joystick or midi" form externally?
  • Keltari
    Keltari about 9 years
    you will spend more on parts to make it work than the $13 it would take to make it. However, I will provide an answer on how to do it, as well.
  • Yorik
    Yorik about 9 years
    It is probably moving a plunger with a coil and magnet, so it is "passive" like a guitar pickup or a diaphragm mic. You microphone jack has an amplifier connected to it, and most computers have a +10 gain option for the microphone. As far as software, there are all kinds of things you can do. If you google around for "simpit switch" you will find all sorts of information about cockpit flight simulators i.e. games)
  • Yorik
    Yorik about 9 years
    where people take switches and convert the inputs. google "keyboard pedalboard" for info about using keybards as a base for footswitches. Hacking a joypad or keyboard is the most common and they are super low volatge/current so your risk of killing yourself or starting a fire are almost nil. Joystick buttons and keyboard keys are just switches that close a circuit by touching two wires together.
  • Yorik
    Yorik about 9 years
    The software might be called "midi translator" for midi >joystick or midi>keyboard. There was something called Joy2Midi for converting joysticks to midi control signals. Joy2Key was a program that did joystick to keyboard. There are lots of options, but look for opensource and be careful that they don't have spamware coinstallers etc
  • Yorik
    Yorik about 9 years
    FYI: moving a wire in a magnetic field induces voltage ( resources.schoolscience.co.uk/CDA/16plus/copelech4pg2.html ) it is a reversible process, so microphones and speakers are basically the same thing with tweaks for their specific function.
  • Yorik
    Yorik about 9 years
    One last thing: it is plausible that a piece of software exists that can listen to the mic input and emit a keypress if the signal is high enough ("loud" enough). Maybe even use text-to-speech and train it with a single letter dictionary.
  • megamer
    megamer over 2 years
    I just wanted to say that this is extremely cool. Thanks a lot for sharing!