app/js/patchwork/core/AudioContextManager.js
/**
         * Listens to several events that can be dispatched by the patch, and acts accordingly.
         * @param type
         * @param data
         */
Listens to several events that can be dispatched by the patch, and acts accordingly.
@param type
@param data
Listens to several events that can be dispatched by the patch and acts accordingly . @param type @param data
handlePatchEvent(type, data) {
            switch (type) {
                case PatchEvent_1.default.MODULE_ADDED:
                    {
                        let module = data.module;
                        switch (module.definition.category) {
                            case ModuleCategories_1.default.NATIVE:
                                {
                                    let jsMethodName = module.definition.js;
                                    let audioNode = this.audioContext[jsMethodName].call(this.audioContext, data.args);
                                    module.setAudioNode(audioNode);
                                    if (module.definition.type === 'oscillator')
                                        module.audioNode.start();
                                    this.dispatchEvent(AudioContextManagerEvent_1.default.MODULE_ADDED, { module: module });
                                    break;
                                }
                            case ModuleCategories_1.default.PROXY:
                                {
                                    break;
                                }
                            default:
                                {
                                    console.error('Unhandled module category: ' + module.definition.category);
                                }
                        }
                        break;
                    }
                case PatchEvent_1.default.MODULE_REMOVED:
                    {
                        break;
                    }
                case PatchEvent_1.default.PATCH_CLEARED:
                    {
                        this.dispatchEvent(AudioContextManagerEvent_1.default.PATCH_CLEARED); 
                        break;
                    }
                case PatchEvent_1.default.CONNECTION_ADDED:
                    {
                        this.addApiConnectionFor(data.connection);
                        break;
                    }
                case PatchEvent_1.default.CONNECTION_PRE_REMOVE:
                    {
                        var apiConnectionsToRemove = data.connection.getApiConnections();
                        var outgoingApiConnections = [];
                        for (var i = 0; i < apiConnectionsToRemove.length; i++) {
                            var apiConnectionToRemove = apiConnectionsToRemove[i];
                            var sourceModuleToClear = apiConnectionToRemove.sourceModule;
                            var outgoingConnections = sourceModuleToClear.getOutgoingConnectionsForOutput(apiConnectionToRemove.sourceOutputIndex);
                            for (var j = 0; j < outgoingConnections.length; j++) {
                                var outgoingApiConnectionsToAdd = outgoingConnections[j].getApiConnections();
                                for (var k = 0; k < outgoingApiConnectionsToAdd.length; k++) {
                                    if (!outgoingApiConnectionsToAdd[k].isInList(outgoingApiConnections)) {
                                        outgoingApiConnections.push(outgoingApiConnectionsToAdd[k]);
                                    }
                                }
                            }
                        }
                        var apiConnectionsToRestore = [];
                        for (var i = 0; i < outgoingApiConnections.length; i++) {
                            if (!outgoingApiConnections[i].isInList(apiConnectionsToRemove)) {
                                apiConnectionsToRestore.push(outgoingApiConnections[i]);
                            }
                        }
                        var removed = [];
                        for (var i = 0; i < apiConnectionsToRemove.length; i++) {
                            var removeConnection = apiConnectionsToRemove[i];
                            if (!removeConnection.isInList(removed, true)) {
                                removeConnection.sourceModule.audioNode.disconnect(removeConnection.sourceOutputIndex);
                                this.dispatchEvent(AudioContextManagerEvent_1.default.OUTPUT_DISCONNECTED, {
                                    module: removeConnection.sourceModule,
                                    outputIndex: removeConnection.sourceOutputIndex
                                });
                                removed.push(removeConnection);
                            }
                        }
                        for (var i = 0; i < apiConnectionsToRestore.length; i++) {
                            this.addApiConnectionFor(apiConnectionsToRestore[i]);
                        }
                        break;
                    }
                case PatchEvent_1.default.MODULE_ATTRIBUTE_CHANGED:
                case PatchEvent_1.default.CONNECTION_POST_REMOVE:
                    {
                        break;
                    }
                default:
                    {
                        console.warn('Unhandled patch event: ' + type);
                    }
            }
        }
