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