Shop OBEX P1 Docs P2 Docs Learn Events
Volume or Amplitude control for Sawtooth / Triangle waveforms — Parallax Forums

Volume or Amplitude control for Sawtooth / Triangle waveforms

I have been successfully using the Smart pin Sawtooth and Triangle commands for audio beeps.

pinSTART(pp, P_OE | P_PWM_SAWTOOTH, x, 0 )
and
pinSTART(pp, P_OE | P_PWM_TRIANGLE, x, 0 )

but the output tone is full on and harsh. Is there a simple & elegant way to lower the volume /amplitude of the waveform to something less loud? from a whisper to shout?

Ultimately, I would like to program simple musical sequences or even voice words, ie. pre-recorded WAV files(?) on the P2 module uSD card, but that feature can wait.

Comments

  • evanhevanh Posts: 16,039
    edited 2022-01-03 23:29

    PWM can be, and is at times, used for audio. But then the individual pulses are used to construct the audio amplitude, piece by piece - Multiple PWM cycles per audio cycle, with an appropriate low-pass analogue filter attached to smooth the audio. Known as pulse code modulation (PCM), or sampling. WAVe files contain these samples verbatim.

    It sounds like what you're doing instead is pulsing at the audio frequency - Somewhat non-ideal unless you only want a single tone.

    PS: Of course the Prop2 also has DACs for an alternative way of producing the PCM amplitude.

  • If you want finer control of the analog output levels you probably eventually need to use a DAC mode and feed the samples yourself (you can multiply them on the fly to achieve your volume levels and mix different sounds). This requires ongoing processing work and is not simple like setting pinSTART once and letting the smartpin do its thing with a single tone but it would allow your WAV stuff later.

    I was also going to suggest enabling the bitDAC mode output too for the pin ("TT" bits) but I'm not sure if it would work with the Smartpin modes you are using. It still might be worth a try though, in theory it could give you a handful (16) of high-low level differences if it worked which is better than nothing.

    If the overall volume level is too loud you might be able to reduce it a bit by experimenting with the different HHH and LLL drive outputs depending on the impedance of your audio stage and resulting voltages the P2 generates into them.

  • JonnyMacJonnyMac Posts: 9,159
    edited 2022-01-04 02:09

    I have been successfully using the Smart pin Sawtooth and Triangle commands for audio beeps.

    pinSTART(pp, P_OE | P_PWM_SAWTOOTH, x, 0 )
    and
    pinSTART(pp, P_OE | P_PWM_TRIANGLE, x, 0 )

    but the output tone is full on and harsh.

    Because the PWM output from a smart pin is a 0 to 3.3v square wave. Those attributes, P_PWM_SAWTOOTH and P_PWM_TRIANGLE, describe the behavior of the internal counter, not the external output. Connect a 'scope and you'll see.

    To do what you want (with level control) you'll need to synthesize waveforms through a DAC pin.

  • Just an idea:
    The drive strength of the pins (fast, 1k5, 15k, 150k) can be controlled, so it should be possible to have some voltage dividing resistor and have at least 4 output signal levels?
    Ah, that's the same idea, that rogloh mentioned in his last sentence.

  • pik33pik33 Posts: 2,388
    edited 2022-01-05 15:30

    These "sawtooth" and "triangle" names are misleading. Both are square waves. The difference is the way they are generated. Use a triangle type along with a noise shaper (find a P1 noise shaped PWM audio driver) if you want to make a D-class amplifier out of this.

    To generate audio using a P2 in a standard way (line out to the amplifier), use DACs, not PWMs. A simple beep can be written in several lines of simple spin code. wrpin and wxpin to make the pin a DAC, then wypin 65535, waitus, wypin 0, waitus, repeat

  • About a month or so ago JonnyMac posted somewhere in these Forums, a short SINE wave code snippet, that included the Pin, Frequency, Amplitude and Duration. Now that I could use it, I can't find it.

    Anyone, please send a link or direct me to it.

  • I think this is what you're looking for. It's Spin, so not capable of running high frequencies. I put it together for my friend John Huffman (JonyJib) so that he could build an audio "healing" device for his wife. I don't know if it heals, but they love the way it sounds. :)

  • JonnyMac - Yes, that is the one, and it works great.

Sign In or Register to comment.