namespace pxt . AudioContextManager { let _frequency = 0 ; let _context ; let _vco ; let _gain ; let _mute = false ; function context ( ) { if ( ! _context ) _context = freshContext ( ) ; return _context ; } function freshContext ( ) { ( < any > window ) . AudioContext = ( < any > window ) . AudioContext || ( < any > window ) . webkitAudioContext ; if ( ( < any > window ) . AudioContext ) { try { return new ( < any > window ) . AudioContext ( ) ; } catch ( e ) { } } return undefined ; } export function mute ( mute ) { if ( ! _context ) return ; _mute = mute ; stop ( ) ; if ( mute && _vco ) { _vco . disconnect ( ) ; _gain . disconnect ( ) ; _vco = undefined ; _gain = undefined ; } } export function stop ( ) { if ( ! _context ) return ; _gain . gain . setTargetAtTime ( 0 , _context . currentTime , 0.015 ) ; _frequency = 0 ; } export function frequency ( ) { return _frequency ; } export function tone ( frequency ) { if ( _mute ) return ; if ( frequency < 0 ) return ; _frequency = frequency ; let ctx = context ( ) as AudioContext ; if ( ! ctx ) return ; try { if ( ! _vco ) { _vco = ctx . createOscillator ( ) ; _vco . type = 'triangle' ; _gain = ctx . createGain ( ) ; _gain . gain . value = 0 ; _gain . connect ( ctx . destination ) ; _vco . connect ( _gain ) ; _vco . start ( 0 ) ; } _vco . frequency . linearRampToValueAtTime ( frequency , _context . currentTime ) ; _gain . gain . setTargetAtTime ( .2 , _context . currentTime , 0.015 ) ; } catch ( e ) { _vco = undefined ; return ; } } }