]>
Commit | Line | Data |
---|---|---|
cbb8c1d3 MR |
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 | ||
2749089a KB |
16 | #include <wx/setup.h> |
17 | ||
18 | #if wxUSE_WAVE | |
19 | ||
cbb8c1d3 MR |
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 | ||
bbe0af5b RR |
40 | //----------------------------------------------------------------- |
41 | // wxWave | |
42 | //----------------------------------------------------------------- | |
cbb8c1d3 MR |
43 | |
44 | wxWave::wxWave() | |
bcd2b961 | 45 | : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE) |
cbb8c1d3 MR |
46 | { |
47 | } | |
48 | ||
49 | wxWave::wxWave(const wxString& sFileName, bool isResource) | |
bcd2b961 | 50 | : m_waveData(NULL), m_waveLength(0), m_isResource(isResource) |
cbb8c1d3 | 51 | { |
bbe0af5b | 52 | Create(sFileName, isResource); |
cbb8c1d3 MR |
53 | } |
54 | ||
57c208c5 | 55 | wxWave::wxWave(int size, const wxByte* data) |
bcd2b961 | 56 | : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE) |
cbb8c1d3 | 57 | { |
bbe0af5b | 58 | Create(size, data); |
cbb8c1d3 MR |
59 | } |
60 | ||
61 | wxWave::~wxWave() | |
62 | { | |
bbe0af5b | 63 | Free(); |
cbb8c1d3 MR |
64 | } |
65 | ||
66 | bool wxWave::Create(const wxString& fileName, bool isResource) | |
67 | { | |
bbe0af5b | 68 | Free(); |
cbb8c1d3 | 69 | |
bbe0af5b RR |
70 | if (isResource) |
71 | { | |
72 | // todo | |
73 | return (m_waveData ? TRUE : FALSE); | |
74 | } | |
75 | else | |
76 | { | |
77 | m_isResource = FALSE; | |
cbb8c1d3 | 78 | |
bbe0af5b RR |
79 | wxFile fileWave; |
80 | if (!fileWave.Open(fileName, wxFile::read)) | |
81 | { | |
82 | return FALSE; | |
83 | } | |
cbb8c1d3 | 84 | |
bbe0af5b | 85 | m_waveLength = (int) fileWave.Length(); |
cbb8c1d3 | 86 | |
57c208c5 | 87 | m_waveData = new wxByte[m_waveLength]; |
bbe0af5b RR |
88 | if (!m_waveData) |
89 | { | |
90 | return FALSE; | |
91 | } | |
cbb8c1d3 | 92 | |
bbe0af5b | 93 | fileWave.Read(m_waveData, m_waveLength); |
cbb8c1d3 | 94 | |
bbe0af5b RR |
95 | return TRUE; |
96 | } | |
cbb8c1d3 MR |
97 | } |
98 | ||
57c208c5 | 99 | bool wxWave::Create(int size, const wxByte* data) |
cbb8c1d3 | 100 | { |
bbe0af5b RR |
101 | Free(); |
102 | m_isResource = FALSE; | |
103 | m_waveLength=size; | |
57c208c5 | 104 | m_waveData = new wxByte[size]; |
bbe0af5b RR |
105 | if (!m_waveData) |
106 | { | |
107 | return FALSE; | |
108 | } | |
cbb8c1d3 | 109 | |
bbe0af5b RR |
110 | for (int i=0; i<size; i++) m_waveData[i] = data[i]; |
111 | ||
112 | return TRUE; | |
cbb8c1d3 MR |
113 | } |
114 | ||
115 | bool wxWave::Play(bool async, bool looped) | |
116 | { | |
bbe0af5b | 117 | if (!IsOk()) return FALSE; |
cbb8c1d3 | 118 | |
bbe0af5b RR |
119 | int dev = OpenDSP(); |
120 | ||
121 | if (dev<0) return FALSE; | |
cbb8c1d3 | 122 | |
bbe0af5b | 123 | ioctl(dev,SNDCTL_DSP_SYNC,0); |
cbb8c1d3 | 124 | |
bbe0af5b RR |
125 | bool play=TRUE; |
126 | int i,l=0; | |
127 | do | |
cbb8c1d3 | 128 | { |
bbe0af5b RR |
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; | |
cbb8c1d3 MR |
139 | } |
140 | ||
141 | bool wxWave::Free() | |
142 | { | |
bbe0af5b | 143 | if (m_waveData) |
cbb8c1d3 | 144 | { |
bbe0af5b RR |
145 | delete[] m_waveData; |
146 | m_waveData = NULL; | |
147 | m_waveLength = 0; | |
148 | return TRUE; | |
cbb8c1d3 MR |
149 | } |
150 | ||
bbe0af5b | 151 | return FALSE; |
cbb8c1d3 MR |
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; | |
bbe0af5b | 163 | } WAVEFORMAT; |
cbb8c1d3 MR |
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 | wxString str; | |
173 | WAVEFORMAT waveformat; | |
174 | int dev=-1; | |
175 | unsigned long ul; | |
176 | ||
177 | if (m_waveLength < (int)(32+sizeof(WAVEFORMAT))) | |
178 | return -1; | |
179 | ||
180 | memcpy(&waveformat,&m_waveData[FMT_INDEX+4],sizeof(WAVEFORMAT)); | |
181 | ||
182 | str= wxString(m_waveData,4); | |
183 | if (str != "RIFF") return -1; | |
184 | str= wxString(&m_waveData[WAVE_INDEX],4); | |
185 | if (str != "WAVE") return -1; | |
186 | str= wxString(&m_waveData[FMT_INDEX],4); | |
187 | if (str != "fmt ") return -1; | |
188 | str= wxString(&m_waveData[FMT_INDEX+waveformat.uiSize+8],4); | |
189 | if(str != "data") return -1; | |
190 | memcpy(&ul,&m_waveData[FMT_INDEX+waveformat.uiSize+12],4); | |
191 | m_sizeData=ul; | |
192 | if ((int)(m_sizeData+FMT_INDEX+waveformat.uiSize+16) != m_waveLength) | |
193 | return -1; | |
194 | m_data=(char *)(&m_waveData[FMT_INDEX+waveformat.uiSize+8]); | |
195 | ||
196 | if (waveformat.uiFormatTag != WAVE_FORMAT_PCM) | |
197 | return -1; | |
198 | if (waveformat.ulSamplesPerSec != waveformat.ulAvgBytesPerSec/waveformat.uiBlockAlign) | |
199 | return -1; | |
200 | ||
201 | if ((dev = open(AUDIODEV,O_RDWR,0)) <0) | |
202 | return -1; | |
203 | ||
204 | if (!InitDSP(dev,(int)waveformat.uiBitsPerSample,waveformat.uiChannels == MONO ? 0:1,waveformat.ulSamplesPerSec)) | |
205 | { | |
206 | close(dev); | |
207 | return -1; | |
208 | } | |
209 | ||
210 | return dev; | |
211 | } | |
212 | ||
213 | bool wxWave::InitDSP(int dev, int iDataBits, int iChannel,unsigned long ulSamplingRate) | |
214 | { | |
215 | if ( ioctl(dev,SNDCTL_DSP_GETBLKSIZE,&m_DSPblkSize) < 0 ) | |
216 | return FALSE; | |
217 | if (m_DSPblkSize < 4096 || m_DSPblkSize > 65536) | |
218 | return FALSE; | |
219 | if ( ioctl(dev,SNDCTL_DSP_SAMPLESIZE,&iDataBits) < 0 ) | |
220 | return FALSE; | |
221 | if ( ioctl(dev,SNDCTL_DSP_STEREO,&iChannel) < 0 ) | |
222 | return FALSE; | |
223 | if ( ioctl(dev,SNDCTL_DSP_SPEED,&ulSamplingRate) < 0 ) | |
224 | return FALSE; | |
225 | ||
226 | return TRUE; | |
227 | } | |
2749089a | 228 | #endif |
cbb8c1d3 | 229 |