PTLib  Version 2.12.9
 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: 30483 $
30  * $Author: rjongbloed $
31  * $Date: 2013-09-10 15:30:39 +1000 (Tue, 10 Sep 2013) $
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 {
259  Closed = -1,
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 P_INT_PTR GetHandle() const;
405 
407  virtual PString GetName() const;
408 
411  {
412  return activeDirection;
413  }
414 
416  static const char * GetDirectionText(Directions dir);
417 
418  virtual const char * GetDirectionText() const
419  {
421  }
422 
430  virtual PBoolean Abort();
432 
444  virtual PBoolean SetFormat(
445  unsigned numChannels = 1,
446  unsigned sampleRate = 8000,
447  unsigned bitsPerSample = 16
448  );
449 
451  virtual unsigned GetChannels() const;
452 
454  virtual unsigned GetSampleRate() const;
455 
457  virtual unsigned GetSampleSize() const;
458 
467  virtual PBoolean SetBuffers(
468  PINDEX size,
469  PINDEX count = 2
470  );
471 
477  virtual PBoolean GetBuffers(
478  PINDEX & size, // Size of each buffer
479  PINDEX & count // Number of buffers
480  );
481 
482  enum {
483  MaxVolume = 100
484  };
485 
494  virtual PBoolean SetVolume(
495  unsigned volume
496  );
497 
506  virtual PBoolean GetVolume(
507  unsigned & volume
508  );
509 
515  virtual bool SetMute(
516  bool mute
517  );
518 
524  virtual bool GetMute(
525  bool & mute
526  );
527 
529 
532 
553  virtual PBoolean Write(const void * buf, PINDEX len);
554 
555 
557  virtual PINDEX GetLastWriteCount() const;
558 
575  virtual PBoolean PlaySound(
576  const PSound & sound,
577  PBoolean wait = true
578  );
579 
595  virtual PBoolean PlayFile(
596  const PFilePath & file,
597  PBoolean wait = true
598  );
599 
606  virtual PBoolean HasPlayCompleted();
607 
615 
617 
648  virtual PBoolean Read(
649  void * buf,
650  PINDEX len
651  );
652 
654  PINDEX GetLastReadCount() const;
655 
673  virtual PBoolean RecordSound(
674  PSound & sound
675  );
676 
689  virtual PBoolean RecordFile(
690  const PFilePath & file
691  );
692 
699  virtual PBoolean StartRecording();
700 
708  virtual PBoolean IsRecordBufferFull();
709 
719 
728 
739 
740  protected:
743 
749 };
750 
751 
753 
754 // define the sound plugin service descriptor
755 
757 {
758  public:
759  virtual PObject * CreateInstance(int /*userData*/) const { return new className; }
760  virtual PStringArray GetDeviceNames(int userData) const { return className::GetDeviceNames((PSoundChannel::Directions)userData); }
761 };
762 
763 #define PCREATE_SOUND_PLUGIN(name, className) \
764  static PSoundChannelPluginServiceDescriptor<className> className##_descriptor; \
765  PCREATE_PLUGIN(name, PSoundChannel, &className##_descriptor)
766 
767 #define P_NULL_AUDIO_DRIVER "NullAudio"
768 #define P_NULL_AUDIO_DEVICE "Null Audio"
770 
771 #ifdef _WIN32
772  #define P_WINDOWS_MULTIMEDIA_DRIVER "WindowsMultimedia"
773  PPLUGIN_STATIC_LOAD(WindowsMultimedia, PSoundChannel);
774 #elif defined(__BEOS__)
776 #endif
777 
778 #if defined(P_DIRECTSOUND)
779  #define P_DIRECT_SOUND_DRIVER "DirectSound"
780  PPLUGIN_STATIC_LOAD(DirectSound, PSoundChannel);
781 #endif
782 
783 #if defined(P_WAVFILE)
784  #define P_WAV_FILE_DRIVER "WAVFile"
786 #endif
787 
788 
789 #endif // PTLIB_SOUND_H
790 
791 
792 // End Of File ///////////////////////////////////////////////////////////////