在Windows上播放任意声音?

时间:2011-04-28 07:01:51

标签: winapi audio wave

如何在Windows上播放具有给定幅度和给定频率构成(例如,由2 kHz和3 kHz频率组成)的声音(32位和64位,最高为Windows 7) ?

(原则上我的意思是不使用外部库。)

我认为这需要waveOutWrite方法,但我不知道它是如何工作的。

4 个答案:

答案 0 :(得分:2)

Beep

BOOL WINAPI Beep(
    __in  DWORD dwFreq,
    __in  DWORD dwDuration
);

答案 1 :(得分:2)

我有点工作......

#define _USE_MATH_DEFINES 1
#include <math.h>
#include <stdio.h>
#include <tchar.h>

#include <windows.h>
#include <mmreg.h>
#include <complex>

#pragma comment(lib, "Winmm.lib")

MMRESULT play(float nSeconds,
    float signal(float timeInSeconds, unsigned short channel, void *context),
    void *context = NULL, unsigned long samplesPerSecond = 48000)
{
    UINT timePeriod = 1;

    MMRESULT mmresult = MMSYSERR_NOERROR;
    WAVEFORMATEX waveFormat = {0};
    waveFormat.cbSize = 0;
    waveFormat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
    waveFormat.nChannels = 2;
    waveFormat.nSamplesPerSec = samplesPerSecond;
    const size_t nBuffer =
        (size_t)(nSeconds * waveFormat.nChannels * waveFormat.nSamplesPerSec);
    float *buffer;
    waveFormat.wBitsPerSample = CHAR_BIT * sizeof(buffer[0]);
    waveFormat.nBlockAlign =
        waveFormat.nChannels * waveFormat.wBitsPerSample / CHAR_BIT;
    waveFormat.nAvgBytesPerSec =
        waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;

    buffer = (float *)calloc(nBuffer, sizeof(*buffer));
    __try
    {
        for (size_t i = 0; i < nBuffer; i += waveFormat.nChannels)
            for (unsigned short j = 0; j < waveFormat.nChannels; j++)
                buffer[i+j] = signal((i+j) * nSeconds / nBuffer, j, context);
        HWAVEOUT hWavOut = NULL;
        mmresult = waveOutOpen(&hWavOut, WAVE_MAPPER,
            &waveFormat, NULL, NULL, CALLBACK_NULL);
        if (mmresult == MMSYSERR_NOERROR)
        {
            __try
            {
                timeBeginPeriod(timePeriod);
                __try
                {
                    WAVEHDR hdr = {0};
                    hdr.dwBufferLength =
                        (ULONG)(nBuffer * sizeof(buffer[0]));
                    hdr.lpData = (LPSTR)&buffer[0];
                    mmresult = waveOutPrepareHeader(hWavOut,
                        &hdr, sizeof(hdr));
                    if (mmresult == MMSYSERR_NOERROR)
                    {
                        __try
                        {
                            ULONG start = GetTickCount();
                            mmresult =
                                waveOutWrite(hWavOut, &hdr, sizeof(hdr));
                            Sleep((ULONG)(1000 * nSeconds
                                - (GetTickCount() - start)));
                        }
                        __finally
                        { waveOutUnprepareHeader(hWavOut, &hdr, sizeof(hdr)); }
                    }
                }
                __finally { timeEndPeriod(timePeriod); }
            }
            __finally { waveOutClose(hWavOut); }
        }
    }
    __finally { free(buffer); }
    return mmresult;
}

// Triangle wave generator
float triangle(float timeInSeconds, unsigned short channel, void *context)
{
    const float frequency = *(const float *)context;
    const float angle = (float)(frequency * 2 * M_PI * timeInSeconds);
    switch (channel)
    {
        case  0: return (float)asin(sin(angle + 0 * M_PI / 2));
        default: return (float)asin(sin(angle + 1 * M_PI / 2));
    }
}

// Pure tone generator
float pure(float timeInSeconds, unsigned short channel, void *context)
{
    const float frequency = *(const float *)context;
    const float angle = (float)(frequency * 2 * M_PI * timeInSeconds);
    switch (channel)
    {
        case  0: return (float)sin(angle + 0 * M_PI / 2);
        default: return (float)sin(angle + 1 * M_PI / 2);
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    float frequency = 2 * 261.626F;
    play(1, pure, &frequency);
    return 0;
}

答案 2 :(得分:1)

waveOut函数处理声音波形数据(如果我没记错的话,采用WAV格式)。

虽然这是针对WPF应用程序的,但以下链接应该对任何桌面应用程序都有帮助:

Sound Generation in WPF Applications

答案 3 :(得分:0)

通过电脑扬声器或使用Directx Sound发出蜂鸣声。 如果需要,我可以提供一些片段。