]> git.saurik.com Git - wxWidgets.git/blob - src/unix/wave.cpp
reSWIGed
[wxWidgets.git] / src / unix / wave.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wave.cpp
3 // Purpose: wxWave
4 // Author: Marcel Rasche
5 // Modified by:
6 // Created: 25/10/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "wave.h"
14 #endif
15
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #include "wx/setup.h"
20
21 #if wxUSE_WAVE
22
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
25
26 #if defined(__BORLANDC__)
27 #pragma hdrstop
28 #endif
29
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <sys/ioctl.h>
34 #include <linux/soundcard.h>
35
36 #ifndef WX_PRECOMP
37 #include "wx/wx.h"
38 #endif
39
40 #include "wx/file.h"
41 #include "wx/wave.h"
42
43 //-----------------------------------------------------------------
44 // wxWave
45 //-----------------------------------------------------------------
46
47 wxWave::wxWave()
48 : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE)
49 {
50 }
51
52 wxWave::wxWave(const wxString& sFileName, bool isResource)
53 : m_waveData(NULL), m_waveLength(0), m_isResource(isResource)
54 {
55 Create(sFileName, isResource);
56 }
57
58 wxWave::wxWave(int size, const wxByte* data)
59 : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE)
60 {
61 Create(size, data);
62 }
63
64 wxWave::~wxWave()
65 {
66 Free();
67 }
68
69 bool wxWave::Create(const wxString& fileName, bool isResource)
70 {
71 Free();
72
73 if (isResource)
74 {
75 // todo
76 return (m_waveData ? TRUE : FALSE);
77 }
78 else
79 {
80 m_isResource = FALSE;
81
82 wxFile fileWave;
83 if (!fileWave.Open(fileName, wxFile::read))
84 {
85 return FALSE;
86 }
87
88 m_waveLength = (int) fileWave.Length();
89
90 m_waveData = new wxByte[m_waveLength];
91 if (!m_waveData)
92 {
93 return FALSE;
94 }
95
96 fileWave.Read(m_waveData, m_waveLength);
97
98 return TRUE;
99 }
100 }
101
102 bool wxWave::Create(int size, const wxByte* data)
103 {
104 Free();
105 m_isResource = FALSE;
106 m_waveLength=size;
107 m_waveData = new wxByte[size];
108 if (!m_waveData)
109 {
110 return FALSE;
111 }
112
113 for (int i=0; i<size; i++) m_waveData[i] = data[i];
114
115 return TRUE;
116 }
117
118 bool wxWave::Play(bool async, bool looped)
119 {
120 if (!IsOk()) return FALSE;
121
122 int dev = OpenDSP();
123
124 if (dev<0) return FALSE;
125
126 ioctl(dev,SNDCTL_DSP_SYNC,0);
127
128 bool play=TRUE;
129 int i,l=0;
130 do
131 {
132 i= (int)((l+m_DSPblkSize) < m_sizeData ? m_DSPblkSize : (m_sizeData-l));
133 if ( write(dev,&m_data[l],i) != i )
134 {
135 play=FALSE;
136 }
137 l +=i;
138 } while (play == TRUE && l<m_sizeData);
139
140 close(dev);
141 return TRUE;
142 }
143
144 bool wxWave::Free()
145 {
146 if (m_waveData)
147 {
148 delete[] m_waveData;
149 m_waveData = NULL;
150 m_waveLength = 0;
151 return TRUE;
152 }
153
154 return FALSE;
155 }
156
157 typedef struct
158 {
159 unsigned long uiSize;
160 unsigned short uiFormatTag;
161 unsigned short uiChannels;
162 unsigned long ulSamplesPerSec;
163 unsigned long ulAvgBytesPerSec;
164 unsigned short uiBlockAlign;
165 unsigned short uiBitsPerSample;
166 } WAVEFORMAT;
167
168 #define MONO 1 // and stereo is 2 by wav format
169 #define WAVE_FORMAT_PCM 1
170 #define WAVE_INDEX 8
171 #define FMT_INDEX 12
172
173 int wxWave::OpenDSP(void)
174 {
175 WAVEFORMAT waveformat;
176 int dev=-1;
177 unsigned long ul;
178
179 if (m_waveLength < (int)(32+sizeof(WAVEFORMAT)))
180 return -1;
181
182 memcpy(&waveformat,&m_waveData[FMT_INDEX+4],sizeof(WAVEFORMAT));
183
184 if (memcmp(m_waveData, "RIFF", 4) != 0)
185 return -1;
186 if (memcmp(&m_waveData[WAVE_INDEX], "WAVE", 4) != 0)
187 return -1;
188 if (memcmp(&m_waveData[FMT_INDEX], "fmt ", 4) != 0)
189 return -1;
190 if (memcmp(&m_waveData[FMT_INDEX+waveformat.uiSize+8], "data", 4) != 0)
191 return -1;
192 memcpy(&ul,&m_waveData[FMT_INDEX+waveformat.uiSize+12],4);
193 m_sizeData=ul;
194 if ((int)(m_sizeData+FMT_INDEX+waveformat.uiSize+16) != m_waveLength)
195 return -1;
196 m_data=(char *)(&m_waveData[FMT_INDEX+waveformat.uiSize+8]);
197
198 if (waveformat.uiFormatTag != WAVE_FORMAT_PCM)
199 return -1;
200 if (waveformat.ulSamplesPerSec != waveformat.ulAvgBytesPerSec/waveformat.uiBlockAlign)
201 return -1;
202
203 if ((dev = open(AUDIODEV,O_RDWR,0)) <0)
204 return -1;
205
206 if (!InitDSP(dev,(int)waveformat.uiBitsPerSample,waveformat.uiChannels == MONO ? 0:1,waveformat.ulSamplesPerSec))
207 {
208 close(dev);
209 return -1;
210 }
211
212 return dev;
213 }
214
215 bool wxWave::InitDSP(int dev, int iDataBits, int iChannel,unsigned long ulSamplingRate)
216 {
217 if ( ioctl(dev,SNDCTL_DSP_GETBLKSIZE,&m_DSPblkSize) < 0 )
218 return FALSE;
219 if (m_DSPblkSize < 4096 || m_DSPblkSize > 65536)
220 return FALSE;
221 if ( ioctl(dev,SNDCTL_DSP_SAMPLESIZE,&iDataBits) < 0 )
222 return FALSE;
223 if ( ioctl(dev,SNDCTL_DSP_STEREO,&iChannel) < 0 )
224 return FALSE;
225 if ( ioctl(dev,SNDCTL_DSP_SPEED,&ulSamplingRate) < 0 )
226 return FALSE;
227
228 return TRUE;
229 }
230 #endif
231