summaryrefslogtreecommitdiff
path: root/attic/muse_qt4_evolution/muse/audio.h
blob: 5d52ee0f978fabd81d416abaf56720a71471fc0f (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
295
296
297
298
299
300
301
302
303
304
305
306
//=============================================================================
//  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.
//=============================================================================

#ifndef __AUDIO_H__
#define __AUDIO_H__

#include "thread.h"
#include "midievent.h"
#include "route.h"
#include "al/tempo.h"
#include "al/pos.h"
#include "event.h"
#include "ctrl.h"

namespace AL {
      class TimeSignature;
      };

using AL::Xml;
class SndFile;
class PluginI;
class MidiPluginI;
class SynthI;
class AudioDriver;
class Track;
class AudioTrack;
class Part;
class Event;
class MidiEvent;
class Event;
class MidiTrack;
class MidiSeq;
class MidiTrackBase;

//---------------------------------------------------------
//   GuiMessages
//    messages from sequencer to GUI
//    used in Audio::sendMsgToGui(char c)
//---------------------------------------------------------

#define MSG_STOP              '0'
#define MSG_PLAY              '1'
#define MSG_RECORD            '2'
#define MSG_SEEK              'G'
#define MSG_JACK_SHUTDOWN     'S'
#define MSG_START_BOUNCE      'f'
#define MSG_STOP_BOUNCE       'F'
#define MSG_GRAPH_CHANGED     'C'
#define MSG_ALSA_CHANGED      'P'

//---------------------------------------------------------
//   AudioMsgId
//    this are the messages send from the GUI thread to
//    the midi thread
//---------------------------------------------------------

enum {
      SEQM_ADD_TRACK,
      SEQM_REMOVE_TRACK,
      SEQM_MOVE_TRACK,
      SEQM_ADD_PART,
      SEQM_REMOVE_PART,
      SEQM_CHANGE_PART,
      SEQM_ADD_EVENT,
      SEQM_ADD_EVENTS,
      SEQM_REMOVE_EVENT,
      SEQM_CHANGE_EVENT,
      SEQM_ADD_TEMPO,
      SEQM_SET_TEMPO,
      SEQM_REMOVE_TEMPO,
      SEQM_ADD_SIG,
      SEQM_REMOVE_SIG,
      SEQM_SET_GLOBAL_TEMPO,
      SEQM_UNDO,
      SEQM_REDO,
      SEQM_RESET_DEVICES,
      SEQM_INIT_DEVICES,
      //
      AUDIO_ROUTEADD,
      AUDIO_ROUTEREMOVE,
      AUDIO_ADDPLUGIN,
      AUDIO_ADDMIDIPLUGIN,
      AUDIO_SET_SEG_SIZE,
      AUDIO_SET_CHANNELS,

      MS_PROCESS,
      MS_START,
      MS_STOP,
      MS_SET_RTC,

      SEQM_IDLE,
      SEQM_ADD_CTRL,
      SEQM_REMOVE_CTRL
      };

extern const char* seqMsgList[];  // for debug

//---------------------------------------------------------
//   Msg
//---------------------------------------------------------

struct AudioMsg : public ThreadMsg {   // this should be an union
      int serialNo;
      SndFile* downmix;
      Route route;
      int ival;
      int iival;
      CVal cval1, cval2;
      PluginI* plugin;
      MidiPluginI* mplugin;
      SynthI* synth;
      Part* spart;
      Part* dpart;
      Track* track;
      QList<Event>* el;

      const void *p1, *p2, *p3;
      Event ev1, ev2;
      char port, channel, ctrl;
      int a, b, c;
      Pos pos;
      unsigned time;
      };

class AudioOutput;


//---------------------------------------------------------
//   SeqTime
//    timing information for sequencer cycle
//---------------------------------------------------------

extern unsigned int segmentSize;

struct SeqTime {
      unsigned lastFrameTime; // free running counter

      // transport values for current cycle:
      Pos pos;                // current play position
      unsigned curTickPos;    // pos at start of frame during play/record
      unsigned nextTickPos;   // pos at start of next frame during play/record

      unsigned startFrame() const { return pos.frame(); }
      unsigned endFrame() const   { return startFrame() + segmentSize; }

      //---------------------------------------------------------
      //   tick2frame
      //    translate from tick to frameTime, this event has to
      //    be scheduled
      //---------------------------------------------------------

      unsigned tick2frame(unsigned tick) {
            return AL::tempomap.tick2frame(tick) - pos.frame() + lastFrameTime + segmentSize;
            }
      unsigned frame2tick(unsigned frame) {
            return AL::tempomap.frame2tick(frame - lastFrameTime + pos.frame());
            }
      };

//---------------------------------------------------------
//   Audio
//---------------------------------------------------------

class Audio {
   public:
      enum State {STOP, START_PLAY, PLAY, LOOP1, LOOP2, SYNC, PRECOUNT};

   private:
      bool recording;         // recording is active
      bool idle;              // do nothing in idle mode
      bool _freewheel;
      int _bounce;
      bool loopPassed;
      unsigned loopFrame;     // startframe of loop if in LOOP mode
      unsigned lmark;         // left loop position
      unsigned rmark;         // right loop position

      SeqTime _seqTime;
      Pos startRecordPos;
      Pos endRecordPos;

      int _curReadIndex;

      State state;
      bool updateController;

      AudioMsg* volatile msg;
      int fromThreadFdw, fromThreadFdr;  // message pipe

      int sigFd;              // pipe fd for messages to gui

      bool filterEvent(const MidiEvent* event, int type, bool thru);

      void startRolling();
      void stopRolling();

      void collectEvents(MidiTrack*, unsigned startTick, unsigned endTick);
      void processMsg();
      void initMidiDevices();
      void resetMidiDevices();

   public:
      Audio();
      virtual ~Audio() {}

      void process(unsigned frames, int jackState);
      bool sync(int state, unsigned frame);
      void shutdown();

      // transport:
      bool start();
      void stop();
      void seek(const Pos& pos);

      bool isPlaying() const    { return state == PLAY || state == LOOP1 || state == LOOP2; }
      bool isRecording() const  { return state == PLAY && recording; }

      //-----------------------------------------
      //   message interface
      //-----------------------------------------

      void msgSeek(const Pos&);
      void msgPlay(bool val);

      void msgRemoveTrack(Track*);
      void msgRemoveTracks();
      void msgMoveTrack(Track*, Track*);

      void msgAddEvent(const Event&, Part*, bool u = true);
      void msgAddEvents(QList<Event>* el, Part* part);
      void msgDeleteEvent(const Event&, Part*, bool u = true);
      void msgChangeEvent(const Event&, const Event&, Part*, bool u = true);

      void msgAddTempo(int tick, int tempo, bool doUndoFlag = true);
      void msgSetTempo(int tick, int tempo, bool doUndoFlag = true);
      void msgSetGlobalTempo(int val);
      void msgDeleteTempo(int tick, int tempo, bool doUndoFlag = true);
      void msgAddSig(int tick, const AL::TimeSignature&, bool doUndoFlag = true);
      void msgRemoveSig(int tick, int z, int n, bool doUndoFlag = true);
      void msgPanic();
      void sendMsg(AudioMsg*);
      bool sendMessage(AudioMsg* m, bool doUndo);
      void msgRoute(bool add, Route);
      void msgRemoveRoute(Route);
      void msgRemoveRoute1(Route);
      void msgAddRoute(Route);
      void msgAddRoute1(Route);
      void msgAddPlugin(AudioTrack*, int idx, PluginI* plugin, bool prefader);
      void msgAddMidiPlugin(MidiTrackBase*, int idx, MidiPluginI* plugin);
      void msgSetMute(AudioTrack*, bool val);
      void msgAddSynthI(SynthI* synth);
      void msgRemoveSynthI(SynthI* synth);
      void msgSetSegSize(int, int);
      void msgSetChannels(AudioTrack*, int);
      void msgSetOff(AudioTrack*, bool);
      void msgUndo();
      void msgRedo();
      void msgLocalOff();
      void msgInitMidiDevices();
      void msgResetMidiDevices();
      void msgIdle(bool);
      void msgBounce();
      void msgAddController(Track*, int id, unsigned time, CVal);
      void msgRemoveController(Track*, int id, unsigned time);
      void msgSetRtc();

      const Pos& getStartRecordPos() const { return startRecordPos; }
      const Pos& getEndRecordPos() const { return endRecordPos; }

      bool freewheel() const       { return _freewheel; }
      void setFreewheel(bool val);

      void sendMsgToGui(char c);
      bool bounce() const { return _bounce != 0; }
      MidiEvent* getMidiEvent();
      void popMidiEvent();
      int curReadIndex() const       { return _curReadIndex;  }

      const SeqTime* seqTime() const { return &_seqTime;      }
      };

extern int processAudio(unsigned long, void*);
extern void processAudio1(void*, void*);

extern Audio* audio;
class AudioDriver;
extern AudioDriver* audioDriver;   // current audio device in use
#endif