4 * Implementation of sound file device
6 * Portable Windows Library
8 * Copyright (C) 2007 Post Increment
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
23 * Robert Jongbloed <robertj@postincrement.com>
25 * All Rights Reserved.
27 * Contributor(s): ______________________________________.
30 * Revision 1.1 2007/04/13 07:03:20 rjongbloed
31 * Added WAV file audio device "plug in".
36 #include <ptclib/pwavfiledev.h>
39 class PSoundChannel_WAVFile_PluginServiceDescriptor
: public PDevicePluginServiceDescriptor
42 virtual PObject
* CreateInstance(int /*userData*/) const
44 return new PSoundChannel_WAVFile
;
46 virtual PStringList
GetDeviceNames(int userData
) const
48 return PSoundChannel_WAVFile::GetDeviceNames((PSoundChannel::Directions
)userData
);
50 virtual bool ValidateDeviceName(const PString
& deviceName
, int userData
) const
52 return (deviceName
.Right(4) *= ".wav") &&
53 PFile::Access(deviceName
, userData
== PSoundChannel::Recorder
? PFile::ReadOnly
: PFile::WriteOnly
);
55 } PSoundChannel_WAVFile_descriptor
;
57 PCREATE_PLUGIN(WAVFile
, PSoundChannel
, &PSoundChannel_WAVFile_descriptor
);
58 PINSTANTIATE_FACTORY(PSoundChannel
, WAVFile
)
64 ///////////////////////////////////////////////////////////////////////////////
66 PSoundChannel_WAVFile::PSoundChannel_WAVFile()
71 PSoundChannel_WAVFile::PSoundChannel_WAVFile(const PString
& device
,
75 unsigned bitsPerSample
)
77 Open(device
, dir
, numChannels
, sampleRate
, bitsPerSample
);
81 PSoundChannel_WAVFile::~PSoundChannel_WAVFile()
87 PString
PSoundChannel_WAVFile::GetName() const
89 return m_WAVFile
.GetFilePath();
93 PStringArray
PSoundChannel_WAVFile::GetDeviceNames(Directions
)
96 devices
.AppendString("*.wav");
101 BOOL
PSoundChannel_WAVFile::Open(const PString
& device
,
103 unsigned numChannels
,
105 unsigned bitsPerSample
)
108 if (dir
== PSoundChannel::Player
) {
109 SetFormat(numChannels
, sampleRate
, bitsPerSample
);
110 return m_WAVFile
.Open(device
, PFile::WriteOnly
);
113 if (!m_WAVFile
.Open(device
, PFile::ReadOnly
))
116 if (m_WAVFile
.GetChannels() == numChannels
&&
117 m_WAVFile
.GetSampleRate() == sampleRate
&&
118 m_WAVFile
.GetSampleSize() == bitsPerSample
)
126 BOOL
PSoundChannel_WAVFile::IsOpen() const
128 return m_WAVFile
.IsOpen();
131 BOOL
PSoundChannel_WAVFile::SetFormat(unsigned numChannels
,
133 unsigned bitsPerSample
)
135 m_WAVFile
.SetChannels(numChannels
);
136 m_WAVFile
.SetSampleRate(sampleRate
);
137 m_WAVFile
.SetSampleSize(bitsPerSample
);
143 unsigned PSoundChannel_WAVFile::GetChannels() const
145 return m_WAVFile
.GetChannels();
149 unsigned PSoundChannel_WAVFile::GetSampleRate() const
151 return m_WAVFile
.GetSampleRate();
155 unsigned PSoundChannel_WAVFile::GetSampleSize() const
157 return m_WAVFile
.GetSampleSize();
161 BOOL
PSoundChannel_WAVFile::Close()
164 return SetErrorValues(NotOpen
, EBADF
);
172 BOOL
PSoundChannel_WAVFile::SetBuffers(PINDEX
, PINDEX
)
178 BOOL
PSoundChannel_WAVFile::GetBuffers(PINDEX
& size
, PINDEX
& count
)
185 BOOL
PSoundChannel_WAVFile::Write(const void * data
, PINDEX size
)
187 BOOL ok
= m_WAVFile
.Write(data
, size
);
188 lastWriteCount
= m_WAVFile
.GetLastWriteCount();
189 m_Pacing
.Delay(lastWriteCount
*8/m_WAVFile
.GetSampleSize()*1000/m_WAVFile
.GetSampleRate());
194 BOOL
PSoundChannel_WAVFile::HasPlayCompleted()
200 BOOL
PSoundChannel_WAVFile::WaitForPlayCompletion()
206 BOOL
PSoundChannel_WAVFile::StartRecording()
212 BOOL
PSoundChannel_WAVFile::Read(void * data
, PINDEX size
)
214 BOOL ok
= m_WAVFile
.Read(data
, size
);
215 lastReadCount
= m_WAVFile
.GetLastReadCount();
216 m_Pacing
.Delay(lastReadCount
*8/m_WAVFile
.GetSampleSize()*1000/m_WAVFile
.GetSampleRate());
221 BOOL
PSoundChannel_WAVFile::IsRecordBufferFull()
227 BOOL
PSoundChannel_WAVFile::AreAllRecordBuffersFull()
233 BOOL
PSoundChannel_WAVFile::WaitForRecordBufferFull()
239 BOOL
PSoundChannel_WAVFile::WaitForAllRecordBuffersFull()
245 // End of File ///////////////////////////////////////////////////////////////