Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed USBDevice PinDetect
Diff: Audio/KarplusStrong.cpp
- Revision:
- 19:894c31ee9ad4
- Parent:
- 16:b25ca34a705f
- Child:
- 20:bf675ba2c454
--- a/Audio/KarplusStrong.cpp Thu Apr 21 01:41:13 2016 +0000 +++ b/Audio/KarplusStrong.cpp Thu Apr 21 01:56:20 2016 +0000 @@ -21,10 +21,7 @@ pluckDampingCoefficient = 0.5; } -void KarplusStrong::midiNoteOn(int key, float velocity) { - // Save the velocity for later. - this->velocity = velocity; - +void KarplusStrong::midiNoteOn(int key, float velocity) { // Calculate the smoothing filter coefficient for this note. float noteIndex = float(key - 24) / 48.0; // normalize the lower two octaves. // Clip to 1.0 @@ -32,9 +29,12 @@ noteIndex = 1.0; } - nextFilterCoefficient = C::STRING_DAMPING // Initial damping. - + 0.5 * (1.0 - C::STRING_DAMPING) * sqrt(noteIndex) // Keyboard tracking - + C::STRING_DAMPING_VARIATION * (1.0 - C::STRING_DAMPING) * getRand(); // Random variation + // The volume is always pretty quiet, even at full velocity, so use velocity as a filter cutoff instead. + float stringDamping = C::STRING_DAMPING_MIN + velocity * (C::STRING_DAMPING_MAX - C::STRING_DAMPING_MIN); + + nextFilterCoefficient = stringDamping // Initial damping. + + 0.5 * (1.0 - stringDamping) * sqrt(noteIndex) // Keyboard tracking + + C::STRING_DAMPING_VARIATION * (1.0 - stringDamping) * getRand(); // Random variation // Calculate the pluck damping for this note. pluckDampingCoefficient = pluckDampingVariationMin + getRand() * pluckDampingVariationDifference; @@ -48,8 +48,6 @@ float noiseSample = noiseSeed[bufferIndex] * (1.0 - C::CHARACTER_VARIATION); // Add in some character randomness to make it more realistic. noiseSample += getRandSample() * C::CHARACTER_VARIATION; - // Account for velocity. - noiseSample *= velocity; // Filter using the pluck damping coefficient to control sprectral content. noiseSample = lowpassFilter(lastOutput, noiseSample, pluckDampingCoefficient);