Emulating PWM sound on the PC

Advice on general approaches or feasibility and discussions about game design

Re: Emulating PWM sound on the PC

Postby rodot » Sat Oct 25, 2014 3:07 pm

I'm working on the headphone jack module, and I don't hear any high pitch distortion due to PWM, even with an HiFi headset (Sennheiser HD428). I may add an optional capacitor on the PCB for those who want to add a first order low pass filter, but I don't think it will be really useful.

Well, that confirms that feeding OCR2B to DAC is the way to go for emulating sound ;)
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: Emulating PWM sound on the PC

Postby jonnection » Sat Oct 25, 2014 5:44 pm

Rodot: as long as you only use square waves there is no whine, because a square wave only has odd harmonics. Try triangle or saw waves.
User avatar
jonnection
 
Posts: 317
Joined: Sun May 04, 2014 8:21 pm

Re: Emulating PWM sound on the PC

Postby Myndale » Sat Oct 25, 2014 9:36 pm

Hang on, I'm confused now, how exactly are you generating your triangle waves? It's impossible to do full 8-bit triangle waves with the standard Gamebuino mixer because it mixes at 15000 samples per second; at 440 Hz you can just about manage a 5-bit triangle wave or a 4-bit sawtooth wave.

It might be worth me hooking a oscilloscope up to mic and seeing how various waves are being reproduced by the Gamebuino speaker, can you provide the mixer code you're using to generate the waves?
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: Emulating PWM sound on the PC

Postby jonnection » Sat Oct 25, 2014 10:26 pm

I am perhaps confusing because I do not use any Gamebuino library code. I am talking in the context of whats possible w/r to the hardware, not w/r to Gamebuino lib. Sorry about that, should be more specific.

Timer1 ISR fires (what was it) 31250 times per second. For a 440Hz saw wave 2 x 35 steps between 0 to 256. Enough resolution to make a saw wave - at least in my view. The sound is different from a square wave. I'll try to record more demos to demonstrate.

(EDIT: lousy video but you can hear the difference between saw and square + you can hear the whine with the saw wave. If the "whine" was a result of some electric interference, I think it should be audible in the square wave as well. I can only attribute it to the way the signal is formed due to PWM ... but I might be wrong. I always though that this was _the_ reason why all Arduino synths I have seen have some sort of a low-pass filter.)




Here's a screenshot of a 30 Hz saw wave, period (0.032sec) is 31Hz (slightly off from 30Hz setting).

I have a function pointer table for different oscillator functions (saw, triangle, noise etc), each oscillator function is addressed simply by redirecting the function pointer (which the ISR calls). Saws and triangles are all handled with simple precalculated dx,dy counters to avoid multiplication and/or division.

Code: Select all
ISR(TIMER1_COMPA_vect){

  osc1.count++;
  osc1.inccount++;

  osc2.count++;
  osc2.inccount++;

  Farr[osc1.wave](&osc1);
  Farr[osc2.wave](&osc2);

//... and then some mixing code that combines the outputs of the two oscillators into OCR2B



It is possible I have misunderstood completely how this whole thing works. On the other hand, I do get the kind of audio signals I am expecting.

Having said that, it would be extremely interesting to see oscilloscope images of Gamebuino output. I'd be very interested if you sometimes took the time to grab a few pictures of what the audio signal actually looks like. Square signal we all know. What happens to "PWM faked" signals like saw waves is more interesting.

Image
User avatar
jonnection
 
Posts: 317
Joined: Sun May 04, 2014 8:21 pm

Previous

Return to Project Guidance & Game development

Who is online

Users browsing this forum: No registered users and 9 guests