PWM Polyphonic

Discussion in 'Embedded Systems and Microcontrollers' started by Art, Feb 1, 2015.

  1. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    Hi Guys,
    I’m working on a polyphonic music player for pic that plays bass progression & melody simultaneously.
    The problem is when only one note is played it’s considerably louder than two simultaneously.
    I have notes, timing, and dual frequency worked out, so this is the only problem left.

    I imagine you could use another pic pin and resistor to lower the level. Is there another common method?
    Thanks, Art.
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    If you are using one pin for both notes the volume change is expected.

    If on separate pins then it doesn't have an (to me) obvious cause.

    Can you share any more details or is this project that secret?
     
  3. Kermit2

    AAC Fanatic!

    Feb 5, 2010
    3,766
    928
    more power is contained in a two freq sound wave than one freq. getting quieter with multiple freq. leads me to guess that the output is maxed out power wise when making a single note output and has to divide the available power between multi-note outputs.
    perhaps a buffer stage op-amp on the output pin or lower the output amplitude of the chip you are using
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    Mixing square waves of different tones will produce intermodulation distortion.
    You want to sum the sine waves first before converting to PWM output.
    Using analog output from a DAC would be an alternative to PWM.
     
  5. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    Lol, it would be secret if polyphonic hadn’t been done a million times ;)
    MrChips you would faint if you saw what I saw on spectrum.

    I made an RS232 cable for retro computer that had TTL serial port,
    then looked at the MAX232 guts, it’s an inverter and voltage doubler,
    and wondered what would happen if I just connected it to a pic and pulsed it.
    it certainly produces a signal with greater than 5 Volt difference that crosses zero.

    [​IMG]

    It wasn’t really meant for audio at that stage, just an experiment,
    bit I’m sure the MAX232 input and output are both binary no matter what filtering I tried at the input,
    it does clean up immensely with a single capacitor between ground and the input to the former.
    If it were an audio amplifier there would be more options. Also HPWM has not been implemented yet.

    Since making this video where I was mixing a 1Hz signal with any solitary note to keep volume even,
    I have used a higher than audible frequency that is potentially easier to filter, and eliminates the fluttering.
    I have also not finished edelweiss, but almost there since the last part is repeated.



    This is the keyboard tutorial I used, where the bass progression comes from:
     
  6. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    Why would I faint? I've never fainted in my whole life.
     
  7. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    Reading your post and looking at this! There is of course “less conditioned" PWM out there.

    [​IMG]
     
  8. BobTPH

    Active Member

    Jun 5, 2013
    782
    114
    How are you constructing the sound? Is it just 2 square waves, or are you generating a more complex waveform and using PWM + filtering?

    Bob
     
  9. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    It’s dirty PWM both freqs out a single pin. The other pin connected is the HPWM channel waiting to introduce a third freq.
    It starts to sound a bit like church bells then. If I filter the output of the MAX232 it is of course nicer, but lower amplitude again.

    I won’t be able to sweep the HPWM because software is always busy with the other software tones,
    but it appears I can just play more notes, or mix a two note chord, and use HPWM for melody
    with it’s own MAX232 channel.
     
  10. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    The code now is verbose and embarrassing, but there will be a trick to it.
    You can make any change to HPWM when any one note starts, stops or changes.
    It should have been driven by a variable interrupt timer for time signature.
    I think I can simulate a drum with the brushes instead of sticks with HPWM and a timer.

     
    Last edited: Feb 4, 2015
  11. BobTPH

    Active Member

    Jun 5, 2013
    782
    114
    You didn't answer my question. I have no idea what "dirty PWM" means. Is the PWM used to simply put out a square wave, or is it following a waveform by varying the duty cycle for each PWM period? And how are the two notes combined?

    Bob
     
  12. Art

    Thread Starter Distinguished Member

    Sep 10, 2007
    785
    61
    The dual frequency is a BASIC command that takes one or two frequencies and a duration.
    It looks like a one bit approximation of a mixed waveform on the scope, ie. varying pulse widths,
    so I would say the two PWM frequencies are added and the best one bit approximation delivered out one pin.

    I have done this with DTMF recently with iOS, where you calculate a sine for two frequencies,
    add each sample and divide by 2 to fill the sample buffer, but they are real 16 bit numbers,
    I imagine it uses two sample buffers summed for pic, and forget about the divide by 2.
     
  13. BobTPH

    Active Member

    Jun 5, 2013
    782
    114
    Okay. I expect that it is dosing something like oring together 2 square wave signals, which would result in a lower output for 2 notes than 1.

    To get this to work better, you would want to use a high frequency PWM (40000Hz if possible) and adjust the duty cycle on each period to approximate a waveform. You would add the waveforms from the 2 independent notes, making sure that the sum would never overflow. It's pretty complicated, and not likely doable in basic. You would need assembly coding and interrupt handlers.

    Bob
     
Loading...