DSP in haxe?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

DSP in haxe?

alex-11-3

Hi,

I was excited to see the flash vorbis player by Arek Korbik:
  http://barelyfocused.net/blog/2008/10/03/flash-vorbis-player/

... in particular because I'd love to do some audio synthesis
programming in HaXe.  I'm not sure where to start though, does anyone
have some example code for generating audio in HaXe?  The flash vorbis
source code link seems broken:
http://people.xiph.org/~arek/bzr/fogg.dev/

Any help much appreciated,

alex


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: DSP in haxe?

alex-11-3
On Mon, 2008-10-27 at 09:52 +0000, alex wrote:
> does anyone have some example code for generating audio in HaXe?

To answer my own question, here's the answer...

Turn your speakers down before running it!

import flash.media.Sound;

class Synth {
    static function main() {
                var buffer = new Sound();
                buffer.addEventListener("sampleData", |onSamplesCallback);
                buffer.play();
        }

        static function onSamplesCallback(event) {
                var i;
                for (i in 0...2048) {
                        event.data.writeFloat(Math.random());
                        event.data.writeFloat(Math.random());
                }
        }
}

>   The flash vorbis
> source code link seems broken:
> http://people.xiph.org/~arek/bzr/fogg.dev/
>
> Any help much appreciated,
>
> alex
>
>


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: DSP in haxe?

alex-11-3
And here's karplus-strong string synthesis in HaXe -- sounds good!

alex



import flash.media.Sound;

class Synth {
        static var delay : Array<Float>;

        static var gain:Float;
        static var prev:Float;
       
    static function main() {
                var i;
                gain = 0.5;
                prev = 0;
               
                var buffer = new Sound();
                delay = new Array<Float>();

                for (i in 0 ... 512) {
                        delay.push((Math.random() - 0.5) * gain);
                }

                buffer.addEventListener("sampleData", onSamplesCallback);
                buffer.play();
                trace("Karplus-Strong");
        }

        static function onSamplesCallback(event) {
                for (i in 0 ... 2048) {
                        var sample = delay.shift();
                        var tmp = prev;
                        prev = sample;
                        sample = (tmp + sample) * 0.5;
               
                        event.data.writeFloat(sample); // left
                        event.data.writeFloat(sample); // right
                        sample = sample * 0.99;
                        delay.push(sample);
                }
        }
}


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: DSP in haxe?

Don-Duong Quach
Hey Alex,

Thanks for the post!  My friends and I were working with flash 10 at Adobe's flashcamp playing around with dynamic sound generation, and this one note sounds way better than what we came up with!  Do you have any links to resources on how to make good sounds?

-Don Q.

On Mon, Oct 27, 2008 at 8:34 AM, alex <[hidden email]> wrote:
And here's karplus-strong string synthesis in HaXe -- sounds good!

alex



import flash.media.Sound;

class Synth {
       static var delay : Array<Float>;

       static var gain:Float;
       static var prev:Float;

   static function main() {
               var i;
               gain = 0.5;
               prev = 0;

               var buffer = new Sound();
               delay = new Array<Float>();

               for (i in 0 ... 512) {
                       delay.push((Math.random() - 0.5) * gain);
               }

               buffer.addEventListener("sampleData", onSamplesCallback);
               buffer.play();
               trace("Karplus-Strong");
       }

       static function onSamplesCallback(event) {
               for (i in 0 ... 2048) {
                       var sample = delay.shift();
                       var tmp = prev;
                       prev = sample;
                       sample = (tmp + sample) * 0.5;

                       event.data.writeFloat(sample); // left
                       event.data.writeFloat(sample); // right
                       sample = sample * 0.99;
                       delay.push(sample);
               }
       }
}


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: DSP in haxe?

Hudson Ansley
not Alex, but this guy is one of the best in flash audio:
http://blog.andre-michelle.com/

here's an example (with source code available:
http://lab.andre-michelle.com/fl-909

Regards,
Hudson

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: DSP in haxe?

alex-11-3
In reply to this post by Don-Duong Quach
On Mon, 2008-10-27 at 11:56 -0700, Don-Duong Quach wrote:
> Thanks for the post!  My friends and I were working with flash 10 at
> Adobe's flashcamp playing around with dynamic sound generation, and
> this one note sounds way better than what we came up with!  Do you
> have any links to resources on how to make good sounds?

Sadly I don't think there are many synthesis algorithms with the beauty,
simplicity and usefulness of karplus-strong synthesis...  But one thing
would be to look how things are done in audio synthesis languages like
pure-data and SuperCollider and port things across.  Or pick up a
textbook like the computer music tutorial
http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=8218 or
real sound synthesis: http://www.cs.princeton.edu/~prc/AKPetersBook.htm

cheers,

alex




--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: DSP in haxe?

Kostas Michalopoulos
In reply to this post by alex-11-3
> static function onSamplesCallback(event) {
> for (i in 0 ... 2048) {

As a side note i would recommend you to avoid using this for loops in
repeatable controls. They seem to create iterator objects which after a
while (if called many times) clutter the garbage collector. I had this
problem with for loops in my raycaster (i thought they were converted to
some sort of while loop) until i realized that they probably are using
iterators. Once i replaced every for loop with while loops that do the
job automatically, all the frame irregularities and random pauses in the
  rendering was gone.

Nicolas, can this be optimized where possible? I mean in many cases a

for (i in a...b) {
}

can be replaced with a

i=a;
while (a<b) {
i++;
}

Can the compiler do that automatically?

Kostas "Bad Sector" Michalopoulos

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: DSP in haxe?

Nicolas Cannasse
Kostas Michalopoulos a écrit :

>>     static function onSamplesCallback(event) {
>>         for (i in 0 ... 2048) {
>
> As a side note i would recommend you to avoid using this for loops in
> repeatable controls. They seem to create iterator objects which after a
> while (if called many times) clutter the garbage collector. I had this
> problem with for loops in my raycaster (i thought they were converted to
> some sort of while loop) until i realized that they probably are using
> iterators. Once i replaced every for loop with while loops that do the
> job automatically, all the frame irregularities and random pauses in the
>  rendering was gone.
>
> Nicolas, can this be optimized where possible? I mean in many cases a
>
> for (i in a...b) {
> }
>
> can be replaced with a
>
> i=a;
> while (a<b) {
> i++;
> }
>
> Can the compiler do that automatically?

For-loop optimization is already done for Array, IntIter (...),
haxe.FastList, and flash.Vector (CVS only for the last one)

All these cases translate to a while without any Iterator creation.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: DSP in haxe?

alex-11-3
In reply to this post by alex-11-3

Slightly more working version of my synth, controlled by javascript:
  http://yaxu.org/hx/synth.html

source here:
  http://yaxu.org/hx/

it needs the flash 10 client.

cheers


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: DSP in haxe?

alex-11-3

I updated my haxe synth stuff and blagged about it here:
  http://doc.gold.ac.uk/~ma503am/alex/dsp-in-haxe/

cheers

alex


--
haXe - an open source web programming language
http://haxe.org