Skip to content

Commit 5553c7c

Browse files
committed
Poretd from CSharp to CPP
2 parents 8abdefa + 9466caa commit 5553c7c

File tree

11 files changed

+778
-36
lines changed

11 files changed

+778
-36
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
##
44
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
55

6+
# Tester
7+
WaveFileTester/
8+
9+
# Assembly
10+
AssemblyInfo.cs
11+
612
# User-specific files
713
*.suo
814
*.user

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# WaveFileManagerLibrary
2+
Using this library, you can create simple wave file quickly.
3+
4+
## Description
5+
6+
With this you can write code to quickly create audio files. You can create audio files without struggle with binary files.
7+
Even if you are not good at file operations, you can write code intuitively.
8+
Since this library was originally developed for C#, there may be problems with the porting process to C++.
9+
10+
## Requirement
11+
12+
C# library depends on .NET framework.
13+
14+
## Usage
15+
16+
```C++
17+
//#include "WaveFileManager.h"
18+
WaveFileManager waveFile;
19+
MusicPropertyMonaural16bit prop16bit;
20+
short soundData[44100 * 2];
21+
//Generate sound data (440Hz)
22+
GenerateSoundMonaural16bits(soundData, 440);
23+
prop16bit = GenerateMonaural16bits(soundData, 44100 * 2);
24+
waveFile.CreateFile("C:\\capra.wav", prop16bit);
25+
```
26+
27+
## Install
28+
29+
Go to [ReleasePage](https://github.com/capra314cabra/WaveFileManagerLibrary/releases) or Download [Code](https://github.com/capra314cabra/WaveFileManagerLibrary)
30+
31+
## Contribution
32+
33+
1. Fork it
34+
2. Create your feature branch (git checkout -b my-new-feature)
35+
3. Commit your changes (git commit -am 'Add some feature')
36+
4. Push to the branch (git push origin my-new-feature)
37+
5. Create new Pull Request
38+
39+
## Licence
40+
41+
[MIT](https://github.com/capra314cabra/WaveFileManagerLibrary/blob/master/LICENSE)
42+
43+
## Author
44+
45+
[capra314cabra](https://github.com/capra314cabra)

WaveFileManager.sln

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ VisualStudioVersion = 15.0.27428.2043
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WaveFileManager", "WaveFileManager\WaveFileManager.csproj", "{73248112-9ED2-4305-9133-9259E3C78CB4}"
77
EndProject
8+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WaveFileManagerCPP", "WaveFileManagerCPP\WaveFileManagerCPP.vcxproj", "{B6D0C2CA-B876-47DE-84FB-AAA07E546E4B}"
9+
EndProject
10+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WaveFileTester", "WaveFileTester\WaveFileTester.vcxproj", "{222A08F3-968F-4FB5-91EC-832B53025EE5}"
11+
EndProject
812
Global
913
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1014
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +31,26 @@ Global
2731
{73248112-9ED2-4305-9133-9259E3C78CB4}.Release|x64.Build.0 = Release|Any CPU
2832
{73248112-9ED2-4305-9133-9259E3C78CB4}.Release|x86.ActiveCfg = Release|Any CPU
2933
{73248112-9ED2-4305-9133-9259E3C78CB4}.Release|x86.Build.0 = Release|Any CPU
34+
{B6D0C2CA-B876-47DE-84FB-AAA07E546E4B}.Debug|Any CPU.ActiveCfg = Debug|Win32
35+
{B6D0C2CA-B876-47DE-84FB-AAA07E546E4B}.Debug|x64.ActiveCfg = Debug|x64
36+
{B6D0C2CA-B876-47DE-84FB-AAA07E546E4B}.Debug|x64.Build.0 = Debug|x64
37+
{B6D0C2CA-B876-47DE-84FB-AAA07E546E4B}.Debug|x86.ActiveCfg = Debug|Win32
38+
{B6D0C2CA-B876-47DE-84FB-AAA07E546E4B}.Debug|x86.Build.0 = Debug|Win32
39+
{B6D0C2CA-B876-47DE-84FB-AAA07E546E4B}.Release|Any CPU.ActiveCfg = Release|Win32
40+
{B6D0C2CA-B876-47DE-84FB-AAA07E546E4B}.Release|x64.ActiveCfg = Release|x64
41+
{B6D0C2CA-B876-47DE-84FB-AAA07E546E4B}.Release|x64.Build.0 = Release|x64
42+
{B6D0C2CA-B876-47DE-84FB-AAA07E546E4B}.Release|x86.ActiveCfg = Release|Win32
43+
{B6D0C2CA-B876-47DE-84FB-AAA07E546E4B}.Release|x86.Build.0 = Release|Win32
44+
{222A08F3-968F-4FB5-91EC-832B53025EE5}.Debug|Any CPU.ActiveCfg = Debug|Win32
45+
{222A08F3-968F-4FB5-91EC-832B53025EE5}.Debug|x64.ActiveCfg = Debug|x64
46+
{222A08F3-968F-4FB5-91EC-832B53025EE5}.Debug|x64.Build.0 = Debug|x64
47+
{222A08F3-968F-4FB5-91EC-832B53025EE5}.Debug|x86.ActiveCfg = Debug|Win32
48+
{222A08F3-968F-4FB5-91EC-832B53025EE5}.Debug|x86.Build.0 = Debug|Win32
49+
{222A08F3-968F-4FB5-91EC-832B53025EE5}.Release|Any CPU.ActiveCfg = Release|Win32
50+
{222A08F3-968F-4FB5-91EC-832B53025EE5}.Release|x64.ActiveCfg = Release|x64
51+
{222A08F3-968F-4FB5-91EC-832B53025EE5}.Release|x64.Build.0 = Release|x64
52+
{222A08F3-968F-4FB5-91EC-832B53025EE5}.Release|x86.ActiveCfg = Release|Win32
53+
{222A08F3-968F-4FB5-91EC-832B53025EE5}.Release|x86.Build.0 = Release|Win32
3054
EndGlobalSection
3155
GlobalSection(SolutionProperties) = preSolution
3256
HideSolutionNode = FALSE

WaveFileManager/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

WaveFileManagerCPP/AssistFunc.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include "DefineClass.h"
4+
5+
#define _USE_MATH_DEFINES
6+
#include<math.h>
7+
8+
// Return sounds data (length : samplesPerSec * 2)
9+
void GenerateSoundMonaural16bits(Int16* sound, double herth, int samplesPerSec = 44100)
10+
{
11+
for (int i = 0; i < samplesPerSec * 2; i++)
12+
{
13+
sound[i] = ((short)(sin(i / (double)samplesPerSec * 2 * M_PI * herth) * 30000));
14+
}
15+
}

WaveFileManagerCPP/CreateInstance.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
3+
#include "DefineClass.h"
4+
5+
MusicPropertyMonaural16bit GenerateMonaural16bits(Int16* data, UInt32 length)
6+
{
7+
MusicPropertyMonaural16bit w;
8+
9+
w.m_FileSize = 36 + (sizeof(Int16) * length);
10+
w.m_PCMWAVEFORMAT_Size = 16;
11+
WAVEFORMATEX waveformat;
12+
w.m_WaveFormatEx = waveformat.GetMonaural16bitsDefault();
13+
MusicDataMonaural16bit data16bit;
14+
data16bit.m_DataSize = (sizeof(Int16) * length);
15+
std::vector<Int16> v;
16+
v.assign(data, data + length);
17+
data16bit.m_Data = v;
18+
w.m_MusicData = data16bit;
19+
20+
return w;
21+
}
22+
23+
MusicPropertyMonaural8bit GenerateMonaural8bits(Int8* data, UInt32 length)
24+
{
25+
MusicPropertyMonaural8bit w;
26+
27+
w.m_FileSize = 36 + (sizeof(Int8) * length);
28+
w.m_PCMWAVEFORMAT_Size = 16;
29+
WAVEFORMATEX waveformat;
30+
w.m_WaveFormatEx = waveformat.GetMonaural8bitsDefault();
31+
MusicDataMonaural8bit data8bit;
32+
data8bit.m_DataSize = (sizeof(Int8) * length);
33+
std::vector<Int8> v;
34+
v.assign(data, data + length);
35+
data8bit.m_Data = v;
36+
w.m_MusicData = data8bit;
37+
38+
return w;
39+
}

WaveFileManagerCPP/DefineClass.h

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#pragma once
2+
3+
#include<vector>
4+
#include<string>
5+
6+
using namespace std;
7+
8+
typedef int Int32;
9+
typedef short Int16;
10+
typedef char Int8;
11+
typedef unsigned int UInt32;
12+
typedef unsigned short UInt16;
13+
14+
class WAVEFORMATEX
15+
{
16+
public:
17+
Int16 wFormatTag;
18+
UInt16 nChannels;
19+
UInt32 nSamplesPerSec;
20+
UInt32 nAvgBytePerSec;
21+
UInt16 nBlockAlign;
22+
UInt16 wBitsPerSample;
23+
24+
/// <summary>
25+
/// Monaural 16bits 44100Hz
26+
/// </summary>
27+
WAVEFORMATEX GetMonaural16bitsDefault();
28+
29+
/// <summary>
30+
/// Monaural 8bits 44100Hz
31+
/// </summary>
32+
WAVEFORMATEX GetMonaural8bitsDefault();
33+
};
34+
35+
#pragma region WAVEFORMATEX Implement
36+
37+
WAVEFORMATEX WAVEFORMATEX::GetMonaural16bitsDefault()
38+
{
39+
WAVEFORMATEX format;
40+
format.wFormatTag = 1;
41+
format.nChannels = 1;
42+
format.nSamplesPerSec = 44100;
43+
format.nAvgBytePerSec = 88200;
44+
format.nBlockAlign = 2;
45+
format.wBitsPerSample = 16;
46+
return format;
47+
}
48+
49+
WAVEFORMATEX WAVEFORMATEX::GetMonaural8bitsDefault()
50+
{
51+
WAVEFORMATEX format;
52+
format.wFormatTag = 1;
53+
format.nChannels = 1;
54+
format.nSamplesPerSec = 44100;
55+
format.nAvgBytePerSec = 44100;
56+
format.nBlockAlign = 1;
57+
format.wBitsPerSample = 8;
58+
return format;
59+
}
60+
61+
#pragma endregion
62+
63+
struct MusicDataMonaural16bit
64+
{
65+
public:
66+
Int32 m_DataSize;
67+
vector<Int16> m_Data;
68+
};
69+
70+
struct MusicDataMonaural8bit
71+
{
72+
public:
73+
Int32 m_DataSize;
74+
vector<Int8> m_Data;
75+
};
76+
77+
class MusicProperty
78+
{
79+
public:
80+
Int32 m_FileSize;
81+
Int32 m_PCMWAVEFORMAT_Size;
82+
WAVEFORMATEX m_WaveFormatEx;
83+
};
84+
85+
class MusicPropertyMonaural16bit : public MusicProperty
86+
{
87+
public:
88+
MusicDataMonaural16bit m_MusicData;
89+
};
90+
91+
class MusicPropertyMonaural8bit : public MusicProperty
92+
{
93+
public:
94+
MusicDataMonaural8bit m_MusicData;
95+
};
96+
97+
class WaveFileManager
98+
{
99+
public:
100+
MusicPropertyMonaural16bit LoadFileMonaural16bits(string path);
101+
102+
void CreateFile(string path, MusicPropertyMonaural16bit prop);
103+
void CreateFile(string path, MusicPropertyMonaural8bit prop);
104+
105+
void WriteMusicProperty(fstream* fs, MusicProperty prop);
106+
107+
void WriteWAVEFORMATEX(fstream* fs, WAVEFORMATEX format);
108+
109+
Int32 ConvertToInt32(Int8* bytes);
110+
};
111+
112+
#pragma region ConvertToInt8*
113+
114+
void ConvertToLittleEndian(Int8* c, Int32 int32)
115+
{
116+
memcpy(c, &int32, sizeof(int32));
117+
}
118+
119+
void ConvertToLittleEndian(Int8* c, Int16 int16)
120+
{
121+
memcpy(c, &int16, sizeof(int16));
122+
}
123+
124+
void ConvertToLittleEndian(Int8* c, UInt32 int32)
125+
{
126+
memcpy(c, &int32, sizeof(int32));
127+
}
128+
129+
void ConvertToLittleEndian(Int8* c, UInt16 int16)
130+
{
131+
memcpy(c, &int16, sizeof(int16));
132+
}
133+
134+
#pragma endregion
135+
136+
137+
template<typename T>
138+
T ConvertFromInt8Array(Int8* bytes)
139+
{
140+
T t;
141+
memcpy(&t, bytes, sizeof(T));
142+
return t;
143+
}
144+
145+
Int32 WaveFileManager::ConvertToInt32(Int8* bytes)
146+
{
147+
Int32 i;
148+
memcpy(&i, bytes, sizeof(Int8) * 4);
149+
return i;
150+
151+
}
152+
153+
Int16 ConvertToInt16(Int8* bytes)
154+
{
155+
Int32 i;
156+
memcpy(&i, bytes, sizeof(Int16));
157+
return i;
158+
}
159+
160+
/// <summary>
161+
/// The arrays are same ?
162+
/// </summary>
163+
template<typename T>
164+
bool SequenceEqual(T* a, T* b, int count)
165+
{
166+
for (int i = 0; i < count; i++)
167+
{
168+
if (a[i] != b[i]) return false;
169+
}
170+
return true;
171+
}

0 commit comments

Comments
 (0)