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

Polyphonic/accord example #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,55 @@ function play(keys) {
play([[660, 0.1], [660, 0.1], [660, 0.1], [510, 0.1], [660, 0.1], [770, 0.1], [1, 0.1], [380, 0.1]]);
```

Wanna go polyphonic? Ok, here's an example of how to play accords (multiple keys at once):

```javascript
function play(keys) {
var key = keys.shift();
if(typeof key == 'undefined') return;
if(typeof key[0] == 'object') {
for(i = 0; i < key.length; i++) {
if(i==key.length-1) { // last one
new Beep(22050).play(parseInt(key[i][0]), key[i][1], [Beep.utils.amplify(8000)], function() { play(keys); });
} else {
new Beep(22050).play(parseInt(key[i][0]), key[i][1], [Beep.utils.amplify(8000)]);
}
}
} else {
new Beep(22050).play(parseInt(key[0]), key[1], [Beep.utils.amplify(8000)], function() { play(keys); });
}
}

play([
// accords:
[ [349, 0.08], [391, 0.08] ],
[ [349, 0.08], [391, 0.08] ],
[ [349, 0.08], [391, 0.08] ],
[ [349, 0.08], [391, 0.08] ],
[ [349, 0.08], [391, 0.08] ],
[ [349, 0.08], [391, 0.08] ],

[ [329, 0.08], [391, 0.08] ],
[ [329, 0.08], [391, 0.08] ],
[ [329, 0.08], [391, 0.08] ],
[ [329, 0.08], [391, 0.08] ],
[ [329, 0.08], [391, 0.08] ],
[ [329, 0.08], [391, 0.08] ],

[ [293, 0.08], [493, 0.08] ],
[ [293, 0.08], [493, 0.08] ],
[ [293, 0.08], [493, 0.08] ],
[ [293, 0.08], [493, 0.08] ],

// single tones:
[440, 0.08],
[493, 0.08],

// an accord again:
[ [261, 0.1], [523, 0.1] ]
]);
```

API
===

Expand All @@ -79,10 +128,10 @@ Methods

Generates a list of numbers representing samples of the sine wave.

#encode(number:frequency, number:duration, [function:filters]) -> string:wavencode
#encode(number:frequency, number:duration [, function:filters]) -> string:wavencode

Repeats the sine samples to fulfill a specified duration and encodes them to a WAV format.

#play(number:frequency, number:duration, [function:filters] [, function:callback ])
#play(number:frequency, number:duration [, function:filters] [, function:callback ])

Generates and encodes a sine wave and tries to `play` it with the `<audio>` element. Also, binds a callback function to the `onended` event of the `<audio>` element.