Chinaunix首页 | 论坛 | 博客
  • 博客访问: 929421
  • 博文数量: 201
  • 博客积分: 8078
  • 博客等级: 中将
  • 技术积分: 2162
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-20 17:22
文章分类

全部博文(201)

文章存档

2013年(3)

2012年(11)

2011年(34)

2010年(25)

2009年(51)

2008年(77)

分类: WINDOWS

2009-09-07 21:21:09

// waveSample.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#include <mmsystem.h>

static const int HDRCOUNT = 10;

int main(int argc, char* argv[])
{
    int i;    
    WAVEHDR *pWaveHdr[HDRCOUNT];
    WAVEFORMATEX waveformat;     
    waveformat.wFormatTag=WAVE_FORMAT_PCM;     
    waveformat.nChannels=1;     
    waveformat.nSamplesPerSec=48000;     
    waveformat.nAvgBytesPerSec=48000;     
    waveformat.nBlockAlign=1;     
    waveformat.wBitsPerSample=8; //指定录音格式     
    waveformat.cbSize=0;
    HWAVEIN hWaveIn;
    int error=waveInOpen(&hWaveIn,WAVE_MAPPER, &waveformat, (DWORD)NULL,0L,CALLBACK_NULL);
    if (error != MMSYSERR_NOERROR){
        printf("Exit8\n");
        return -1;
    }

    for (i = 0; i < HDRCOUNT; i++){
        pWaveHdr[i]= (WAVEHDR*)malloc( 24*1024 + sizeof(WAVEHDR) );
        if (pWaveHdr[i] == NULL){
            return -1;
        }
        ZeroMemory( pWaveHdr[i], sizeof(WAVEHDR) );    
        pWaveHdr[i]->dwBufferLength = 24*1024;
        pWaveHdr[i]->lpData = (LPSTR)(pWaveHdr[i] + 1);
        error=waveInPrepareHeader(hWaveIn, pWaveHdr[i], sizeof(WAVEHDR));
        if (error != MMSYSERR_NOERROR){
            printf("Exit7\n");
            return -1;
        }
        error=waveInAddBuffer(hWaveIn, pWaveHdr[i], sizeof(WAVEHDR));
        if (error != MMSYSERR_NOERROR){
            printf("Exit6\n");
            return -1;
        }
    }
    
    error=waveInStart(hWaveIn);
    if (error != MMSYSERR_NOERROR){
        printf("Exit5\n");
        return -1;
    }
    
    int nhdr = 0;
    FILE *fp = fopen("hello.wav", "wb");
    fwrite("RIFF", 1, 4, fp);

    unsigned int dwDataLength = 1024*1024;
    unsigned int waveformatSize = sizeof WAVEFORMATEX;
    unsigned int Sec=(1024*1024 + waveformatSize);
    fwrite(&Sec,sizeof(Sec), 1, fp);
    fwrite("WAVE",4, 1, fp);
    fwrite("fmt ", 1, 4, fp);
    fwrite(&waveformatSize, sizeof(waveformatSize), 1, fp);
    fwrite(&waveformat.wFormatTag,sizeof(waveformat.wFormatTag), 1, fp);
    fwrite(&waveformat.nChannels,sizeof(waveformat.nChannels), 1, fp);
    fwrite(&waveformat.nSamplesPerSec,sizeof(waveformat.nSamplesPerSec), 1, fp);
    fwrite(&waveformat.nAvgBytesPerSec,sizeof(waveformat.nAvgBytesPerSec), 1, fp);
    fwrite(&waveformat.nBlockAlign,sizeof(waveformat.nBlockAlign), 1, fp);
    fwrite(&waveformat.wBitsPerSample,sizeof(waveformat.wBitsPerSample), 1, fp);
    fwrite(&waveformat.cbSize,sizeof(waveformat.cbSize), 1, fp);
    fwrite("data",4, 1, fp);
    fwrite(&dwDataLength,sizeof(dwDataLength), 1, fp);

    for(;;){
        printf("goto sleep\n");
        Sleep(500);
        while (pWaveHdr[nhdr]->dwFlags & WHDR_DONE){
            printf("do some output: %d!\n", nhdr);
            int offset, writed=0;
            int length = pWaveHdr[nhdr]->dwBytesRecorded;
            for (offset = 0; writed >= 0 && offset < length; offset += writed){
                writed = fwrite(pWaveHdr[nhdr]->lpData+offset, 1, length-offset, fp);
                printf("write: %d\n", writed);
            }
            error = waveInAddBuffer(hWaveIn, pWaveHdr[nhdr], sizeof(WAVEHDR));            
            if (error != MMSYSERR_NOERROR){
                printf("Exit4dd\n");
                return -1;
            }
            nhdr = (nhdr+1)%HDRCOUNT;
        }
    }
    fclose(fp);
    
    error=waveInStop(hWaveIn);
    if (error != MMSYSERR_NOERROR){
        printf("Exit3\n");
        return -1;
    }

    error=waveInReset(hWaveIn);
    if (error != MMSYSERR_NOERROR){
        printf("Exit2\n");
        return -1;
    }
    
    for (i = 0; i < HDRCOUNT; i++){
        error=waveInUnprepareHeader(hWaveIn, pWaveHdr[i], sizeof(WAVEHDR));
        if (error != MMSYSERR_NOERROR){
            printf("Exit1\n");
            return -1;
        }
        free(pWaveHdr[i]);
    }

    error=waveInClose(hWaveIn);
    if (error != MMSYSERR_NOERROR){
        printf("Exit0\n");
        return -1;
    }
    return 0;
}

阅读(7005) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~