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