Skip to content

Commit

Permalink
update audio api
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo committed Sep 25, 2024
1 parent a55ce8a commit 14d645e
Showing 1 changed file with 68 additions and 4 deletions.
72 changes: 68 additions & 4 deletions extensions/BeatBlox/webAudioAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,64 @@ const ModificationType = {
TurnUpper: 124, TurnLower: 125, Glissando: 126, Portamento: 127
};

/**
* Object representing modification types that conflict with one another.
* @constant {Object<number, number[]>}
*/
const ModificationIncompatibilities = {
[ModificationType.Velocity]: [ModificationType.Velocity, ModificationType.Piano, ModificationType.Forte, ModificationType.MezzoPiano, ModificationType.MezzoForte,
ModificationType.Pianissimo, ModificationType.Fortissimo, ModificationType.Pianississimo, ModificationType.Fortissimo],
[ModificationType.Piano]: [ModificationType.Velocity, ModificationType.Piano, ModificationType.Forte, ModificationType.MezzoPiano, ModificationType.MezzoForte,
ModificationType.Pianissimo, ModificationType.Fortissimo, ModificationType.Pianississimo, ModificationType.Fortissimo],
[ModificationType.Forte]: [ModificationType.Velocity, ModificationType.Piano, ModificationType.Forte, ModificationType.MezzoPiano, ModificationType.MezzoForte,
ModificationType.Pianissimo, ModificationType.Fortissimo, ModificationType.Pianississimo, ModificationType.Fortissimo],
[ModificationType.MezzoPiano]: [ModificationType.Velocity, ModificationType.Piano, ModificationType.Forte, ModificationType.MezzoPiano, ModificationType.MezzoForte,
ModificationType.Pianissimo, ModificationType.Fortissimo, ModificationType.Pianississimo, ModificationType.Fortissimo],
[ModificationType.MezzoForte]: [ModificationType.Velocity, ModificationType.Piano, ModificationType.Forte, ModificationType.MezzoPiano, ModificationType.MezzoForte,
ModificationType.Pianissimo, ModificationType.Fortissimo, ModificationType.Pianississimo, ModificationType.Fortissimo],
[ModificationType.Pianissimo]: [ModificationType.Velocity, ModificationType.Piano, ModificationType.Forte, ModificationType.MezzoPiano, ModificationType.MezzoForte,
ModificationType.Pianissimo, ModificationType.Fortissimo, ModificationType.Pianississimo, ModificationType.Fortissimo],
[ModificationType.Fortissimo]: [ModificationType.Velocity, ModificationType.Piano, ModificationType.Forte, ModificationType.MezzoPiano, ModificationType.MezzoForte,
ModificationType.Pianissimo, ModificationType.Fortissimo, ModificationType.Pianississimo, ModificationType.Fortissimo],
[ModificationType.Pianississimo]: [ModificationType.Velocity, ModificationType.Piano, ModificationType.Forte, ModificationType.MezzoPiano, ModificationType.MezzoForte,
ModificationType.Pianissimo, ModificationType.Fortissimo, ModificationType.Pianississimo, ModificationType.Fortissimo],
[ModificationType.Fortississimo]: [ModificationType.Velocity, ModificationType.Piano, ModificationType.Forte, ModificationType.MezzoPiano, ModificationType.MezzoForte,
ModificationType.Pianissimo, ModificationType.Fortissimo, ModificationType.Pianississimo, ModificationType.Fortissimo],
[ModificationType.Crescendo]: [ModificationType.Crescendo, ModificationType.Decrescendo, ModificationType.Diminuendo],
[ModificationType.Decrescendo]: [ModificationType.Crescendo, ModificationType.Decrescendo, ModificationType.Diminuendo],
[ModificationType.Diminuendo]: [ModificationType.Crescendo, ModificationType.Decrescendo, ModificationType.Diminuendo],
[ModificationType.Accent]: [ModificationType.Accent, ModificationType.Marcato],
[ModificationType.Marcato]: [ModificationType.Accent, ModificationType.Marcato],
[ModificationType.Staccato]: [ModificationType.Staccato, ModificationType.Staccatissimo, ModificationType.Tenuto],
[ModificationType.Staccatissimo]: [ModificationType.Staccato, ModificationType.Staccatissimo, ModificationType.Tenuto],
[ModificationType.Tenuto]: [ModificationType.Staccato, ModificationType.Staccatissimo, ModificationType.Tenuto],
[ModificationType.OctaveShiftUp]: [ModificationType.OctaveShiftUp, ModificationType.OctaveShiftDown],
[ModificationType.OctaveShiftDown]: [ModificationType.OctaveShiftUp, ModificationType.OctaveShiftDown],
[ModificationType.GraceAcciaccatura]: [ModificationType.GraceAcciaccatura, ModificationType.GraceAppoggiatura],
[ModificationType.GraceAppoggiatura]: [ModificationType.GraceAcciaccatura, ModificationType.GraceAppoggiatura],
[ModificationType.Tuplet]: [ModificationType.Tuplet, ModificationType.Triplet, ModificationType.Quintuplet, ModificationType.Sextuplet, ModificationType.Septuplet],
[ModificationType.Triplet]: [ModificationType.Tuplet, ModificationType.Triplet, ModificationType.Quintuplet, ModificationType.Sextuplet, ModificationType.Septuplet],
[ModificationType.Quintuplet]: [ModificationType.Tuplet, ModificationType.Triplet, ModificationType.Quintuplet, ModificationType.Sextuplet, ModificationType.Septuplet],
[ModificationType.Sextuplet]: [ModificationType.Tuplet, ModificationType.Triplet, ModificationType.Quintuplet, ModificationType.Sextuplet, ModificationType.Septuplet],
[ModificationType.Septuplet]: [ModificationType.Tuplet, ModificationType.Triplet, ModificationType.Quintuplet, ModificationType.Sextuplet, ModificationType.Septuplet],
[ModificationType.TrillUpper]: [ModificationType.TrillUpper, ModificationType.TrillLower, ModificationType.MordentUpper, ModificationType.MordentLower,
ModificationType.TurnUpper, ModificationType.TurnLower, ModificationType.Glissando, ModificationType.Portamento],
[ModificationType.TrillLower]: [ModificationType.TrillUpper, ModificationType.TrillLower, ModificationType.MordentUpper, ModificationType.MordentLower,
ModificationType.TurnUpper, ModificationType.TurnLower, ModificationType.Glissando, ModificationType.Portamento],
[ModificationType.MordentUpper]: [ModificationType.TrillUpper, ModificationType.TrillLower, ModificationType.MordentUpper, ModificationType.MordentLower,
ModificationType.TurnUpper, ModificationType.TurnLower, ModificationType.Glissando, ModificationType.Portamento],
[ModificationType.MordentLower]: [ModificationType.TrillUpper, ModificationType.TrillLower, ModificationType.MordentUpper, ModificationType.MordentLower,
ModificationType.TurnUpper, ModificationType.TurnLower, ModificationType.Glissando, ModificationType.Portamento],
[ModificationType.TurnUpper]: [ModificationType.TrillUpper, ModificationType.TrillLower, ModificationType.MordentUpper, ModificationType.MordentLower,
ModificationType.TurnUpper, ModificationType.TurnLower, ModificationType.Glissando, ModificationType.Portamento],
[ModificationType.TurnLower]: [ModificationType.TrillUpper, ModificationType.TrillLower, ModificationType.MordentUpper, ModificationType.MordentLower,
ModificationType.TurnUpper, ModificationType.TurnLower, ModificationType.Glissando, ModificationType.Portamento],
[ModificationType.Glissando]: [ModificationType.TrillUpper, ModificationType.TrillLower, ModificationType.MordentUpper, ModificationType.MordentLower,
ModificationType.TurnUpper, ModificationType.TurnLower, ModificationType.Glissando, ModificationType.Portamento],
[ModificationType.Portamento]: [ModificationType.TrillUpper, ModificationType.TrillLower, ModificationType.MordentUpper, ModificationType.MordentLower,
ModificationType.TurnUpper, ModificationType.TurnLower, ModificationType.Glissando, ModificationType.Portamento]
};

