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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
// -*- c-basic-offset: 4 -*-
/*
Rosegarden
A sequencer and musical notation editor.
This program is Copyright 2000-2008
Guillaume Laurent <[email protected]>,
Chris Cannam <[email protected]>,
Richard Bown <[email protected]>
The moral right of the authors to claim authorship of this work
has been asserted.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#ifndef _AUDIO_PROCESS_H_
#define _AUDIO_PROCESS_H_
#include "SoundDriver.h"
#include "Instrument.h"
#include "RealTime.h"
#include "RingBuffer.h"
#include "RunnablePluginInstance.h"
#include "AudioPlayQueue.h"
#include "RecordableAudioFile.h"
namespace Rosegarden
{
class AudioThread
{
public:
typedef float sample_t;
AudioThread(std::string name, // for diagnostics
SoundDriver *driver,
unsigned int sampleRate);
virtual ~AudioThread();
// This is to be called by the owning class after construction.
void run();
// This is to be called by the owning class to cause the thread to
// exit and clean up, before destruction.
void terminate();
bool running() const { return m_running; }
int getLock();
int tryLock();
int releaseLock();
void signal();
protected:
virtual void threadRun() = 0;
virtual int getPriority() { return 0; }
std::string m_name;
SoundDriver *m_driver;
unsigned int m_sampleRate;
pthread_t m_thread;
pthread_mutex_t m_lock;
pthread_cond_t m_condition;
bool m_running;
volatile bool m_exiting;
private:
static void *staticThreadRun(void *arg);
static void staticThreadCleanup(void *arg);
};
class AudioInstrumentMixer;
class AudioBussMixer : public AudioThread
{
public:
AudioBussMixer(SoundDriver *driver,
AudioInstrumentMixer *instrumentMixer,
unsigned int sampleRate,
unsigned int blockSize);
virtual ~AudioBussMixer();
void kick(bool wantLock = true, bool signalInstrumentMixer = true);
/**
* Prebuffer. This should be called only when the transport is
* not running. This also calls fillBuffers on the instrument
* mixer.
*/
void fillBuffers(const RealTime ¤tTime);
/**
* Empty and discard buffer contents.
*/
void emptyBuffers();
int getBussCount() {
return m_bussCount;
}
/**
* A buss is "dormant" if every readable sample on every one of
* its buffers is zero. It can therefore be safely skipped during
* playback.
*/
bool isBussDormant(int buss) {
return m_bufferMap[buss].dormant;
}
/**
* Busses are currently always stereo.
*/
RingBuffer<sample_t> *getRingBuffer(int buss, unsigned int channel) {
if (channel < m_bufferMap[buss].buffers.size()) {
return m_bufferMap[buss].buffers[channel];
} else {
return 0;
}
}
/// For call from MappedStudio. Pan is in range -100.0 -> 100.0
void setBussLevels(int buss, float dB, float pan);
/// For call regularly from anywhere in a non-RT thread
void updateInstrumentConnections();
protected:
virtual void threadRun();
void processBlocks();
void generateBuffers();
AudioInstrumentMixer *m_instrumentMixer;
unsigned int m_blockSize;
int m_bussCount;
std::vector<sample_t *> m_processBuffers;
struct BufferRec
{
BufferRec() : dormant(true), buffers(), instruments(),
gainLeft(0.0), gainRight(0.0) { }
~BufferRec();
bool dormant;
std::vector<RingBuffer<sample_t> *> buffers;
std::vector<bool> instruments; // index is instrument id minus base
float gainLeft;
float gainRight;
};
typedef std::map<int, BufferRec> BufferMap;
BufferMap m_bufferMap;
};
class AudioFileReader;
class AudioFileWriter;
class AudioInstrumentMixer : public AudioThread
{
public:
typedef std::vector<RunnablePluginInstance *> PluginList;
typedef std::map<InstrumentId, PluginList> PluginMap;
typedef std::map<InstrumentId, RunnablePluginInstance *> SynthPluginMap;
AudioInstrumentMixer(SoundDriver *driver,
AudioFileReader *fileReader,
unsigned int sampleRate,
unsigned int blockSize);
virtual ~AudioInstrumentMixer();
void kick(bool wantLock = true);
void setBussMixer(AudioBussMixer *mixer) { m_bussMixer = mixer; }
void setPlugin(InstrumentId id, int position, TQString identifier);
void removePlugin(InstrumentId id, int position);
void removeAllPlugins();
void setPluginPortValue(InstrumentId id, int position,
unsigned int port, float value);
float getPluginPortValue(InstrumentId id, int position,
unsigned int port);
void setPluginBypass(InstrumentId, int position, bool bypass);
TQStringList getPluginPrograms(InstrumentId, int);
TQString getPluginProgram(InstrumentId, int);
TQString getPluginProgram(InstrumentId, int, int, int);
unsigned long getPluginProgram(InstrumentId, int, TQString);
void setPluginProgram(InstrumentId, int, TQString);
TQString configurePlugin(InstrumentId, int, TQString, TQString);
void resetAllPlugins(bool discardEvents = false);
void discardPluginEvents();
void destroyAllPlugins();
RunnablePluginInstance *getSynthPlugin(InstrumentId id) { return m_synths[id]; }
/**
* Return the plugins intended for a particular buss. (By coincidence,
* this will also work for instruments, but it's not to be relied on.)
* It's purely by historical accident that the instrument mixer happens
* to hold buss plugins as well -- this could do with being refactored.
*/
PluginList &getBussPlugins(unsigned int bussId) { return m_plugins[bussId]; }
/**
* Return the total of the plugin latencies for a given instrument
* or buss id.
*/
size_t getPluginLatency(unsigned int id);
/**
* Prebuffer. This should be called only when the transport is
* not running.
*/
void fillBuffers(const RealTime ¤tTime);
/**
* Ensure plugins etc have enough buffers. This is also done by
* fillBuffers and only needs to be called here if the extra work
* involved in fillBuffers is not desirable.
*/
void allocateBuffers();
/**
* Empty and discard buffer contents.
*/
void emptyBuffers(RealTime currentTime = RealTime::zeroTime);
/**
* An instrument is "empty" if it has no audio files, synths or
* plugins assigned to it, and so cannot generate sound. Empty
* instruments can safely be ignored during playback.
*/
bool isInstrumentEmpty(InstrumentId id) {
return m_bufferMap[id].empty;
}
/**
* An instrument is "dormant" if every readable sample on every
* one of its buffers is zero. Dormant instruments can safely be
* skipped rather than mixed during playback, but they should not
* be ignored (unless also empty).
*/
bool isInstrumentDormant(InstrumentId id) {
return m_bufferMap[id].dormant;
}
/**
* We always have at least two channels (and hence buffers) by
* this point, because even on a mono instrument we still have a
* Pan setting which will have been applied by the time we get to
* these buffers.
*/
RingBuffer<sample_t, 2> *getRingBuffer(InstrumentId id, unsigned int channel) {
if (channel < m_bufferMap[id].buffers.size()) {
return m_bufferMap[id].buffers[channel];
} else {
return 0;
}
}
/// For call from MappedStudio. Pan is in range -100.0 -> 100.0
void setInstrumentLevels(InstrumentId instrument, float dB, float pan);
/// For call regularly from anywhere in a non-RT thread
void updateInstrumentMuteStates();
protected:
virtual void threadRun();
virtual int getPriority() { return 3; }
void processBlocks(bool &readSomething);
void processEmptyBlocks(InstrumentId id);
bool processBlock(InstrumentId id, PlayableAudioFile **, size_t, bool &readSomething);
void generateBuffers();
AudioFileReader *m_fileReader;
AudioBussMixer *m_bussMixer;
unsigned int m_blockSize;
// The plugin data structures will all be pre-sized and so of
// fixed size during normal run time; this will allow us to add
// and edit plugins without locking.
RunnablePluginInstance *getPluginInstance(InstrumentId, int);
PluginMap m_plugins;
SynthPluginMap m_synths;
// maintain the same number of these as the maximum number of
// channels on any audio instrument
std::vector<sample_t *> m_processBuffers;
struct BufferRec
{
BufferRec() : empty(true), dormant(true), zeroFrames(0),
filledTo(RealTime::zeroTime), channels(2),
buffers(), gainLeft(0.0), gainRight(0.0), volume(0.0),
muted(false) { }
~BufferRec();
bool empty;
bool dormant;
size_t zeroFrames;
RealTime filledTo;
size_t channels;
std::vector<RingBuffer<sample_t, 2> *> buffers;
float gainLeft;
float gainRight;
float volume;
bool muted;
};
typedef std::map<InstrumentId, BufferRec> BufferMap;
BufferMap m_bufferMap;
};
class AudioFileReader : public AudioThread
{
public:
AudioFileReader(SoundDriver *driver,
unsigned int sampleRate);
virtual ~AudioFileReader();
bool kick(bool wantLock = true);
/**
* Prebuffer. This should be called only when the transport is
* not running.
*/
void fillBuffers(const RealTime ¤tTime);
protected:
virtual void threadRun();
};
class AudioFileWriter : public AudioThread
{
public:
AudioFileWriter(SoundDriver *driver,
unsigned int sampleRate);
virtual ~AudioFileWriter();
void kick(bool wantLock = true);
bool openRecordFile(InstrumentId id, const std::string &fileName);
bool closeRecordFile(InstrumentId id, AudioFileId &returnedId);
bool haveRecordFileOpen(InstrumentId id);
bool haveRecordFilesOpen();
void write(InstrumentId id, const sample_t *, int channel, size_t samples);
protected:
virtual void threadRun();
typedef std::pair<AudioFile *, RecordableAudioFile *> FilePair;
typedef std::map<InstrumentId, FilePair> FileMap;
FileMap m_files;
};
}
#endif
|