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