7

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

  • "but that's not what happens when you press 2 keys on a piano" : The beat effect happens on piano, why shouldn't it? – Von Huffman Dec 11 '19 at 9:36
  • Write the two notes to separate wave files and try mixing them together with an external known-good program? – piiperi Reinstate Monica Dec 11 '19 at 9:37
  • then why in the stereo example it produces a harmonic sound just like what you would expect – robeddieson Dec 11 '19 at 10:24
  • 1
    Write the notes to separate wave files and mix externally. Same result or different? If it's different, then something is wrong with your code. That's software development: debugging and problem diagnosis. – piiperi Reinstate Monica Dec 11 '19 at 10:27
  • you seam to miss the point of my question. im not questioning my impementation im questioning my approach. the implementation is correct because that happens when you add 2 notes but it does not yield the expected effect. the expected effect would be what you can hear on the second wav file and that is simmilar to what you would hear on a real instrument. so im not trying to test my impementation, im asking how to mix the tones to achieve the result i can only achieve when i the stereo version because adding tones certainly isnt the correct approach – robeddieson Dec 11 '19 at 10:35
1

You seem insistent that "YOUR CODE IS CORRECT" but looking at the wave forms in your links, I can't see anything that corresponds to what you say you are doing.

For example both files are actually stereo even though you say one is mono, but both have almost the audio in one channel. Your "stereo" version does NOT "play the second tone on a different channel". It plays both tones (mixed) on both channels.

I suggest you go back a bit and PROVE that your code is doing exactly what you think it does, one step at a time.

  • you seem to be trying no nitpick my post. the first file is te regular version and it uses a SINGLE channel. just to test how two notes playing at the same time would blend irl i made a version that plays the first tone on left channel and the second on the right channel and the result you can hear for yourself. as for correctness, it is correct because this is the kind of redult you yield by adding sinewaves i graphed them and generated similar waves in audacity and it all matches my result, that id like to sound like what you can hear in second wav file, hence the post – robeddieson Dec 11 '19 at 19:57
1

Most probably the problem is that your pure sine waves don't have any harmonics, and that's why the beating is so extreme. Any real instrument will have some harmonics, and when you play a sine wave out of a physical loudspeaker, it adds harmonics as well, in the form of distortion. This should be evident on your recording (made with a smartphone mic?), because that's not a clean sine wave at all. If you stereo-separate, each note gets its own separate speaker with separate distortion and set of harmonics. But if you sum first in the digital domain, then it's the beating sound that gets the distortion.

You can add harmonics to the sine waves by raising each sample/sine value to a power, before adding them together.

note1sample = pow(sin(2*pi*freq1), 8.0)
note2sample = pow(sin(2*pi*freq2), 8.0)
output = note1sample + note2sample

For more of the same effect, add more harmonics. https://dsp.stackexchange.com/questions/51759/harmonic-distortion-of-sine-signal-plotting-resulting-spectrum

Mixing i.e. summing (addition) is a linear operation, but distortion is non-linear. In non-linear systems, the order of operations changes the output, but in linear systems the ordering doesn't matter. It's easy to understand with the math operations above: A + B = B + A, the same thing. But when you add the pow() function, it's different. pow(A) + pow(B) is not the same as pow(A+B).

Here's a video how to recreate the phenomenon in Ableton's Operator synth

First there's just sine waves, and then harmonics are added one by one. With enough harmonics added, the beating is less obvious, and you can distinguish the notes better.

Here's the same with an envelope and then changing the wave from sine to triangle

And finally, the A and A# sine waves are separated to individual channels and run through overdrive effects, first individually, and then through a common master overdrive. I think this corresponds to what happened with your test. The overdrive simulates the speaker distortion. When both sines go through the same distortion, the beating is greatly amplified, but when each sine has its own distortion, the pitches can be distinguished much better.

  • yes you are right i recorded it with my smartphone mic. i even tried inspecting the waveforms in audacity but even the first two pure sines didn't look exactly lile sine waves, probably useless to analyse the waveform of stereo wave. also im excited to see something new i can try, will test it in an hour or so and hopefully accept this as an answer if it works – robeddieson Dec 11 '19 at 14:04
  • files.catbox.moe/ss0n9z.jpg looks like it still produces beats – robeddieson Dec 11 '19 at 14:57
  • It will still produce beats, but can you hear the distinct notes better now? How about if you just leave the pow(sin()) or use a higher power. Or add more harmonics. You should be able to recreate the same phenomena with a modular synth by the way. – piiperi Reinstate Monica Dec 11 '19 at 15:12
  • can you shed some light on the modular synthesizer? not sure what you mean by that – robeddieson Dec 11 '19 at 15:20
  • also how did you come up with the idea of raising sin to the second power? it resulted in a malformed wave because the negative values will be positive – robeddieson Dec 11 '19 at 15:28
0

Close tones result beats as you already know (all tones, really). When adding two notes on same channel, the output waveform is rendered as the sum; the beat effect occurs in the file. When making the stereo file, each sine conserves its integrity. Your ears distinguishes them nicely, and the beat effect is much less perceived, since it occurs physically through air from different position of speakers and acoustics. In this case the waves are not already committed together to cause a chop/annihilation/beat.

The discussion about your method is off topic.

Top track: 440 Hz. Middle track: 445 Hz. Bottom track: both mixed together into a mono file (they were first halved amplitude)

Top track: 440 Hz. Middle track: 445 Hz. Bottom track: both mixed together into a mono file (they were first halved amplitude).

The frequency modulation effect happened at 445 - 440 = 5 Hz. At each second, the wave forms will cancel each other completely 5 times. Reversal can be achieved by subtracting a waveform, however, this will work defectively once a master mix has been applied.

In a stereo file, each sine wave is through a distinct channel. The effect will still happen in air, though, but will be more or less significant according to node positions. Also, having two ears, in this case, makes less likely to be perceived.

All samples given were sines. More complex timbres are made of many harmonics, which can be understood as many sines added together. In nature, usually resonances occur in perfect proportions in the harmonic spectrum (as Pythagoras has demonstrated): 2/1, 3/1, 4/1, 5/1 and so on. Since different timbres have different harmonic signatures, not all sub-vibrations will participate for a 'beat' effect, and timbres will cancel out only phase differences from each other.

  • Are you able to demonstrate this in practice, or are you just speculating? – piiperi Reinstate Monica Dec 15 '19 at 17:59
  • Unfortunately, the files sent by the OP are not the originals, but were captured by a cellphone. Anyway, you can see the third beep oscillations on the third beep - that does not happen in the second file (it must be very visible in the originals). Sound levitation makes use of acoustic nodes to levitate things. I have once seen studio technician hanging small foam balls in a room to detect nodes. I also happen to love La Monte Young's Excerpt 31 - 69 c. 12-17-33-12-25 - 33 PM NYC: you can hear the beats inside your head differing as you move yourself around (without headphones, of course). – fde-capu Dec 15 '19 at 20:51
  • im sorry for abandoning the question for a few days ive tested different things, some suggested by @piiperi Reinstate Monica and will update the question soon with some videos – robeddieson Dec 16 '19 at 11:36
  • 1
    At least I was able to recreate the effect completely in mono - if you distort/overdrive both notes individually before summing, the pitches are distinguished nicely. But if you first sum and then distort, it's like in the OP's without-stereo example. My example was done completely inside the box, without acoustic anything. – piiperi Reinstate Monica Dec 16 '19 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.