先行基本知识见此
https://blog.csdn.net/bcbobo21cn/article/details/109087252
win7, vc6;新建一个对话框工程;添加一个文本框;为文本框添加一个对话框类的成员变量;文本框用来输入一些数据;
对话框cpp文件头部加入;
#include
#include
......
/*
typedef struct{
WORD wFormatTag;
WORD nChannels;
DWORD nSamplesPerSec;
DWORD nAvgBytesPerSec;
WORD nBlockAlign;
WORD wBitsPerSample;
WORD cbSize; } WAVEFORMATEX;
typedef struct {
LPSTR lpData;
DWORD dwBufferLength;
DWORD dwBytesRecorded;
DWORD dwUser;
DWORD dwFlags;
DWORD dwLoops;
struct wavehdr_tag * lpNext;
DWORD reserved; } WAVEHDR;
*/
这两个结构体不用自己定义;包含前面两个头文件就有了;
#pragma comment(lib, "winmm.lib"),引入音频操作库;
WAVEFORMATEX waveform; 定义格式结构体变量;
添加一个按钮;按钮处理代码如下;
void CWavtestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
DWORD dwDataLength = 50;
CFile m_file;
CFileException fileException;
CString m_csFileName= "S:\\testaudio.wav";//保存路径
m_file.Open(m_csFileName,CFile::modeCreate|CFile::modeReadWrite, &fileException);
DWORD m_WaveHeaderSize = 38;
DWORD m_WaveFormatSize = 18;
m_file.SeekToBegin();
m_file.Write("RIFF",4);
//unsigned int Sec=(sizeof pSaveBuffer + m_WaveHeaderSize);
unsigned int Sec=(50 + m_WaveHeaderSize);
m_file.Write(&Sec,sizeof(Sec));
m_file.Write("WAVE",4);
m_file.Write("fmt ",4);
m_file.Write(&m_WaveFormatSize,sizeof(m_WaveFormatSize));
m_file.Write(&waveform.wFormatTag,sizeof(waveform.wFormatTag));
m_file.Write(&waveform.nChannels,sizeof(waveform.nChannels));
m_file.Write(&waveform.nSamplesPerSec,sizeof(waveform.nSamplesPerSec));
m_file.Write(&waveform.nAvgBytesPerSec,sizeof(waveform.nAvgBytesPerSec));
m_file.Write(&waveform.nBlockAlign,sizeof(waveform.nBlockAlign));
m_file.Write(&waveform.wBitsPerSample,sizeof(waveform.wBitsPerSample));
m_file.Write(&waveform.cbSize,sizeof(waveform.cbSize));
m_file.Write("data",4);
m_file.Write(&dwDataLength,sizeof(dwDataLength));
UpdateData(TRUE);
char* pchar=new char[100];
pchar=m_wavdata.GetBuffer(m_wavdata.GetLength());
m_file.Write(pchar,dwDataLength);
m_file.Seek(dwDataLength,CFile::begin);
m_file.Close();
}
UpdateData(TRUE);
char* pchar=new char[100];
pchar=m_wavdata.GetBuffer(m_wavdata.GetLength());
m_file.Write(pchar,dwDataLength);
获取文本框数据,转换为char *类型,通过调用CString的GetBuffer函数;因为文件的Write函数的第一个参数是void * 类型,char * 可直接写入;
运行程序;输入一堆111...;点保存按钮;看下能不能把输入的数据保存为音频wav文件;
保存以后,磁盘上出来一个wav文件;
但是不能播放;
可能保存的格式,什么地方不对;
先大体看一下;
m_file.Write("RIFF",4); 文件中先写入"RIFF"四个字符,没错,头四个字节是固定的,文件标识符;
//unsigned int Sec=(sizeof pSaveBuffer + m_WaveHeaderSize);
unsigned int Sec=(50 + m_WaveHeaderSize);
m_file.Write(&Sec,sizeof(Sec)); 然后写入一个4字节,是一个尺寸(长度)值,可能是数据加文件头部的长度;
m_file.Write("WAVE",4); 然后写入"WAVE"四个字符,没错;
m_file.Write("fmt ",4); 然后写入"fmt "四个字节,感觉到这里可能有问题;
往下没时间细看了;先到此;
找到2个wave文件解析和编程详细些的资料,记一下先;
https://www.cnblogs.com/lidabo/p/3701959.html
https://www.cnblogs.com/tocy/p/WAV_file-format.html