Simple record, playback and save as WAV file program

Source: Internet
Author: User
Tags header prepare reserved knowledge base

Introduction

I am a beginner of C + +, the introduction all depend on vckbase, fortunately there are a lot of examples suitable for beginners, let me take a lot less detours, in order to give back to everyone, I have recently completed a simple small program to provide to everyone, so that those who have been wandering in the C + + gate with the same people can quickly grasp the essentials, You may wish to take a look at it.

This paper is based on the VC knowledge Base, Mr. Yi Yiming's "audio recorder program based on API" in the 26th phase, thanks! The same place no longer repeat, I added on this basis to save the recording as WAV file format, easy to reference.

Basic steps and ideas: Set the audio acquisition parameters (sampling rate, channel, etc.), open the audio device, prepare wave data headers and open the cache, operation of collected data and saved as WAV files. Set audio playback parameters, turn on playback devices, prepare wave headers, and write wave data. Additional sample procedures should include #include <mmsystem.h> and #pragma comment (lib, "winmm". LIB ") Multimedia support.

Before introducing the program, you need to understand the format of the wave file and some basic concepts, which can be found in MSDN, for the convenience of understanding, we will organize it, if you have knowledge of these basics, you can skip.

Concept 1, define waveform data format

typedef struct{WORD wFormatTag;
   WORD nChannels;
   DWORD nSamplesPerSec;
   DWORD nAvgBytesPerSec;
   WORD nBlockAlign;
   WORD wBitsPerSample;
   WORD cbSize; } WAVEFORMATEX;

The specific parameters are explained as follows:

wFormatTag: Format of waveform data, defined in MMREG.H file

Nchannels: Channel number of waveform data: Mono or stereo

Nsamplespersec: Sampling rate, for PCM format waveform data, sample rate has 8.0 khz,11.025khz,22.05 khz,44.1 kHz, etc.

NAVGBYTESPERSEC: Data rate, for PCM format waveform data, the data rate is equal to the sample rate multiplied by the number of bytes per sample point

nBlockAlign: Number of bytes per sample

wBitsPerSample: Sampling accuracy, for PCM format waveform data, sampling accuracy of 8 or 16

cbsize: Data block size with additional format information

Concept 2, define device header structure

WAVEHDR defines the device header that points to the waveform data buffer.

typedef struct { LPSTR lpData;
  DWORD dwBufferLength;
  DWORD dwBytesRecorded;
  DWORD dwUser;
  DWORD dwFlags;
  DWORD dwLoops;
  struct wavehdr_tag * lpNext;
  DWORD reserved; } WAVEHDR;

Lpdata: Buffer address for waveform data

Dwbufferlength: The length of the buffer address of the waveform data

Dwbytesrecorded: When the device is used for recording, the data length that the mark has entered

Dwuser: User Data

Dwflags: Properties of the buffer for waveform data

Dwloops: The number of playback loops, only for playback control

Lpnext and reserved are reserved values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.