]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/wave.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Marcel Rasche
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "wave.h"
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
30 #include <sys/ioctl.h>
31 #include <linux/soundcard.h>
40 //-----------------------------------------------------------------
42 //-----------------------------------------------------------------
45 : m_waveData(NULL
), m_waveLength(0), m_isResource(FALSE
)
49 wxWave::wxWave(const wxString
& sFileName
, bool isResource
)
50 : m_waveData(NULL
), m_waveLength(0), m_isResource(isResource
)
52 Create(sFileName
, isResource
);
55 wxWave::wxWave(int size
, const wxByte
* data
)
56 : m_waveData(NULL
), m_waveLength(0), m_isResource(FALSE
)
66 bool wxWave::Create(const wxString
& fileName
, bool isResource
)
73 return (m_waveData
? TRUE
: FALSE
);
80 if (!fileWave
.Open(fileName
, wxFile::read
))
85 m_waveLength
= (int) fileWave
.Length();
87 m_waveData
= new wxByte
[m_waveLength
];
93 fileWave
.Read(m_waveData
, m_waveLength
);
99 bool wxWave::Create(int size
, const wxByte
* data
)
102 m_isResource
= FALSE
;
104 m_waveData
= new wxByte
[size
];
110 for (int i
=0; i
<size
; i
++) m_waveData
[i
] = data
[i
];
115 bool wxWave::Play(bool async
, bool looped
)
117 if (!IsOk()) return FALSE
;
121 if (dev
<0) return FALSE
;
123 ioctl(dev
,SNDCTL_DSP_SYNC
,0);
129 i
= (int)((l
+m_DSPblkSize
) < m_sizeData
? m_DSPblkSize
: (m_sizeData
-l
));
130 if ( write(dev
,&m_data
[l
],i
) != i
)
135 } while (play
== TRUE
&& l
<m_sizeData
);
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
;
165 #define MONO 1 // and stereo is 2 by wav format
166 #define WAVE_FORMAT_PCM 1
170 int wxWave::OpenDSP(void)
172 WAVEFORMAT waveformat
;
176 if (m_waveLength
< (int)(32+sizeof(WAVEFORMAT
)))
179 memcpy(&waveformat
,&m_waveData
[FMT_INDEX
+4],sizeof(WAVEFORMAT
));
181 if (memcmp(m_waveData
, "RIFF", 4) != 0)
183 if (memcmp(&m_waveData
[WAVE_INDEX
], "WAVE", 4) != 0)
185 if (memcmp(&m_waveData
[FMT_INDEX
], "fmt ", 4) != 0)
187 if (memcmp(&m_waveData
[FMT_INDEX
+waveformat
.uiSize
+8], "data", 4) != 0)
189 memcpy(&ul
,&m_waveData
[FMT_INDEX
+waveformat
.uiSize
+12],4);
191 if ((int)(m_sizeData
+FMT_INDEX
+waveformat
.uiSize
+16) != m_waveLength
)
193 m_data
=(char *)(&m_waveData
[FMT_INDEX
+waveformat
.uiSize
+8]);
195 if (waveformat
.uiFormatTag
!= WAVE_FORMAT_PCM
)
197 if (waveformat
.ulSamplesPerSec
!= waveformat
.ulAvgBytesPerSec
/waveformat
.uiBlockAlign
)
200 if ((dev
= open(AUDIODEV
,O_RDWR
,0)) <0)
203 if (!InitDSP(dev
,(int)waveformat
.uiBitsPerSample
,waveformat
.uiChannels
== MONO
? 0:1,waveformat
.ulSamplesPerSec
))
212 bool wxWave::InitDSP(int dev
, int iDataBits
, int iChannel
,unsigned long ulSamplingRate
)
214 if ( ioctl(dev
,SNDCTL_DSP_GETBLKSIZE
,&m_DSPblkSize
) < 0 )
216 if (m_DSPblkSize
< 4096 || m_DSPblkSize
> 65536)
218 if ( ioctl(dev
,SNDCTL_DSP_SAMPLESIZE
,&iDataBits
) < 0 )
220 if ( ioctl(dev
,SNDCTL_DSP_STEREO
,&iChannel
) < 0 )
222 if ( ioctl(dev
,SNDCTL_DSP_SPEED
,&ulSamplingRate
) < 0 )