Skip to content

Commit c758929

Browse files
author
전상현
committed
[200_Common] WriteWavFile 함수 추가
1 parent 8ad862a commit c758929

File tree

5 files changed

+79
-7
lines changed

5 files changed

+79
-7
lines changed

Src/200_Common/200_Common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
#include "zip.h"
2525
#include "unzip.h"
2626
#include "RingBuffer.h"
27-
#include "WavFileReader.h"
27+
#include "WavFile.h"

Src/200_Common/200_CommonA.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@
292292
<ClInclude Include="Uncopyable.h" />
293293
<ClInclude Include="unzip.h" />
294294
<ClInclude Include="Utility.h" />
295-
<ClInclude Include="WavFileReader.h" />
295+
<ClInclude Include="WavFile.h" />
296296
<ClInclude Include="zip.h" />
297297
</ItemGroup>
298298
<ItemGroup>
@@ -320,7 +320,7 @@
320320
<ClCompile Include="unzip.cpp" />
321321
<ClCompile Include="Utility.cpp" />
322322
<ClCompile Include="UtilityT.cpp" />
323-
<ClCompile Include="WavFileReader.cpp" />
323+
<ClCompile Include="WavFile.cpp" />
324324
<ClCompile Include="zip.cpp" />
325325
</ItemGroup>
326326
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

Src/200_Common/200_CommonW.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@
293293
<ClInclude Include="Uncopyable.h" />
294294
<ClInclude Include="unzip.h" />
295295
<ClInclude Include="Utility.h" />
296-
<ClInclude Include="WavFileReader.h" />
296+
<ClInclude Include="WavFile.h" />
297297
<ClInclude Include="zip.h" />
298298
<ClInclude Include="zipcommon.h" />
299299
</ItemGroup>
@@ -324,7 +324,7 @@
324324
<ClCompile Include="unzip.cpp" />
325325
<ClCompile Include="Utility.cpp" />
326326
<ClCompile Include="UtilityT.cpp" />
327-
<ClCompile Include="WavFileReader.cpp" />
327+
<ClCompile Include="WavFile.cpp" />
328328
<ClCompile Include="zip.cpp" />
329329
</ItemGroup>
330330
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "stdafx.h"
2-
#include "WavFileReader.h"
2+
#include "WavFile.h"
33
#include "MemoryMappedFile.h"
44

55
namespace core
@@ -70,4 +70,50 @@ namespace core
7070

7171
return EC_SUCCESS;
7272
}
73+
74+
ECODE WriteWavFile(std::tstring strWavFile, const ST_WAVE_FORMATEX& stFormat, const std::vector<unsigned char>& vecPCM)
75+
{
76+
ECODE nRet = EC_SUCCESS;
77+
HANDLE hFile = NULL;
78+
79+
try
80+
{
81+
hFile = CreateFile(strWavFile.c_str(), GENERIC_WRITE_, CREATE_ALWAYS_, 0);
82+
83+
nRet = EC_OPEN_FAILURE;
84+
if (NULL == hFile)
85+
throw exception_format(TEXT("CreateFile(%s) failure, %d"), strWavFile.c_str(), nRet);
86+
87+
DWORD dwWritten = 0;
88+
89+
ST_WAV_HEADER stHeader;
90+
stHeader.dwPayloadSize = 4 + sizeof(ST_WAV_CHUNK_FMT) + sizeof(ST_WAV_CHUNK_DATA) + vecPCM.size();
91+
WriteFile(hFile, &stHeader, sizeof(stHeader), &dwWritten);
92+
93+
ST_WAV_CHUNK_FMT stChunkFmt;
94+
stChunkFmt.wAudioFormat = stFormat.wFormatTag;
95+
stChunkFmt.wNumOfChannel = stFormat.nChannels;
96+
stChunkFmt.dwSampleRate = stFormat.nSamplesPerSec;
97+
stChunkFmt.dwByteRate = stFormat.nAvgBytesPerSec;
98+
stChunkFmt.wBlockAlign = stFormat.nBlockAlign;
99+
stChunkFmt.wBitPerSample = stFormat.wBitsPerSample;
100+
WriteFile(hFile, &stChunkFmt, sizeof(stChunkFmt), &dwWritten);
101+
102+
ST_WAV_CHUNK_DATA stChunkData;
103+
stChunkData.dwChunkSize = vecPCM.size();
104+
WriteFile(hFile, &stChunkData, sizeof(stChunkData), &dwWritten);
105+
WriteFile(hFile, vecPCM.data(), vecPCM.size(), &dwWritten);
106+
107+
CloseFile(hFile);
108+
}
109+
catch (std::exception& e)
110+
{
111+
Log_Error("%s", e.what());
112+
if (hFile)
113+
CloseFile(hFile);
114+
return nRet;
115+
}
116+
117+
return EC_SUCCESS;
118+
}
73119
}
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44

55
namespace core
66
{
7+
#pragma pack(push, 1)
78
struct ST_WAV_HEADER
89
{
910
char szMAGIC[4];
1011
DWORD dwPayloadSize;
1112
char szFormat[4];
13+
14+
ST_WAV_HEADER(void)
15+
: szMAGIC { 'R', 'I', 'F', 'F' }
16+
, dwPayloadSize(0)
17+
, szFormat { 'W', 'A', 'V', 'E' }
18+
{}
1219
};
1320

1421
struct ST_WAV_CHUNK
@@ -27,19 +34,36 @@ namespace core
2734
DWORD dwByteRate; // Average Bytes Per Second
2835
WORD wBlockAlign; // Size of Sample Frame -> dwSampleRate * wBlockAlign == dwByteRate
2936
WORD wBitPerSample; // Bit Depth
37+
38+
ST_WAV_CHUNK_FMT(void)
39+
: szChunkID{ 'f', 'm', 't', ' ' }
40+
, dwChunkSize(16)
41+
{}
3042
};
3143

3244
struct ST_WAV_CHUNK_DATA
3345
{
3446
char szChunkID[4]; // "data"
3547
DWORD dwChunkSize;
48+
49+
ST_WAV_CHUNK_DATA(void)
50+
: szChunkID{ 'd', 'a', 't', 'a' }
51+
, dwChunkSize(0)
52+
{}
3653
};
3754

3855
struct ST_WAV_CHUNK_LIST
3956
{
4057
char szChunkID[4]; // "LIST"
4158
DWORD dwChunkSize;
4259
char szSubType[4]; // "INFO"
60+
61+
62+
ST_WAV_CHUNK_LIST(void)
63+
: szChunkID{ 'L', 'I', 'S', 'T' }
64+
, dwChunkSize(0)
65+
, szSubType{ 'I', 'N', 'F', 'O' }
66+
{}
4367
};
4468

4569
struct ST_WAVE_FORMATEX
@@ -51,9 +75,11 @@ namespace core
5175
WORD nBlockAlign; /* block size of data */
5276
WORD wBitsPerSample; /* number of bits per sample of mono data */
5377
WORD cbSize; /* the count in bytes of the size of */
54-
/* extra information (after cbSize) */
5578
};
79+
#pragma pack(pop)
5680

5781
ECODE ReadWavFile(std::string strWavFile, ST_WAVE_FORMATEX* outFormat, std::vector<unsigned char>& vecPCM);
5882
ECODE ReadWavFile(std::wstring strWavFile, ST_WAVE_FORMATEX* outFormat, std::vector<unsigned char>& vecPCM);
83+
84+
ECODE WriteWavFile(std::tstring strWavFile, const ST_WAVE_FORMATEX& stFormat, const std::vector<unsigned char>& vecPCM);
5985
}

0 commit comments

Comments
 (0)