MuseScore  3.4
Music composition and notation
alsa.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Linux Music Score Editor
4 //
5 // Copyright (C) 2002-2009 Werner Schweer and others
6 //
7 // AlsaDriver based on code from Fons Adriaensen (clalsadr.cc)
8 // Copyright (C) 2003 Fons Adriaensen
9 // partly based on original work from Paul Davis
10 //
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License version 2.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 //=============================================================================
23 
24 #ifndef __ALSA_H__
25 #define __ALSA_H__
26 
27 #include <alsa/asoundlib.h>
28 #include <poll.h>
29 
30 #include "config.h"
31 #include "driver.h"
32 #include "mididriver.h"
33 
34 typedef struct pollfd PollFd;
35 
36 #define ALSA_PCM_NEW_HW_PARAMS_API
37 #define ALSA_PCM_NEW_SW_PARAMS_API
38 
39 namespace Ms {
40 
41 class MidiDriver;
42 enum class Transport : char;
43 
44 //---------------------------------------------------------
45 // AlsaDriver
46 //---------------------------------------------------------
47 
48 class AlsaDriver {
49  QString _name;
50  typedef char* (*play_function)(const float*, char*, int, int);
51  typedef char* (*clear_function)(char*, int, int);
52 
53  enum { MAXPFD = 8, MAXPLAY = 4 };
54 
55  bool setHwpar(snd_pcm_t* handle, snd_pcm_hw_params_t* hwpar);
56  bool setSwpar(snd_pcm_t* handle, snd_pcm_sw_params_t* swpar);
57  bool recover();
58 
59  unsigned int _rate;
60  snd_pcm_uframes_t _frsize;
61  unsigned int _nfrags;
62  snd_pcm_format_t _play_format;
63  snd_pcm_access_t _play_access;
64  snd_pcm_t* _play_handle;
65  snd_pcm_hw_params_t* _play_hwpar;
66  snd_pcm_sw_params_t* _play_swpar;
67  unsigned int _play_nchan;
69  PollFd _pfd [MAXPFD];
70  snd_pcm_uframes_t _capt_offs;
71  snd_pcm_uframes_t _play_offs;
73  char* _play_ptr [MAXPLAY];
74  int _stat;
75  int _pcnt;
76  bool _xrun;
77  clear_function _clear_func;
78  play_function _play_func;
80 
81  static char* clear_32le(char* dst, int step, int nfrm);
82  static char* clear_24le(char* dst, int step, int nfrm);
83  static char* clear_16le(char* dst, int step, int nfrm);
84  static char* play_32le(const float* src, char* dst, int step, int nfrm);
85  static char* play_24le(const float* src, char* dst, int step, int nfrm);
86  static char* play_16le(const float* src, char* dst, int step, int nfrm);
87  int playInit(snd_pcm_uframes_t len);
88  snd_pcm_sframes_t pcmWait();
89  snd_pcm_t* playHandle() const { return _play_handle; }
90  void clearChan(int chan, snd_pcm_uframes_t len) {
91  _play_ptr[chan] = _clear_func(_play_ptr[chan], _play_step, len);
92  }
93 
94  public:
95  AlsaDriver(QString, unsigned, snd_pcm_uframes_t, unsigned);
96  ~AlsaDriver();
97  bool init();
98  void printinfo();
99  bool pcmStart();
100  int pcmStop();
101  snd_pcm_uframes_t fsize() const { return _frsize; }
102  unsigned int sampleRate() const { return _rate; }
103  void write(int n, float* l, float* r);
104  };
105 
106 //---------------------------------------------------------
107 // AlsaAudio
108 //---------------------------------------------------------
109 
110 class AlsaAudio : public Driver {
111  pthread_t thread;
113  volatile int runAlsa;
115  bool seekflag;
116 
118 
119  void registerClient();
120 
121  public:
122  AlsaAudio(Seq*);
123  virtual ~AlsaAudio();
124  virtual bool init(bool hot = false);
125  virtual bool start(bool hotPlug = false);
126  virtual bool stop();
127  float* getLBuffer(long n);
128  float* getRBuffer(long n);
129  virtual bool isRealtime() const { return false; }
130  virtual void startTransport();
131  virtual void stopTransport();
132  virtual Transport getState() override;
133  virtual int sampleRate() const;
134  void alsaLoop();
135  void write(int n, void* l);
136 
137  virtual void midiRead();
138  virtual void updateOutPortCount(int maxport);
139  };
140 
141 }
142 #endif
143 
snd_pcm_hw_params_t * _play_hwpar
Definition: alsa.h:65
snd_pcm_sw_params_t * _play_swpar
Definition: alsa.h:66
pthread_t thread
Definition: alsa.h:111
bool seekflag
Definition: alsa.h:115
snd_pcm_access_t _play_access
Definition: alsa.h:63
bool _xrun
Definition: alsa.h:76
Definition: alsa.h:110
int _pcnt
Definition: alsa.h:75
snd_pcm_t * playHandle() const
Definition: alsa.h:89
int _play_npfd
Definition: alsa.h:68
int _stat
Definition: alsa.h:74
struct pollfd PollFd
Definition: alsa.h:34
play_function _play_func
Definition: alsa.h:78
Definition: alsa.h:48
Definition: driver.h:33
Definition: mididriver.h:68
AlsaDriver * alsa
Definition: alsa.h:112
Transport
Definition: seq.h:92
snd_pcm_uframes_t _capt_offs
Definition: alsa.h:70
MidiDriver * midiDriver
Definition: alsa.h:117
snd_pcm_uframes_t fsize() const
Definition: alsa.h:101
void clearChan(int chan, snd_pcm_uframes_t len)
Definition: alsa.h:90
Definition: seq.h:104
unsigned int _nfrags
Definition: alsa.h:61
bool mmappedInterface
Definition: alsa.h:79
snd_pcm_format_t _play_format
Definition: alsa.h:62
snd_pcm_t * _play_handle
Definition: alsa.h:64
clear_function _clear_func
Definition: alsa.h:77
unsigned int sampleRate() const
Definition: alsa.h:102
snd_pcm_uframes_t _frsize
Definition: alsa.h:60
Transport state
Definition: alsa.h:114
Definition: aeolus.cpp:26
snd_pcm_uframes_t _play_offs
Definition: alsa.h:71
unsigned int _play_nchan
Definition: alsa.h:67
QString _name
Definition: alsa.h:49
virtual bool isRealtime() const
Definition: alsa.h:129
unsigned int _rate
Definition: alsa.h:59
volatile int runAlsa
Definition: alsa.h:113
int _play_step
Definition: alsa.h:72