handlePatchEvent ( type , data ) { switch ( type ) { case PatchEvent_1 . default . MODULE_ADDED : { let module = data . module ; switch ( module . definition . category ) { case ModuleCategories_1 . default . NATIVE : { let jsMethodName = module . definition . js ; let audioNode = this . audioContext [ jsMethodName ] . call ( this . audioContext , data . args ) ; module . setAudioNode ( audioNode ) ; if ( module . definition . type === 'oscillator' ) module . audioNode . start ( ) ; this . dispatchEvent ( AudioContextManagerEvent_1 . default . MODULE_ADDED , { module : module } ) ; break ; } case ModuleCategories_1 . default . PROXY : { break ; } default : { console . error ( 'Unhandled module category: ' + module . definition . category ) ; } } break ; } case PatchEvent_1 . default . MODULE_REMOVED : { break ; } case PatchEvent_1 . default . PATCH_CLEARED : { this . dispatchEvent ( AudioContextManagerEvent_1 . default . PATCH_CLEARED ) ; break ; } case PatchEvent_1 . default . CONNECTION_ADDED : { this . addApiConnectionFor ( data . connection ) ; break ; } case PatchEvent_1 . default . CONNECTION_PRE_REMOVE : { var apiConnectionsToRemove = data . connection . getApiConnections ( ) ; var outgoingApiConnections = [ ] ; for ( var i = 0 ; i < apiConnectionsToRemove . length ; i ++ ) { var apiConnectionToRemove = apiConnectionsToRemove [ i ] ; var sourceModuleToClear = apiConnectionToRemove . sourceModule ; var outgoingConnections = sourceModuleToClear . getOutgoingConnectionsForOutput ( apiConnectionToRemove . sourceOutputIndex ) ; for ( var j = 0 ; j < outgoingConnections . length ; j ++ ) { var outgoingApiConnectionsToAdd = outgoingConnections [ j ] . getApiConnections ( ) ; for ( var k = 0 ; k < outgoingApiConnectionsToAdd . length ; k ++ ) { if ( ! outgoingApiConnectionsToAdd [ k ] . isInList ( outgoingApiConnections ) ) { outgoingApiConnections . push ( outgoingApiConnectionsToAdd [ k ] ) ; } } } } var apiConnectionsToRestore = [ ] ; for ( var i = 0 ; i < outgoingApiConnections . length ; i ++ ) { if ( ! outgoingApiConnections [ i ] . isInList ( apiConnectionsToRemove ) ) { apiConnectionsToRestore . push ( outgoingApiConnections [ i ] ) ; } } var removed = [ ] ; for ( var i = 0 ; i < apiConnectionsToRemove . length ; i ++ ) { var removeConnection = apiConnectionsToRemove [ i ] ; if ( ! removeConnection . isInList ( removed , true ) ) { removeConnection . sourceModule . audioNode . disconnect ( removeConnection . sourceOutputIndex ) ; this . dispatchEvent ( AudioContextManagerEvent_1 . default . OUTPUT_DISCONNECTED , { module : removeConnection . sourceModule , outputIndex : removeConnection . sourceOutputIndex } ) ; removed . push ( removeConnection ) ; } } for ( var i = 0 ; i < apiConnectionsToRestore . length ; i ++ ) { this . addApiConnectionFor ( apiConnectionsToRestore [ i ] ) ; } break ; } case PatchEvent_1 . default . MODULE_ATTRIBUTE_CHANGED : case PatchEvent_1 . default . CONNECTION_POST_REMOVE : { break ; } default : { console . warn ( 'Unhandled patch event: ' + type ) ; } } }
Listens to several events that can be dispatched by the patch, and acts accordingly.
Listens to several events that can be dispatched by the patch and acts accordingly .
// call the function with supplied arguments to create the audionode and store it // start if osc TODO methods as buttons? // console.log(data.attributes); // todo what is this? // if(data.attributes) // { // 	for(var i = 0; i < data.attributes.length; i++) // 	{ // 		var attribute = data.attributes[i]; // 		//console.log(ModuleDefinitions); // 		var attributeDefinition = ModuleDefinitions.findAttribute(module.definition.type, attribute.id); // 		switch(attributeDefinition.type) // 		{ // 			case AttributeTypes.AUDIO_PARAM: // 			{ // 				break; // 			} // 			default: // 			{ // 				console.error('Unhandled attribute type: ', attribute); // 			} // 		} // 	} // } // do nothing // todo what is this //this.codeGenerator.addToLiveCode(this.codeGenerator.getStringForModuleRemoved(data.module));	 //this.dispatchEvent(AudioContextManagerEvent.MODULE_REMOVED, {module: data.module}); // todo this is not caught by anyone? // get the api connections for this connection // we have to clear the source outputs of these api connections // get all outgoing connections for the source modules+outputs of these connections // get all the api connections for these connections // loop through them so we can check if we havent already added each of them // we now have a list of the outgoing api connections, which should include the ones to remove // make a new list without the ones we want to remove (which is the list of connections we need to restore) // loop through all connections // and for each connection, see if it exists in the apiconnections to remove // now that we know what to remove and to restore, disconnect all source modules. // note that disconnecting disregards the destination (+destinput), so chances are that there are // multiple disconnects done on the same source+sourceouput (with different destinations) // so we need to keep track of what we already disconnected to avoid doing unneccessary disconnect on the  // same source+output // and restore connections // does nothing