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.4 2002/02/09 00:52:01 robertj
31 * Slight adjustment to API and documentation for volume functions.
33 * Revision 1.3 2002/02/07 20:57:21 dereks
34 * add SetVolume and GetVolume methods to PSoundChannel
36 * Revision 1.2 2001/09/27 08:37:45 rogerh
37 * remove unwanted lastError
39 * Revision 1.1 2001/02/23 08:48:10 rogerh
40 * Implement a dummy PSoundChannel class. There is no functionality
41 * but it allows OpenH323 to link.
46 #pragma implementation "sound.h"
50 PSound::PSound(unsigned channels
,
51 unsigned samplesPerSecond
,
52 unsigned bitsPerSample
,
57 numChannels
= channels
;
58 sampleRate
= samplesPerSecond
;
59 sampleSize
= bitsPerSample
;
62 memcpy(GetPointer(), buffer
, bufferSize
);
66 PSound::PSound(const PFilePath
& filename
)
76 PSound
& PSound::operator=(const PBYTEArray
& data
)
78 PBYTEArray::operator=(data
);
83 void PSound::SetFormat(unsigned channels
,
84 unsigned samplesPerSecond
,
85 unsigned bitsPerSample
)
88 numChannels
= channels
;
89 sampleRate
= samplesPerSecond
;
90 sampleSize
= bitsPerSample
;
91 formatInfo
.SetSize(0);
95 BOOL
PSound::Load(const PFilePath
& /*filename*/)
101 BOOL
PSound::Save(const PFilePath
& /*filename*/)
107 ///////////////////////////////////////////////////////////////////////////////
109 PSoundChannel::PSoundChannel()
115 PSoundChannel::PSoundChannel(const PString
& device
,
117 unsigned numChannels
,
119 unsigned bitsPerSample
)
122 Open(device
, dir
, numChannels
, sampleRate
, bitsPerSample
);
126 void PSoundChannel::Construct()
131 PSoundChannel::~PSoundChannel()
137 PStringArray
PSoundChannel::GetDeviceNames(Directions
/*dir*/)
141 array
[0] = "/dev/audio";
142 array
[1] = "/dev/dsp";
148 PString
PSoundChannel::GetDefaultDevice(Directions
/*dir*/)
154 BOOL
PSoundChannel::Open(const PString
& device
,
156 unsigned numChannels
,
158 unsigned bitsPerSample
)
162 if (!ConvertOSError(os_handle
= ::open(device
, dir
== Player
? O_RDONLY
: O_WRONLY
)))
165 return SetFormat(numChannels
, sampleRate
, bitsPerSample
);
169 BOOL
PSoundChannel::Close()
171 return PChannel::Close();
175 BOOL
PSoundChannel::SetFormat(unsigned numChannels
,
177 unsigned bitsPerSample
)
181 PAssert(numChannels
>= 1 && numChannels
<= 2, PInvalidParameter
);
182 PAssert(bitsPerSample
== 8 || bitsPerSample
== 16, PInvalidParameter
);
188 BOOL
PSoundChannel::SetBuffers(PINDEX size
, PINDEX count
)
192 PAssert(size
> 0 && count
> 0 && count
< 65536, PInvalidParameter
);
198 BOOL
PSoundChannel::GetBuffers(PINDEX
& size
, PINDEX
& count
)
204 BOOL
PSoundChannel::Write(const void * buffer
, PINDEX length
)
206 return PChannel::Write(buffer
, length
);
210 BOOL
PSoundChannel::PlaySound(const PSound
& sound
, BOOL wait
)
214 if (!Write((const BYTE
*)sound
, sound
.GetSize()))
218 return WaitForPlayCompletion();
224 BOOL
PSoundChannel::PlayFile(const PFilePath
& filename
, BOOL wait
)
230 BOOL
PSoundChannel::HasPlayCompleted()
236 BOOL
PSoundChannel::WaitForPlayCompletion()
242 BOOL
PSoundChannel::Read(void * buffer
, PINDEX length
)
244 return PChannel::Read(buffer
, length
);
248 BOOL
PSoundChannel::RecordSound(PSound
& sound
)
254 BOOL
PSoundChannel::RecordFile(const PFilePath
& filename
)
260 BOOL
PSoundChannel::StartRecording()
266 BOOL
PSoundChannel::IsRecordBufferFull()
272 BOOL
PSoundChannel::AreAllRecordBuffersFull()
278 BOOL
PSoundChannel::WaitForRecordBufferFull()
284 return PXSetIOBlock(PXReadBlock
, readTimeout
);
288 BOOL
PSoundChannel::WaitForAllRecordBuffersFull()
294 BOOL
PSoundChannel::Abort()
299 BOOL
PSoundChannel::SetVolume(unsigned newVolume
)
304 BOOL
PSoundChannel::GetVolume(unsigned & volume
)