1

I am making a software audio synthesizer and so far i've managed to play a single tone at once.

My goal was to make it polyphonic, i.e when i press 2 keys both are active and produce sound (i'm aware that a speaker can only output one waveform at a time).

From what i've read so far, to achieve a pseudo-polyphonic effect what you are supposed do, is to add the tones to each other with different amplitudes.

The code i have is too big to post in it's entirety but i've tested it and it's correct (it implements what i described above, as for whenever it's the correct thing to do i'm not so sure anymore)

Here is some pseudo-code of my mixing

sample = 0.8 * sin(2pi * freq[key1] * time) + 0.2 * sin(2pi * freq[key2] * time)

The issue i have with this approach is that when i tried to play C C# it resulted in a wierd wobble like sound with distortions, it appears to make the entire waveform oscillate at around 3-5 Hz.

I'm also aware that this is the "correct" behavior because i graphed a scenario like this and the waveform is very similar to what i'm experiencing here.

I know this is the beat effect and that's what happens when you add two tones close in frequency but that's not what happens when you press 2 keys on a piano, which means this approach is incorrect.

Just for test i made a second version that uses stereo configuration and when a second key is pressed it plays the second tone on a different channel and it produces the exact effect i was looking for.

Here is a comparison

Any help would be appreciated, but don't say it's impossible because all of the serious synthesizers can achieve this effect

  • 1
    You may have better luck asking this at https://sound.stackexchange.com/ - it's not a good fit for this site – Ryan M Dec 11 '19 at 01:50

1 Answers1

0

Working backwards from the sound, the "beating" sound is one that would arise from two pitches in the vicinity of 5 or 6 Hz apart. (It was too short for me to count the exact number of beats per second.) Are you playing Midi 36 (C2) = 65.4Hz and Midi 37 (C#2) 69.3Hz? These could be expected to beat at roughly 4 x per sec. Midi 48 & 49 would be closer to 8 times a second.

The pitch I'm hearing sounds more like an A than a C. And A2 (110) + A#2 (116.5) would have beat rate that plausibly matches what's heard.

I would double check that the code you are using in the two scenarios (mono and stereo) are truly sending the frequencies that you think you are.

What sample rate are you using? I wonder if the result could be an artifact due to an abnormally low number of samples per second in your data generation. The tones I hear have a lot of overtones for being sine functions. I'm assuming the harmonics are due to a lack of smoothness due to there being relatively few steps (a very "blocky" looking signal).

I'm not sure my reasoning is right here, but maybe this is a plausible scenario. Let's assume your computer is able to send out signals at 44100 fps. This should be able to decode a rather "blocky" sine (with lots of harmonics) pretty well. There might be some aliasing due to high frequency content (over the Nyquist value) arising from the blockiness.

Let's further assume that your addition function is NOT occurring at 44100 fps, but at a much lower sample rate. This would lower the Nyquist and increase the aliasing. Thus the mixed sounds would be more subject to aliasing-related distortion than the scenario where the signals are output separately.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
  • i think ill update the question with some more specifics regarding the actual code although that was not my original intention. btw i used 48khz SR and 220hz scale but on 440 you can hear the beating as well so its not that – robeddieson Dec 13 '19 at 06:43
  • So, the note is A, not C? Also, in your equation, what is the amount you are using for "time", are you updating every frame at 48khz? Also, it might be a good idea to try at the "sound" stack exchange or the DSP area. I've had good responses from both areas in the past. – Phil Freihofner Dec 13 '19 at 08:24
  • 1
    i increment by 1/SR – robeddieson Dec 13 '19 at 08:44
  • i actually have posted on sound and on music with the latter giving me the most responses – robeddieson Dec 13 '19 at 08:45