-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyMMFile.cpp
128 lines (107 loc) · 4.45 KB
/
MyMMFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "MyMMFile.h"
//---------------------------------------------------------------------------
// Constructor + Destructor
//---------------------------------------------------------------------------
MyMMFile :: MyMMFile(pPARAMS pParams,TCHAR* szFileName){
hMMFileW=NULL;
params=*pParams;
wfxC.cbSize= sizeof(WAVEFORMATEX);
wfxC.wFormatTag= WAVE_FORMAT_PCM;
wfxC.nChannels= params.iNumberOfChannels;
wfxC.nSamplesPerSec= params.iSampleRate;
wfxC.wBitsPerSample= params.iBitsPerSample;
wfxC.nBlockAlign= (wfxC.nChannels*wfxC.wBitsPerSample)/8;
wfxC.nAvgBytesPerSec= wfxC.nBlockAlign*wfxC.nSamplesPerSec;
OpenMMFileWrite(szFileName);
WriteHeader();
}
MyMMFile :: ~MyMMFile(){
CloseMMFile();
}
//---------------------------------------------------------------------------
// Opens file for Writing
//---------------------------------------------------------------------------
BOOL MyMMFile :: OpenMMFileWrite(TCHAR *szFileName){
if(mmioOpen(szFileName, NULL, MMIO_EXIST)) //if the file exists
mmioOpen(szFileName, NULL, MMIO_DELETE); //delete it
if(NULL==(hMMFileW=mmioOpen(szFileName, NULL, MMIO_CREATE | MMIO_WRITE))){
MessageBox(params.hwnd, TEXT("Error opening/creating file for writing"), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
CloseMMFile();
return 0;
}
return 1;
}
//---------------------------------------------------------------------------
// Writes the Header to the MMFile
//---------------------------------------------------------------------------
BOOL MyMMFile :: WriteHeader(){
MMCKINFO chunkInf;//chunk information
HRESULT hr;
chunkInf.fccType=mmioStringToFOURCC("WAVE",0); //creating the Main Chunk
chunkInf.cksize=36+wfxC.nAvgBytesPerSec*params.iFileSizeInSeconds;
if(MMSYSERR_NOERROR!=mmioCreateChunk(hMMFileW, &chunkInf, MMIO_CREATERIFF)){
MessageBox(params.hwnd, TEXT("Error creating RIFF chunk"), TEXT("Error"), MB_OK);
CloseMMFile();
return 0;
}
chunkInf.ckid=mmioStringToFOURCC("fmt ",0);
chunkInf.cksize=sizeof(wfxC);
//Creating the first subchunk-"fmt"
hr=mmioCreateChunk(hMMFileW, &chunkInf,0);
if(hr==MMIOERR_CANNOTSEEK){
MessageBox(params.hwnd, TEXT("Error creating fmt chunk: MMIOERR_CANNOTSEEK"), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
CloseMMFile();
return 0;
}
if(hr==MMIOERR_CANNOTWRITE){
MessageBox(params.hwnd, TEXT("Error creating fmt chunk: MMIOERR_CANNOTWRITE"), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
CloseMMFile();
return 0;
}
if(mmioWrite(hMMFileW, (HPSTR)&wfxC, sizeof(WAVEFORMATEX))==-1){
MessageBox(params.hwnd, TEXT("Error writing wfxC structure to fmt chunk."), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
CloseMMFile();
return 0;
}
if(MMSYSERR_NOERROR!=mmioAscend(hMMFileW, &chunkInf, 0)){//Ascending from fmt chunk
MessageBox(params.hwnd, TEXT("Error when Ascending from fmt chunk"), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
CloseMMFile();
return 0;
}
chunkInf.ckid=mmioStringToFOURCC("data",0); //Creating the second subchunk-"data"
chunkInf.cksize=wfxC.nAvgBytesPerSec*params.iFileSizeInSeconds;
hr=mmioCreateChunk(hMMFileW, &chunkInf,0);
if(hr==MMIOERR_CANNOTSEEK){
MessageBox(params.hwnd, TEXT("Error creating fmt chunk: MMIOERR_CANNOTSEEK"), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
CloseMMFile();
return 0;
}
if(hr==MMIOERR_CANNOTWRITE){
MessageBox(params.hwnd, TEXT("Error creating fmt chunk: MMIOERR_CANNOTWRITE"), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
CloseMMFile();
return 0;
}
return 1;
}
//---------------------------------------------------------------------------
// Appends data to MMfile(used in ReadFrom SoundCard)
//---------------------------------------------------------------------------
BOOL MyMMFile :: AppendDataToFile(signed char *lpDataWrite){
//mmioSeek(hMMFileW, 0, SEEK_END);//Appends any data to the end of the file
if(mmioWrite(hMMFileW, (HPSTR)lpDataWrite, wfxC.nAvgBytesPerSec/2)==-1){
MessageBox(params.hwnd, TEXT("Error when writing sound data to file"), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
CloseMMFile();
return 0;
}
return 1;
}
//---------------------------------------------------------------------------
// Close MultiMEdia FIle
//---------------------------------------------------------------------------
BOOL MyMMFile :: CloseMMFile(){
if(mmioClose(hMMFileW,NULL)!=0){//close the old file
MessageBox(params.hwnd, TEXT("Error closing file"), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
return 0;
}
return 1;
}