1 #include <ptlib.h>
2 #include <ptlib/sound.h>
3 
4 #define ALSA_PCM_NEW_HW_PARAMS_API 1
5 #include <alsa/asoundlib.h>
6 
7 
8 class PSoundChannelALSA : public PSoundChannel {
9  public:
10   PSoundChannelALSA();
11   void Construct();
12   PSoundChannelALSA(
13     const PString &device,
14     PSoundChannel::Directions dir,
15     unsigned numChannels,
16     unsigned sampleRate,
17     unsigned bitsPerSample
18   );
19   ~PSoundChannelALSA();
20   static PStringArray GetDeviceNames(PSoundChannel::Directions);
21   static PString GetDefaultDevice(PSoundChannel::Directions);
22   PBoolean Open(
23     const PString & device,
24     Directions dir,
25     unsigned numChannels,
26     unsigned sampleRate,
27     unsigned bitsPerSample
28   );
29   PBoolean Setup();
30   PBoolean Close();
31   PBoolean Write(const void * buf, PINDEX len);
32   PBoolean Read(void * buf, PINDEX len);
33   PBoolean SetFormat(unsigned numChannels, unsigned sampleRate, unsigned bitsPerSample);
34   unsigned GetChannels() const;
35   unsigned GetSampleRate() const;
36   unsigned GetSampleSize() const;
37   PBoolean SetBuffers(PINDEX size, PINDEX count);
38   PBoolean GetBuffers(PINDEX & size, PINDEX & count);
39   PBoolean PlaySound(const PSound & sound, PBoolean wait);
40   PBoolean PlayFile(const PFilePath & filename, PBoolean wait);
41   PBoolean HasPlayCompleted();
42   PBoolean WaitForPlayCompletion();
43   PBoolean RecordSound(PSound & sound);
44   PBoolean RecordFile(const PFilePath & filename);
45   PBoolean StartRecording();
46   PBoolean IsRecordBufferFull();
47   PBoolean AreAllRecordBuffersFull();
48   PBoolean WaitForRecordBufferFull();
49   PBoolean WaitForAllRecordBuffersFull();
50   PBoolean Abort();
51   PBoolean SetVolume(unsigned);
52   PBoolean GetVolume(unsigned &);
53   PBoolean IsOpen() const;
54 
55  private:
56   static void UpdateDictionary(PSoundChannel::Directions);
57   bool SetHardwareParams();
58   PBoolean Volume(PBoolean, unsigned, unsigned &);
59 
60   PSoundChannel::Directions direction;
61   PString device;
62   unsigned mNumChannels;
63   unsigned mSampleRate;
64   unsigned mBitsPerSample;
65   PBoolean isInitialised;
66 
67   snd_pcm_t *os_handle; /* Handle, different from the PChannel handle */
68   int card_nr;
69 
70   PMutex device_mutex;
71 
72   PINDEX m_bufferSize;
73   PINDEX m_bufferCount;
74 
75   /** Number of bytes in a ALSA frame. a frame may only be 4ms long*/
76   int frameBytes;
77 };
78