]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/wave.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Marcel Rasche
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "wave.h"
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
26 #if defined(__BORLANDC__)
33 #include <sys/ioctl.h>
34 #include <linux/soundcard.h>
43 //-----------------------------------------------------------------
45 //-----------------------------------------------------------------
48 : m_waveData(NULL
), m_waveLength(0), m_isResource(FALSE
)
52 wxWave::wxWave(const wxString
& sFileName
, bool isResource
)
53 : m_waveData(NULL
), m_waveLength(0), m_isResource(isResource
)
55 Create(sFileName
, isResource
);
58 wxWave::wxWave(int size
, const wxByte
* data
)
59 : m_waveData(NULL
), m_waveLength(0), m_isResource(FALSE
)
69 bool wxWave::Create(const wxString
& fileName
, bool isResource
)
76 return (m_waveData
? TRUE
: FALSE
);
83 if (!fileWave
.Open(fileName
, wxFile::read
))
88 m_waveLength
= (int) fileWave
.Length();
90 m_waveData
= new wxByte
[m_waveLength
];
96 fileWave
.Read(m_waveData
, m_waveLength
);
102 bool wxWave::Create(int size
, const wxByte
* data
)
105 m_isResource
= FALSE
;
107 m_waveData
= new wxByte
[size
];
113 for (int i
=0; i
<size
; i
++) m_waveData
[i
] = data
[i
];
118 bool wxWave::Play(bool async
, bool looped
)
120 if (!IsOk()) return FALSE
;
124 if (dev
<0) return FALSE
;
126 ioctl(dev
,SNDCTL_DSP_SYNC
,0);
132 i
= (int)((l
+m_DSPblkSize
) < m_sizeData
? m_DSPblkSize
: (m_sizeData
-l
));
133 if ( write(dev
,&m_data
[l
],i
) != i
)
138 } while (play
== TRUE
&& l
<m_sizeData
);
159 unsigned long uiSize
;
160 unsigned short uiFormatTag
;
161 unsigned short uiChannels
;
162 unsigned long ulSamplesPerSec
;
163 unsigned long ulAvgBytesPerSec
;
164 unsigned short uiBlockAlign
;
165 unsigned short uiBitsPerSample
;
168 #define MONO 1 // and stereo is 2 by wav format
169 #define WAVE_FORMAT_PCM 1
173 int wxWave::OpenDSP(void)
175 WAVEFORMAT waveformat
;
179 if (m_waveLength
< (int)(32+sizeof(WAVEFORMAT
)))
182 memcpy(&waveformat
,&m_waveData
[FMT_INDEX
+4],sizeof(WAVEFORMAT
));
184 if (memcmp(m_waveData
, "RIFF", 4) != 0)
186 if (memcmp(&m_waveData
[WAVE_INDEX
], "WAVE", 4) != 0)
188 if (memcmp(&m_waveData
[FMT_INDEX
], "fmt ", 4) != 0)
190 if (memcmp(&m_waveData
[FMT_INDEX
+waveformat
.uiSize
+8], "data", 4) != 0)
192 memcpy(&ul
,&m_waveData
[FMT_INDEX
+waveformat
.uiSize
+12],4);
194 if ((int)(m_sizeData
+FMT_INDEX
+waveformat
.uiSize
+16) != m_waveLength
)
196 m_data
=(char *)(&m_waveData
[FMT_INDEX
+waveformat
.uiSize
+8]);
198 if (waveformat
.uiFormatTag
!= WAVE_FORMAT_PCM
)
200 if (waveformat
.ulSamplesPerSec
!= waveformat
.ulAvgBytesPerSec
/waveformat
.uiBlockAlign
)
203 if ((dev
= open(AUDIODEV
,O_RDWR
,0)) <0)
206 if (!InitDSP(dev
,(int)waveformat
.uiBitsPerSample
,waveformat
.uiChannels
== MONO
? 0:1,waveformat
.ulSamplesPerSec
))
215 bool wxWave::InitDSP(int dev
, int iDataBits
, int iChannel
,unsigned long ulSamplingRate
)
217 if ( ioctl(dev
,SNDCTL_DSP_GETBLKSIZE
,&m_DSPblkSize
) < 0 )
219 if (m_DSPblkSize
< 4096 || m_DSPblkSize
> 65536)
221 if ( ioctl(dev
,SNDCTL_DSP_SAMPLESIZE
,&iDataBits
) < 0 )
223 if ( ioctl(dev
,SNDCTL_DSP_STEREO
,&iChannel
) < 0 )
225 if ( ioctl(dev
,SNDCTL_DSP_SPEED
,&ulSamplingRate
) < 0 )