Guitar
<PLUGGED STRING>
Very simple Karplus-Strong example for FFK09 '10 Hot Minutes' in cologne.
An advancement, more guitar-like implementation can be found here:
http://lab.andre-michelle.com/karplus-strong-guitar
@author Andre Michelle
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.utils.setTimeout;
/**
* <PLUGGED STRING>
*
* Very simple Karplus-Strong example for FFK09 '10 Hot Minutes' in cologne.
*
* An advancement, more guitar-like implementation can be found here:
* http://lab.andre-michelle.com/karplus-strong-guitar
*
* @author Andre Michelle
*/
[SWF(width='215',height='600',frameRate='255',backgroundColor='0xcccccc')]
public class GuitarTest extends Sprite
{
private var _guitarString: Vector.<GuitarString>;
public function GuitarTest()
{
_init();
}
private function _init():void
{
_guitarString = new Vector.<GuitarString>;
_guitarString[0] = _createGuitarString( 22, 20, 82 );
_guitarString[1] = _createGuitarString( 52, 20, 110 );
_guitarString[2] = _createGuitarString( 82, 20, 147 );
_guitarString[3] = _createGuitarString( 112, 20, 196 );
_guitarString[4] = _createGuitarString( 142, 20, 247 );
_guitarString[5] = _createGuitarString( 172, 20, 330 );
setTimeout( _start, 100);
}
private function _start(): void
{
var container: GuitarStringContainer = new GuitarStringContainer();
container.x = 10;
container.y = 10;
addChild( container );
var i: int;
var n: int = _guitarString.length;
for( i = 0 ; i < 6 ; ++i )
container.addChild( _guitarString[i] );
}
private function _createGuitarString( x: Number, y: Number, note: Number): GuitarString
{
var guitarString: GuitarString = new GuitarString( note);
guitarString.x = x;
guitarString.y = y;
return guitarString;
}
}
}
import flash.display.Sprite;
class GuitarStringContainer extends Sprite
{
public function GuitarStringContainer()
{
graphics.clear();
graphics.beginFill( 0xFF0000 );
graphics.drawRect( 0, 0, 195, 580 );
graphics.endFill();
}
}
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.SampleDataEvent;
import flash.media.Sound;
import flash.utils.setTimeout;
class GuitarString extends Sprite
{
private var _note: Number;
private const NUM_SAMPLES: int = 3072;
private var _sound: Sound;
private var _periodN: int;
private var _period: Vector.<Number>;
private var _periodIndex: int;
private var _current: Number;
private var _feedNoise: Boolean;
public function GuitarString( note: Number)
{
_note = note;
_periodN = 44100 / _note;
_period = new Vector.<Number>( _periodN, true );
_periodIndex = 0;
_feedNoise = false;
_current = 0.0;
_sound = new Sound();
_sound.addEventListener( SampleDataEvent.SAMPLE_DATA, sampleData );
setTimeout( _sound.play, 5 );
_init();
}
private function _init(): void
{
_off();
addEventListener( MouseEvent.MOUSE_OVER, mouseDownOn );
}
public function mouseDownOn( event:MouseEvent ): void
{
_feedNoise = true;
_on();
removeEventListener( MouseEvent.MOUSE_OVER, mouseDownOn );
addEventListener( MouseEvent.MOUSE_OUT, mouseDownOff );
}
public function mouseDownOff( event:MouseEvent ): void
{
_off();
removeEventListener( MouseEvent.MOUSE_OUT, mouseDownOff );
addEventListener( MouseEvent.MOUSE_OVER, mouseDownOn );
}
private function _off(): void
{
graphics.clear();
graphics.beginFill( 0x333333 );
graphics.drawRect( 0, 0, 4, 500 );
graphics.endFill();
}
private function _on(): void
{
graphics.clear();
graphics.beginFill( 0xFFFFFF );
graphics.drawRect( 0, 0, 4, 500 );
graphics.endFill();
}
public function getNote():Number
{
return _note;
}
public function getfeedNoise():Boolean
{
return _feedNoise;
}
private function sampleData(event: SampleDataEvent): void
{
for( var i: int = 0 ; i < NUM_SAMPLES ; ++i )
{
if( _periodIndex == _periodN ) // wrap around delay-line
{
_periodIndex = 0;
_feedNoise = false;
}
if( _feedNoise )
{
_period[ _periodIndex ] += Math.random() - Math.random();
}
_current += ( _period[ _periodIndex ] - _current ) * .4;
_period[ _periodIndex ] = _current;
event.data.writeFloat( _current );
event.data.writeFloat( _current );
++_periodIndex;
}
}
}