Fixed DevStudio 2003 build with memory check code.
[pwlib.git] / src / ptclib / pwavfiledev.cxx
blobc6d11f586675fcf7c63d09e4f73b81e83d82a845
1 /*
2 * pwavfiledev.cxx
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
18 * under the License.
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): ______________________________________.
29 * $Log$
30 * Revision 1.1 2007/04/13 07:03:20 rjongbloed
31 * Added WAV file audio device "plug in".
35 #include <ptlib.h>
36 #include <ptclib/pwavfiledev.h>
39 class PSoundChannel_WAVFile_PluginServiceDescriptor : public PDevicePluginServiceDescriptor
41 public:
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)
61 #define new PNEW
64 ///////////////////////////////////////////////////////////////////////////////
66 PSoundChannel_WAVFile::PSoundChannel_WAVFile()
71 PSoundChannel_WAVFile::PSoundChannel_WAVFile(const PString & device,
72 Directions dir,
73 unsigned numChannels,
74 unsigned sampleRate,
75 unsigned bitsPerSample)
77 Open(device, dir, numChannels, sampleRate, bitsPerSample);
81 PSoundChannel_WAVFile::~PSoundChannel_WAVFile()
83 Close();
87 PString PSoundChannel_WAVFile::GetName() const
89 return m_WAVFile.GetFilePath();
93 PStringArray PSoundChannel_WAVFile::GetDeviceNames(Directions)
95 PStringArray devices;
96 devices.AppendString("*.wav");
97 return devices;
101 BOOL PSoundChannel_WAVFile::Open(const PString & device,
102 Directions dir,
103 unsigned numChannels,
104 unsigned sampleRate,
105 unsigned bitsPerSample)
107 Close();
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))
114 return FALSE;
116 if (m_WAVFile.GetChannels() == numChannels &&
117 m_WAVFile.GetSampleRate() == sampleRate &&
118 m_WAVFile.GetSampleSize() == bitsPerSample)
119 return TRUE;
121 Close();
122 return FALSE;
126 BOOL PSoundChannel_WAVFile::IsOpen() const
128 return m_WAVFile.IsOpen();
131 BOOL PSoundChannel_WAVFile::SetFormat(unsigned numChannels,
132 unsigned sampleRate,
133 unsigned bitsPerSample)
135 m_WAVFile.SetChannels(numChannels);
136 m_WAVFile.SetSampleRate(sampleRate);
137 m_WAVFile.SetSampleSize(bitsPerSample);
139 return TRUE;
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()
163 if (!IsOpen())
164 return SetErrorValues(NotOpen, EBADF);
166 m_WAVFile.Close();
167 os_handle = -1;
168 return TRUE;
172 BOOL PSoundChannel_WAVFile::SetBuffers(PINDEX, PINDEX)
174 return FALSE;
178 BOOL PSoundChannel_WAVFile::GetBuffers(PINDEX & size, PINDEX & count)
180 size = count = 0;
181 return FALSE;
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());
190 return ok;
194 BOOL PSoundChannel_WAVFile::HasPlayCompleted()
196 return TRUE;
200 BOOL PSoundChannel_WAVFile::WaitForPlayCompletion()
202 return TRUE;
206 BOOL PSoundChannel_WAVFile::StartRecording()
208 return TRUE;
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());
217 return ok;
221 BOOL PSoundChannel_WAVFile::IsRecordBufferFull()
223 return TRUE;
227 BOOL PSoundChannel_WAVFile::AreAllRecordBuffersFull()
229 return TRUE;
233 BOOL PSoundChannel_WAVFile::WaitForRecordBufferFull()
235 return TRUE;
239 BOOL PSoundChannel_WAVFile::WaitForAllRecordBuffersFull()
241 return TRUE;
245 // End of File ///////////////////////////////////////////////////////////////