File: SampleBuffer.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 (124 lines) | stat: -rw-r--r-- 3,858 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
/*************************************************************************
         SampleBuffer.h  -  simple buffer for sample arrays
                             -------------------
    begin                : Sun Oct 17 2010
    copyright            : (C) 2010 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 SAMPLE_BUFFER_H
#define SAMPLE_BUFFER_H

#include "config.h"
#include "libkwave_export.h"

#include <QObject>
#include <QSemaphore>

#include "libkwave/SampleArray.h"
#include "libkwave/SampleSink.h"

//***************************************************************************
namespace Kwave
{

    class LIBKWAVE_EXPORT SampleBuffer: public Kwave::SampleSink
    {
        Q_OBJECT
    public:

        /**
         * Constructor
         * @param parent a parent object, passed to QObject (optional)
         */
        explicit SampleBuffer(QObject *parent = nullptr);

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

        /** returns true if no sample data is present */
        virtual bool isEmpty() const;

        /** returns a reference to the sample data */
        virtual Kwave::SampleArray &data();

        /** returns a const reference to the sample data */
        virtual const Kwave::SampleArray &constData() const;

        /** returns the number of samples that can be fetched with get() */
        virtual unsigned int available() const;

        /**
         * returns a pointer to the raw data and advances the internal
         * offset afterwards
         * @param len maximum number of samples to get
         * @return pointer to the raw data in the buffer
         */
        virtual const sample_t *get(unsigned int len);

        /**
         * Appends one samples to the buffer. If the buffer is full afterwards,
         * the buffer will be emitted.
         *
         * @param sample a single sample
         */
        void put(sample_t sample);

        /** emit the sample data stored in m_data */
        virtual void finished();

    public slots:

        /** slot for taking input data, stores it into m_data */
        virtual void input(Kwave::SampleArray data);

    protected:

        friend class BufferJob;

        /**
         * Emit data from this object, used from a buffer thread
         * @param data a sample array to emit
         */
        void emitData(Kwave::SampleArray data);

    private:

        /** enqueue some data */
        void enqueue(Kwave::SampleArray data);

    signals:

        /** emits the data received via input() */
        void output(Kwave::SampleArray data);

    private:

        /** array with sample data */
        Kwave::SampleArray m_data;

        /** offset within the data, for reading */
        unsigned int m_offset;

        /** number of samples buffered, e.g. through put() */
        unsigned int m_buffered;

        /** semaphore for limiting queue depth to 1 */
        QSemaphore m_sema;
    };

}

#endif /* SAMPLE_BUFFER_H */

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