]>
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 | ||
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 | ||
bbe0af5b RR |
36 | //----------------------------------------------------------------- |
37 | // wxWave | |
38 | //----------------------------------------------------------------- | |
cbb8c1d3 MR |
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 | { | |
bbe0af5b | 48 | Create(sFileName, isResource); |
cbb8c1d3 MR |
49 | } |
50 | ||
51 | wxWave::wxWave(int size, const byte* data) | |
52 | : m_waveLength(0), m_isResource(FALSE), m_waveData(NULL) | |
53 | { | |
bbe0af5b | 54 | Create(size, data); |
cbb8c1d3 MR |
55 | } |
56 | ||
57 | wxWave::~wxWave() | |
58 | { | |
bbe0af5b | 59 | Free(); |
cbb8c1d3 MR |
60 | } |
61 | ||
62 | bool wxWave::Create(const wxString& fileName, bool isResource) | |
63 | { | |
bbe0af5b | 64 | Free(); |
cbb8c1d3 | 65 | |
bbe0af5b RR |
66 | if (isResource) |
67 | { | |
68 | // todo | |
69 | return (m_waveData ? TRUE : FALSE); | |
70 | } | |
71 | else | |
72 | { | |
73 | m_isResource = FALSE; | |
cbb8c1d3 | 74 | |
bbe0af5b RR |
75 | wxFile fileWave; |
76 | if (!fileWave.Open(fileName, wxFile::read)) | |
77 | { | |
78 | return FALSE; | |
79 | } | |
cbb8c1d3 | 80 | |
bbe0af5b | 81 | m_waveLength = (int) fileWave.Length(); |
cbb8c1d3 | 82 | |
bbe0af5b RR |
83 | m_waveData = new byte[m_waveLength]; |
84 | if (!m_waveData) | |
85 | { | |
86 | return FALSE; | |
87 | } | |
cbb8c1d3 | 88 | |
bbe0af5b | 89 | fileWave.Read(m_waveData, m_waveLength); |
cbb8c1d3 | 90 | |
bbe0af5b RR |
91 | return TRUE; |
92 | } | |
cbb8c1d3 MR |
93 | } |
94 | ||
95 | bool wxWave::Create(int size, const byte* data) | |
96 | { | |
bbe0af5b RR |
97 | Free(); |
98 | m_isResource = FALSE; | |
99 | m_waveLength=size; | |
100 | m_waveData = new byte[size]; | |
101 | if (!m_waveData) | |
102 | { | |
103 | return FALSE; | |
104 | } | |
cbb8c1d3 | 105 | |
bbe0af5b RR |
106 | for (int i=0; i<size; i++) m_waveData[i] = data[i]; |
107 | ||
108 | return TRUE; | |
cbb8c1d3 MR |
109 | } |
110 | ||
111 | bool wxWave::Play(bool async, bool looped) | |
112 | { | |
bbe0af5b | 113 | if (!IsOk()) return FALSE; |
cbb8c1d3 | 114 | |
bbe0af5b RR |
115 | int dev = OpenDSP(); |
116 | ||
117 | if (dev<0) return FALSE; | |
cbb8c1d3 | 118 | |
bbe0af5b | 119 | ioctl(dev,SNDCTL_DSP_SYNC,0); |
cbb8c1d3 | 120 | |
bbe0af5b RR |
121 | bool play=TRUE; |
122 | int i,l=0; | |
123 | do | |
cbb8c1d3 | 124 | { |
bbe0af5b RR |
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; | |
cbb8c1d3 MR |
135 | } |
136 | ||
137 | bool wxWave::Free() | |
138 | { | |
bbe0af5b | 139 | if (m_waveData) |
cbb8c1d3 | 140 | { |
bbe0af5b RR |
141 | delete[] m_waveData; |
142 | m_waveData = NULL; | |
143 | m_waveLength = 0; | |
144 | return TRUE; | |
cbb8c1d3 MR |
145 | } |
146 | ||
bbe0af5b | 147 | return FALSE; |
cbb8c1d3 MR |
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; | |
bbe0af5b | 159 | } WAVEFORMAT; |
cbb8c1d3 MR |
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 |