Make wxMMedia2 compile on VC++ 5
[wxWidgets.git] / utils / wxMMedia2 / lib / sndaiff.cpp
1 // --------------------------------------------------------------------------
2 // Name: sndaiff.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 "sndaiff.cpp"
10 #endif
11
12 #include <wx/wxprec.h>
13
14 #include <wx/stream.h>
15 #include <wx/datstrm.h>
16 #include <wx/filefn.h>
17 #include "sndbase.h"
18 #include "sndcodec.h"
19 #include "sndfile.h"
20 #include "sndpcm.h"
21 #include "sndaiff.h"
22
23 #define BUILD_SIGNATURE(a,b,c,d) (((wxUint32)a) | (((wxUint32)b) << 8) | (((wxUint32)c) << 16) | (((wxUint32)d) << 24))
24
25 #define FORM_SIGNATURE BUILD_SIGNATURE('F','O','R','M')
26 #define AIFF_SIGNATURE BUILD_SIGNATURE('A','I','F','F')
27 #define AIFC_SIGNATURE BUILD_SIGNATURE('A','I','F','C')
28 #define COMM_SIGNATURE BUILD_SIGNATURE('C','O','M','M')
29 #define SSND_SIGNATURE BUILD_SIGNATURE('S','S','N','D')
30
31 wxSoundAiff::wxSoundAiff(wxInputStream& stream, wxSoundStream& io_sound)
32 : wxSoundFileStream(stream, io_sound)
33 {
34 }
35
36 wxSoundAiff::wxSoundAiff(wxOutputStream& stream, wxSoundStream& io_sound)
37 : wxSoundFileStream(stream, io_sound)
38 {
39 }
40
41 wxSoundAiff::~wxSoundAiff()
42 {
43 }
44
45 #define FAIL_WITH(condition, err) if (condition) { m_snderror = err; return FALSE; }
46
47 bool wxSoundAiff::PrepareToPlay()
48 {
49 wxDataInputStream data(*m_input);
50 wxUint32 signature, len, ssnd;
51 bool end_headers;
52
53 if (!m_input) {
54 m_snderror = wxSOUND_INVSTRM;
55 return FALSE;
56 }
57
58 data.BigEndianOrdered(TRUE);
59
60 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
61 FAIL_WITH(wxUINT32_SWAP_ON_BE(signature) != FORM_SIGNATURE, wxSOUND_INVSTRM);
62 // "FORM"
63
64 len = data.Read32();
65 FAIL_WITH(m_input->LastRead() != 4, wxSOUND_INVSTRM);
66 // dummy len
67
68 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
69 FAIL_WITH(
70 wxUINT32_SWAP_ON_BE(signature) != AIFF_SIGNATURE &&
71 wxUINT32_SWAP_ON_BE(signature) != AIFC_SIGNATURE, wxSOUND_INVSTRM);
72 // "AIFF" / "AIFC"
73
74 end_headers = FALSE;
75 while (!end_headers) {
76 FAIL_WITH(m_input->Read(&signature, 4).LastRead() != 4, wxSOUND_INVSTRM);
77
78 len = data.Read32();
79 FAIL_WITH(m_input->LastRead() != 4, wxSOUND_INVSTRM);
80
81 switch (wxUINT32_SWAP_ON_BE(signature)) {
82 case COMM_SIGNATURE: { // "COMM"
83 wxUint16 channels, bps;
84 wxUint32 num_samples;
85 double srate;
86 wxSoundFormatPcm sndformat;
87
88 data >> channels >> num_samples >> bps >> srate;
89
90 sndformat.SetSampleRate((wxUint32) srate);
91 sndformat.SetBPS(bps);
92 sndformat.SetChannels(channels);
93 sndformat.Signed(TRUE);
94 sndformat.SetOrder(wxBIG_ENDIAN);
95
96 if (!SetSoundFormat(sndformat))
97 return FALSE;
98 m_input->SeekI(len-18, wxFromCurrent);
99 break;
100 }
101 case SSND_SIGNATURE: { // "SSND"
102 data >> ssnd;
103 // m_input->SeekI(4, wxFromCurrent); // Pass an INT32
104 // m_input->SeekI(len-4, wxFromCurrent); // Pass the rest
105 m_input->SeekI(ssnd + 4, wxFromCurrent);
106 end_headers = TRUE;
107 break;
108 }
109 default:
110 m_input->SeekI(len, wxFromCurrent);
111 break;
112 }
113 }
114 return TRUE;
115 }
116
117 bool wxSoundAiff::PrepareToRecord(unsigned long time)
118 {
119 return FALSE;
120 }
121
122 bool wxSoundAiff::FinishRecording()
123 {
124 return FALSE;
125 }
126
127 size_t wxSoundAiff::GetData(void *buffer, size_t len)
128 {
129 return m_input->Read(buffer, len).LastRead();
130 }
131
132 size_t wxSoundAiff::PutData(const void *buffer, size_t len)
133 {
134 return m_output->Write(buffer, len).LastWrite();
135 }