action-functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
action-functions [2024/01/01 18:15] steve.wangaction-functions [2024/04/24 17:37] (current) – [midiSequencedRhythm/callMusicSynthesizerRhythm:] steve.wang
Line 2: Line 2:
 **Overview: ** This tutorial will teach you about what actions function do in Konduktiva and how they work. **Overview: ** This tutorial will teach you about what actions function do in Konduktiva and how they work.
  
-**Things To Note:** This tutorial assumes you have already installed Konduktiva successfully using the [[https://github.com/renickbell/konduktiva|Konduktiva installation]] instructions and have read through and understood the [[:first_steps|first steps tutorial]]. The tutorial also assumes that you have imported Konduktiva to K variable and assigned any of the example MusicalEnvironments to e 
  
-An Action Function is the function the player calls when the Konduktiva scheduler tells it that it is time to do something. The action function the player uses can be controlled at the variable //e.players.playerName.action//. It should be a string and the strings refer to a variable which is a function in the //e.actions// object.+**Things To Note:** This tutorial assumes you have already installed Konduktiva successfully using the [[https://github.com/renickbell/konduktiva|Konduktiva installation]] instructions and have read through and understood the [[:first_steps|first steps tutorial]]. The tutorial also assumes 2 things. One, Konduktiva has been assigned to the //K// variable. Two, user created a Musical Environment using the //setUpMusicalEnvironment// function using //K.defaultConfigurationObject// as the first argument and //'exampleMidiPlayer'// as the third argument then, assigned the output to the //e// variable. 
 +<code javascript> 
 +const K = require('konduktiva'
 +let e = K.setUpMusicalEnvironment(K.defaultConfigurationObject,4,'exampleMidiPlayer', K.exampleMusicalEnvironmentsExtraConfig) 
 +</code> 
 + 
 + 
 +An Action Function is the function the player calls when the Konduktiva scheduler tells it that to do something.  The action function the player uses can be controlled at the variable //e.players.playerName.action//. It should be a string and the strings refer to a variable which is a function in the //e.actions// object. This analogy might help, players are like an employee working in a company. The action function is the job the employee has to do and the scheduler is the manager telling the employee when to start or stop working.
 <code javascript> <code javascript>
 //Code will work if you have imported Konduktiva to K variable and assigned any of the example MusicalEnvironments to e. //Code will work if you have imported Konduktiva to K variable and assigned any of the example MusicalEnvironments to e.
Line 14: Line 20:
 </code> </code>
  
-To test if the action function is being triggered you can assign the player to use the default action function and turn on verbose:+To test if the action function is being triggered you can assign the player to use the default action function and turn on verbose. Verbose is a feature that will prompt the functions to log many things which is useful for debugging.:
 <code javascript> <code javascript>
 e.players.exampleMidiPlayer1.action = 'default' e.players.exampleMidiPlayer1.action = 'default'
Line 30: Line 36:
 If you do not, restart Konduktiva and assign the //MusicalEnvironment// to //e// again.  If you do not, restart Konduktiva and assign the //MusicalEnvironment// to //e// again. 
  
-===== Action Functions =====+===== Existing Action Functions =====
 ==== default: ==== ==== default: ====
 {{ :default-action-funciton-flowchart.svg |}} {{ :default-action-funciton-flowchart.svg |}}
-==== midiSequencedRhythm/callMusicSynthesizerRhythm: ==== 
  
-{{ :midisequencedrhythm-callmusicsynthesizerrhythm--action-function-flowchart.svg |}} 
 ==== sendNotesMidiInfo: ===== ==== sendNotesMidiInfo: =====
  
Line 46: Line 50:
 {{ :superdirt-playsuperdirtsample-action-function-flowchart.svg |}} {{ :superdirt-playsuperdirtsample-action-function-flowchart.svg |}}
  
-[[http://konduktiva.org/doku.php?id=custom-actions|Click her to see how to create your own custom action function]]+====== How to write an action function ====== 
 + 
 +**Overview: ** This tutorial will teach you how to make your own action function by going through the steps of making a new action function called //testAction//
 + 
 +**Things To Note:** This tutorial assumes you have already installed Konduktiva successfully using the [[https://github.com/renickbell/konduktiva|Konduktiva installation]] instructions and have read through and understood the [[:first_steps|first steps tutorial]]. The [[http://konduktiva.org/doku.php?id=action-functions|action functions tutorial should]] also be read. 
 + 
 +Setup: 
 +<code javascript> 
 +const K = require('konduktiva'
 +let e = K.setUpMusicalEnvironment(K.defaultConfigurationObject,4,'exampleMidiPlayer', K.exampleMusicalEnvironmentsExtraConfig) 
 +</code> 
 + 
 +===== Example 1 ===== 
 +The action function arguments should be in this order for Konduktiva to be able to use it properly: player name, beat, MusicalEnvironment. 
 +So we will start this function like this: 
 +<code javascript> 
 +function testAction (playerName, b, e) { 
 +</code> 
 +Next we will make the action function do something very simple like log messages in the console. When making an action function, it is important to keep in mind that because JavaScript is single threaded, a computationally heavy action function could block the next events, and that's especially weird for events that are supposed to be simultaneous or temporally near. 
 +<code javascript> 
 +function testAction (playerName, b, e) { 
 +    console.log('Hi this is my new action function called testAction.'
 +    console.log('testAction playerName: ', playerName) 
 +    console.log('testAction beat: ', e.currentBeat()) 
 +
 +</code> 
 + 
 +Notice we did not use b argument. The b argument is for next onset. To get beat use e.currentBeat(). 
 + 
 +Next we have to make it so the players in the MusicalEnvironment can use it. To do so, we have the add it to the actions object of the MusicalEnvironment: 
 +<code javascript> 
 +e.actions.testAction = testAction 
 +</code> 
 +Now to get a player to use it, modify the action variable of the Player: 
 +<code javascript> 
 +e.players.exampleMidiPlayer1.action = 'testAction' 
 +</code> 
 +Now if you play the player you will see that it will use the //testAction// action function: 
 +<code javascript> 
 +e.play('exampleMidiPlayer1'
 +</code> 
 + 
 +===== Example 2 ===== 
 +Making an action function that plays a random note each time it is triggered. 
 + 
 +<code javascript> 
 +function sendRandomMidiNote(){ 
 +   K.checkIfUseVerboseLogging('Playing random note to midi output 0') 
 +    let noteToPlay = K.randomRange(0, 127) 
 +    e.midiOutputs[0].send('noteon',
 +        note: noteToPlay, 
 +      velocity: 100, 
 +      channel: 0, 
 +    }); 
 +    setTimeout(() => { 
 +        e.midiOutputs[0].send('noteon',
 +          note: noteToPlay, 
 +          velocity: 100, 
 +          channel: 0, 
 +        }); 
 +   }, 1000) 
 +
 +e.actions.sendRandomMidiNote = sendRandomMidiNote 
 +e.players.exampleMidiPlayer1.action = 'sendRandomMidiNote' 
 +e.play('exampleMidiPlayer1'
 +</code> 
 + 
 +It should sound something like this: 
 + 
 +{{ :random-notes.mp3 |}} 
 + 
 +[[http://konduktiva.org/doku.php?id=quantizedmaps-tutorial|Click here to learn about QuantizedMaps]] OR [[https://github.com/renickbell/konduktiva/blob/main/konduktiva-documentation.md|Click her to see full Konduktiva documentation]]
  • action-functions.1704161730.txt.gz
  • Last modified: 2024/01/01 18:15
  • by steve.wang