digitalmars.D - Is D cool or what?
- Andrej Mitrovic (37/37) Apr 28 2011 Here's a little 100-line thingy that generates a wavetable of some
Here's a little 100-line thingy that generates a wavetable of some shape (sine, saw, square - although only sine is implemented at the moment), of any length, and of either float or double element type. It stores this inside a struct which can also keep a number of different phases, which do bounds checking themselves and wrap around themselves when the phase reaches a limit. https://gist.github.com/947429 The cool thing about this is that it's all done at compile time. And, yes it does actually work! I have tested it with ASIO realtime playback and I can hear the sounds of sines. You can even have some fun and generate pitch changes using randomization by controlling the phase stepping. Here's a little snippet from my other code which generates some random melodic tones with sinewaves: // generate a new phase half-way through the buffer. // If the buffer is filled with 22050 samples per second, // the phase stepping will change every half of a second. int step = inputBuffer.length / 2; int index; foreach (ref sample; inputBuffer) { if (index == step) // generate new stepping { index = 0; leftPhase = uniform(1, 7); rigthPhase = uniform(1, 7); } index++; if (channel.channel == 0) // left channel { sample = audioTable.table[audioTable.phase[0] += step]; } else if (channel.channel == 1) // right channel { sample = audioTable.table[audioTable.phase[1] += step+2]; } }
Apr 28 2011