How does pwm audio work
The compressor ratio compares the number of decibels the input signal is above the threshold to the number of decibels the output signal is above the threshold.
In other words, it is the relative amount of attenuation the compressor will apply to the signal. All compressors work with a gain reduction circuit that effectively compresses the audio signal in response to a control signal. This control signal also referred to as the sidechain is derived from the input audio signal common or via an external audio signal less common. It is manipulated via the aforementioned compressor parameters.
In the case of PWM compressors, the gain reduction circuit is centred around a pulse width modulator. As the name suggests, the width of the pulses is modulated over time. This is shown in the image below:. Note that the pulsing rate must be very fast to avoid any auditory changes in amplitude.
In theory, the faster, the better when it comes to PWM compressor switching frequencies. This is to avoid potential sonic artifacts. However, the entire design must work well with whatever PWM chip frequency is used.
The percentage of time the pulse width modulation signal is on is equal to the percentage of the initial input level that gets outputted by the gain reduction circuit. So we can easily see how PWM switching is a tool for reducing the signal amplitude and, therefore, could be used as the gain reduction element of a compressor.
Time constants must also be taken into account in the pulse width modulator. The following is a simple signal flow chart to express the compressor sidechain. When designed correctly, a PWM compressor has the potential to be the fastest-acting compression type with the lowest distortion. Then we must configure when it should transfer the data, i.
In this case, the source from where to fetch the data is the array containing our audio samples. The destination is the register of timer 2 which contains the PWM compare value. This is the value which determines the duty-cycle of the PWM signal. The DMA transfer has to be triggered with the sample rate of the recording.
In this case, I use the standard of You can find the whole source code here. As you can see in the source code, the main while-loop is empty. To test it, I created two audio files. In fact, according to documentation , the use of tone interferes with PWM output on pins 3 and 11, which indicates that the timer normally used to generate the Hz Arduino PWM frequency is modified to create other frequencies. To demonstrate the difference between the built-in tone function in Arduino and "manually" generating the same tone via for-loops, the A4 note Hz was synthesized via the functions individually, and then simultaneously on both pins.
In both cases the "manually" made tone is about 5Hz slower than the tone function's tone. This is true even when the tones are played separately, which shows that tone is neither interfering with the for-loop-based method, nor is it affected by other processes the Arduino performs.
The delay in the for-loop method is a result of the Arduino needing to perform other processes between calls to the tone generation function. Changing the duty cycle while keeping the frequency the same doesn't change the pitch of the sound, but it does affect the way the ear and brain interpret it. This results from the fact that square waves consist of the sum of a near-infinite number of sinewaves, specifically the odd harmonics.
Harmonics are higher-frequency components of some fundamental frequency. For example, if a signal is oscillating at fundamental frequency of Hz, the second harmonic would be oscillating at twice the rate, or Hz, the third would be at Hz, and so on. Each is a multiple of the fundamental 55Hz frequency. Odd harmonics are the odd-multiplier of the fundamental frequency, or every other note in the sequence A3, A5, A7, for example. Thus, when a square wave is heard, the brain interprets it at the frequency the signal is being generated at, but the other less dominant frequencies also play a role in affecting the character of the sound.
If the duty cycle is changed, the sound and its harmonic composition changes. Yamaha Synth has an excellent article on the topic of square waves and harmonics, and is recommended for anyone interested in exploring the topic further. In simple terms, an FFT is a mathematical process used to find the component frequencies of any signal. This is extremely similar to a spectral analysis performed on light, which shows which wavelengths are present.
In this case the frequencies in an electrical signal, instead of light wave, are being interpreted. Amplitude in terms of voltage is plotted on the y-axis, and the frequencies are plotted on the x-axis. Unless the signals you are generating are going to be very low frequency, it is almost always better to sacrifice a bit or two of resolution for the reduced distortion that Phase Correct PWM gives you.
A few other odds and ends to keep in mind: 1. Generating and outputting 2 x 8 bit values is less computation than 2 x 7 bit values, and 2 x 4 bit values is also fairly easy to generate. It is quite common on sound cards and mixing boards to use 30kHz or 40kHz as the low-pass frequency, so be wary. Your signal could easily distort the input amplifier stages, even though your signals are outside of the audible range. In these cases, a 3rd order active filter with a single opamp will solve the problem.
So, now that you have an idea of what PWM can do, here is a quick tutorial and Arduino sketch that has register settings for using Timer1 as a high quality, audio rate PWM generator.
So click the link above to get started and sorry for all the external linking, there is just too much info to fit coherently on one page. Open Music Labs. Skip to content. What is PWM, and how does it work? Dual and triple PWMs. Distortion in PWMs. Final Considerations. Setting up your ATmega Arduino. Recent Articles x0x-heart at Crowd Supply! My summer vacation, by guest. Codec Shields on Sale! Article Topics arduino avr floppy-audio getting started how-to ISP microdec modifications updates wiki.
0コメント