/**
* Object representing a mapping between an acoustic analysis type and its unique internal code.
* @constant {Object<string, number>}
Expand Down Expand Up @@ -916,7 +974,9 @@ class Mordent extends ModificationBase {
}
if (!Number.isInteger(mordentNote) || (Number(mordentNote) < 1))
throw new WebAudioValueError(`The offset value (${mordentNote}) must be a positive integer > 0`);
const mordentNoteDuration = 60.0 / ((32.0 / this.tempo.beatBase) * this.tempo.beatsPerMinute);
const mordentNoteDuration = (this.unmodifiedDetails.duration >= 16) ?
(60.0 / ((3.0 * this.unmodifiedDetails.duration / this.tempo.beatBase) * this.tempo.beatsPerMinute)) :
(60.0 / ((32.0 / this.tempo.beatBase) * this.tempo.beatsPerMinute));
const primaryNoteDuration = ((this.unmodifiedDetails.duration < 0) ?
-this.unmodifiedDetails.duration : (60.0 / ((this.unmodifiedDetails.duration / this.tempo.beatBase) * this.tempo.beatsPerMinute))) -
(2 * mordentNoteDuration);
Expand Down Expand Up @@ -1608,7 +1668,9 @@ class Trill extends ModificationBase {
const trill = [];
const fullNoteDuration = (this.unmodifiedDetails.duration < 0) ?
-this.unmodifiedDetails.duration : (60.0 / ((this.unmodifiedDetails.duration / this.tempo.beatBase) * this.tempo.beatsPerMinute));
const trillNoteDuration = 60.0 / ((32.0 / this.tempo.beatBase) * this.tempo.beatsPerMinute);
const trillNoteDuration = (this.unmodifiedDetails.duration >= 16) ?
(60.0 / ((3.0 * this.unmodifiedDetails.duration / this.tempo.beatBase) * this.tempo.beatsPerMinute)) :
(60.0 / ((32.0 / this.tempo.beatBase) * this.tempo.beatsPerMinute));
const numNotes = Math.floor(fullNoteDuration / trillNoteDuration);
for (let i = 0; i < numNotes; ++i)
trill.push(new NoteDetails(
Expand Down Expand Up @@ -1865,7 +1927,9 @@ class Turn extends ModificationBase {
lowerNote -= Turn.lowerOffsetsMajor[lowerNote % 12];
lowerNote += this.key.offsets[lowerNote % 12];
}
const turnNoteDuration = 60.0 / ((32.0 / this.tempo.beatBase) * this.tempo.beatsPerMinute);
const turnNoteDuration = (this.unmodifiedDetails.duration >= 8) ?
(60.0 / ((5.0 * this.unmodifiedDetails.duration / this.tempo.beatBase) * this.tempo.beatsPerMinute)) :
(60.0 / ((32.0 / this.tempo.beatBase) * this.tempo.beatsPerMinute));
const primaryNoteDuration = ((this.unmodifiedDetails.duration < 0) ?
-this.unmodifiedDetails.duration : (60.0 / ((this.unmodifiedDetails.duration / this.tempo.beatBase) * this.tempo.beatsPerMinute))) -
(4 * turnNoteDuration);
Expand Down Expand Up @@ -5246,7 +5310,7 @@ function createTrack(name, audioContext, tempo, keySignature, trackAudioSink) {
// Remove any duplicate modifications, keeping only the last one
const exists = [];
for (let i = modifications.length - 1; i >= 0; --i)
if (exists.includes(modifications[i].type))
if (ModificationIncompatibilities[modifications[i].type].some(incompatibility => exists.includes(incompatibility)))
modifications.splice(i, 1);
else
exists.push(modifications[i].type);
Expand Down

0 comments on commit 14d645e

Please sign in to comment.