File: CaptureThread.h

package info (click to toggle)
fmit 1.2.6-0.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 2,828 kB
  • sloc: cpp: 9,490; sh: 62; makefile: 15
file content (392 lines) | stat: -rw-r--r-- 10,188 bytes parent folder | download
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
391
392
// Copyright 2004 "Gilles Degottex"

// This file is part of "Music"

// "Music" is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// "Music" 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


#ifndef _CaptureThread_h_
#define _CaptureThread_h_

#include <deque>
#include <vector>
using namespace std;
#include <qobject.h>
#include <qthread.h>
#include <qmutex.h>

class CaptureThread;

// ----------------------- the implementations ----------------------

class CaptureThreadImpl;

void AddValue2ChannelFirst(CaptureThreadImpl* impl, double value, int i);
void AddValue2ChannelMix(CaptureThreadImpl* impl, double value, int i);
void AddValue1Channel(CaptureThreadImpl* impl, double value, int i);

class CaptureThreadImpl
{
	friend class CaptureThread;
	friend void AddValue2ChannelFirst(CaptureThreadImpl* impl, double value, int i);
	friend void AddValue2ChannelMix(CaptureThreadImpl* impl, double value, int i);
	friend void AddValue1Channel(CaptureThreadImpl* impl, double value, int i);

  protected:
	CaptureThread* m_capture_thread;
	QString m_name;
	QString m_descr;

	int m_sampling_rate;
	QString m_source;
	QString m_status;

	int m_format_size;
	bool m_format_signed;
	bool m_format_float;
	int m_channel_count;

	virtual void setSamplingRate(int rate)=0;
	virtual void startCapture()=0;
	virtual void stopCapture()=0;
	virtual bool is_available()=0;

    double m_tmp_value; // used when two channels have to be mixed down
	double (*decodeValue)(void* buffer, int i); // not used for JACK
	void (*addValue)(CaptureThreadImpl* impl, double value, int i);
	void setFormatDescrsAndFns(int format_size, bool format_signed, bool format_float, int channel_count);

  public:
	CaptureThreadImpl(CaptureThread* capture_thread, const QString& name, const QString& descr);

	const QString& getStatus();

	const QString& getName() const {return m_name;}
	const QString& getDescription() const {return m_descr;}
	const QString& getSource() const {return m_source;}
    QString getASCIISource() const;

	virtual ~CaptureThreadImpl(){}
};

// ---------------------- the Qt implementation ---------------------

#ifdef CAPTURE_QT
#include <QAudioDeviceInfo>
#include <QAudioInput>
class CaptureThreadImplQt : public QObject, public CaptureThreadImpl
{
    Q_OBJECT

    void set_params(bool test=false);

    void capture_init();
    void capture_finished();

    const QList<QAudioDeviceInfo> m_availableAudioInputDevices;
    QAudioDeviceInfo    m_audioInputDevice;
    QAudioInput*        m_audioInput;
    QIODevice*          m_audioInputIODevice;

    QAudio::State       m_state;

    QAudioFormat        m_format;

  public:
    CaptureThreadImplQt(CaptureThread* capture_thread);

    virtual void setSamplingRate(int rate);
    virtual void startCapture();
    virtual void stopCapture();
    virtual bool is_available();

    ~CaptureThreadImplQt();

public slots:
    void audioStateChanged(QAudio::State state);
    void audioDataReady();
};
#endif

// ---------------------- the ALSA implementation ---------------------

#ifdef CAPTURE_ALSA
#include <alsa/asoundlib.h>
class CaptureThreadImplALSA : public CaptureThreadImpl, public QThread
{
	snd_pcm_t* m_alsa_capture_handle;
	snd_pcm_hw_params_t* m_alsa_hw_params;
	char* m_alsa_buffer;
	snd_pcm_format_t m_format;

	// view
	volatile bool m_alive;
	volatile bool m_in_run;

	// control
	volatile bool m_loop;
	volatile bool m_wait_for_start;

	void set_params(bool test=false);
	void capture_init();
	void capture_loop();
	void capture_finished();

	virtual void run();

  public:
	CaptureThreadImplALSA(CaptureThread* capture_thread);

	virtual void setSamplingRate(int rate);
	virtual void startCapture();
	virtual void stopCapture();
	virtual bool is_available();

	~CaptureThreadImplALSA();
};
#endif

// ---------------------- the JACK implementation ---------------------

#ifdef CAPTURE_JACK
#include <jack/jack.h>
#include <jack/ringbuffer.h>
class CaptureThreadImplJACK : public CaptureThreadImpl, public QThread
{
	static int JackProcess(jack_nframes_t nframes, void* arg);
	static void JackShutdown(void* arg);
	static int JackSampleRate(jack_nframes_t nframes, void* arg);

    jack_ringbuffer_t* m_ringbuffer;
	jack_client_t* m_jack_client;
	jack_port_t* m_jack_port;
	int jackSampleRate(jack_nframes_t nframes);
	int jackProcess(jack_nframes_t nframes);
	void jackShutdown();

    // view
    volatile bool m_alive;
    volatile bool m_in_run;

    // control
    volatile bool m_loop;
    volatile bool m_wait_for_start;

    void capture_init();
    void capture_loop();
	void capture_finished();

    virtual void run();

  public:
	CaptureThreadImplJACK(CaptureThread* capture_thread);

