4 * Sound driver implementation.
6 * Portable Windows Library
8 * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25 * All Rights Reserved.
27 * Contributor(s): ______________________________________.
30 * Revision 1.5 2005/07/13 13:02:35 csoutheren
31 * Unified interface across Windows and Unix
33 * Revision 1.4 2002/02/09 00:52:01 robertj
34 * Slight adjustment to API and documentation for volume functions.
36 * Revision 1.3 2002/02/07 20:57:21 dereks
37 * add SetVolume and GetVolume methods to PSoundChannel
39 * Revision 1.2 2001/09/27 08:37:45 rogerh
40 * remove unwanted lastError
42 * Revision 1.1 2001/02/23 08:48:10 rogerh
43 * Implement a dummy PSoundChannel class. There is no functionality
44 * but it allows OpenH323 to link.
49 #pragma implementation "sound.h"
53 PSound::PSound(unsigned channels
,
54 unsigned samplesPerSecond
,
55 unsigned bitsPerSample
,
60 numChannels
= channels
;
61 sampleRate
= samplesPerSecond
;
62 sampleSize
= bitsPerSample
;
65 memcpy(GetPointer(), buffer
, bufferSize
);
69 PSound::PSound(const PFilePath
& filename
)
79 PSound
& PSound::operator=(const PBYTEArray
& data
)
81 PBYTEArray::operator=(data
);
86 void PSound::SetFormat(unsigned channels
,
87 unsigned samplesPerSecond
,
88 unsigned bitsPerSample
)
91 numChannels
= channels
;
92 sampleRate
= samplesPerSecond
;
93 sampleSize
= bitsPerSample
;
94 formatInfo
.SetSize(0);
98 BOOL
PSound::Load(const PFilePath
& /*filename*/)
104 BOOL
PSound::Save(const PFilePath
& /*filename*/)
109 BOOL
PSound::Play(const PString
& device
)
112 PSoundChannel
channel(device
,
113 PSoundChannel::Player
);
114 if (!channel
.IsOpen())
117 return channel
.PlaySound(*this, TRUE
);
120 ///////////////////////////////////////////////////////////////////////////////
122 PSoundChannel::PSoundChannel()
128 PSoundChannel::PSoundChannel(const PString
& device
,
130 unsigned numChannels
,
132 unsigned bitsPerSample
)
135 Open(device
, dir
, numChannels
, sampleRate
, bitsPerSample
);
139 void PSoundChannel::Construct()
144 PSoundChannel::~PSoundChannel()
150 PStringArray
PSoundChannel::GetDeviceNames(Directions
/*dir*/)
154 array
[0] = "/dev/audio";
155 array
[1] = "/dev/dsp";
161 PString
PSoundChannel::GetDefaultDevice(Directions
/*dir*/)
167 BOOL
PSoundChannel::Open(const PString
& device
,
169 unsigned numChannels
,
171 unsigned bitsPerSample
)
175 if (!ConvertOSError(os_handle
= ::open(device
, dir
== Player
? O_RDONLY
: O_WRONLY
)))
178 return SetFormat(numChannels
, sampleRate
, bitsPerSample
);
182 BOOL
PSoundChannel::Close()
184 return PChannel::Close();
188 BOOL
PSoundChannel::SetFormat(unsigned numChannels
,
190 unsigned bitsPerSample
)
194 PAssert(numChannels
>= 1 && numChannels
<= 2, PInvalidParameter
);
195 PAssert(bitsPerSample
== 8 || bitsPerSample
== 16, PInvalidParameter
);
201 BOOL
PSoundChannel::SetBuffers(PINDEX size
, PINDEX count
)
205 PAssert(size
> 0 && count
> 0 && count
< 65536, PInvalidParameter
);
211 BOOL
PSoundChannel::GetBuffers(PINDEX
& size
, PINDEX
& count
)
217 BOOL
PSoundChannel::Write(const void * buffer
, PINDEX length
)
219 return PChannel::Write(buffer
, length
);
223 BOOL
PSoundChannel::PlaySound(const PSound
& sound
, BOOL wait
)
227 if (!Write((const BYTE
*)sound
, sound
.GetSize()))
231 return WaitForPlayCompletion();
237 BOOL
PSoundChannel::PlayFile(const PFilePath
& filename
, BOOL wait
)
243 BOOL
PSoundChannel::HasPlayCompleted()
249 BOOL
PSoundChannel::WaitForPlayCompletion()
255 BOOL
PSoundChannel::Read(void * buffer
, PINDEX length
)
257 return PChannel::Read(buffer
, length
);
261 BOOL
PSoundChannel::RecordSound(PSound
& sound
)
267 BOOL
PSoundChannel::RecordFile(const PFilePath
& filename
)
273 BOOL
PSoundChannel::StartRecording()
279 BOOL
PSoundChannel::IsRecordBufferFull()
285 BOOL
PSoundChannel::AreAllRecordBuffersFull()
291 BOOL
PSoundChannel::WaitForRecordBufferFull()
297 return PXSetIOBlock(PXReadBlock
, readTimeout
);
301 BOOL
PSoundChannel::WaitForAllRecordBuffersFull()
307 BOOL
PSoundChannel::Abort()
312 BOOL
PSoundChannel::SetVolume(unsigned newVolume
)
317 BOOL
PSoundChannel::GetVolume(unsigned & volume
)