Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to enable/disable keys #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/detailspecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ If the `fire` parameter is `undefined` or `false`, this function will not fire `
`webaudio-keyboard`
**description**: webaudio-keyboard can be setup pressing state with this function from JavaScript. corresponding key specified by the `note` is pressed if the `state` is non-zero otherwise the key is released. This function will NOT fire the 'change' event. If the `audioContext` and `when` arguments are specified, the pressing state will be updated after the specified time. `when` is the time in seconds on the `currentTime` time axis of the `audioContext`.

### setDisabledRange(state, start, end)
`webaudio-keyboard`
**description**: Disable a range of keys using non-zero `state` or zero to re-enable keys. Pass `start` value for the first key in the range. `end` for the last key in the range. All keys between start/end will be disabled/enabled. Disabled keys will be greyed out and will not trigger events.

---
## Events

Expand Down
26 changes: 24 additions & 2 deletions webaudio-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,7 @@ ${this.basestyle}
this.bheight = this.height * 0.55;
this.max=this.min+this.keys-1;
this.dispvalues=[];
this.disabledvalues=[];
this.valuesold=[];
if(this.kf[this.min%12])
--this.min;
Expand Down Expand Up @@ -1682,7 +1683,9 @@ ${this.basestyle}
for(let i=this.min,j=0;i<=this.max;++i) {
if(this.kf[i%12]==0) {
let x=this.wwidth*(j++)+1;
if(this.dispvalues.indexOf(i)>=0)
if(this.disabledvalues.indexOf(i)>=0)
rrect(this.ctx,x,1,this.wwidth-1,this.height-2,r,this.coltab[3],this.coltab[3]);
else if(this.dispvalues.indexOf(i)>=0)
rrect(this.ctx,x,1,this.wwidth-1,this.height-2,r,this.coltab[5],this.coltab[6]);
else
rrect(this.ctx,x,1,this.wwidth-1,this.height-2,r,this.coltab[1],this.coltab[2]);
Expand All @@ -1692,7 +1695,9 @@ ${this.basestyle}
for(let i=this.min;i<this.max;++i) {
if(this.kf[i%12]) {
let x=this.wwidth*this.ko[this.min%12]+this.bwidth*(i-this.min)+1;
if(this.dispvalues.indexOf(i)>=0)
if(this.disabledvalues.indexOf(i)>=0)
rrect(this.ctx,x,1,this.bwidth,h2,r,this.coltab[3],this.coltab[3]);
else if(this.dispvalues.indexOf(i)>=0)
rrect(this.ctx,x,1,this.bwidth,h2,r,this.coltab[7],this.coltab[8]);
else
rrect(this.ctx,x,1,this.bwidth,h2,r,this.coltab[3],this.coltab[4]);
Expand Down Expand Up @@ -1826,6 +1831,7 @@ ${this.basestyle}
ev.stopPropagation();
}
sendEventFromKey(s,k){
if (this.disabledvalues.includes(k)) return;
let ev=document.createEvent('HTMLEvents');
ev.initEvent('change',true,true);
ev.note=[s,k];
Expand All @@ -1844,6 +1850,7 @@ ${this.basestyle}
if(notes.length) {
this.valuesold=this.values;
for(let i=0;i<notes.length;++i) {
if (this.disabledvalues.includes(notes[i][1])) return;
this.setdispvalues(notes[i][0],notes[i][1]);
let ev=document.createEvent('HTMLEvents');
ev.initEvent('change',true,true);
Expand All @@ -1852,6 +1859,21 @@ ${this.basestyle}
}
}
}
setDisabledRange(state,start,end) {
for (var i=start; i<end;i++) {
this.setdisabledvalues(state,i);
}
this.redraw();
}
setdisabledvalues(state,note) {
let n=this.disabledvalues.indexOf(note);
if(state) {
if(n<0) this.disabledvalues.push(note);
}
else {
if(n>=0) this.disabledvalues.splice(n,1);
}
}
setdispvalues(state,note) {
let n=this.dispvalues.indexOf(note);
if(state) {
Expand Down