PencilCode / jquery-turtle

Turtle Graphics for JQuery
Other
44 stars 25 forks source link

Improve Music Support #67

Closed davidbau closed 10 years ago

davidbau commented 10 years ago

This pull request is a significant improvement for music support in the library:

(1) The "Piano" object is now built-in to the library instead of requiring a separate import.

(2) Each turtle has a "turtleTimbre" property that lets you set 9 numbers (and an oscillator waveform) to determine the quality of the sound of an instrument. The timbre can also be set for a specific song or a specific voice of a song. For example, a piano timbre corresponds to this css call:

this.css({ turtleTimbre:
        "wave:sawtooth;gain:1;" +
        "attack:0.001;decay:0.3;sustain:0.005;release:0.1;" +
        "cutoff:150;cutfollow:0.4;resonance:2;detune:1.001;" });

wave: sawtooth (vs square, triangle, sine - from the Web Audio standard) Then the ADSR envelope: attack: seconds to ramp up from zero when starting a note decay: seconds to decay (exponentially, by a factor of e) after the attack. sustain: the asymptotic volume that you decay to after a while. release: seconds to go to zero after a note is released. Then some things for the quality of the note: cutoff: cutoff frequency for a low-pass filter. cutfollow: for notes with a principal frequency higher than the cutoff, how much to raise cutoff (as a factor of the frequency difference). resonance: the Q value for the low-pass filter, which boosts the principal frequency. detune: frequency for a second (detuned) oscillator, relative to the primary.

(3) In addition to "play" there is a lower-level "tone" method that kids can use to just play a tone immediately without sequencing. It takes either an ABC pitch string or a numeric Hertz value. tone('C') to play a C, then tone('C', 0) to turn it off again.

(4) And there are "noteon" and "noteoff" callbacks: the Instrument base class of Piano sets these up, and you can override them to do various effects when a note is played. The callback is passed an object with a midi number for the note that is turning on or off.

davidbau commented 10 years ago

OK, I've incorporated James Synge's first batch of comments (so far he has reviewed the Piano sprite code but not yet the audio support).

davidbau commented 10 years ago

In _doPoll, James suggests accumulating all callbacks into an array and then calling them all at once.

davidbau commented 10 years ago

James also suggests that, if there is an array of callbacks, _doPoll could sort events by the relevant time.

davidbau commented 10 years ago

Suggestion: make _doPoll's relationship with "now()" more robust.

davidbau commented 10 years ago

Comment from James:

Add a comment on the structure of a note (decorations - accidentals - pitch - duration - ties)

Make header comment reflect the presence of "V:" header properly

Consider renaming stem[j].note ->stems[j].notes

Consider, instead of ugly id-switching handling, just cleanup empty voices at the end of parseABC

Key signature "extras" logic doesn't work. Consider removing.

if (parsed !== null) in "Process a parsed stem" isn't needed.

davidbau commented 10 years ago

OK, I've incorporated code review comments from James (thanks James for reading the code together over several breakfasts!) I'm merging and pushing.

jamessynge commented 10 years ago

Congratulations! That is a really nice new feature.

Jamrs On May 30, 2014 10:46 PM, "David Bau" notifications@github.com wrote:

Merged #67 https://github.com/PencilCode/jquery-turtle/pull/67.

— Reply to this email directly or view it on GitHub https://github.com/PencilCode/jquery-turtle/pull/67#event-126672852.