lundi 28 décembre 2015

Tone Generator converted for specified tones

I was hoping that you could help me figure out how to change this code generator from playing specific tones already designated to keys on the keyboard to using a textbox to specify which frequency of tone to play.

Thank you for your help.

var _sound       :Sound         = null;
var _channel     :SoundChannel  = null;
var _pitchRatios :Object        = null;
const BASE_PTICH :Number        = 440;
var _pitch       :Number        = 5;
var _timer       :Timer         = null;



function SoundGeneration()
{

_pitchRatios = {97:1, 115:1.059, 100:1.122, 102:1.189, 103:1.259, 104:1.334, 106:1.414, 107:1.498, 108:1.587};

_timer = new Timer(9000);
_timer.addEventListener(TimerEvent.TIMER, TimerStop,false, 0, true);

stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown, false, 0, true);

stage.focus = this;
}

function KeyDown($e:KeyboardEvent):void
{
if(_timer.running == false) _timer.start();
PlaySound(_pitchRatios[$e.charCode])
}

function PlaySound($pitchRatio:Number):void 
{
StopSound();
_pitch = BASE_PTICH * $pitchRatio;
_sound = new Sound();
_sound.addEventListener(SampleDataEvent.SAMPLE_DATA, SoundGenerator, false, 0, true);
_channel = _sound.play();
}

function SoundGenerator($e:SampleDataEvent):void
{
var $lev:Number;

for (var i:uint = 0; i < 8192; i++) 
{
    $lev = 0;
    $lev += Math.sin((i + $e.position) * Math.PI / (44100 / _pitch ));
    $lev /= 2;
    $e.data.writeFloat($lev);
    $e.data.writeFloat($lev);
}
}

function TimerStop($e:TimerEvent):void
{
if(_timer.running) _timer.stop();

}

function StopSound():void 
{
if (_channel) 
{
    _channel.stop();
    _sound.removeEventListener(SampleDataEvent.SAMPLE_DATA,SoundGenerator);
}
}

SoundGeneration();

Aucun commentaire:

Enregistrer un commentaire