]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/wave.cpp
Rename wxGenericImageList to wxImageList in generic wxListCtrl, too.
[wxWidgets.git] / src / gtk1 / wave.cpp
CommitLineData
cbb8c1d3
MR
1/////////////////////////////////////////////////////////////////////////////
2// Name: wave.cpp
3// Purpose: wxWave
4// Author: Marcel Rasche
5// Modified by:
6// Created: 25/10/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "wave.h"
14#endif
15
3096bd2f 16#include "wx/setup.h"
2749089a
KB
17
18#if wxUSE_WAVE
19
cbb8c1d3
MR
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#if defined(__BORLANDC__)
24#pragma hdrstop
25#endif
26
27#include <stdio.h>
28#include <unistd.h>
29#include <fcntl.h>
30#include <sys/ioctl.h>
31#include <linux/soundcard.h>
32
33#ifndef WX_PRECOMP
34#include "wx/wx.h"
35#endif
36
37#include "wx/file.h"
38#include "wx/wave.h"
39
bbe0af5b
RR
40//-----------------------------------------------------------------
41// wxWave
42//-----------------------------------------------------------------
cbb8c1d3
MR
43
44wxWave::wxWave()
bcd2b961 45 : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE)
cbb8c1d3
MR
46{
47}
48
49wxWave::wxWave(const wxString& sFileName, bool isResource)
bcd2b961 50 : m_waveData(NULL), m_waveLength(0), m_isResource(isResource)
cbb8c1d3 51{
bbe0af5b 52 Create(sFileName, isResource);
cbb8c1d3
MR
53}
54
57c208c5 55wxWave::wxWave(int size, const wxByte* data)
bcd2b961 56 : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE)
cbb8c1d3 57{
bbe0af5b 58 Create(size, data);
cbb8c1d3
MR
59}
60
61wxWave::~wxWave()
62{
bbe0af5b 63 Free();
cbb8c1d3
MR
64}
65
66bool wxWave::Create(const wxString& fileName, bool isResource)
67{
bbe0af5b 68 Free();
cbb8c1d3 69
bbe0af5b
RR
70 if (isResource)
71 {
72 // todo
73 return (m_waveData ? TRUE : FALSE);
74 }
75 else
76 {
77 m_isResource = FALSE;
cbb8c1d3 78
bbe0af5b
RR
79 wxFile fileWave;
80 if (!fileWave.Open(fileName, wxFile::read))
81 {
82 return FALSE;
83 }
cbb8c1d3 84
bbe0af5b 85 m_waveLength = (int) fileWave.Length();
cbb8c1d3 86
57c208c5 87 m_waveData = new wxByte[m_waveLength];
bbe0af5b
RR
88 if (!m_waveData)
89 {
90 return FALSE;
91 }
cbb8c1d3 92
bbe0af5b 93 fileWave.Read(m_waveData, m_waveLength);
cbb8c1d3 94
bbe0af5b
RR
95 return TRUE;
96 }
cbb8c1d3
MR
97}
98
57c208c5 99bool wxWave::Create(int size, const wxByte* data)
cbb8c1d3 100{
bbe0af5b
RR
101 Free();
102 m_isResource = FALSE;
103 m_waveLength=size;
57c208c5 104 m_waveData = new wxByte[size];
bbe0af5b
RR
105 if (!m_waveData)
106 {
107 return FALSE;
108 }
cbb8c1d3 109
bbe0af5b
RR
110 for (int i=0; i<size; i++) m_waveData[i] = data[i];
111
112 return TRUE;
cbb8c1d3
MR
113}
114
115bool wxWave::Play(bool async, bool looped)
116{
bbe0af5b 117 if (!IsOk()) return FALSE;
cbb8c1d3 118
bbe0af5b
RR
119 int dev = OpenDSP();
120
121 if (dev<0) return FALSE;
cbb8c1d3 122
bbe0af5b 123 ioctl(dev,SNDCTL_DSP_SYNC,0);
cbb8c1d3 124
bbe0af5b
RR
125 bool play=TRUE;
126 int i,l=0;
127 do
cbb8c1d3 128 {
bbe0af5b
RR
129 i= (int)((l+m_DSPblkSize) < m_sizeData ? m_DSPblkSize : (m_sizeData-l));
130 if ( write(dev,&m_data[l],i) != i )
131 {
132 play=FALSE;
133 }
134 l +=i;
135 } while (play == TRUE && l<m_sizeData);
136
137 close(dev);
138 return TRUE;
cbb8c1d3
MR
139}
140
141bool wxWave::Free()
142{
bbe0af5b 143 if (m_waveData)
cbb8c1d3 144 {
bbe0af5b
RR
145 delete[] m_waveData;
146 m_waveData = NULL;
147 m_waveLength = 0;
148 return TRUE;
cbb8c1d3
MR
149 }
150
bbe0af5b 151 return FALSE;
cbb8c1d3
MR
152}
153
154typedef struct
155{
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;
bbe0af5b 163} WAVEFORMAT;
cbb8c1d3
MR
164
165#define MONO 1 // and stereo is 2 by wav format
166#define WAVE_FORMAT_PCM 1
167#define WAVE_INDEX 8
168#define FMT_INDEX 12
169
170int wxWave::OpenDSP(void)
171{
cbb8c1d3
MR
172 WAVEFORMAT waveformat;
173 int dev=-1;
174 unsigned long ul;
175
176 if (m_waveLength < (int)(32+sizeof(WAVEFORMAT)))
177 return -1;
178
179 memcpy(&waveformat,&m_waveData[FMT_INDEX+4],sizeof(WAVEFORMAT));
180
59bc036c
OK
181 if (memcmp(m_waveData, "RIFF", 4) != 0)
182 return -1;
183 if (memcmp(&m_waveData[WAVE_INDEX], "WAVE", 4) != 0)
184 return -1;
185 if (memcmp(&m_waveData[FMT_INDEX], "fmt ", 4) != 0)
186 return -1;
187 if (memcmp(&m_waveData[FMT_INDEX+waveformat.uiSize+8], "data", 4) != 0)
188 return -1;
cbb8c1d3
MR
189 memcpy(&ul,&m_waveData[FMT_INDEX+waveformat.uiSize+12],4);
190 m_sizeData=ul;
191 if ((int)(m_sizeData+FMT_INDEX+waveformat.uiSize+16) != m_waveLength)
192 return -1;
193 m_data=(char *)(&m_waveData[FMT_INDEX+waveformat.uiSize+8]);
194
195 if (waveformat.uiFormatTag != WAVE_FORMAT_PCM)
196 return -1;
197 if (waveformat.ulSamplesPerSec != waveformat.ulAvgBytesPerSec/waveformat.uiBlockAlign)
198 return -1;
199
200 if ((dev = open(AUDIODEV,O_RDWR,0)) <0)
201 return -1;
202
203 if (!InitDSP(dev,(int)waveformat.uiBitsPerSample,waveformat.uiChannels == MONO ? 0:1,waveformat.ulSamplesPerSec))
204 {
205 close(dev);
206 return -1;
207 }
208
209 return dev;
210}
211
212bool wxWave::InitDSP(int dev, int iDataBits, int iChannel,unsigned long ulSamplingRate)
213{
214 if ( ioctl(dev,SNDCTL_DSP_GETBLKSIZE,&m_DSPblkSize) < 0 )
215 return FALSE;
216 if (m_DSPblkSize < 4096 || m_DSPblkSize > 65536)
217 return FALSE;
218 if ( ioctl(dev,SNDCTL_DSP_SAMPLESIZE,&iDataBits) < 0 )
219 return FALSE;
220 if ( ioctl(dev,SNDCTL_DSP_STEREO,&iChannel) < 0 )
221 return FALSE;
222 if ( ioctl(dev,SNDCTL_DSP_SPEED,&ulSamplingRate) < 0 )
223 return FALSE;
224
225 return TRUE;
226}
2749089a 227#endif
cbb8c1d3 228