]>
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>
36 //-----------------------------------------------------------------
38 //-----------------------------------------------------------------
41 : m_waveLength(0), m_isResource(FALSE
), m_waveData(NULL
)
45 wxWave::wxWave(const wxString
& sFileName
, bool isResource
)
46 : m_waveLength(0), m_isResource(isResource
), m_waveData(NULL
)
48 Create(sFileName
, isResource
);
51 wxWave::wxWave(int size
, const wxByte
* data
)
52 : m_waveLength(0), m_isResource(FALSE
), m_waveData(NULL
)
62 bool wxWave::Create(const wxString
& fileName
, bool isResource
)
69 return (m_waveData
? TRUE
: FALSE
);
76 if (!fileWave
.Open(fileName
, wxFile::read
))
81 m_waveLength
= (int) fileWave
.Length();
83 m_waveData
= new wxByte
[m_waveLength
];
89 fileWave
.Read(m_waveData
, m_waveLength
);
95 bool wxWave::Create(int size
, const wxByte
* data
)
100 m_waveData
= new wxByte
[size
];
106 for (int i
=0; i
<size
; i
++) m_waveData
[i
] = data
[i
];
111 bool wxWave::Play(bool async
, bool looped
)
113 if (!IsOk()) return FALSE
;
117 if (dev
<0) return FALSE
;
119 ioctl(dev
,SNDCTL_DSP_SYNC
,0);
125 i
= (int)((l
+m_DSPblkSize
) < m_sizeData
? m_DSPblkSize
: (m_sizeData
-l
));
126 if ( write(dev
,&m_data
[l
],i
) != i
)
131 } while (play
== TRUE
&& l
<m_sizeData
);
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
;
161 #define MONO 1 // and stereo is 2 by wav format
162 #define WAVE_FORMAT_PCM 1
166 int wxWave::OpenDSP(void)
169 WAVEFORMAT waveformat
;
173 if (m_waveLength
< (int)(32+sizeof(WAVEFORMAT
)))
176 memcpy(&waveformat
,&m_waveData
[FMT_INDEX
+4],sizeof(WAVEFORMAT
));
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);
188 if ((int)(m_sizeData
+FMT_INDEX
+waveformat
.uiSize
+16) != m_waveLength
)
190 m_data
=(char *)(&m_waveData
[FMT_INDEX
+waveformat
.uiSize
+8]);
192 if (waveformat
.uiFormatTag
!= WAVE_FORMAT_PCM
)
194 if (waveformat
.ulSamplesPerSec
!= waveformat
.ulAvgBytesPerSec
/waveformat
.uiBlockAlign
)
197 if ((dev
= open(AUDIODEV
,O_RDWR
,0)) <0)
200 if (!InitDSP(dev
,(int)waveformat
.uiBitsPerSample
,waveformat
.uiChannels
== MONO
? 0:1,waveformat
.ulSamplesPerSec
))
209 bool wxWave::InitDSP(int dev
, int iDataBits
, int iChannel
,unsigned long ulSamplingRate
)
211 if ( ioctl(dev
,SNDCTL_DSP_GETBLKSIZE
,&m_DSPblkSize
) < 0 )
213 if (m_DSPblkSize
< 4096 || m_DSPblkSize
> 65536)
215 if ( ioctl(dev
,SNDCTL_DSP_SAMPLESIZE
,&iDataBits
) < 0 )
217 if ( ioctl(dev
,SNDCTL_DSP_STEREO
,&iChannel
) < 0 )
219 if ( ioctl(dev
,SNDCTL_DSP_SPEED
,&ulSamplingRate
) < 0 )