/[projet1]/public/pc/tools/oric/Bin2Tap/Source/PulseWaveWriter.cpp
Defence Force logotype

Contents of /public/pc/tools/oric/Bin2Tap/Source/PulseWaveWriter.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 314 - (show annotations)
Tue Apr 27 13:26:32 2010 UTC (9 years, 7 months ago) by Vampire
Original Path: public/pc/tools/oric/PulseWaveWriter.cpp
File size: 4124 byte(s)


1 #include "StdAfx.h"
2 #include "PulseWaveWriter.h"
3 #include "mmsystem.h"
4
5 #define MAKEFOURCC(ch0, ch1, ch2, ch3) \
6 ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \
7 ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24 ))
8
9 CPulseWaveWriter::CPulseWaveWriter(EBitFormat format, ESampleRate rate)
10 {
11 switch ( format)
12 {
13 case eBF_8Bit:
14 m_BitDepth = 8;
15 m_NegativeEdge = 0;
16 m_NeuteralEdge = 128;
17 m_PositiveEdge = 255;
18 break;
19
20 case eBF_16Bit:
21 m_BitDepth = 16;
22 m_NegativeEdge = -32768;
23 m_NeuteralEdge = 0;
24 m_PositiveEdge = 32767;
25 break;
26 }
27
28 switch ( rate)
29 {
30 case eSR_4800:
31 m_Rate = 4800;
32 m_ShortPulseLength = 1;
33 m_LongPulseLength = 2;
34 break;
35
36 case eSR_10000:
37 m_Rate = 10000;
38 m_ShortPulseLength = 2;
39 m_LongPulseLength = 4;
40 break;
41
42 case eSR_44100:
43 m_Rate = 44100;
44 m_ShortPulseLength = 10;
45 m_LongPulseLength = 20;
46 break;
47
48 case eSR_48000:
49 m_Rate = 48000;
50 m_ShortPulseLength = 10;
51 m_LongPulseLength = 20;
52 break;
53 }
54
55 m_OutputFile = NULL;
56 }
57
58 CPulseWaveWriter::~CPulseWaveWriter(void)
59 {
60 delete m_OutputFile;
61 }
62
63 bool CPulseWaveWriter::Open( LPCTSTR filename)
64 {
65 try
66 {
67 if ( filename != NULL)
68 {
69 m_OutputFile = new CFile();
70 if ( !m_OutputFile->Open( filename, CFile::modeWrite | CFile::modeCreate))
71 {
72 return false;
73 }
74 m_ReallyClose = true;
75 }
76 else
77 {
78 m_OutputFile = new CMemFile();
79 m_ReallyClose = false;
80 }
81
82 SWaveHeader header;
83 header.m_FileHeader.m_ID = MAKEFOURCC('R', 'I', 'F', 'F');
84 header.m_FileHeader.m_Length = 0;
85 header.m_Format = MAKEFOURCC('W', 'A', 'V', 'E');
86
87 m_OutputFile->Write( &header, sizeof(header));
88
89 SWaveFormat format;
90 format.m_FileHeader.m_ID = MAKEFOURCC('f', 'm', 't', ' ');
91 format.m_FileHeader.m_Length = 16;
92 format.m_AudioFormat = 1;
93 format.m_NumChannels = 1;
94 format.m_SampleRate = m_Rate;
95 format.m_BlockAlign = (m_BitDepth / 8);
96 format.m_ByteRate = m_Rate * format.m_BlockAlign;
97 format.m_BitsPerSample = m_BitDepth;
98
99 m_OutputFile->Write( &format, sizeof(format));
100
101 SChunkHeader data_header;
102 data_header.m_ID = MAKEFOURCC('d', 'a', 't', 'a');
103 data_header.m_Length = 0;
104
105 m_OutputFile->Write( &data_header, sizeof(data_header));
106 m_DataLengthOffset = (int)m_OutputFile->GetPosition() - 4;
107
108 m_PositivePulse = true;
109 }
110 catch ( CFileException /* e */)
111 {
112 return false;
113 }
114
115 return true;
116 }
117
118 void CPulseWaveWriter::Close( void)
119 {
120 DWORD length = (DWORD)m_OutputFile->GetPosition();
121 m_OutputFile->Seek( m_DataLengthOffset, CFile::begin);
122 DWORD size = (length - m_DataLengthOffset) - 4;
123 m_OutputFile->Write( &size, sizeof(size));
124
125 m_OutputFile->Seek( 4, CFile::begin);
126 size = length - 8;
127 m_OutputFile->Write( &size, sizeof(size));
128
129 if ( m_ReallyClose)
130 {
131 m_OutputFile->Close();
132 }
133 else
134 {
135 byte *buffer = new byte[(int)m_OutputFile->GetLength()];
136 m_OutputFile->SeekToBegin();
137 m_OutputFile->Read( buffer, (int)m_OutputFile->GetLength());
138 PlaySound( (LPCWSTR)buffer, 0, SND_MEMORY | SND_SYNC);
139 delete buffer;
140 }
141 }
142
143 void CPulseWaveWriter::WritePulse( bool long_pulse)
144 {
145 int length = long_pulse ? m_LongPulseLength : m_ShortPulseLength;
146
147 if ( m_BitDepth == 8)
148 {
149 byte value = m_PositivePulse ? m_PositiveEdge : m_NegativeEdge;
150 for ( int i = 0; i < length; i++)
151 {
152 m_OutputFile->Write( &value, sizeof(value));
153 }
154 }
155 else
156 {
157 short value = m_PositivePulse ? m_PositiveEdge : m_NegativeEdge;
158 for ( int i = 0; i < length; i++)
159 {
160 m_OutputFile->Write( &value, sizeof(value));
161 }
162 }
163
164 m_PositivePulse = !m_PositivePulse;
165 }
166
167 void CPulseWaveWriter::WriteBlank( float time)
168 {
169 int pulses = (int)((float)m_Rate * time);
170
171 if ( m_BitDepth == 8)
172 {
173 byte value = m_NeuteralEdge;
174 for ( int i = 0; i < pulses; i++)
175 {
176 m_OutputFile->Write( &value, sizeof(value));
177 }
178 }
179 else
180 {
181 short value = m_NeuteralEdge;
182 for ( int i = 0; i < pulses; i++)
183 {
184 m_OutputFile->Write( &value, sizeof(value));
185 }
186 }
187 }

  ViewVC Help
Powered by ViewVC 1.1.26