PTLib  Version 2.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sound.h
Go to the documentation of this file.
1 /*
2  * sound.h
3  *
4  * Sound interface class.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
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/
14  *
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.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  *
29  * $Revision: 26804 $
30  * $Author: rjongbloed $
31  * $Date: 2011-12-17 01:23:10 -0600 (Sat, 17 Dec 2011) $
32  */
33 
34 
35 #ifndef PTLIB_SOUND_H
36 #define PTLIB_SOUND_H
37 
38 #ifdef P_USE_PRAGMA
39 #pragma interface
40 #endif
41 
42 #include <ptlib/plugin.h>
43 #include <ptlib/pluginmgr.h>
44 
52 class PSound : public PBYTEArray
53 {
54  PCLASSINFO(PSound, PBYTEArray);
55 
56  public:
65  PSound(
66  unsigned numChannels = 1,
67  unsigned sampleRate = 8000,
68  unsigned bitsPerSample = 16,
69  PINDEX bufferSize = 0,
70  const BYTE * data = NULL
71  );
72 
75  PSound(
76  const PFilePath & filename
77  );
78 
81  PSound & operator=(
82  const PBYTEArray & data
83  );
85 
97  PBoolean Load(
98  const PFilePath & filename
99  );
100 
107  PBoolean Save(
108  const PFilePath & filename
109  );
111 
114 
115  PBoolean Play();
116 
118  PBoolean Play(const PString & device);
119 
123  void SetFormat(
124  unsigned numChannels,
125  unsigned sampleRate,
126  unsigned bitsPerSample
127  );
128 
132  unsigned GetEncoding() const { return encoding; }
133 
135  unsigned GetChannels() const { return numChannels; }
136 
138  unsigned GetSampleRate() const { return sampleRate; }
139 
141  unsigned GetSampleSize() const { return sampleSize; }
142 
144  DWORD GetErrorCode() const { return dwLastError; }
145 
147  PINDEX GetFormatInfoSize() const { return formatInfo.GetSize(); }
148 
150  const void * GetFormatInfoData() const { return (const BYTE *)formatInfo; }
152 
163  static PBoolean PlayFile(
164  const PFilePath & file,
165  PBoolean wait = true
166  );
167 
169  static void Beep();
171 
172  protected:
174  unsigned encoding;
176  unsigned numChannels;
178  unsigned sampleRate;
180  unsigned sampleSize;
182  DWORD dwLastError;
185 };
186 
187 
251 class PSoundChannel : public PChannel
252 {
253  PCLASSINFO(PSoundChannel, PChannel);
254 
255  public:
258  enum Directions {
262  };
263 
265  PSoundChannel();
266 
271  const PString & device,
272  Directions dir,
273  unsigned numChannels = 1,
274  unsigned sampleRate = 8000,
275  unsigned bitsPerSample = 16
276  );
277  //
278 
279  virtual ~PSoundChannel();
280  // Destroy and close the sound driver
282 
288  PPluginManager * pluginMgr = NULL
289  );
290 
296  const PString & driverName,
297  Directions direction,
298  PPluginManager * pluginMgr = NULL
299  );
300 
301  // For backward compatibility
303  const PString & driverName,
304  Directions direction,
305  PPluginManager * pluginMgr = NULL
306  ) { return GetDriversDeviceNames(driverName, direction, pluginMgr); }
307 
310  static PSoundChannel * CreateChannel (
311  const PString & driverName,
312  PPluginManager * pluginMgr = NULL
313  );
314 
315  /* Create the matching sound channel that corresponds to the device name.
316  So, for "fake" return a device that will generate fake video.
317  For "Phillips 680 webcam" (eg) will return appropriate grabber.
318  Note that Phillips will return the appropriate grabber also.
319 
320  This is typically used with the return values from GetDeviceNames().
321  */
323  const PString & deviceName,
324  Directions direction,
325  PPluginManager * pluginMgr = NULL
326  );
327 
334  const PString & driverName,
335  const PString & deviceName,
336  Directions direction,
337  unsigned numChannels = 1,
338  unsigned sampleRate = 8000,
339  unsigned bitsPerSample = 16,
340  PPluginManager * pluginMgr = NULL
341  );
342 
351  static PString GetDefaultDevice(
352  Directions dir // Sound I/O direction
353  );
354 
368  Directions direction,
369  PPluginManager * pluginMgr = NULL
370  );
371 
378  virtual PBoolean Open(
379  const PString & device,
380  Directions dir,
381  unsigned numChannels = 1,
382  unsigned sampleRate = 8000,
383  unsigned bitsPerSample = 16
384  );
385 
391  virtual PBoolean IsOpen() const;
392 
397  virtual PBoolean Close();
398 
404  virtual int GetHandle() const;
405 
407  virtual PString GetName() const;
408 
410  virtual Directions GetDirection() const
411  {
412  return activeDirection;
413  }
414 
416  static const char * GetDirectionText(Directions dir)
417  {
418  return (dir == Player)? "Playback" : ((dir == Recorder)? "Recording" : "Closed");
419  }
420 
421  virtual const char * GetDirectionText() const
422  {
424  }
425 
433  virtual PBoolean Abort();
435 
447  virtual PBoolean SetFormat(
448  unsigned numChannels = 1,
449  unsigned sampleRate = 8000,
450  unsigned bitsPerSample = 16
451  );
452 
454  virtual unsigned GetChannels() const;
455 
457  virtual unsigned GetSampleRate() const;
458 
460  virtual unsigned GetSampleSize() const;
461 
470  virtual PBoolean SetBuffers(
471  PINDEX size,
472  PINDEX count = 2
473  );
474 
480  virtual PBoolean GetBuffers(
481  PINDEX & size, // Size of each buffer
482  PINDEX & count // Number of buffers
483  );
484 
485  enum {
486  MaxVolume = 100
487  };
488 
497  virtual PBoolean SetVolume(
498  unsigned volume
499  );
500 
509  virtual PBoolean GetVolume(
510  unsigned & volume
511  );
513 
516 
537  virtual PBoolean Write(const void * buf, PINDEX len);
538 
539 
562  virtual PBoolean Write(
563  const void * buf,
564  PINDEX len,
565  const void * mark
566  );
567 
569  virtual PINDEX GetLastWriteCount() const;
570 
587  virtual PBoolean PlaySound(
588  const PSound & sound,
589  PBoolean wait = true
590  );
591 
607  virtual PBoolean PlayFile(
608  const PFilePath & file,
609  PBoolean wait = true
610  );
611 
618  virtual PBoolean HasPlayCompleted();
619 
627 
629 
660  virtual PBoolean Read(
661  void * buf,
662  PINDEX len
663  );
664 
666  PINDEX GetLastReadCount() const;
667 
685  virtual PBoolean RecordSound(
686  PSound & sound
687  );
688 
701  virtual PBoolean RecordFile(
702  const PFilePath & file
703  );
704 
711  virtual PBoolean StartRecording();
712 
720  virtual PBoolean IsRecordBufferFull();
721 
731 
740 
751 
752  protected:
754 
760 };
761 
762 
764 
765 // define the sound plugin service descriptor
766 
768 {
769  public:
770  virtual PObject * CreateInstance(int /*userData*/) const { return new className; }
771  virtual PStringArray GetDeviceNames(int userData) const { return className::GetDeviceNames((PSoundChannel::Directions)userData); }
772 };
773 
774 #define PCREATE_SOUND_PLUGIN(name, className) \
775  static PSoundChannelPluginServiceDescriptor<className> className##_descriptor; \
776  PCREATE_PLUGIN(name, PSoundChannel, &className##_descriptor)
777 
778 #ifdef _WIN32
779  PPLUGIN_STATIC_LOAD(WindowsMultimedia, PSoundChannel);
780 #elif defined(__BEOS__)
782 #endif
783 
784 #if defined(P_DIRECTSOUND)
785  PPLUGIN_STATIC_LOAD(DirectSound, PSoundChannel);
786 #endif
787 
788 #if defined(P_WAVFILE)
790 #endif
791 
792 
793 #endif // PTLIB_SOUND_H
794 
795 
796 // End Of File ///////////////////////////////////////////////////////////////