	virtual void setSamplingRate(int rate);
	virtual void startCapture();
	virtual void stopCapture();
	virtual bool is_available();
    
    ~CaptureThreadImplJACK();
};
#endif

// ---------------------- the PortAudio implementation ---------------------

#ifdef CAPTURE_PORTAUDIO
#include <portaudio.h>
class CaptureThreadImplPortAudio : public CaptureThreadImpl
{
	static int PortAudioCallback( const void *inputBuffer, void *outputBuffer,
							   unsigned long framesPerBuffer,
							   const PaStreamCallbackTimeInfo* timeInfo,
							   PaStreamCallbackFlags statusFlags,
							   void *userData );

	int portAudioCallback(const void *inputBuffer,
						  unsigned long framesPerBuffer,
						  const PaStreamCallbackTimeInfo* timeInfo,
						  PaStreamCallbackFlags statusFlags);

	PaStream* m_stream;
	PaError m_err;

	void set_params(bool test=false);
	virtual void capture_init();
	virtual void capture_finished();

  public:
	CaptureThreadImplPortAudio(CaptureThread* capture_thread);

	virtual void setSamplingRate(int rate);
	virtual void startCapture();
	virtual void stopCapture();
	virtual bool is_available();

	virtual ~CaptureThreadImplPortAudio();
};
#endif

// ---------------------- the OSS implementation ---------------------

#ifdef CAPTURE_OSS
class CaptureThreadImplOSS : public CaptureThreadImpl, public QThread
{
	int m_fd_in;
	char* m_oss_buffer;
	int m_format;

	// view
	volatile bool m_alive;
	volatile bool m_in_run;

	// control
	volatile bool m_loop;
	volatile bool m_wait_for_start;

	void set_params(bool test=false);
	void capture_init();
	void capture_loop();
	void capture_finished();

	virtual void run();

  public:
	CaptureThreadImplOSS(CaptureThread* capture_thread);

	virtual void setSamplingRate(int rate);
	virtual void startCapture();
	virtual void stopCapture();
	virtual bool is_available();

	~CaptureThreadImplOSS();
};
#endif

// --------------------- the real accessible thread -------------------------

class CaptureThread : public QObject
{
	Q_OBJECT

	friend class CaptureThreadImpl;
#ifdef CAPTURE_QT
    friend class CaptureThreadImplQt;
#endif
#ifdef CAPTURE_ALSA
	friend class CaptureThreadImplALSA;
#endif
#ifdef CAPTURE_JACK
	friend class CaptureThreadImplJACK;
#endif
#ifdef CAPTURE_PORTAUDIO
	friend class CaptureThreadImplPortAudio;
#endif
#ifdef CAPTURE_OSS
	friend class CaptureThreadImplOSS;
#endif

	vector<CaptureThreadImpl*> m_impls;
	CaptureThreadImpl* m_current_impl;

	void emitError(const QString& error);
	void emitSamplingRateChanged();
	void emitCaptureStarted();
	void emitCaptureStoped();
	void emitCaptureToggled(bool value);

	bool m_capturing;

	int m_packet_size;
	int m_packet_size_sll;
	QString m_name;

	// control
	volatile bool m_pause;
	bool m_mix_multiple_channel;

	QMutex m_lock;
	bool m_ext_lock;

  public:

	deque<double> m_values;

	enum {SAMPLING_RATE_UNKNOWN=-1, SAMPLING_RATE_MAX=0};

	CaptureThread(const QString& name="bastard_thread");

	void lock()						{m_lock.lock(); m_ext_lock=true;}
	void unlock()					{m_lock.unlock();}

	bool isCapturing() const						{return m_capturing;}
	int getSamplingRate() const;
	int getPacketSize() const						{return m_packet_size;}
	int getPacketSizeSinceLastLock() const			{return m_packet_size_sll;}
    int getNbPendingData() const					{return int(m_values.size());}
	const CaptureThreadImpl* getCurrentTransport() const;
	int getCurrentTransportIndex() const;
	QString getCurrentTransportDescr() const;
	QString getFormatDescr() const;
	const vector<CaptureThreadImpl*>& getTransports() const;
	const CaptureThreadImpl* getTransport(const QString& name) const;
	void listTransports();

	virtual ~CaptureThread();

  signals:
	void samplingRateChanged(int sampling_rate);
	void portNameChanged(const QString& name);
	void sourceChanged(const QString& src);
	void transportChanged(const QString& name);
	void captureStarted();
	void captureStoped();
	void captureToggled(bool run);
	void errorRaised(const QString& error);

  public slots:
	//! auto detect a working transport
	void autoDetectTransport();
	//! select a specific transport
	void selectTransport(const QString& name);
	//! select a specific transport
	void selectTransport(int index);
	//! reset capture (stop/start)
	void reset();
	//! start capture
	void startCapture();
	//! stop capture
	void stopCapture();
	//! set capture status
	void toggleCapture(bool run);
	//! set pause status
	/*! keep capture system connected, but throw away all incoming data
	 */
	void togglePause(bool pause);

	//! set the sampling rate
	/*! not always available, depending on the implementation
	 * (reset the capture system !)
	 */
	void setSamplingRate(int value);
	//! the source name
	/*! 'hw:0' for example for ALSA, something like alsa_pcm:capture_1 for JACK
	 * (reset the capture system !)
	 */
	void setSource(const QString& src);

	void setMixMultipleChannels(bool mix);
};

#endif // _CaptureThread_h_