Really inefficient and awful sounding version:
output[pos] = (sin (self>phase * 2 * PI ) >= 0 ? 1 : 1) * volume;
Dirty trick: Round the phase increment so that the period becomes an
integer number of sample frames! This produces crystal clear square
waves, but frequency resolution is awful for high frequencies and/or
low sample rates, of course.
The simplest "correct" way is to construct the square wave by adding
up partials (sines), and just stop below Nyqvist, which essentially
brickwallfilters the waveform. Perfect results, but pretty
expensive...
The proper answer is that it's actually really complicated to do this
well, because an ideal geometric waveform contains an infinite number
of partials, and the ones above Nyqvist (sample rate / 2) will be
mirrored down below Nyqvist, making it all sound awful.
Some keywords: band limited oscillator, BLIT, BLEP, minBLEP
You may find interesting code examples here:
http://musicdsp.org/
On Tue, Oct 21, 2014 at 3:56 PM, Philippe Coatmeur <philcm(a)gnu.org> wrote:
Hi
If this
// calculate the next sample: phase ranges from 0 > 1, but sin()
// expects its input in radians. Just multiply by 2 PI to convert it!
// We also multiply by volume so its not extremely loud.
output[pos] = sin( self>phase * 2 * PI ) * volume;
Sends a sine wave to output (this is the standard sin math function, right?)
then what would send a square wave? What would send noise?
Thanks Phil

Philippe "xaccrocheur" Coatmeur
http://bitbucket.org/xaccrocheur /
https://github.com/xaccrocheur
http://opensimo.org/play/
_______________________________________________
Linuxaudiodev mailing list
Linuxaudiodev(a)lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linuxaudiodev

//David Olofson  Consultant, Developer, Artist, Open Source Advocate
. Games, examples, libraries, scripting, sound, music, graphics .

http://consulting.olofson.net http://olofsonarcade.com 
''