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