(02-19-2024, 07:02 PM)johnno56 Wrote: Marcus,
I am curious as to "how" your SFX program actually generates tones. For instance: Most Music Notation software relies on a "soundfont" (database of stored samples) to play various instruments but usually require the use of a midi compatible sound card... Are you using a 'secret' method (similar to KFC's herbs and spices) to access the midi port?... Perhaps a little magic or 'slight of hand'? Just curious...
J
I'm not sure at which level I should answer this question.
N7 uses a rather low level sound api named PortAudio, which is cross platform and open source. It's a very simple library. I (as in every running n7 program) constantly feed PortAudio with a stream of bytes, which ends up as sound in the speakers. So when no sound is playing, I just send a constant stream of zeroes to PortAudio. If one or more sound effects and maybe some music are playing, I manually mix (simple addition) those sound sources and feed PortAudio with the resulting data.
A sound effect is just some data that varies over time. The sample rate decides how many data points a sound is made up from per second. So, a sound effect that lasts for 1 second at a sample rate of 16000 requires exactly 16000 data points. Those data points are what the SFX library creates. The 'createsound' function, that SFX uses, just creates a sound effect in memory from the data and sample rate provided. There's not much difference between doing that and loading a WAV file with 'loadsound' (which creates a sound effect in memory from the content of a WAV file).
Creating sound data (like SFX does) isn't very difficult. To create a simple sine wave sound (BEEEEEEEEEEEEP) of a certain frequency, you just calculate how much the angle (sen't to 'sin') should change per data point based on the sample rate. Then you fill an array with those sin values. To create cool effects, I also let the frequency vary over time (the 'freq' array that you send to SFX.SineWave). Add some variable volume to that (the 'vol' array parameter) and all sorts of fun things start to happen. The echo effect is just a matter of copying, modifying and pasting the original sine wave effect to a new array (well, not quite that simple).