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