Outputting audio (Windows Audio API)

Alright, so I have a small program in which user can control the output frequency of the audio signal and play songs (in theory :) ).

I generate the sine wave myself in code by figuring out how many points it will take for a single period of that waveform given it's sampled at 44100Hz. Like this:

    for (int k = 0; k < points_sine_wave; k++)
        {
            // Generate sine wave
            sineWave[k] = pow(2.0,10) * (sin(2*3.14159*f*td*k + old_angle) + 1);

        }

td is delta t (constant), f is frequency, k is just an index. To ensure continuity of the sine wave when the frequency changes, I use old_angle which is saved to remember which angle to resume from. I checked the waveforms generated here and it works (the sine wave remains continuous but will change frequency). The +1 is to make everything positive.

Now, depending on what I put into that pow(2.0, x), I may not be able to hear low frequencies. It seems like at 2^7, I can hear everything just fine (100Hz is something of interest), but if I go to 2^6 or 2^5, I cannot hear the low frequency anymore.

I looked at my output through Audacity, and noticed that high frequency components always occupy the whole vertical range (-1 to 1), while the low frequency are kind of between -0.4 and 0.4.

Why is that? The signal I write to the buffer is all the same max amplitude (checked in Matlab - can post this if you'd like).

The format is set to PCM, the # of bits is 32 and # of valid bits is also 32 (although 32 seems to rail everything).


The most likely answer is that the speakers you are playing the sound through aren't good at reproducing wavelengths that low. Desktop computer speakers, for example, often won't produce sounds below 50Hz. If you have a subwoofer or really high-quality speakers, of course, you'll do better.

The other possibility is of course that something is wrong with the sine wave you are generating... but in general that would be obvious in Audacity if you zoomed in closely enough to see the individual samples. That is, if the visual looks like a proper sine wave, then it very likely is a proper sine wave. (and if it's not, eg if there are any sudden discontinuities, you will hear very obvious noise in the audio as it plays back!)

One final note: Assuming your PCM format is using signed sample values, I suspect you'll be better off if you keep your sine wave vertically centered on the X axis; that is with an equal number of positive and negative values. If you keep all the values positive, then your speakers will play back the audio with a DC offset, which can be bad (or at least sub-optimal) for various reasons.


You have 31 bits of possible amplitude, and use 2^7 or 2^6 as the actual amplitude? There are many soundcards which can only do 24 bits. In fact, 16 bits isn't considered awfully bad.

链接地址: http://www.djcxy.com/p/33838.html

上一篇: 我对FFT和音高检测的理解是否正确?

下一篇: 输出音频(Windows Audio API)