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

  • Home
  • SEARCH
  • 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 3945238
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:59:40+00:00 2026-05-20T00:59:40+00:00

Hey all, I’m writing an application which records microphone input to a WAV file.

  • 0

Hey all, I’m writing an application which records microphone input to a WAV file. Previously, I had written this to fill a buffer of a specified size and that worked fine. Now, I’d like to be able to record to an arbitrary length. Here’s what I’m trying to do:

  • Set up 32 small audio buffers (circular buffering)
  • Start a WAV file with ofstream — write the header with PCM length set to 0
  • Add a buffer to input
  • When a buffer completes, append its data to the WAV file and update the header; recycle the buffer
  • When the user hits “stop”, write the remaining buffers to file and close

It kind of works in that the files are coming out to the correct length (header and file size and are correct). However, the data is wonky as hell. I can make out a semblance of what I said — and the timing is correct — but there’s this repetitive block of distortion. It basically sounds like only half the data is getting into the file.

Here are some variables the code uses (in header)

// File writing
ofstream mFile;
WAVFILEHEADER mFileHeader;
int16_t * mPcmBuffer;
int32_t mPcmBufferPosition;
int32_t mPcmBufferSize;
uint32_t mPcmTotalSize;
bool mRecording;

Here is the code that prepares the file:

// Start recording audio
void CaptureApp::startRecording()
{

    // Set flag
    mRecording = true;

    // Set size values
    mPcmBufferPosition = 0;
    mPcmTotalSize = 0;

    // Open file for streaming
    mFile.open("c:\my.wav", ios::binary|ios::trunc);

}

Here’s the code that receives the buffer. This assumes the incoming data is correct — it should be, but I haven’t ruled out that it isn’t.

// Append file buffer to output WAV
void CaptureApp::writeData()
{

    // Update header with new PCM length
    mPcmBufferPosition *= sizeof(int16_t);
    mPcmTotalSize += mPcmBufferPosition;
    mFileHeader.bytes = mPcmTotalSize + sizeof(WAVFILEHEADER);
    mFileHeader.pcmbytes = mPcmTotalSize;
    mFile.seekp(0);
    mFile.write(reinterpret_cast<char *>(&mFileHeader), sizeof(mFileHeader));

    // Append PCM data
    if (mPcmBufferPosition > 0)
    {
        mFile.seekp(mPcmTotalSize - mPcmBufferPosition + sizeof(WAVFILEHEADER));
        mFile.write(reinterpret_cast<char *>(&mPcmBuffer), mPcmBufferPosition);
    }

    // Reset file buffer position
    mPcmBufferPosition = 0;

}

And this is the code that closes the file:

// Stop recording
void CaptureApp::stopRecording()
{

    // Save remaining data
    if (mPcmBufferSize > 0) 
        writeData();

    // Close file
    if (mFile.is_open())
    {
        mFile.flush();
        mFile.close();
    }

    // Turn off recording flag
    mRecording = false;

}

If there’s anything here that looks like it would result in bad data getting appended to the file, please let me know. If not, I’ll triple check the input data (in the callback). This data should be good, because it works if I copy it to a larger buffer (eg, two minutes) and then save that out.

  • 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-20T00:59:41+00:00Added an answer on May 20, 2026 at 12:59 am

    I am just wondering, how

    void CaptureApp::writeData()
    {
        mPcmBufferPosition *= sizeof(int16_t); // mPcmBufferPosition = 0, so 0*2 = 0;
    
    // (...)
        mPcmBufferPosition = 0;
    
    }
    

    works (btw. sizeof int16_t is always 2). Are you setting mPcmBufferPosition somewhere else?

    void CaptureApp::writeData()
    {
    
        // Update header with new PCM length
        long pos = mFile.tellp();
        mPcmBufferBytesToWrite *= 2;
        mPcmTotalSize += mPcmBufferBytesToWrite;
        mFileHeader.bytes = mPcmTotalSize + sizeof(WAVFILEHEADER);
        mFileHeader.pcmbytes = mPcmTotalSize;
    
        mFile.seekp(0);
        mFile.write(reinterpret_cast<char *>(&mFileHeader), sizeof(mFileHeader));
        mFile.seekp(pos);
    
        // Append PCM data
        if (mPcmBufferBytesToWrite > 0)    
            mFile.write(reinterpret_cast<char *>(mPcmBuffer), mPcmBufferBytesToWrite);
    }
    

    Also mPcmBuffer is a pointer, so don’t know why you use & in write.

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

Sidebar

Related Questions

Hey all, I am writing a logviewer application. These logs are between 100-200 megs
hey all, is there any way to convert a given file (this could be
Hey all, weird question. My company has an application from another company that records
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey all this is the first time i am calling a stored procedure via
Hey all of you at Stackoverflow. I got this incredibly annoying problem. I try
Hey all. I've been been trying to figure this out for a while now.
Hey all, just wondering what the best way to add this capability was. I
Hey all- I have looked this up on here and Google but none of
hey all, throwing this one out there... hope it's a simple one. using the

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.