]> git.saurik.com Git - wxWidgets.git/blame - utils/wxMMedia2/lib/sndwav.cpp
Modifed ShowModal() function so that you can have dialogs with yes, no and cancel...
[wxWidgets.git] / utils / wxMMedia2 / lib / sndwav.cpp
CommitLineData
526ddb13
GL
1// --------------------------------------------------------------------------
2// Name: sndwav.cpp
3// Purpose:
4// Date: 08/11/1999
5// Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
6// CVSID: $Id$
7// --------------------------------------------------------------------------
8#ifdef __GNUG__
9#pragma implementation "sndwav.cpp"
10#endif
11
6c5e6376
GL
12#include <wx/wxprec.h>
13
526ddb13
GL
14#include <wx/stream.h>
15#include <wx/datstrm.h>
16#include <wx/filefn.h>
6c5e6376 17
526ddb13
GL
18#include "sndbase.h"
19#include "sndcodec.h"
20#include "sndfile.h"
21#include "sndpcm.h"
0662cd32 22#include "sndg72x.h"
526ddb13
GL
23#include "sndwav.h"
24
25#define BUILD_SIGNATURE(a,b,c,d) (((wxUint32)a) | (((wxUint32)b) << 8) | (((wxUint32)c) << 16) | (((wxUint32)d) << 24))
26
27#define RIFF_SIGNATURE BUILD_SIGNATURE('R','I','F','F')
28#define WAVE_SIGNATURE BUILD_SIGNATURE('W','A','V','E')
29#define FMT_SIGNATURE BUILD_SIGNATURE('f','m','t',' ')
30#define DATA_SIGNATURE BUILD_SIGNATURE('d','a','t','a')
31
32#define HEADER_SIZE 4+4 + 4+4+16 + 4+4
33 // 4+4 => NAME + LEN
34 // 16 => fmt size
35
36wxSoundWave::wxSoundWave(wxInputStream& stream, wxSoundStream& io_sound)
37 : wxSoundFileStream(stream, io_sound)
38{
39}
40
41wxSoundWave::wxSoundWave(wxOutputStream& stream, wxSoundStream& io_sound)
42 : wxSoundFileStream(stream, io_sound)
43{
44}
45
46wxSoundWave::~wxSoundWave()
47{
48}
49
50#define FAIL_WITH(condition, err) if (condition) { m_snderror = err; return FALSE; }
51
52bool wxSoundWave::CanRead()
53{
54 wxUint32 len, signature;
55 m_snderror = wxSOUND_NOERR;
56
57 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
58
59 if (wxUINT32_SWAP_ON_BE(signature) != RIFF_SIGNATURE) {
60 m_input->Ungetch(&signature, 4);
61 return FALSE;
62 }
63
64 m_input->Read(&len, 4);
65 FAIL_WITH(m_input->LastRead() != 4, wxSOUND_INVSTRM);
66
67 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
68 if (wxUINT32_SWAP_ON_BE(signature) != WAVE_SIGNATURE) {
69 m_input->Ungetch(&signature, 4);
70 return FALSE;
71 }
72
73 m_input->Ungetch("RIFF", 4);
74 m_input->Ungetch(&len, 4);
75 m_input->Ungetch("WAVE", 4);
76
77 return TRUE;
78}
79
0662cd32
GL
80bool wxSoundWave::HandlePCM(wxDataInputStream& data, wxUint16 channels,
81 wxUint32 sample_fq, wxUint32 byte_p_sec,
82 wxUint16 byte_p_spl, wxUint16 bits_p_spl)
83{
84 wxSoundFormatPcm sndformat;
85
86 sndformat.SetSampleRate(sample_fq);
87 sndformat.SetBPS(bits_p_spl);
88 sndformat.SetChannels(channels);
89 sndformat.Signed(TRUE);
90 sndformat.SetOrder(wxLITTLE_ENDIAN);
91
92 if (!SetSoundFormat(sndformat))
93 return FALSE;
94
95 return TRUE;
96}
97
98bool wxSoundWave::HandleG721(wxDataInputStream& data, wxUint16 channels,
99 wxUint32 sample_fq, wxUint32 byte_p_sec,
100 wxUint16 byte_p_spl, wxUint16 bits_p_spl)
101{
102 wxSoundFormatG72X sndformat;
103
104 sndformat.SetSampleRate(sample_fq);
105 sndformat.SetG72XType(wxSOUND_G721);
106
107 if (!SetSoundFormat(sndformat))
108 return FALSE;
109
110 return TRUE;
111}
112
526ddb13
GL
113bool wxSoundWave::PrepareToPlay()
114{
115 wxUint32 signature, len;
116 bool end_headers;
117
118 if (!m_input) {
119 m_snderror = wxSOUND_INVSTRM;
120 return FALSE;
121 }
122
123 wxDataInputStream data(*m_input);
124 data.BigEndianOrdered(FALSE);
125
126 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
127 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature) != RIFF_SIGNATURE, wxSOUND_INVSTRM);
128 // "RIFF"
129
130 len = data.Read32();
131 FAIL_WITH(m_input->LastRead() != 4, wxSOUND_INVSTRM);
132 // dummy len
133
134 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
135 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature) != WAVE_SIGNATURE, wxSOUND_INVSTRM);
136 // "WAVE"
137
138 end_headers = FALSE;
139 while (!end_headers) {
140 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
141
142 len = data.Read32();
143 FAIL_WITH(m_input->LastRead() != 4, wxSOUND_INVSTRM);
144
145 switch (wxUINT32_SWAP_ON_BE(signature)) {
146 case FMT_SIGNATURE: { // "fmt "
147 wxUint16 format, channels, byte_p_spl, bits_p_spl;
148 wxUint32 sample_fq, byte_p_sec;
526ddb13
GL
149
150 data >> format >> channels >> sample_fq
151 >> byte_p_sec >> byte_p_spl >> bits_p_spl;
0662cd32
GL
152
153 switch (format) {
154 case 0x01:
155 if (!HandlePCM(data, channels, sample_fq,
156 byte_p_sec, byte_p_spl, bits_p_spl))
157 return FALSE;
158 break;
159 case 0x40:
160 if (!HandleG721(data, channels, sample_fq,
161 byte_p_sec, byte_p_spl, bits_p_spl))
162 return FALSE;
163 break;
164 default:
165 m_snderror = wxSOUND_NOCODEC;
526ddb13 166 return FALSE;
0662cd32 167 }
526ddb13
GL
168 break;
169 }
170 case DATA_SIGNATURE: // "data"
171 end_headers = TRUE;
172 break;
173 default:
174 m_input->SeekI(len, wxFromCurrent);
175 break;
176 }
177 }
178 return TRUE;
179}
180
181bool wxSoundWave::PrepareToRecord(unsigned long time)
182{
183#define WRITE_SIGNATURE(sig) \
184signature = sig; \
185signature = wxUINT32_SWAP_ON_BE(signature); \
186FAIL_WITH(m_output->Write(&signature, 4).LastWrite() != 4, wxSOUND_INVSTRM);
187
188 wxUint32 signature, len;
189
190 if (!m_output) {
191 m_snderror = wxSOUND_INVSTRM;
192 return FALSE;
193 }
194
195 wxDataOutputStream data(*m_output);
196 data.BigEndianOrdered(FALSE);
197
622e48cb 198 len = m_sndformat->GetBytesFromTime(time);
526ddb13
GL
199
200 len += HEADER_SIZE;
201
202 WRITE_SIGNATURE(RIFF_SIGNATURE);
203
204 data << len;
205 FAIL_WITH(m_output->LastWrite() != 4, wxSOUND_INVSTRM);
206
207 WRITE_SIGNATURE(WAVE_SIGNATURE);
208
209 {
210 wxUint16 format, channels, byte_p_spl, bits_p_spl;
211 wxUint32 sample_fq, byte_p_sec;
212 wxSoundFormatPcm *pcm;
213
214 if (m_sndformat->GetType() != wxSOUND_PCM) {
215 m_snderror = wxSOUND_NOCODEC;
216 return FALSE;
217 }
218
219 pcm = (wxSoundFormatPcm *)(m_sndformat->Clone());
220
221 WRITE_SIGNATURE(FMT_SIGNATURE);
222 data.Write32(16);
223
224 sample_fq = pcm->GetSampleRate();
225 bits_p_spl = pcm->GetBPS();
226 channels = pcm->GetChannels();
227 byte_p_spl = pcm->GetBPS() / 8;
622e48cb 228 byte_p_sec = pcm->GetBytesFromTime(1);
526ddb13
GL
229 format = 1;
230 data << format << channels << sample_fq
231 << byte_p_sec << byte_p_spl << bits_p_spl;
232
233 pcm->Signed(TRUE);
234 pcm->SetOrder(wxLITTLE_ENDIAN);
235
b83290c3
GL
236 if (!SetSoundFormat(*pcm)) {
237 delete pcm;
526ddb13 238 return FALSE;
b83290c3 239 }
526ddb13
GL
240
241 delete pcm;
242 }
243
244 WRITE_SIGNATURE(DATA_SIGNATURE);
622e48cb 245 data.Write32(m_sndformat->GetBytesFromTime(time));
526ddb13
GL
246 return TRUE;
247}
248
249bool wxSoundWave::FinishRecording()
250{
251 // TODO: Update headers when we stop before the specified time (if possible)
252 return TRUE;
253}
254
0662cd32 255wxUint32 wxSoundWave::GetData(void *buffer, wxUint32 len)
526ddb13
GL
256{
257 return m_input->Read(buffer, len).LastRead();
258}
259
0662cd32 260wxUint32 wxSoundWave::PutData(const void *buffer, wxUint32 len)
526ddb13
GL
261{
262 return m_output->Write(buffer, len).LastWrite();
263}