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 8388019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:22:30+00:00 2026-06-09T18:22:30+00:00

I am trying to record the audio at windows, here is my code. it

  • 0

I am trying to record the audio at windows, here is my code. it works well for 8 bit, but it cannot work for 16 bit. Can anyone help me?
#include
#include
#include
#pragma comment(lib,”winmm.lib”)
using namespace std;

int test(){

    HWAVEIN microHandle;
    WAVEHDR waveHeader;

    MMRESULT result = 0;

    WAVEFORMATEX waveformat;
    waveformat.wFormatTag = WAVE_FORMAT_PCM;

    waveformat.wBitsPerSample=8;
    waveformat.nSamplesPerSec=16000;//8000;
    waveformat.nAvgBytesPerSec=waveformat.nSamplesPerSec*waveformat.nSamplesPerSec/8;

    waveformat.nChannels=1;
    waveformat.nBlockAlign=waveformat.nChannels*waveformat.wBitsPerSample/8;

    waveformat.cbSize=0;

    result = waveInOpen(&microHandle, WAVE_MAPPER, &waveformat, 0L, 0L, CALLBACK_EVENT);

    if (result)
    {
        cout << "Fail step 1" << endl;
        cout << result << endl;
        Sleep(10000);
        return 0; 
    }

    const int BUFSIZE = 16000*4;
    char *  buf = (char *)malloc(BUFSIZE);
    // Set up and prepare header for input
    waveHeader.lpData = (LPSTR)buf;
    waveHeader.dwBufferLength = BUFSIZE;
    waveHeader.dwBytesRecorded=0;
    waveHeader.dwUser = 0L;
    waveHeader.dwFlags = 0L;
    waveHeader.dwLoops = 0L;
    waveInPrepareHeader(microHandle, &waveHeader, sizeof(WAVEHDR));

    // Insert a wave input buffer
    result = waveInAddBuffer(microHandle, &waveHeader, sizeof(WAVEHDR));

    if (result)
    {
        cout << "Fail step 2" << endl;
        cout << result << endl;
        Sleep(10000);
        return 0;
    }

    result = waveInStart(microHandle);

    if (result)
    {
        cout << "Fail step 3" << endl;
        cout << result << endl;
        Sleep(10000);
        return 0;
    }


    // Wait until finished recording
    do {} while (waveInUnprepareHeader(microHandle, &waveHeader, sizeof(WAVEHDR))==WAVERR_STILLPLAYING);

    FILE *fp = fopen("output.pcm","w");
    fwrite(buf,1,BUFSIZE,fp);
    fclose(fp);

    waveInClose(microHandle);

    return 0;
}
void main()
{
    test();
}

If I set the parameter waveformat.wBitsPerSample = 8, it can record the audio correctly,
but if i set it waveformat.wBitsPerSample = 16, it record the Noise!!!
Can anyone help me?

thanks.

  • 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-06-09T18:22:31+00:00Added an answer on June 9, 2026 at 6:22 pm

    it should bu FILE *fp = fopen(“output.pcm”,”wb”); NOT FILE *fp = fopen(“output.pcm”,”w”);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to record sound by creating an android application. here is the code:
I'm trying to record a sample audio with phoneGap, but with no success. What
I'm sorry if the title is confusing but I'm trying to record audio for
I am trying out the record outgoing call using mic written this code but
I am trying to record audio in android but I am facing a problem.
I am trying to record audio by MediaRecorder, and I get an error, I
I'm trying to record audio this.recorder = new android.media.MediaRecorder(); this.recorder.setAudioSource(android.media.MediaRecorder.AudioSource.MIC); this.recorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.DEFAULT); this.recorder.setAudioEncoder(android.media.MediaRecorder.AudioEncoder.DEFAULT); this.recorder.setOutputFile(pruebaAudioRecorder.mp4); **this.recorder.prepare();**
I am trying to record using the iphones microphone: This is my code: NSArray
I've tried to overcome this for a while. I'm trying to record sound, but
Simply stated: I'm trying to record audio in a browser, and get that data

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.