Looking for an Audio Output (PWM?) Example
David Betz
Posts: 14,516
Is there an example showing how to configure a smart pin for PWM output?
Comments
|< converts the bits parameter to a mask before writing to WXPIN.
Then the audio samples are written to WYPIN.
While I get mine to work, and it works well, I'm sure it could make better use of the DAC modes when I get around to looking at it again.
In all cases the low-level DAC has to be setup in the P field of a WRPIN.
/Johannes
http://forums.parallax.com/discussion/comment/1496551/#Comment_1496551
https://code.google.com/archive/p/tinkerit/wikis/Auduino.wiki
A grain is basically a soundbuffer of a very small size that you apply realtime frequency control, volume control and the ability to change the data ptr on the fly. Having a lot of grains mixed in realtime into two stereo channels is the basis of granular synthesis. Sounds almost like a perfect match for reSound. Up to 64 grains could be mixed in realtime with independant frequency, volume, panning and grain size. The driver supports everything, the API just needs a method to set the grain parameters. It was actually on my todo list + a very basic granular synth examples. It wouldn't take much effort to implement it since it basically is just setting some parameters in hub that reSound already reads in at the mixing rate.
You may find these very simple -- they're intended to be beginner-friendly.
Edit: Whoops, I posted very late and in response to your first quote -- these may not be helpful.
The P2 has real 8-bit R2R DACs; it's not just PWM. Smartpins can be used to do dithering to achieve 16 effective bits, which is effectively using PWM to append 8 more bits to an 8 bit value. Using the DACs should sound a lot better, i.e. have a lot less noise, than just using plain PWM.
EDIT: It's apparently a resistor-string DAC, not an R2R DAC. See Ariba's post below if you're reading this in the future.
Depends... R2R DACs have some non-linearity, i.e. it could happen that each of the 256 steps are not exactly the same size if the Rs are not perfectly matched. So DAC mode has less noise (if you only use simple RC filtering) but more inter-modulation and distortion.
For speech distortion doesn't matter much. There are even cases where you want distortion. E-guitars and old synthesizers (C64...) sound better if you have a little bit inter-modulation. But if you want to play high quality music I'd go for PWM with good filtering. A 3rd order sallen-key filter can be built with only one OP amp.
Here's a very basic stereo audio example code in pasm...
You can't update the sample more often than what the sample period dictates because the smart pin will only update the value on each NCO rollover. Therefore it's necessary to have that "waitse1" to sync everything.
Chip said, it's a resistor string DAC, so there are 255 equal resistors in series and a 256 to 1 multiplexer that selects the output voltage.
This should work much better than a R2R DAC for 16 bit dithering.
Andy
https://forums.parallax.com/discussion/comment/1452997/#Comment_1452997
If you choose to use the PWM mode, it's important to have a sample period that is a multiple of 256 to get the best results; In other words the 8 LSB's needs to be zero. The reason is that the PWM has 8bit = 256 CPU cycles of resolution. Not having a multiple of 256 will possible give you audible jitter/distortion. If it actually is audible depends on the ratio of the period to 256 and the sample rate. A sample period with bit 7 set will not be audible if the sample rate is 88200 Hz, because the jitter will be above the upper limit of the human hearing. (Dogs may complain though! )
Ok, it really looks like it's a 256:1 mux instead of a R-2R ladder. If it was R-2R there should be larger steps at transitions where many bits change, especially from $7F to $80. So please forget my 1st post, DAC mode seems to be the better way compared to PWM.
forums.parallax.com/discussion/171196/pnut-spin2-latest-version-v34s
QFRAC x,y effectively has an implicit 32-bit left shift of x. So that'll be the one used there. The actual difference is QDIV gets the dividend high 32 bits from the preceeding SETQ, or zero, and the low 32 bits from D, whereas QFRAC gets the dividend low 32 bits from the preceeding SETQ, or zero, and the high 32 bits from D.