Square Waves and Basic Frequencies!
SONIC the Hedgehog
Posts: 321
So I was experimenting earlier, and made a function that generates square waves.
The result was some pretty interesting sounds, but they are square waves. So my question is how would you use spin to create saw waves? Would that even be possible or would you be limited to square and triangle waves?
' ' File: SquareWave.spin CON _clkmode =xtal1 + pll1x _xinfreq = 5_000_000 PUB Main SquareWave(1, clkfreq/1000, clkfreq/2000) SquareWave(1, clkfreq/2000, clkfreq/4000) SquareWave(1, clkfreq/4000, clkfreq/8000) SquareWave(1, clkfreq/8000, clkfreq/16000) SquareWave(1, clkfreq/16000, clkfreq/32000) PUB SquareWave(pin, tHigh, tLow) : success | tL, tH outa[pin]~ dira[pin]~~ tHIgh := tH tLow := tL Repeat clkfreq + cnt outa[pin]~~ waitcnt(tH + cnt) outa[pin]~ waitcnt(tL + cnt)I wired pin one to a loudspeaker, and then to the other end of the loudspeaker I gave +6 volts.
The result was some pretty interesting sounds, but they are square waves. So my question is how would you use spin to create saw waves? Would that even be possible or would you be limited to square and triangle waves?
Comments
-Phil
Propeller pin
loudspeaker
+6 volts. It seems the prop would see the six volts, but it doesn't. I've done many circuits using CMOS devices running at 4.5v and below using a loudspeaker that sees 6 volts and greater.
Connect the other side of the speaker, through a capacitor (100uF or so), to 0V, and it should be just as loud.
Why do you say it doesn't? Have you put a scope on it and measured it? You still won't see 6V at the prop pin because of the internal protection diode in the chip, but you will see 4.2 or so, as the protection diode is forward-biased through the low impedance of the speaker. And, by the way, the speaker will allow enough current to flow that you could, very well, damage the I/O pin on the prop.
-Phil
But still back to topic. Any ideas on sawtooth waves?
-Phil
When you run the speaker straight from the prop pin to 0V, the constant DC across it will push the voice coil out of the magnetic gap and make the speaker less sensitive. The capacitor changes the speaker from getting 0-3.3V, to -1.65-1.65. That way, the average is 0, and the coil stays well within the gap and it has higher efficiency.
Even a very simple single channel DDS with basic waveforms and some kind of volume adjustment or envelope will produce a less than acceptable sample rate.
There are, of course, different ways of doing DDS with acceptable results on the Propeller.
*For a simple square wave tone, you can use the two counter modules in a cog to achieve extremely good results; Almost no jitter or aliasing at all thanks to the very high sample rate
The problem is that you can't adjust the volume
*You can use the video generator in a cog to playback samples with 1, 2 or 8 bit resolution as long as you feed it with data fast enough; Almost no jitter or aliasing distortion (apart from the sample itself)
You can't adjust the volume, but the sample itself may have a natural declining amplitude.
*You can do DDS in pasm and launch it as a software peripheral in a separate cog. This is the prefered way to achieve more complex synthesis.
The results can vary very much; From a very simple PSG with 3 channel square waves and 16 step volume adjustment to very complex sound synthesis with 8+ channels with envelopes and different kind of modulations.
The general rule is that simple sound synthesis will run at high sample rates with very low little jitter and aliasing problems; Simple sounds, but Hifi!!
Complex sound syntesis can sound very good but will often have lower sample rates; Complex sounds, but less Hifi!!
It's quite hard to find the perfect balance between complexness <-> Hifiness
/Johannes
It's basically just: 32bitRegister += 32bitFrequency.
32bitRegister can be feed directly to a dac or a pwm output to produce a saw wave signal.
* The sample rate will be the rate at wich you call "32bitRegister += 32bitFrequency" and then output the value of 32bitRegister to the dac.
* The oscillator frequency can be calculated by this formula: Freq = SampleRate * (32bitFrequency/(32^2))
/Johannes
This only results in a DC current flowing through the speaker which at worst will blow the speaker coil or whatever is driving it, the Prop in this case.
Don't forget that for DC the speaker has just the low resistance of the voice coil.
At best the DC current simply drives the speaker to one end of its range of movement. In which case the range of movement available to create sound is reduced and will result in distortion.
Hence you will often see a capacitor in series with the speaker to block DC if your driver can only produce positive voltage swings (or only negative). Or you will see that the driver amplifier can swing its output both positive and negative with the signal giving a zero DC bias.
Also, the 555 output used was prolly the open-collector (Pin 7). Raising the supply voltage to the speaker when driven from an open-collector will indeed increase the volume without hitting any clamping diodes, just like Phil's example - http://forums.parallax.com/showthread.php?137615-Square-Waves-and-Basic-Frequencies!&p=1070597&viewfull=1#post1070597
A moving coil speaker should never be connected directly to logic outputs - its asking for trouble. If you want to get sensible levels of audio out use a buffer amp (I'd favour the MIC4422 MOSFET driver personally). Connect a large electrolytic in series with the speaker to lose the DC component.
Someone mentioned the 555 - thats a bipolar chip rated at 300mA if I remember correctly, far more robust than the prop (and much cheaper to replace!).
A fast buffer like the MIC4422 (which goes up to 18V, 9A, and has significant latch-up protection) allows class-D operation too
-Phil
Reference: http://www.puiaudio.com/resources-white-papers-electromechanical-transducers.aspx