Guitare accordée
<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
// forked from archflex's forked from: Guitar
// forked from simpleex's Guitar
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='0x000000')]
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, Note.freq(2,4) );
_guitarString[1] = _createGuitarString( 52, 20, Note.freq(2,9) );
_guitarString[2] = _createGuitarString( 82, 20, Note.freq(3,2) );
_guitarString[3] = _createGuitarString( 112, 20, Note.freq(3,7) );
_guitarString[4] = _createGuitarString( 142, 20, Note.freq(3,11) );
_guitarString[5] = _createGuitarString( 172, 20, Note.freq(4,4) );
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;
private var _noise: Number;
private var _noiseDirection: int;
public function GuitarString( note: Number)
{
_note = note;
_periodN = 44100 / _note;
_period = new Vector.<Number>( _periodN, true );
_periodIndex = 0;
_feedNoise = false;
_current = 0.0;
_noise = 0.0;
_noiseDirection=1;
_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( -5, 0, 10, 500 );
graphics.endFill();
}
private function _on(): void
{
graphics.clear();
graphics.beginFill( 0xFFFFFF );
graphics.drawRect( -5, 0, 10, 500 );
graphics.endFill();
}
public function getNote():Number
{
return _note;
}
public function getfeedNoise():Boolean
{
return _feedNoise;
}
private function sampleData(event: SampleDataEvent): void
{
var newNoise: Number;
for( var i: int = 0 ; i < NUM_SAMPLES ; ++i )
{
if( _periodIndex == _periodN ) // wrap around delay-line
{
_periodIndex = 0;
_feedNoise = false;
}
if( _feedNoise )
{
newNoise = (2.0 - (4.0 * Math.random()));
if (newNoise<-1.0)
{
newNoise = -1.0;
}
if (newNoise>1.0)
{
newNoise = 1.0;
}
// _period[ _periodIndex ] += _noise- Math.random();
_period[ _periodIndex ] += newNoise;
}
_current += ( _period[ _periodIndex ] - _current ) * .35;
_period[ _periodIndex ] = _current;
event.data.writeFloat( _current );
event.data.writeFloat( _current );
++_periodIndex;
}
}
}
class Note
{
private static var la:Number = 440;
private static var r12_2:Number = Math.pow(2, 1 / 12);
public function Note()
{
}
public static function freq(octave:int,note:int):Number
{
var o:int = (2 << octave) * la / 32;
var n:Number = Math.pow(r12_2, note-9);
return o * n;
}
}