File: Record-ALSA.h

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (288 lines) | stat: -rw-r--r-- 9,982 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
/*************************************************************************
          Record-ALSA.h  -  device for audio recording via ALSA
                             -------------------
    begin                : Sun Jul 24 2005
    copyright            : (C) 2005 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef RECORD_ALSA_H
#define RECORD_ALSA_H

#include "config.h"
#ifdef HAVE_ALSA_SUPPORT

/*
 * use the new ALSA HW/SW params API, needed to compile under SuSE-9.0
 * (workaround as seen in http://www.linuxjournal.com/article/6735)
 */
#define ALSA_PCM_NEW_HW_PARAMS_API
#define ALSA_PCM_NEW_SW_PARAMS_API

#include <alsa/asoundlib.h>

#include <QList>
#include <QMap>
#include <QString>

#include "libkwave/Compression.h"
#include "libkwave/SampleFormat.h"

#include "RecordDevice.h"

namespace Kwave
{
    class RecordALSA: public Kwave::RecordDevice
    {
    public:

        /** Constructor */
        RecordALSA();

        /** Destructor */
        ~RecordALSA() override;

        /**
         * Open the record device.
         * @param dev path of the record device
         * @retval QString() if successful
         * @retval QString::number(ENODEV) if device not found
         * @retval QString::number(EBUSY) if device is busy
         * @retval QString::number(EINVAL) on invalid parameters
         * @retval QString(...) device specific error message
         *                      (already translated)
         */
        QString open(const QString &dev) override;

        /**
         * Read the raw audio data from the record device.
         * @param buffer array of bytes to receive the audio data
         *        might be resized for alignment
         * @param offset offset in bytes within the buffer
         * @return number of bytes read, zero or negative if failed
         */
        virtual int read(QByteArray &buffer, unsigned int offset)
            override;

        /** Close the device */
        int close() override;

        /** return a string list with supported device names */
        QStringList supportedDevices() override;

        /**
         * Detect the minimum and maximum number of tracks.
         * If the detection fails, minimum and maximum are set to zero.
         * @param min receives the lowest supported number of tracks
         * @param max receives the highest supported number of tracks
         * @return zero or positive number if ok,
         *         negative error number if failed
         */
        virtual int detectTracks(unsigned int &min, unsigned int &max)
            override;

        /**
         * Try to set a new number of tracks.
         * @note the device must be open
         * @param tracks the number of tracks to be set, can be modified and
         *        decreased to the next supported number of tracks if the
         *        underlying driver supports that.
         * @return zero on success, negative error code if failed
         */
        int setTracks(unsigned int &tracks) override;

        /** Returns the current number of tracks */
        int tracks() override;

        /** get a list of supported sample rates */
        QList<double> detectSampleRates() override;

        /**
         * Try to set a new sample rate.
         * @param new_rate the sample rate to be set [samples/second], can
         *        be modified and rounded up/down to the nearest supported
         *        sample rate if the underlying driver supports that.
         * @return zero on success, negative error code if failed
         */
        int setSampleRate(double &new_rate) override;

        /** Returns the current sample rate of the device */
        double sampleRate() override;

        /**
         * Gets a list of supported compression types. If no compression is
         * supported, the list might be empty.
         */
        virtual QList<Kwave::Compression::Type> detectCompressions()
            override;

        /**
         * Try to set a new compression type.
         * @param new_compression the identifier of the new compression
         * @return zero on success, negative error code if failed
         * @see class Compression
         */
        virtual int setCompression(Kwave::Compression::Type new_compression)
            override;

        /** Returns the current compression type (0==none) */
        Kwave::Compression::Type compression() override;

        /**
         * Detect a list of supported bits per sample.
         * @note this depends on the compression type
         * @return a list of bits per sample, empty if failed
         */
        QList<unsigned int> supportedBits() override;

        /**
         * Set the resolution in bits per sample
         * @param new_bits resolution [bits/sample]
         */
        int setBitsPerSample(unsigned int new_bits) override;

        /**
         * Returns the current resolution in bits per sample or a negative
         * error code if failed
         */
        int bitsPerSample() override;

        /**
         * Gets a list of supported sample formats.
         * @note this depends on the current setting of the compression!
         */
        virtual QList<Kwave::SampleFormat::Format> detectSampleFormats()
            override;

        /**
         * Try to set a new sample format (signed/unsigned)
         * @param new_format the identifier for the new format
         * @return zero on success, negative error code if failed
         * @see class SampleFormat
         */
        virtual int setSampleFormat(Kwave::SampleFormat::Format new_format)
            override;

        /** Returns the current sample format (signed/unsigned) */
        Kwave::SampleFormat::Format sampleFormat() override;

        /** Returns the current endianness (big/little) */
        Kwave::byte_order_t endianness() override;

    private:

        /**
         * Walk through the list of all known formats and collect the
         * ones that are supported into "m_supported_formats".
         */
        void detectSupportedFormats();

        /**
         * Initialize the ALSA device with current parameters and
         * prepare it for recording.
         * @return zero on success or negative error code
         *         -EINVAL or -EIO
         */
        int initialize();

        /**
         * create a ALSA device format (enum) from parameters.
         * @param compression the compression type
         * @see Compression
         * @param bits the number of bits per sample, related
         *        to the decoded stream
         * @param sample_format the sample format, as defined in
         *        libaudiofile (signed or unsigned)
         * @return the index of the best matching format within the list
         *         of known formats, or -1 if no match was found
         */
        int mode2format(Kwave::Compression::Type compression, int bits,
                        Kwave::SampleFormat::Format sample_format);

        /** scan all ALSA devices, re-creates m_device_list */
        void scanDevices();

        /**
         * Translate a verbose device name into a ALSA hardware device name.
         *
         * @param name verbose name of the device
         * @return device name that can be used for snd_pcm_open()
         */
        QString alsaDeviceName(const QString &name);

    private:

        /** handle of the source device or null if not open */
        snd_pcm_t *m_handle;

        /** ALSA hardware parameters */
        snd_pcm_hw_params_t *m_hw_params;

        /** ALSA software parameters */
        snd_pcm_sw_params_t *m_sw_params;

        /** result of the "open" call, of interest when m_handle == 0 */
        int m_open_result;

        /**
         * dictionary for translating verbose device names
         * into ALSA hardware device names
         */
        static QMap<QString, QString> m_device_list;

        /** number of tracks [0...N-1] */
        unsigned int m_tracks;

        /** sample rate  */
        double m_rate;

        /** compression mode */
        Kwave::Compression::Type m_compression;

        /** resolution [bits per sample] */
        unsigned int m_bits_per_sample;

        /**
         * Number of bytes per sample, already multiplied with
         * the number of channels (m_channels)
         */
        unsigned int m_bytes_per_sample;

        /** sample format (signed int, unsigned int, float, ... */
        Kwave::SampleFormat::Format m_sample_format;

        /**
         * list of supported formats of the current device, indices in
         * the global list of known formats.
         * Only valid after a successful call to "open()",
         * otherwise empty
         */
        QList<int> m_supported_formats;

        /** true if initialize() has been successfully been run */
        bool m_initialized;

        /** size of the transfer buffer in bytes */
        unsigned int m_buffer_size;

        /** number of samples per period */
        snd_pcm_uframes_t m_chunk_size;

    };
}

#endif /* HAVE_ALSA_SUPPORT */

#endif /* RECORD_ALSA_H */

//***************************************************************************
//***************************************************************************