一个wav文件生成类

 下面是我写的一个wav文件生成类,wav文件的格式参照此链接:

https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

//WAVWriter.h

#pragma once

struct WAVRIFFChunk
{
    char    id[4];      //"RIFF"
    UINT32  size;       //filelen - 8
    char    format[4];  //"WAVE"

    WAVRIFFChunk():size(0)
    {
        memcpy(id, "RIFF", 4);        
        memcpy(format, "WAVE", 4);
    }
};

struct WAVFormatChunk
{
    char    id[4];          //"fmt "
    UINT32  size;           //16 for pcm, sizeof this struct
    UINT16  AudioFormat;    //PCM = 1
    UINT16  NumChannels;    //Mono = 1, Stereo = 2, etc.
    UINT32  SampleRate;     //8000, 44100, etc.
    UINT32  ByteRate;       //== SampleRate * NumChannels * BitsPerSample/8
    UINT16  BlockAlign;     //== NumChannels * BitsPerSample/8
    UINT16  BitsPerSample;  //8 bits = 8, 16 bits = 16, etc.

    WAVFormatChunk():size(16),AudioFormat(1),//fixed
        NumChannels(0),SampleRate(0),ByteRate(0),BlockAlign(0),BitsPerSample(0)
    {
        memcpy(id, "fmt ", 4);
    }
};

struct WAVDataChunk
{
    char    id[4];          //"data"
    UINT32  size;           //== NumSamples * NumChannels * BitsPerSample/8

    WAVDataChunk():size(0)
    {
        memcpy(id, "data", 4);
    }
};

class CWAVWriter
{
    HANDLE m_hFile;

    //information
    WAVRIFFChunk    m_Riff;
    WAVFormatChunk  m_Format;
    WAVDataChunk    m_Data;

public:
    CWAVWriter(void);
    ~CWAVWriter(void);

    bool Open(const char* lpFile, UINT16 NumChannels, UINT32 SampleRate, UINT16  BitsPerSample);
    bool WriteASample(UINT8* buf, UINT32 len);
    void Close();
};

//WAVWriter.cpp
#include "StdAfx.h"
#include "WAVWriter.h"

CWAVWriter::CWAVWriter(void):m_hFile(NULL)
{
}

CWAVWriter::~CWAVWriter(void)
{
    Close();
}

bool CWAVWriter::Open(const char* lpFile, UINT16 NumChannels, UINT32 SampleRate, UINT16  BitsPerSample)
{
    m_Format.NumChannels = NumChannels;
    m_Format.SampleRate = SampleRate;
    m_Format.BitsPerSample = BitsPerSample;

    m_Format.ByteRate = SampleRate * NumChannels * BitsPerSample/8;
    m_Format.BlockAlign = NumChannels*BitsPerSample/8;

    if (!lpFile)
        return false;

    m_hFile = ::CreateFile(lpFile, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (!m_hFile)
        return false;

    ::SetFilePointer(m_hFile, sizeof(m_Riff)+sizeof(m_Format)+sizeof(m_Data), NULL, FILE_BEGIN);

    return true;    
}

bool CWAVWriter::WriteASample(UINT8* buf, UINT32 len)
{
    if (!buf || !len)
        return false;

    DWORD dwWritten;
    if (!WriteFile(m_hFile, buf, len, &dwWritten, NULL))
        return false;

    m_Riff.size += len;
    m_Data.size += len;
    return true;
}

void CWAVWriter::Close()
{
    if (m_hFile)
    {
        if (INVALID_SET_FILE_POINTER != ::SetFilePointer(m_hFile, 0, NULL, FILE_BEGIN))
        {
            DWORD dwWritten;
            WriteFile(m_hFile, &m_Riff, sizeof(m_Riff), &dwWritten, NULL);
            WriteFile(m_hFile, &m_Format, sizeof(m_Format), &dwWritten, NULL);
            WriteFile(m_hFile, &m_Data, sizeof(m_Data), &dwWritten, NULL);
        }

        ::CloseHandle(m_hFile);
        m_hFile = NULL;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值