Andy beat me to it. But anyway. I am a power electronics specialist - that sound you hear is the harmonic noise being caused by the square waves you are passing into the motor. It is physically damaging the motor and reducing efficiency by creating heating losses in the coils that don't create useful work, because they occur at other frequencies than where the work can happen.
This is a basic two level sawtooth modulated PWM signal, just for reference.
See how the PWM signal is high much longer at the peak of the sinusoidal wave? That's where the magic is. If we divide it up into enough pieces and put well selected low-pass filter on it, it will practically turn into a pure sine wave.
Well, that's not really true. The voltage will still be blocky but the current waveform will have so much less distortion that you no longer need to worry about life-shortening harmonics and ear-grating noise.
You're already halfway there in many ways. If you're already generating a duty cycle now, with very little effort you will be able to find a library to bit-bang this kind of PWM signal output. Depending on the exact controller you are using, it may actually have an onboard function for this very thing.