Please help me with very specific question

Any topic not related to ZGE.

Moderator: Moderators

Post Reply
darkhog
Posts: 58
Joined: Sun Sep 09, 2012 7:59 pm

Please help me with very specific question

Post 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.
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Post 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
Attachments
Samples.zgeproj
(1.44 KiB) Downloaded 631 times
darkhog
Posts: 58
Joined: Sun Sep 09, 2012 7:59 pm

Post by darkhog »

As it has nothing to do with ZGE, I'd appreciate more Pascal/pseudocode example.
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Post 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
Post Reply