1 /*************************************************************************
2          RecordThread.h  -  thread for lowlevel audio recording
3                              -------------------
4     begin                : Mon Oct 20 2003
5     copyright            : (C) 2003 by Thomas Eschenbacher
6     email                : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
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 as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef RECORD_THREAD_H
19 #define RECORD_THREAD_H
20 
21 #include "config.h"
22 
23 #include <QByteArray>
24 #include <QQueue>
25 #include <QRecursiveMutex>
26 
27 #include "libkwave/WorkerThread.h"
28 
29 namespace Kwave
30 {
31 
32     class RecordDevice;
33 
34     class RecordThread: public Kwave::WorkerThread
35     {
36 	Q_OBJECT
37     public:
38 
39 	/** Constructor */
40 	RecordThread();
41 
42 	/** Destructor */
43         virtual ~RecordThread() Q_DECL_OVERRIDE;
44 
45 	/** does the recording */
46         virtual void run() Q_DECL_OVERRIDE;
47 
48 	/**
49 	 * Select a new record device.
50 	 * @param device a RecordDevice that is opened and set up for reading
51 	 * @note this must not be called during recording
52 	 */
53 	void setRecordDevice(Kwave::RecordDevice *device);
54 
55 	/**
56 	 * Set the number of buffers and their size
57 	 * @param count the number of buffer, minimum allowed is two
58 	 * @param size the number of bytes for each buffer
59 	 * @return number of allocated buffers or -ENOMEM if less than two
60 	 * @note this must not be called during recording
61 	 */
62 	int setBuffers(unsigned int count, unsigned int size);
63 
64 	/** Returns the amount of remaining empty buffers */
65 	unsigned int remainingBuffers();
66 
67 	/** Returns the number of queued filled buffers */
68 	unsigned int queuedBuffers();
69 
70 	/** De-queues a buffer from the m_full_queue. */
71 	QByteArray dequeue();
72 
73     signals:
74 
75 	/**
76 	 * emitted when a buffer was full and has been de-queued
77 	 * with dequeue()
78 	 */
79 	void bufferFull();
80 
81 	/**
82 	 * emitted when the recording stops or aborts
83 	 * @param errorcode zero if stopped normally or a negative
84 	 *        error code if aborted
85 	 */
86 	void stopped(int errorcode);
87 
88     private:
89 
90 	/** lock for protecting the queues */
91 	QRecursiveMutex m_lock;
92 
93 	/** the device used as source */
94 	Kwave::RecordDevice *m_device;
95 
96 	/** queue with empty buffers for raw input data */
97 	QQueue<QByteArray>m_empty_queue;
98 
99 	/** queue with filled buffers with raw input data */
100 	QQueue<QByteArray>m_full_queue;
101 
102 	/** number of buffers to allocate */
103 	unsigned int m_buffer_count;
104 
105 	/** size of m_buffer in bytes */
106 	unsigned int m_buffer_size;
107 
108     };
109 }
110 
111 #endif /* RECORD_THREAD_H */
112 
113 //***************************************************************************
114 //***************************************************************************
115