]>
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"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if defined(__BORLANDC__)
26 #include <sys/ioctl.h>
27 #include <linux/soundcard.h>
38 : m_waveLength(0), m_isResource(FALSE
), m_waveData(NULL
)
42 wxWave::wxWave(const wxString
& sFileName
, bool isResource
)
43 : m_waveLength(0), m_isResource(isResource
), m_waveData(NULL
)
45 Create(sFileName
, isResource
);
48 wxWave::wxWave(int size
, const byte
* data
)
49 : m_waveLength(0), m_isResource(FALSE
), m_waveData(NULL
)
59 bool wxWave::Create(const wxString
& fileName
, bool isResource
)
67 return (m_waveData
? TRUE
: FALSE
);
74 if (!fileWave
.Open(fileName
, wxFile::read
))
77 m_waveLength
= (int) fileWave
.Length();
79 m_waveData
= new byte
[m_waveLength
];
83 fileWave
.Read(m_waveData
, m_waveLength
);
89 bool wxWave::Create(int size
, const byte
* data
)
94 m_waveData
= new byte
[size
];
98 for (int i
=0; i
<size
; i
++) m_waveData
[i
] = data
[i
];
102 bool wxWave::Play(bool async
, bool looped
)
111 ioctl(dev
,SNDCTL_DSP_SYNC
,0);
117 i
= (int)((l
+m_DSPblkSize
) < m_sizeData
? m_DSPblkSize
: (m_sizeData
-l
));
118 if ( write(dev
,&m_data
[l
],i
) != i
)
121 }while(play
== TRUE
&& l
<m_sizeData
);
144 unsigned long uiSize
;
145 unsigned short uiFormatTag
;
146 unsigned short uiChannels
;
147 unsigned long ulSamplesPerSec
;
148 unsigned long ulAvgBytesPerSec
;
149 unsigned short uiBlockAlign
;
150 unsigned short uiBitsPerSample
;
154 #define MONO 1 // and stereo is 2 by wav format
155 #define WAVE_FORMAT_PCM 1
159 int wxWave::OpenDSP(void)
162 WAVEFORMAT waveformat
;
166 if (m_waveLength
< (int)(32+sizeof(WAVEFORMAT
)))
169 memcpy(&waveformat
,&m_waveData
[FMT_INDEX
+4],sizeof(WAVEFORMAT
));
171 str
= wxString(m_waveData
,4);
172 if (str
!= "RIFF") return -1;
173 str
= wxString(&m_waveData
[WAVE_INDEX
],4);
174 if (str
!= "WAVE") return -1;
175 str
= wxString(&m_waveData
[FMT_INDEX
],4);
176 if (str
!= "fmt ") return -1;
177 str
= wxString(&m_waveData
[FMT_INDEX
+waveformat
.uiSize
+8],4);
178 if(str
!= "data") return -1;
179 memcpy(&ul
,&m_waveData
[FMT_INDEX
+waveformat
.uiSize
+12],4);
181 if ((int)(m_sizeData
+FMT_INDEX
+waveformat
.uiSize
+16) != m_waveLength
)
183 m_data
=(char *)(&m_waveData
[FMT_INDEX
+waveformat
.uiSize
+8]);
185 if (waveformat
.uiFormatTag
!= WAVE_FORMAT_PCM
)
187 if (waveformat
.ulSamplesPerSec
!= waveformat
.ulAvgBytesPerSec
/waveformat
.uiBlockAlign
)
190 if ((dev
= open(AUDIODEV
,O_RDWR
,0)) <0)
193 if (!InitDSP(dev
,(int)waveformat
.uiBitsPerSample
,waveformat
.uiChannels
== MONO
? 0:1,waveformat
.ulSamplesPerSec
))
202 bool wxWave::InitDSP(int dev
, int iDataBits
, int iChannel
,unsigned long ulSamplingRate
)
204 if ( ioctl(dev
,SNDCTL_DSP_GETBLKSIZE
,&m_DSPblkSize
) < 0 )
206 if (m_DSPblkSize
< 4096 || m_DSPblkSize
> 65536)
208 if ( ioctl(dev
,SNDCTL_DSP_SAMPLESIZE
,&iDataBits
) < 0 )
210 if ( ioctl(dev
,SNDCTL_DSP_STEREO
,&iChannel
) < 0 )
212 if ( ioctl(dev
,SNDCTL_DSP_SPEED
,&ulSamplingRate
) < 0 )