Sending MIDI messages using PING ultrasonic sensor
There is quite a bit that needs to be addressed for these programs to work, and I don't know too much about midi, but i'll use the arduino midi tutorial for info. http://arduino.cc/en/Tutorial/Midi?from=Tutorial.MIDI

The first code will take one measurement of an analog value, set it to note, make one digital reading of pingPin, set that to val, and then write noteOn() forever afterwards. The second code will continually make analog and Ping readings and then write those to serial if the ping reading is greater than 127.

Infinite loop aside, Your programs don't work largely because they send the sensor data in the wrong ways. The first byte sent to noteOn is a command byte, it should not contain sensor data. the arduino tutorial uses the command 0x90 to control the note being played on channel one. the second byte is the note and the third byte is velocity, which seems to work like volume.

to set the pitch and volume of channel one, you need to repeatedly send 

noteOn( 0x90, note, volume); 


If you want to play a note based on the analog readings that both of your codes set to "note" only when the ping value is larger than 127, you need something like this

if(ultrasoundValue &gt; 127){
    noteOn(0x90, note, 0x45); //middle volume according to arduino tutorial
} else {
    noteOn(0x90, note, 0x00); //make the note silent by using 0 volume
}


if you instead wanted to control the pitch with the ultrasoundValue, you would need to scale the value to being within 0-255, and then use that as note instead of the analog reading. You can get much more complicated than this, but you will need to find a reference for the MIDI protocol you can use.