views:

88

answers:

2

Hello Everybody!

Inspired by Andre michelle, I`m building a tone matrix in AS3. I managed to create the matrix and generate the different sounds. They don´t sound that good, but I´m getting there One big problem I have is when more than one dot is set to play, it sounds just horrible. I googled a lot and found the additive synthesis method but don´t have a clue how to apply it to as3. anybody out there knows how to play multiple sounds together? any hint?

my demo is at www.inklink.co.at/tonematrix

any help is more than appreciated! thanks!

A: 

I have tried your demo and thought it sounded pretty good actually. What do you mean it doesn't sound that good? What's missing? My main area of interest is music and I haven't found anything wrong , only it's a little frustrating , because after creating a sequence, I feel the need to add new sounds! Had I been able to record what I was playing with, I would have sent it to you.

Going into additive synthesis doesn't look like a light undertaking though. How far do you want to push it, would you want to create some form of synthesizer?

PatrickS
:) Thanks PatrickS, basicaly I want to learn how to generate sounds in AS3, and learn the math and a little bit of sound theory too :)in the end I´ll try to make an step sequencer and connect it with an arduino board to make a real synthetiser, but that´s the dream part of the project...
Drala
Admittedly your sound is saturated when two sounds are playing , I overlooked that when playing with it yesterday :) you may want to check the amplitude settings when more than one sound is playing, this shouldn't be a major issue. Actually , a lot of electronic musicians don't mind distortion but to be fair, the user should have some form of control over it.
PatrickS
A: 

Oh common the sound was horrible...

Checked wiki? It is not that hard to understand... Even if you don't know that much of mathematics... Which you should - PROGRAMMING music is not easy.

So:

Let's first define something:

var harmonics:Array = new Array();

harmonics is the array in which we will store individual harmonics. Each child will be another array, containing ["amplitude"] (technically the volume), ["frequency"] and ["wavelength"] (period). We also need a function that can give us the phase of the wave given the amplitude, wavelength and offset (from the beginning of the wave). For square wave something like:

function getSquarePhase(amp:Number, wl:Number, off:Number):Number {
    while (off > wl){off -= wl;}
    return (off > wl / 2 ? -amp : amp); // Return amp in first half, -amp in 2.
}

You might add other types, or even custom vector waves if you want.

Now for the harder part.

var samplingFrequency; // set this to your SF

function getAddSyn(harmonics:Array, time:Number):Number {
    if (harmonics.length == 1){ // We do not need to perform AS here
        return getSquarePhase(harmonics[0]["amplitude"], harmonics[0]["wavelength"], time);
    } else {
        var hs:Number = 0;
        hs += 0.5 * (harmonics[0]["amplitude"] * Math.cos(getSquarePhase(harmonics[0]["amplitude"], harmonics[0]["wavelength"], time)));
        // ^ You can try to remove the line above if it does not sound right.
        for (var i:int = 1; i < harmonics.length; i++){
            hs += (harmonics[0]["amplitude"] * Math.cos(getSquarePhase(harmonics[0]["amplitude"], harmonics[0]["wavelength"], time)) * Math.cos((Math.PI * 2 * harmonics[0]["frequency"] / samplingFrequency) * time);
            hs -= Math.sin(getSquarePhase(harmonics[0]["amplitude"], harmonics[0]["wavelength"], time)) * Math.sin((Math.PI * 2 * harmonics[0]["frequency"] / samplingFrequency) * time);
        }

        return hs;
    }
}

This is all just converted (weakly :D) from the Wikipedia, I may have done a mistake somewhere in there... But I think you should get the idea... And if not, try to convert the AS from Wikipedia yourself, as I said, it is not so hard.

I also somehow ignored the Nyquist frequency...

Aurel300
there is no such thing as a horrible sound in music. you can have basic sounds , aggressive sounds , distorted sounds etc... but horrible, nope ;)
PatrickS
Well ok, It´s not horrible! but I have to work on the attack, decay, sustain and release...
Drala
Thanks Aurel300!! I´m in the work now, so I probably won´t have the time to test it. As soon as I´m home I will try it and post the result! thank you very much. And about the math, yeah, I know, I have to learn a LOT!
Drala
:D no problem...
Aurel300