Uncle Osama's Voice synthesis 1

Source: Internet
Author: User

I've always wanted to write something about sound synthesis. However, because there is no good material to find the very slow action . So now we can only find some material patchwork write this article .

First , let's take a look at some of the basic structures of a sound object, if you control it through code and create random sounds. Later , learn how to create real sound waveforms and sound compositions, and so on .

Straight

Flash10 has the function of sound synthesis, the actual function is already in Flash9 , but the same effect is a headache for Flash9 , and these sound synthesis API The FLASH10 became more standardized .

To make a sound composition , you first create an acoustic object and listen for its sample_data event (sampledataevent.sample_data) . This event is triggered when there is no sound data playing in the sound. then play the sound .

var sound:sound=new sound ();

Sound.addeventlistener (Sampledataevent.sample_data,onsampledata);

Sound.play ();

At this point , because no sound such as mp3,wav is loaded, no sound stream is imported, that is, no sound is played , so the Sample_data event is immediately triggered . So we're going to add a listener function :

function Onsampledata (se:sampledataevent): void

{

}

Next add some sound data to this sound object to play sampledataevent there is a binary data" property bytearray.float method -1.0 to 1.0 Span lang= "en-us" xml:lang= "en-us". At this point the binary variable is set to each float value is commonly said sample 2048 and 8192.

OK. This range is already wide enough . Which is the best value ? If you choose a lower value such as 2048, the stream will quickly exceed these values when the sound is played, and then start the sample_data event again , Requires that the sound data be added again . If you choose a larger value such as 8192 , the sound needs to take 4 times times more than 2048 of the time to exceed these values , so the frequency of event listeners being triggered decreases by 4 times times. .

So the larger the number of samples, the better the running performance.. But if the sound data is generated dynamically, the larger the number of samples means the greater the delayui or program change sounds and the actual sound changes that are heard 400hz tones change to 800hz. But when the user presses the button, the sound 400hz tones on 8,000 samples 8,000 samples until the playback is complete , and then call sample_data event listener requires more data -2048-can shorten the delay and make it less noticeable

Now let's make some noise . We have a selection of 2048 random sample values from 1.0 to 1.0 and are written into the binary data . One thing you have to know is that we did two writes of binary data , Once to the left channel , once to the right channel , the following is the complete code :

Import Flash.media.Sound;

Import flash.events.SampleDataEvent;

var sound:sound=new sound ();

Sound.addeventlistener (Sampledataevent.sample_data,onsampledata);

Sound.play ();

function Onsampledata (event:sampledataevent): void

{

for (var i:int = 0; i < 2048; i++)

{

var sample:number = math.random () * 2.0-1.0; -1 to 1

Event.data.writeFloat (sample); Left

Event.data.writeFloat (sample); Right

}

}

Test the code above and you'll hear some of the sounds of the radio not finding the channel . Note that now we generate the noise in which the left and right channels are the same sample value, because two channels of binary data are written to the same value , So what we're generating is a single channel sound . The code that generates the stereo sound is as follows :

function Onsampledata (event:sampledataevent): void

{

for (var i:int = 0; i < 2048; i++)

{

var samplea:number = math.random () * 2.0-1.0; -1 to 1

var sampleb:number = math.random () * 2.0-1.0; -1 to 1

Event.data.writeFloat (Samplea); Left

Event.data.writeFloat (Sampleb); Right

}

}

Now we write a different random value for each channel as a sample . Test the code , you can feel the sound a little "space " feeling (with headphones more obvious effect ). This "space " feeling is very subtle , You may not feel the difference between him and the single channel , so in order to quickly switch between the two effects , let's change the code slightly as follows :

Import Flash.media.Sound;

Import flash.events.SampleDataEvent;

Import flash.events.MouseEvent;

var sound:sound = new sound ();

Sound.addeventlistener (Sampledataevent.sample_data, onsampledata);

Sound.play ();

var Mono:boolean = true;

Stage.addeventlistener (Mouseevent.click, OnClick);

function OnClick (event:mouseevent): void

{

Mono =!mono;

}

function Onsampledata (event:sampledataevent): void

{

for (var i:int = 0; i < 2048; i++)

{

var samplea:number = math.random () * 2.0-1.0; -1 to 1

var sampleb:number = math.random () * 2.0-1.0; -1 to 1

Event.data.writeFloat (Samplea); Left

if (mono)

{

Event.data.writeFloat (Samplea); Left again

}

Else

{

Event.data.writeFloat (Sampleb); Right

}

}

}

I added a Boolean variable , mono, to toggle between true and False when the mouse clicked . If mono is true, write Samplea in the left and right channels separately . If Mono is false, the left channel is written to Samplea, and the Sampleb is written to the right channel . Test the code and click the mouse on the stage . The difference is subtle , but now you should be able to feel it .

To see (more specifically , hear) The result of the delay , change 2048 in the for loop by 8192. Now when you click the mouse , you can obviously feel the delay between the mouse click and the sound from mono to stereo. .

Also pay attention to the number of samples. I said"Usually"is to use2048 to 8192 values If you try to use greater than 8192 value "an invalid parameter ". So the number of samples is forced to limit less than 8192. You can select a value less than 2048 sample _data event complete event 2048 samples

Uncle Bin Laden's voice synthesis 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.