Adding sound
kbd.wrl

	

#VRML V2.0 utf8
#
#  kbd.wrl
#  Synthesizer keyboard
#	by David R. Nadeau
#
#  This world creates a 'playable' synthesizer keyboard.  Each key on
#  the keyboard has a sound and touch sensor associated with it.
#  Touching a key triggers the touch sensor, which triggers the sound.
#
#  Things to experiment with...
#	Use a PROTO to create a piano key that plays a note of a given
#	pitch, supplied as a PROTO argument.  Make the sound file an
#	argument as well.
#
#	Use a Script to trigger sounds on **Mouse button down** not up.
#	Huh, you say?  Well, a TouchSensor generates a touch time event
#	on mouse down, not up.  Since means that sounds triggered by
#	a TouchSensor seem to happen late... they happen when you release
#	the mouse button, not when you press it.  But, a TouchSensor
#	sends a TRUE on it's isActive output on mouse down.  So, you
#	could route that into a Script node and grab the associated
#	time stamp, then send THAT out as an event to start the sound.
#	This will remove the lag and make sounds more responsive.
#
WorldInfo {
	title "Synthesizer keyboard"
	info [ "Copyright (c) 1997, David R. Nadeau" ]
}
Viewpoint {
	position 0.875 1.5 1.5
	orientation 1.0 0.0 0.0 -0.785
	description "Entry view"
}
NavigationInfo {
	type [ "EXAMINE", "ANY" ]
	headlight FALSE
}
DirectionalLight {
	direction 1.0 -2.0 -0.5
}

# Middle C (C4)
Transform {
    children [
        DEF WhiteKey Shape {
            appearance Appearance {
                material Material { diffuseColor 1.0 1.0 1.0 }
            }
            geometry Box { size 0.23 0.1 1.5 }
        }
        DEF C4 TouchSensor { }
        Sound {
            source DEF PitchC4 AudioClip {
                url "tone1.wav"
                pitch 1.0
            }
            maxFront 100.0
            maxBack 100.0
        }
    ]
}
# C# above middle C (Cs4)
Transform { translation 0.125 0.1 -0.375
    children [
        DEF BlackKey Shape {
            appearance Appearance {
                material Material {
                    diffuseColor 0.4 0.4 0.4
                }
            }
            geometry Box { size 0.2 0.1 0.75 }
        }
        DEF Cs4 TouchSensor { }
        Sound {
            source DEF PitchCs4 AudioClip {
                url "tone1.wav"
                pitch 1.059
            }
            maxFront 100.0
            maxBack 100.0
        }
    ]
}
# D above middle C (D4)
Transform { translation 0.25 0.0 0.0
    children [
        USE WhiteKey,
        DEF D4 TouchSensor { }
        Sound {
            source DEF PitchD4 AudioClip {
                url "tone1.wav"
                pitch 1.122
            }
            maxFront 100.0
            maxBack 100.0
        }
    ]
}
# D# above middle C (Ds4)
Transform { translation 0.375 0.1 -0.375
    children [
        USE BlackKey,
        DEF Ds4 TouchSensor { }
        Sound {
            source DEF PitchDs4 AudioClip {
                url "tone1.wav"
                pitch 1.189
            }
            maxFront 100.0
            maxBack 100.0
        }
    ]
}
# E above middle C (E4)
Transform { translation 0.5 0.0 0.0
    children [
        USE WhiteKey,
        DEF E4 TouchSensor { }
        Sound {
            source DEF PitchE4 AudioClip {
                url "tone1.wav"
                pitch 1.260
            }
            maxFront 100.0
            maxBack 100.0
        }
    ]
}
# F above middle C (F4)
Transform { translation 0.75 0.0 0.0
    children [
        USE WhiteKey,
        DEF F4 TouchSensor { }
        Sound {
            source DEF PitchF4 AudioClip {
                url "tone1.wav"
                pitch 1.335
            }
            maxFront 100.0
            maxBack 100.0
        }
    ]
}
# F# above middle C (Fs4)
Transform { translation 0.875 0.1 -0.375
    children [
        USE BlackKey,
        DEF Fs4 TouchSensor { }
        Sound {
            source DEF PitchFs4 AudioClip {
                url "tone1.wav"
                pitch 1.414
            }
            maxFront 100.0
            maxBack 100.0
        }
    ]
}
# G above middle C (G4)
Transform { translation 1.0 0.0 0.0
    children [
        USE WhiteKey,
        DEF G4 TouchSensor { }
        Sound {
            source DEF PitchG4 AudioClip {
                url "tone1.wav"
                pitch 1.498
            }
            maxFront 100.0
            maxBack 100.0
        }
    ]
}
# G# above middle C (Gs4)
Transform { translation 1.125 0.1 -0.375
    children [
        USE BlackKey,
        DEF Gs4 TouchSensor { }
        Sound {
            source DEF PitchGs4 AudioClip {
                url "tone1.wav"
                pitch 1.587
            }
            maxFront 100.0
            maxBack 100.0
        }
    ]
}
# A above middle C (A5)
Transform { translation 1.25 0.0 0.0
    children [
        USE WhiteKey,
        DEF A5 TouchSensor { }
        Sound {
            source DEF PitchA5 AudioClip {
                url "tone1.wav"
                pitch 1.682
            }
            maxFront 100.0
            maxBack 100.0
        }
    ]
}
# A# above middle C (As5)
Transform { translation 1.375 0.1 -0.375
    children [
        USE BlackKey,
        DEF As5 TouchSensor { }
        Sound {
            source DEF PitchAs5 AudioClip {
                url "tone1.wav"
                pitch 1.782
            }
            maxFront 100.0
            maxBack 100.0
        }
    ]
}
# B above middle C (B5)
Transform { translation 1.5 0.0 0.0
    children [
        USE WhiteKey,
        DEF B5 TouchSensor { }
        Sound {
            source DEF PitchB5 AudioClip {
                url "tone1.wav"
                pitch 1.888
            }
            maxFront 100.0
            maxBack 100.0
        }
    ]
}
# C above middle C (C5)
Transform { translation 1.75 0.0 0.0
    children [
        USE WhiteKey,
        DEF C5 TouchSensor { }
        Sound {
            source DEF PitchC5 AudioClip {
                url "tone1.wav"
                pitch 2.0
            }
            maxFront 100.0
            maxBack 100.0
        }
    ]
}
ROUTE C4.touchTime  TO PitchC4.set_startTime
ROUTE Cs4.touchTime TO PitchCs4.set_startTime
ROUTE D4.touchTime  TO PitchD4.set_startTime
ROUTE Ds4.touchTime TO PitchDs4.set_startTime
ROUTE E4.touchTime  TO PitchE4.set_startTime
ROUTE F4.touchTime  TO PitchF4.set_startTime
ROUTE Fs4.touchTime TO PitchFs4.set_startTime
ROUTE G4.touchTime  TO PitchG4.set_startTime
ROUTE Gs4.touchTime TO PitchGs4.set_startTime
ROUTE A5.touchTime  TO PitchA5.set_startTime
ROUTE As5.touchTime TO PitchAs5.set_startTime
ROUTE B5.touchTime  TO PitchB5.set_startTime
ROUTE C5.touchTime  TO PitchC5.set_startTime