]>
Commit | Line | Data |
---|---|---|
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 | 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 | } | |
228 | #endif | |
229 |