summaryrefslogtreecommitdiff
path: root/attic/muse_qt4_evolution/muse/dummyaudio.cpp
blob: b4bc73ffbe7bd4771c3f3b214918feb5aea2fc31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//=============================================================================
//  MusE
//  Linux Music Editor
//  $Id:$
//
//  Copyright (C) 2002-2006 by Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

#include "al/al.h"
#include "widgets/utils.h"
#include "audio.h"
#include "audiodev.h"
#include "globals.h"
#include "song.h"

static const unsigned dummyFrames = 1024;
static double startTime;

//---------------------------------------------------------
//   DummyAudio
//---------------------------------------------------------

class DummyAudio : public AudioDriver {
      float* buffer;
      pthread_t dummyThread;
      std::vector<QString> oPorts;
      std::vector<QString> iPorts;
      int realTimePriority;

   public:
      int state;
      bool seekflag;
      unsigned pos;

      DummyAudio() {
            state = Audio::STOP;
            seekflag = false;
            startTime = curTime();
            posix_memalign((void**)&buffer, 16, sizeof(float) * dummyFrames);
            }
      virtual ~DummyAudio() {
            free(buffer);
            }

      virtual bool init()   { return true; }
      virtual void start(int);
      virtual void stop ();
      virtual unsigned frameTime() const {
            return lrint(curTime() * AL::sampleRate);
            }
      virtual unsigned lastFrameTime() const {
            return lrint(startTime * AL::sampleRate);
            }
      virtual unsigned curFrame() const { return pos; }

      virtual float* getBuffer(Port /*port*/, unsigned long nframes)
            {
            if (nframes > dummyFrames) {
                  fprintf(stderr, "error: segment size > %d\n", dummyFrames);
                  exit(-1);
                  }
            memset(buffer, 0, nframes * sizeof(float));
            return buffer;
            }

      virtual QList<PortName> outputPorts(bool midi = false);
      virtual QList<PortName> inputPorts(bool midi = false);

      virtual void registerClient() {}

      virtual Port registerOutPort(const QString& s, bool) {
            iPorts.push_back(QString(s));
            Port port(0, iPorts.size() + 3000);
            return port;
            }
      virtual Port registerInPort(const QString& s, bool) {
            oPorts.push_back(QString(s));
            Port port(0, oPorts.size() + 40);
            return port;
            }
      virtual void unregisterPort(Port) {
/*            if (long(p) >= 100)
                  oPorts.erase(oPorts.begin() + (long(p)-40));
            else
                  iPorts.erase(iPorts.begin() + long(p)-30);
*/
            }
      virtual bool connect(Port, Port)           { return true; }
      virtual bool disconnect(Port, Port)        { return true; }
      virtual void setPortName(Port, const QString&) {}
      virtual Port findPort(const QString& s) {
            if (s == "input1")
                  return Port(0, 10);
            if (s == "input2")
                  return Port(0, 11);
            if (s == "output1")
                  return Port(0, 20);
            if (s == "output2")
                  return Port(0, 21);
            int k = 0;
            for (std::vector<QString>::const_iterator i = iPorts.begin(); i != iPorts.end(); ++i, ++k) {
                  if (s == *i)
                        return Port(0, 30+k);
                  }
            k = 0;
            for (std::vector<QString>::const_iterator i = oPorts.begin(); i != oPorts.end(); ++i, ++k) {
                  if (s == *i)
                        return Port(0, 40);
                  }
            return Port();
            }
      virtual QString portName(Port port) {
            if (port.alsaPort() == 10)
                  return QString("input1");
            if (port.alsaPort() == 11)
                  return QString("input2");
            if (port.alsaPort() == 20)
                  return QString("output1");
            if (port.alsaPort() == 21)
                  return QString("output2");
            if (port.alsaPort() >= 40)
                  return QString(oPorts[port.alsaPort() - 40]);
            else
                  return QString(iPorts[port.alsaPort() - 30]);
            }
      virtual int realtimePriority() const { return 40; }
      virtual void startTransport() {
            state = Audio::PLAY;
            }
      virtual void stopTransport() {
            state = Audio::STOP;
            }
      virtual void seekTransport(unsigned n) {
            seekflag = true;
            pos = n;
            }
      virtual void setFreewheel(bool) {}
      virtual void putEvent(Port, const MidiEvent&) {}
      };

