1 #include <ptlib.h>
2 #include <ptlib/sound.h>
3 #include <ptclib/delaychan.h>
4 
5 class PAudioDelay : public PObject
6 {
7   PCLASSINFO(PAudioDelay, PObject);
8 
9   public:
10     PAudioDelay();
11     PBoolean Delay(int time);
12     void Restart();
13     int  GetError();
14 
15   protected:
16     PTime  previousTime;
17     PBoolean   firstTime;
18     int    error;
19 };
20 
21 #define MIN_HEADROOM    30
22 #define MAX_HEADROOM    60
23 
24 class SoundHandleEntry : public PObject {
25 
26   PCLASSINFO(SoundHandleEntry, PObject)
27 
28   public:
29     SoundHandleEntry();
30 
31     int handle;
32     int direction;
33 
34     unsigned numChannels;
35     unsigned sampleRate;
36     unsigned bitsPerSample;
37     unsigned fragmentValue;
38     PBoolean isInitialised;
39 };
40 
41 #define LOOPBACK_BUFFER_SIZE 5000
42 #define BYTESINBUF ((startptr<endptr)?(endptr-startptr):(LOOPBACK_BUFFER_SIZE+endptr-startptr))
43 
44 
45 
46 class PSoundChannelSHM : public PSoundChannel {
47  public:
48   PSoundChannelSHM();
49   void Construct();
50   PSoundChannelSHM(const PString &device,
51 		   PSoundChannel::Directions dir,
52 		   unsigned numChannels,
53 		   unsigned sampleRate,
54 		   unsigned bitsPerSample);
55   ~PSoundChannelSHM();
56   static PStringArray GetDeviceNames(PSoundChannel::Directions);
57   static PString GetDefaultDevice(PSoundChannel::Directions);
58   PBoolean Open(const PString & _device,
59        Directions _dir,
60        unsigned _numChannels,
61        unsigned _sampleRate,
62        unsigned _bitsPerSample);
63   PBoolean Setup();
64   PBoolean Close();
65   PBoolean Write(const void * buf, PINDEX len);
66   int writeSample( void *aData, int aLen );
67   PBoolean Read(void * buf, PINDEX len);
68   PBoolean SetFormat(unsigned numChannels,
69 	    unsigned sampleRate,
70 	    unsigned bitsPerSample);
71   unsigned GetChannels() const;
72   unsigned GetSampleRate() const;
73   unsigned GetSampleSize() const;
74   PBoolean SetBuffers(PINDEX size, PINDEX count);
75   PBoolean GetBuffers(PINDEX & size, PINDEX & count);
76   PBoolean PlaySound(const PSound & sound, PBoolean wait);
77   PBoolean PlayFile(const PFilePath & filename, PBoolean wait);
78   PBoolean HasPlayCompleted();
79   PBoolean WaitForPlayCompletion();
80   PBoolean RecordSound(PSound & sound);
81   PBoolean RecordFile(const PFilePath & filename);
82   PBoolean StartRecording();
83   PBoolean IsRecordBufferFull();
84   PBoolean AreAllRecordBuffersFull();
85   PBoolean WaitForRecordBufferFull();
86   PBoolean WaitForAllRecordBuffersFull();
87   PBoolean Abort();
88   PBoolean SetVolume (unsigned);
89   PBoolean GetVolume (unsigned &);
90   PBoolean IsOpen() const;
91 
92  private:
93 
94   static void UpdateDictionary(PSoundChannel::Directions);
95   PBoolean Volume (PBoolean, unsigned, unsigned &);
96   PSoundChannel::Directions direction;
97   PString device;
98   unsigned mNumChannels;
99   unsigned mSampleRate;
100   unsigned mBitsPerSample;
101   PBoolean isInitialised;
102 
103   void *os_handle;
104   int card_nr;
105 
106   PMutex device_mutex;
107   PAdaptiveDelay m_Pacing;
108 
109 
110   /**number of 30 (or 20) ms long sound intervals stored by ALSA. Typically, 2.*/
111   PINDEX storedPeriods;
112 
113   /**Total number of bytes of audio stored by ALSA.  Typically, 2*480 or 960.*/
114   PINDEX storedSize;
115 
116   /** Number of bytes in a ALSA frame. a frame may only be 4ms long*/
117   int frameBytes;
118 
119 };
120