Console Commands: Difference between revisions

Jump to navigation Jump to search
Working on the advanced techniques section
(Fill in intermediate techniques.)
(Working on the advanced techniques section)
Line 47: Line 47:


In JS, functions parameters can have [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters default values]. This means if you don't pass a value to that parameter, it will default to some value. A lot of the functions in this section have default parameters. They're written in the documentation like this: "fadeNotes(fadeIn = false)". This means the "fadeIn" parameter defaults to false, so instead of writing "fadeNotes(false)" you can just write "fadeNotes()".
In JS, functions parameters can have [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters default values]. This means if you don't pass a value to that parameter, it will default to some value. A lot of the functions in this section have default parameters. They're written in the documentation like this: "fadeNotes(fadeIn = false)". This means the "fadeIn" parameter defaults to false, so instead of writing "fadeNotes(false)" you can just write "fadeNotes()".
The functions in this section are defined [https://onlinesequencer.net/app/consoleCommands.js here]. If you know some JS, you can read this file to learn how they work and create your own.


=== Fade notes ===
=== Fade notes ===
Line 103: Line 105:


== Advanced techniques ==
== Advanced techniques ==
You will need to know a little JS to use these functions. In particular, we're going to be using a lot of [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#syntax lambda functions], so familiarize yourself with these first. This section also assumes you know basic programming concepts like [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Variables variables], [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics objects], [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Looping_code loops], and [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/conditionals if statements].
You will need to know some JS to use these functions. In particular, we're going to be using a lot of [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#syntax lambda functions], so familiarize yourself with these first. This section also assumes you know basic programming concepts like [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Variables variables], [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics objects], [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Looping_code loops], and [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/conditionals if statements].


=== Selecting notes and markers ===
=== Working with the selection ===
Most of the functions in the previous section work on the selected notes and markers. The notes and markers selected in the UI are stored in the "selection" object.
console.log(selection.notes)
console.log(selection.markers)
console.log(selection.getTimeSpan())
You can find all the methods on the selection object [https://onlinesequencer.net/app/selection.js here]. It's sometimes useful to be able to loop over all the notes or markers in the selection, but if you want to modify the notes in the selection, use tweakNotes below.
for (const note of selection.notes) {
  console.log(note);
}
for (const marker of selection.marker) {
  console.log(marker);
}
You can also add or remove markers and notes from the selection using the selectNote, selectMarker, deselectNote, and deselectMarker methods. However, it's usually easier to use these functions:
  selectNotesIf(predicate, addToSelection = false)
  selectNotesIf(predicate, addToSelection = false)
  selectMarkersIf(predicate, addToSelection = false)
  selectMarkersIf(predicate, addToSelection = false)
These functions take a predicate (a function that returns true or false) and select all the notes or markers for which the predicate returns true. If addToSelection is true, these notes or markers are added to the existing selection, otherwise they replace the current selection. Both these functions include undo/redo support.
For example, say you wanted to delete the [https://tiusic.com/thumb.html thumbnail] notes from a sequence, so you can change the thumbnail. Thumbnail notes all have 0 volume, so you can just select all the notes with 0 volume:
selectNotesIf(n => n.volume == 0)
This selects all the thumbnail notes, so now you can delete them as normal (though the UI focus will still be on the console, so you'll have to click on the sequencer before you press delete).


=== Tweaking notes ===
=== Tweaking notes ===
  tweakNotes(tweakFunction)
  tweakNotes(tweakFunction)
This is probably the single most useful console function. It basically runs tweakFunction on every selected note, and has undo/redo support for any changes that tweakFunction makes to the note. It's roughly equivalent to:
for (const note of selection.notes) {
  tweakFunction(note);
}
There are a few problems with doing a for loop like this and manually tweaking the notes. For starters you won't get undo/redo support, but the bigger problem is that there are a few optimizations that OS does based on the note time, instrument, length, or pitch that require extra work if you modify those parameters (namely "song.moveNote()" and "song.updateLoopTime()"). Failure to do so could corrupt your sequence. tweakNotes takes care of this work for you so you don't have to worry about it.
The fields of the note object that you can modify are:
{| class="wikitable"
|+The fields of the Note object
!Name
!Type
!Description
|-
|instrument
|Integer
|The instrument id, eg 22 for sitar
|-
|type
|String
|The pitch or keyboard note, eg 'C5' or 'F#7'
|-
|time
|Number
|The time when the note starts in standard time units (quarter notes)
|-
|length
|Number
|The length of the note in standard time units
|-
|volume
|Number
|The volume of the note, usually from 0 to 1.
|}
If you console.log the note you'll see other internal fields, but '''DO NOT MODIFY THESE'''.
TODO: example


=== Tweaking markers ===
=== Tweaking markers ===
There is currently no marker equivalent of tweakNotes. Markers also have optimizations that mean you can't modify the time, instrument, or setting, without doing extra work. The only things you can safely modify are the value and the blend. If you need to modify the other parameters, you're better off asking the devs to add a tweakMarkers function, rather than trying to modify them and corrupting your sequence.
{| class="wikitable"
|+The fields of the Marker object
!Name
!Type
!Description
!Can modify?
|-
|instrument
|Integer
|The instrument id, eg 19 for xylophone
|NO!
|-
|time
|Integer
|The marker's time (quarter notes, must be a whole number)
|NO!
|-
|setting
|Integer
|The setting (one of the kMarkerSetting... constants, eg kMarkerSettingBpm)
|NO!
|-
|value
|Dynamic
|The marker value. Its type depends on the marker setting.
|yes
|-
|blend
|Boolean
|Whether or not the marker is blended.
|yes
|}
TODO: example


=== Creating and deleting notes ===
=== Creating and deleting notes ===
Line 119: Line 209:


=== Other handy tools ===
=== Other handy tools ===
selection

Navigation menu