PTLib  Version 2.14.3
 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: 31801 $
30  * $Author: rjongbloed $
31  * $Date: 2014-04-29 14:00:16 +1000 (Tue, 29 Apr 2014) $
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 
252 {
253  PCLASSINFO(PSoundChannel, PIndirectChannel);
254 
255  public:
258  P_DECLARE_STREAMABLE_ENUM(Directions,
259  Recorder,
260  Player,
261  Closed
262  );
263 
265  PSoundChannel();
266 
267  enum {
268  DefaultBufferSize = 320, // 20ms at 8kHz
270  };
271 
273  struct Params {
274  Params(
275  Directions dir = Player,
276  const PString & device = PString::Empty(),
277  const PString & driver = PString::Empty(),
278  unsigned channels = 1,
279  unsigned sampleRate = 8000,
280  unsigned bitsPerSample = 16,
281  unsigned bufferSize = DefaultBufferSize,
282  unsigned bufferCount = DefaultBufferCount,
283  PPluginManager * pluginMgr = NULL
284  );
285 
286  Directions m_direction;
289  unsigned m_channels;
290  unsigned m_sampleRate;
291  unsigned m_bitsPerSample;
292  unsigned m_bufferSize;
295  unsigned m_bufferCount;
297 
298  void SetBufferCountFromMS(unsigned milliseconds);
299  friend ostream & operator<<(ostream &, const Params & params);
300  };
301 
306  const Params & params
307  );
308 
309  // Backward compatibility
311  const PString & device,
312  Directions dir,
313  unsigned numChannels = 1,
314  unsigned sampleRate = 8000,
315  unsigned bitsPerSample = 16
316  );
318 
324  PPluginManager * pluginMgr = NULL
325  );
326 
332  const PString & driverName,
333  Directions direction,
334  PPluginManager * pluginMgr = NULL
335  );
336 
337  // For backward compatibility
339  const PString & driverName,
340  Directions direction,
341  PPluginManager * pluginMgr = NULL
342  ) { return GetDriversDeviceNames(driverName, direction, pluginMgr); }
343 
346  static PSoundChannel * CreateChannel (
347  const PString & driverName,
348  PPluginManager * pluginMgr = NULL
349  );
350 
351  /* Create the matching sound channel that corresponds to the device name.
352  So, for "fake" return a device that will generate fake video.
353  For "Phillips 680 webcam" (eg) will return appropriate grabber.
354  Note that Phillips will return the appropriate grabber also.
355 
356  This is typically used with the return values from GetDeviceNames().
357  */
359  const PString & deviceName,
360  Directions direction,
361  PPluginManager * pluginMgr = NULL
362  );
363 
370  const Params & params
371  );
372 
373  // For backward compatibility
375  const PString & driverName,
376  const PString & deviceName,
377  Directions direction,
378  unsigned numChannels = 1,
379  unsigned sampleRate = 8000,
380  unsigned bitsPerSample = 16,
381  PPluginManager * pluginMgr = NULL
382  );
383 
392  static PString GetDefaultDevice(
393  Directions dir // Sound I/O direction
394  );
395 
409  Directions direction,
410  PPluginManager * pluginMgr = NULL
411  );
412 
419  virtual bool Open(
420  const Params & params
421  );
422 
423  // For backward compatibility
424  bool Open(
425  const PString & device,
426  Directions dir,
427  unsigned numChannels = 1,
428  unsigned sampleRate = 8000,
429  unsigned bitsPerSample = 16,
430  PPluginManager * pluginMgr = NULL
431  );
432 
434  Directions GetDirection() const
435  {
436  return activeDirection;
437  }
438 
440  static const char * GetDirectionText(Directions dir);
441 
443  virtual const char * GetDirectionText() const
444  {
446  }
447 
455  virtual PBoolean Abort();
457 
469  virtual PBoolean SetFormat(
470  unsigned numChannels = 1,
471  unsigned sampleRate = 8000,
472  unsigned bitsPerSample = 16
473  );
474 
476  virtual unsigned GetChannels() const;
477 
479  virtual unsigned GetSampleRate() const;
480 
482  virtual unsigned GetSampleSize() const;
483 
492  virtual PBoolean SetBuffers(
493  PINDEX size,
494  PINDEX count = 2
495  );
496 
502  virtual PBoolean GetBuffers(
503  PINDEX & size, // Size of each buffer in bytes
504  PINDEX & count // Number of buffers
505  );
506 
507  enum {
508  MaxVolume = 100
509  };
510 
519  virtual PBoolean SetVolume(
520  unsigned volume
521  );
522 
531  virtual PBoolean GetVolume(
532  unsigned & volume
533  );
534 
540  virtual bool SetMute(
541  bool mute
542  );
543 
549  virtual bool GetMute(
550  bool & mute
551  );
552 
554 
573  virtual PBoolean PlaySound(
574  const PSound & sound,
575  PBoolean wait = true
576  );
577 
593  virtual PBoolean PlayFile(
594  const PFilePath & file,
595  PBoolean wait = true
596  );
597 
604  virtual PBoolean HasPlayCompleted();
605 
613 
623  static PString TestPlayer(
624  const Params & params,
625  const PNotifier & progress = PNotifier(),
626  const char * toneSpec = NULL
627  );
629 
649  virtual PBoolean RecordSound(
650  PSound & sound
651  );
652 
665  virtual PBoolean RecordFile(
666  const PFilePath & file
667  );
668 
675  virtual PBoolean StartRecording();
676 
684  virtual PBoolean IsRecordBufferFull();
685 
695 
704 
714 
722  static PString TestRecorder(
723  const Params & recorderParams,
724  const Params & playerParams,
725  const PNotifier & progress = PNotifier(),
726  unsigned seconds = 5
727  );
729 
730  protected:
731  PSoundChannel * GetSoundChannel() const { return dynamic_cast<PSoundChannel *>(readChannel); }
732 
737  Directions activeDirection;
738 
739  P_REMOVE_VIRTUAL(PBoolean, Open(const PString &,Directions,unsigned,unsigned,unsigned),false);
740 };
741 
742 
744 
745 // define the sound plugin service descriptor
746 
748 
749 #define PCREATE_SOUND_PLUGIN_EX(name, InstanceClass, extra) \
750  PCREATE_PLUGIN(name, PSoundChannel, InstanceClass, PPlugin_PSoundChannel, \
751  virtual PStringArray GetDeviceNames(P_INT_PTR userData) const { return InstanceClass::GetDeviceNames((PSoundChannel::Directions)userData); } \
752  extra)
753 
754 #define PCREATE_SOUND_PLUGIN(name, InstanceClass) PCREATE_SOUND_PLUGIN_EX(name, InstanceClass, )
755 
756 
757 #define P_NULL_AUDIO_DEVICE "NullAudio"
759 
760 #ifdef _WIN32
761  #define P_WINDOWS_MULTIMEDIA_DRIVER "WindowsMultimedia"
762  PPLUGIN_STATIC_LOAD(WindowsMultimedia, PSoundChannel);
763 #elif defined(P_ANDROID)
765 #elif defined(P_MACOSX) || defined(P_IOS)
767 #elif defined(__BEOS__)
769 #endif
770 
771 #if defined(P_DIRECTSOUND)
772  #define P_DIRECT_SOUND_DRIVER "DirectSound"
773  PPLUGIN_STATIC_LOAD(DirectSound, PSoundChannel);
774 #endif
775 
776 #if defined(P_WAVFILE)
777  #define P_WAV_FILE_DRIVER "WAVFile"
779 #endif
780 
781 #if P_DTMF
783 #endif
784 
785 
786 #endif // PTLIB_SOUND_H
787 
788 
789 // End Of File ///////////////////////////////////////////////////////////////