Note Class
The Note class represents a single musical note with expressions defining its properties.
Constructor
const note = new Note(id, variables = {})Parameters
| Name | Type | Description |
|---|---|---|
id | number | Unique note identifier |
variables | object | Initial expression strings |
WARNING
Notes should be created via module.addNote(), not directly.
Properties
id
note.id // number (read-only)Unique identifier. BaseNote has ID 0, other notes have positive IDs.
expressions
note.expressions // objectMap of property names to BinaryExpression objects:
{
startTime: BinaryExpression,
duration: BinaryExpression,
frequency: BinaryExpression,
tempo: BinaryExpression,
beatsPerMeasure: BinaryExpression,
measureLength: BinaryExpression
}properties
note.properties // objectNon-expression properties:
{
color: string | null, // CSS color
instrument: string | null // Instrument name
}parentId
note.parentId // number | nullOptional parent note ID for hierarchical organization.
Variable Access
getVariable()
const value = note.getVariable(name)Returns the evaluated value from the module's cache.
| Parameter | Type | Description |
|---|---|---|
name | string | Property name |
Returns: Fraction object.
Example:
const freq = note.getVariable('frequency') // Fraction
freq.valueOf() // 440 (JavaScript number)setVariable()
note.setVariable(name, value)Sets an expression or property.
| Parameter | Type | Description |
|---|---|---|
name | string | Property name |
value | string | Expression text (for expressions) or value (for properties) |
Example:
// Set expression
note.setVariable('frequency', "module.baseNote.getVariable('frequency').mul(new Fraction(2))")
// Set property
note.setVariable('color', '#ff0000')getExpressionSource()
const source = note.getExpressionSource(name)
// → "module.baseNote.getVariable('frequency').mul(new Fraction(3, 2))"Returns the original expression text.
getExpression()
const expr = note.getExpression(name)
// → BinaryExpressionReturns the compiled BinaryExpression object.
hasExpression()
const has = note.hasExpression(name)
// → booleanChecks if an expression is defined.
getAllVariables()
const all = note.getAllVariables()
// → { startTime: Fraction, duration: Fraction, frequency: Fraction, ... }Returns all evaluated values.
Dependency Tracking
getAllDependencies()
const deps = note.getAllDependencies()
// → Set<number>Returns IDs of all notes referenced in any expression.
referencesBaseNote()
const refs = note.referencesBaseNote()
// → booleanChecks if any expression references the BaseNote.
Expression Properties
| Property | Type | Description |
|---|---|---|
startTime | Fraction | When note plays (seconds) |
duration | Fraction | How long note plays (seconds) |
frequency | Fraction | Pitch in Hz |
tempo | Fraction | Tempo in BPM |
beatsPerMeasure | Fraction | Time signature numerator |
measureLength | Fraction | Computed measure duration |
Non-Expression Properties
| Property | Type | Description |
|---|---|---|
color | string | CSS color for visualization |
instrument | string | Instrument name |
Internal Methods
_setExpression()
note._setExpression(name, exprText)Compiles expression and notifies module.
_setExpressionSilent()
note._setExpressionSilent(name, exprText)Sets expression without notification (used in batch operations).
_notifyChange()
note._notifyChange()Triggers module cache invalidation and event emission.
Serialization
toJSON()
const json = note.toJSON()
// → { id, frequency, startTime, duration, color, instrument, ... }Serializes note with expression source text.
Example Usage
// Create via module
const note = module.addNote({
frequency: "module.baseNote.getVariable('frequency').mul(new Fraction(5, 4))",
startTime: "new Fraction(0)",
duration: "new Fraction(60).div(module.findTempo(module.baseNote))"
})
// Set color
note.setVariable('color', '#4a90d9')
// Change frequency
note.setVariable('frequency', "module.baseNote.getVariable('frequency').mul(new Fraction(3, 2))")
// Read values (after evaluation)
const freq = note.getVariable('frequency')
console.log(freq.valueOf()) // 660
// Check dependencies
const deps = note.getAllDependencies()
console.log(deps.has(0)) // true (references BaseNote)
// Get source
const source = note.getExpressionSource('frequency')
console.log(source) // "module.baseNote.getVariable('frequency').mul(new Fraction(3, 2))"See Also
- Module Class - Module API
- BinaryExpression - Expression objects
- Creating Notes - User guide