]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/wave.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Marcel Rasche
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
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)
173 WAVEFORMAT waveformat
;
177 if (m_waveLength
< (int)(32+sizeof(WAVEFORMAT
)))
180 memcpy(&waveformat
,&m_waveData
[FMT_INDEX
+4],sizeof(WAVEFORMAT
));
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);
192 if ((int)(m_sizeData
+FMT_INDEX
+waveformat
.uiSize
+16) != m_waveLength
)
194 m_data
=(char *)(&m_waveData
[FMT_INDEX
+waveformat
.uiSize
+8]);
196 if (waveformat
.uiFormatTag
!= WAVE_FORMAT_PCM
)
198 if (waveformat
.ulSamplesPerSec
!= waveformat
.ulAvgBytesPerSec
/waveformat
.uiBlockAlign
)
201 if ((dev
= open(AUDIODEV
,O_RDWR
,0)) <0)
204 if (!InitDSP(dev
,(int)waveformat
.uiBitsPerSample
,waveformat
.uiChannels
== MONO
? 0:1,waveformat
.ulSamplesPerSec
))
213 bool wxWave::InitDSP(int dev
, int iDataBits
, int iChannel
,unsigned long ulSamplingRate
)
215 if ( ioctl(dev
,SNDCTL_DSP_GETBLKSIZE
,&m_DSPblkSize
) < 0 )
217 if (m_DSPblkSize
< 4096 || m_DSPblkSize
> 65536)
219 if ( ioctl(dev
,SNDCTL_DSP_SAMPLESIZE
,&iDataBits
) < 0 )
221 if ( ioctl(dev
,SNDCTL_DSP_STEREO
,&iChannel
) < 0 )
223 if ( ioctl(dev
,SNDCTL_DSP_SPEED
,&ulSamplingRate
) < 0 )