Page 1 of 1

Please help me with very specific question

Posted: Tue Oct 30, 2012 8:55 pm
by darkhog
http://stackoverflow.com/questions/1302 ... -in-lazaru

Since there are many demoscene minds here, I think it won't hurt to ask for help.

Posted: Tue Oct 30, 2012 10:07 pm
by Kjell
Hi darkhog,

Generating sounds is really easy .. after all, PCM is simply a 1D ( usually unsigned 16-bit ) array. Attached is a example project that contains the formulas to generate Sine / Triangle / Square / Noise samples. Keep in mind that ZGE uses floating-point values ( instead of unsigned shorts ).

K

Posted: Tue Oct 30, 2012 10:15 pm
by darkhog
As it has nothing to do with ZGE, I'd appreciate more Pascal/pseudocode example.

Posted: Tue Oct 30, 2012 10:33 pm
by Kjell
Hi darkhog,
darkhog wrote:As it has nothing to do with ZGE, I'd appreciate more Pascal/pseudocode example.
A .zgeproj fits perfectly in the psuedo-code category :wink: Anyway, if you'd want to generate a 1-second mono sine-wave at 44100Hz in C, the following ..

Code: Select all

<Sample Name="SineSample" Length="1">
  <Producers>
    <SampleExpression>
      <Expression>
        <![CDATA[this.Sample = sin(this.Time*PI*512);]]>
      </Expression>
    </SampleExpression>
  </Producers>
</Sample>
.. becomes this ..

Code: Select all

float sineWave[44100]; // Warning: Stack overflow ;-P

for(int i=0; i<44100; i++)
{
	float time = (float)i / 44100.0f;
	
	sineWave[i] = sin(time * PI * 512.0f);
}
As far as API's are concerned I'd recommend DirectSound on Windows, Core Audio on MacOS and ALSA on Linux.

K