DummyAudio* dummyAudio;

//---------------------------------------------------------
//   initDummyAudio
//---------------------------------------------------------

bool initDummyAudio()
      {
      dummyAudio = new DummyAudio();
      audioDriver = dummyAudio;
      return false;
      }

//---------------------------------------------------------
//   outputPorts
//---------------------------------------------------------

QList<PortName> DummyAudio::outputPorts(bool midi)
      {
      QList<PortName> clientList;
      if (!midi) {
            PortName p1;
            p1.name = QString("output1");
            p1.port = Port(0, 100);
            PortName p2;
            p2.name = QString("output2");
            p2.port = Port(0, 101);
            clientList.append(p1);
            clientList.append(p2);
            }
      return clientList;
      }

//---------------------------------------------------------
//   inputPorts
//---------------------------------------------------------

QList<PortName> DummyAudio::inputPorts(bool midi)
      {
      QList<PortName> clientList;
      if (!midi) {
            PortName p1;
            p1.name = QString("input1");
            p1.port = Port(0, 0);
            PortName p2;
            p2.name = QString("input2");
            p2.port = Port(0, 1);
            clientList.append(p1);
            clientList.append(p2);
            }
      return clientList;
      }

//---------------------------------------------------------
//   dummyLoop
//---------------------------------------------------------

static void* dummyLoop(void*)
      {
#ifndef __APPLE__
      if (realTimePriority) {
            //
            // check if we really got realtime priviledges
            //
      	    int policy;
            if ((policy = sched_getscheduler (0)) < 0) {
      	        printf("cannot get current client scheduler for audio dummy thread: %s!\n", strerror(errno));
                }
      	    else
                {
            	if (policy != SCHED_FIFO)
	                  printf("audio dummy thread _NOT_ running SCHED_FIFO\n");
      	        else if (debugMsg) {
            		struct sched_param rt_param;
      	      	    memset(&rt_param, 0, sizeof(sched_param));
	            	int type;
      	      	    int rv = pthread_getschedparam(pthread_self(), &type, &rt_param);
            		if (rv == -1)
	                  	perror("get scheduler parameter");
      	            printf("audio dummy thread running SCHED_FIFO priority %d\n",
	                     rt_param.sched_priority);
                    }
      	        }
            }
#endif

      for (;;) {
            if (audioState == AUDIO_RUNNING)
	            audio->process(segmentSize, dummyAudio->state);
            else if (audioState == AUDIO_START1)
                  audioState = AUDIO_START2;
            usleep(dummyFrames*1000000/AL::sampleRate);
            if (dummyAudio->seekflag) {
                  audio->sync(Audio::STOP, dummyAudio->pos);
                  dummyAudio->seekflag = false;
                  }
            if (dummyAudio->state == Audio::PLAY) {
                  dummyAudio->pos += dummyFrames;
                  }
            }
      pthread_exit(0);
      }

//---------------------------------------------------------
//   start
//---------------------------------------------------------

void DummyAudio::start(int priority)
      {
      realTimePriority = priority;
      pthread_attr_t* attributes = 0;

      if (priority) {
            attributes = (pthread_attr_t*) malloc(sizeof(pthread_attr_t));
            pthread_attr_init(attributes);

            if (pthread_attr_setschedpolicy(attributes, SCHED_FIFO)) {
                  printf("cannot set FIFO scheduling class for RT thread\n");
                  }
            if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
                  printf("Cannot set scheduling scope for RT thread\n");
                  }
            struct sched_param rt_param;
            memset(&rt_param, 0, sizeof(rt_param));
            rt_param.sched_priority = priority;
            if (pthread_attr_setschedparam (attributes, &rt_param)) {
                  printf("Cannot set scheduling priority %d for RT thread (%s)\n",
                     priority, strerror(errno));
                  }
            }
      if (pthread_create(&dummyThread, attributes, ::dummyLoop, this))
            perror("creating thread failed:");
      if (priority)
	      pthread_attr_destroy(attributes);
      }

void DummyAudio::stop ()
      {
      pthread_cancel(dummyThread);
      pthread_join(dummyThread, 0);
      }