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