Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6856829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:56:07+00:00 2026-05-27T01:56:07+00:00

How do I, say, play a sound with a given amplitude and a given

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:56:07+00:00Added an answer on May 27, 2026 at 1:56 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's fairly simple to play a sound file, say temp.wav, from Visual Studio C#.
I'm trying to play only part of a sound using FMOD, say frames 50000-100000
Finch uses OpenAL. However, when I have an instance of Sound, and say -play,
I am trying to play a sound on Windows XP in multi-channel (parallel) manner.
I am Using Following code to play sound NSString *laughSoundPath = [[NSBundle mainBundle]pathForResource:@laugh ofType:@wav];
Scenario: I have a Long file - Say 30 Minutes. I want to play
let's say i want to play an mp3 song in the background of the
are there any ways to play media files inside a WinForms application without having
Let's say I have a Student entity like this implemented using the Play Framework's
I have an Android app which needs to play a sound resource on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.