Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
action-functions [2023/12/14 18:54] – [superDirt/playSuperDirtSample:] steve.wang | action-functions [2024/04/24 17:37] (current) – [midiSequencedRhythm/callMusicSynthesizerRhythm:] steve.wang | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Action Functions ====== | ====== Action Functions ====== | ||
+ | **Overview: ** This tutorial will teach you about what actions function do in Konduktiva and how they work. | ||
- | **Warning: | ||
- | 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 // | + | **Things To Note:** This tutorial assumes you have already installed Konduktiva successfully using the [[https:// |
+ | <code javascript> | ||
+ | const K = require(' | ||
+ | let e = K.setUpMusicalEnvironment(K.defaultConfigurationObject, | ||
+ | </ | ||
+ | |||
+ | |||
+ | An Action Function is the function the player calls when the Konduktiva scheduler tells it that to do something. | ||
<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 13: | Line 20: | ||
</ | </ | ||
- | 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 = ' | e.players.exampleMidiPlayer1.action = ' | ||
Line 29: | Line 36: | ||
If you do not, restart Konduktiva and assign the // | If you do not, restart Konduktiva and assign the // | ||
- | ===== Action Functions ===== | + | ===== Existing |
==== default: ==== | ==== default: ==== | ||
{{ : | {{ : | ||
- | ==== midiSequencedRhythm/ | ||
- | {{ : | ||
==== sendNotesMidiInfo: | ==== sendNotesMidiInfo: | ||
Line 44: | Line 49: | ||
==== superDirt/ | ==== superDirt/ | ||
{{ : | {{ : | ||
+ | |||
+ | ====== 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 // | ||
+ | |||
+ | **Things To Note:** This tutorial assumes you have already installed Konduktiva successfully using the [[https:// | ||
+ | |||
+ | Setup: | ||
+ | <code javascript> | ||
+ | const K = require(' | ||
+ | let e = K.setUpMusicalEnvironment(K.defaultConfigurationObject, | ||
+ | </ | ||
+ | |||
+ | ===== 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, | ||
+ | </ | ||
+ | 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, | ||
+ | console.log(' | ||
+ | console.log(' | ||
+ | console.log(' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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 | ||
+ | </ | ||
+ | Now to get a player to use it, modify the action variable of the Player: | ||
+ | <code javascript> | ||
+ | e.players.exampleMidiPlayer1.action = ' | ||
+ | </ | ||
+ | Now if you play the player you will see that it will use the // | ||
+ | <code javascript> | ||
+ | e.play(' | ||
+ | </ | ||
+ | |||
+ | ===== Example 2 ===== | ||
+ | Making an action function that plays a random note each time it is triggered. | ||
+ | |||
+ | <code javascript> | ||
+ | function sendRandomMidiNote(){ | ||
+ | | ||
+ | let noteToPlay = K.randomRange(0, | ||
+ | e.midiOutputs[0].send(' | ||
+ | note: noteToPlay, | ||
+ | velocity: 100, | ||
+ | channel: 0, | ||
+ | }); | ||
+ | setTimeout(() => { | ||
+ | e.midiOutputs[0].send(' | ||
+ | note: noteToPlay, | ||
+ | velocity: 100, | ||
+ | channel: 0, | ||
+ | }); | ||
+ | }, 1000) | ||
+ | } | ||
+ | e.actions.sendRandomMidiNote = sendRandomMidiNote | ||
+ | e.players.exampleMidiPlayer1.action = ' | ||
+ | e.play(' | ||
+ | </ | ||
+ | |||
+ | It should sound something like this: | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | [[http:// |