SFX - Printable Version +- NaaLaa (https://www.naalaa.com/forum) +-- Forum: NaaLaa (https://www.naalaa.com/forum/forum-1.html) +--- Forum: NaaLaa 7 Questions (https://www.naalaa.com/forum/forum-3.html) +--- Thread: SFX (/thread-85.html) |
SFX - johnno56 - 02-19-2024 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 RE: SFX - Marcus - 02-20-2024 (02-19-2024, 07:02 PM)johnno56 Wrote: Marcus, 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). RE: SFX - Marcus - 02-20-2024 I walked to the store and back and did some more thinking on the subject. You could probably never use the SFX library to create, let's say, a piano tone. Because a piano tone contains lots of overtones and subtones. That is, a piano tone isn't a single sine wave. You would need to add several layers of sine waves with double (and double double, can't find the word, and so on) the frequency, and half (and half half ...) the frequency, all of these at much lower volume. And then there's the thing called "attack, sustain, release and decay", but that you could probably control with the 'vol' array. Hm, maybe you could just call SFX.SineWaveData to get data for the main tone and all the over- and subtones and add them together into a new array and then send that array to 'createsound'. I think I've got to try this, to create the sound of a piano, when I get the time Sorry for rambling. RE: SFX - johnno56 - 02-20-2024 Rambling? Nah! I am a pro at rambling... lol Portaudio, you say? Cool. Do not panic... I am not expecting piano tones... That would be silly... Even Music Notation software does not 'create' a piano tone... that is why they use pre-recorded samples... What I am expecting is a Synthesizer... Moo Ha Ha Ha... Hang on... "walked to the store and back"... Correct me if I am wrong... It 'is' Winter where you are, is it not? lol RE: SFX - Marcus - 02-21-2024 Here's an example where I create a plain sound and the same sound but with some over- and subtones. Sounds like some sort of electric piano But maybe SFX could get another parameter dealing with these things. Code: include "sfx.n7" RE: SFX - 1micha.elok - 02-21-2024 (02-20-2024, 06:52 PM)johnno56 Wrote: ... I am not expecting piano tones... Just curious with "do re mi" generated by SquareWave function, let's try this Pianoman ..... Code: '---------------- RE: SFX - johnno56 - 02-21-2024 Interesting... that tune sounds strangely familiar... I have a feeling that I may have heard it a few time some years back... Nicely done! RE: SFX - Marcus - 02-21-2024 (02-21-2024, 06:38 AM)1micha.elok Wrote:(02-20-2024, 06:52 PM)johnno56 Wrote: ... I am not expecting piano tones... Nice But you're creating new sound effects every time the user presses a key. Sound effect (and other resources, such as images) aren't garbage collected (they can't be). So the program will eat more and more memory. Better create the different piano sound effects once and re-use them, or release the old sound when a new one is created ('free sound'). RE: SFX - 1micha.elok - 02-22-2024 (02-21-2024, 04:07 PM)Marcus Wrote: ... I made two experiments to see the memory's consumption. I played the song "Waltzing Matilda" ten times in each experiment. I'm glad that my computer had not exploded yet during the experiment Waltzing Matilda, Waltzing Matilda You'll come a waltzing Matilda with me And he sang as he sat And waited till his billy boiled You'll come a waltzing Matilda with me Just imagine, I sang it 20 times Herewith the piece of code that makes a difference Code: 'This function will play sound frequency x at duration b The First Experiment (without free sound memory) Commit (KB) 57.344 Working Set (KB) 59.164 Shareable (KB) 7.324 Private (KB) 51.840 The Second Experiment (with free sound memory) Commit (KB) 26.816 Working Set (KB) 28.796 Shareable (KB) 7.440 Private (KB) 21.356 Thank you Marcus for your suggestion in free-ing the sound memory. |