先行根本知识见此
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
来源:https://blog.csdn.net/bcbobo21cn/article/details/112001540
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |