// срр файл
#include "sound_2.h"
using namespace std;
int fact;
#define N2 44100
//WAVEFORMATEX pcmWaveFormat;
DWORD r_buff[1024];
WAVEHDR whdr;
HWAVEIN h_out;
HMMIO H_IN;
bool Param_record=0;
MMCKINFO ckOutRIFF;
MMCKINFO ckOut;
PCMWAVEFORMAT pcmWaveFormat;
MMIOINFO mmioinfoOut;
long lSamples; // number of samples
sound_2::sound_2(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
ui.setupUi(this);
CreateWaveFile();
Tim=new QTimer(this);
connect(Tim,SIGNAL(timeout()),SLOT(WriteDataToFile()));
Tim->start(200);
}
sound_2::~sound_2()
{
}
void sound_2::func(bool param)
{
MMRESULT MRez;
MRez=waveInOpen( (LPHWAVEIN)&h_out, WAVE_MAPPER,(LPCWAVEFORMATEX)&pcmWaveFormat, NULL, NULL, CALLBACK_THREAD);
char* lpDst;
ZeroMemory(&whdr,sizeof(whdr));
lpDst=new char[1024];
whdr.lpData=(LPSTR)lpDst;
whdr.dwFlags= WHDR_BEGINLOOP;
for(int i=0;i<3;i++)
{
waveInPrepareHeader (h_out,&whdr,sizeof(whdr));// подготовка буферов в которые будет записываться информация с микрофона
waveInAddBuffer(h_out,&whdr,sizeof(whdr));
}
// H_IN=mmioOpen(L"Sound1.wav",NULL,MMIO_CREATE|MMIO_WRITE|MMIO_ALLOCBUF );
//mmioWrite(H_IN,lpDst,sizeof(lpDst));
//waveInUnprepareHeader(h_out,&whdr,sizeof(whdr));
// mmioClose(H_IN,NULL);
}
void sound_2::WriteDataToFile()
{
if (Param_record==1)
{
waveInUnprepareHeader(h_out,&whdr,sizeof(whdr));
writeWaveFile();
waveInPrepareHeader (h_out,&whdr,sizeof(whdr));
waveInAddBuffer(h_out,&whdr,sizeof(whdr));
}
}
void sound_2::CreateWaveFile()// Создание контейнеров звукового файла
{
H_IN=mmioOpen(L"Sound1.wav",NULL,MMIO_CREATE|MMIO_WRITE|MMIO_ALLOCBUF );
// Write the PCMWAVEFORMAT structure
//----------------------------------------------
pcmWaveFormat.wf.wFormatTag=WAVE_FORMAT_PCM;
pcmWaveFormat.wf.nChannels=1;
pcmWaveFormat.wf.nBlockAlign=2;
pcmWaveFormat.wf.nSamplesPerSec=N2;
pcmWaveFormat.wf.nAvgBytesPerSec=N2*2;
pcmWaveFormat.wBitsPerSample=16;
//----------------------------------------------
if (H_IN == NULL)
cout<<"error opening out file "<<endl;
// Write the PCMWAVEFORMAT structure
ckOutRIFF.fccType = mmioFOURCC('W', 'A', 'V', 'E');
if (mmioCreateChunk(H_IN, &ckOutRIFF, MMIO_CREATERIFF) != 0)
cout<<"error creating out file wave "<<endl;
ckOut.ckid = mmioFOURCC('f', 'm', 't', ' ');
if (mmioCreateChunk(H_IN, &ckOut, 0) != 0)
cout<<"error creating out file fmt "<<endl;
if (mmioWrite(H_IN, (HPSTR) &pcmWaveFormat, sizeof(pcmWaveFormat))
!= sizeof(pcmWaveFormat))
cout<<"error writing size of fmt "<<endl; // cannot write file, probably
if (mmioAscend(H_IN, &ckOut, 0) != 0)
cout<<"error writing size of fmt 2 "<<endl; // cannot write file, probably
ckOut.ckid = mmioFOURCC('d', 'a', 't', 'a');
if (mmioCreateChunk(H_IN, &ckOut, 0) != 0)
cout<<"error creating out file data "<<endl;
}
void sound_2::writeWaveFile()
{
mmioWrite(H_IN,whdr.lpData,sizeof(whdr.lpData));// запись информации в файл
/*for (int lSamples =2*N2; lSamples > 0; lSamples--)
{
if (mmioinfoOut.pchNext == mmioinfoOut.pchEndWrite)
{
mmioinfoOut.dwFlags |= MMIO_DIRTY;
if (mmioAdvance(hmmioOut, &mmioinfoOut, MMIO_WRITE) != 0)
cout<<"error write "<<endl;
}
*(mmioinfoOut.pchNext)++ =pDataEcho[2*N2-lSamples];
}*/
}
void sound_2::on_pushButton_clicked(bool nn)// Старт кнопка
{
if (nn==true)
{
func(true);
waveInStart(h_out);
Param_record=1;
}
}
void sound_2::on_pushButton_2_clicked()// Стоп кнопка
{
Param_record=0;
waveInReset(h_out);
if (mmioGetInfo(H_IN, &mmioinfoOut, 0) != 0)
cout<<"error writing data in file "<<endl;
ckOut.cksize = sizeof(pcmWaveFormat)*10;
mmioinfoOut.dwFlags |= MMIO_DIRTY;
if (mmioSetInfo(H_IN, &mmioinfoOut, 0) != 0)
cout<<"error end "<<endl;
if (mmioAscend(H_IN, &ckOut, 0) != 0)
cout<<"error end "<<endl;
if (mmioAscend(H_IN, &ckOutRIFF, 0) != 0)
cout<<"error end "<<endl;
if (H_IN != NULL)
mmioClose(H_IN, 0);
waveInClose(h_out);
}
//*********************************
// h файл
#ifndef SOUND_2_H
#define SOUND_2_H
#include <QtGui/QWidget>
#include "ui_sound_2.h"
#include "Windows.h"
#include "mmsystem.h"
#include <iostream>
#include <QFile>
#include <QTimer>
class sound_2 : public QWidget
{
Q_OBJECT
public:
sound_2(QWidget *parent = 0, Qt::WFlags flags = 0);
~sound_2();
QTimer *Tim;
void func(bool param);
void CreateWaveFile();
void writeWaveFile();
private:
Ui::sound_2Class ui;
WAVEFORMATEX wf;
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked(bool nn);
void WriteDataToFile();
};
#endif // SOUND_2_H