在 Windows 上播放任意声音?

发布于 2024-11-03 15:33:56 字数 257 浏览 1 评论 0原文

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

(本机我的意思是不使用外部库。)

我相信这需要 waveOutWrite< /a> 方法,但我不知道它是如何工作的。

How do I, say, play a sound with a given amplitude and a given frequency makeup (say, consisting of frequencies 2 kHz and 3 kHz) natively on Windows (32-bit and 64-bit, up to Windows 7)?

(By natively I mean without using an external library.)

I believe this needs the waveOutWrite method but I have no idea how it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吲‖鸣 2024-11-10 15:33:56

我有东西在工作...

#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;
}

I got something working...

#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;
}
墨离汐 2024-11-10 15:33:56

蜂鸣声

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

Beep

BOOL WINAPI Beep(
    __in  DWORD dwFreq,
    __in  DWORD dwDuration
);
狠疯拽 2024-11-10 15:33:56

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

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

WPF 应用程序中的声音生成

The waveOut functions deal with sound waveform data (in WAV format, if I recall correctly).

While this is targeted at WPF applications, the following link should prove helpful for any desktop application:

Sound Generation in WPF Applications

惯饮孤独 2024-11-10 15:33:56

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

Beep via pc speaker, or using Directx Sound.
I can offer some snippets if you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文