File: SampleArray.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 (140 lines) | stat: -rw-r--r-- 4,316 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
/*************************************************************************
          SampleArray.h  -  array with Kwave's internal sample_t
                             -------------------
    begin                : Sun Oct 07 2007
    copyright            : (C) 2007 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_ARRAY_H
#define SAMPLE_ARRAY_H

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

#include <QtGlobal>
#include <QSharedData>
#include <QSharedDataPointer>

#include "libkwave/Sample.h"

namespace Kwave
{

    /**
     * array with sample_t, for use in Kwave::SampleSource, Kwave::SampleSink
     * and other streaming classes.
     */
    class LIBKWAVE_EXPORT SampleArray
    {
    public:

        /** Default constructor, creates an empty array */
        SampleArray();

        /**
         * Constructor, creates an array with predefined size
         * (not initialized)
         * @param size number of samples to hold
         */
        explicit SampleArray(unsigned int size);

        /** Destructor */
        virtual ~SampleArray();

        /** returns a const pointer to the raw data (non-mutable) */
        inline const sample_t * constData() const
        {
            if (Q_UNLIKELY(!m_storage)) return nullptr;
            return m_storage->m_data;
        }

        /** returns a pointer to the raw data (mutable) */
        inline sample_t *data() /* __attribute__((deprecated)) <- for debug */
        {
            if (Q_UNLIKELY(!m_storage)) return nullptr;
            return m_storage->m_data;
        }

        /** fills the array with a sample value */
        void fill(sample_t value);

        /**
         * operator [], non-const.
         * @param index sample index [0...count()-1]
         * @return reference to the requested sample (read/write)
         */
        sample_t & operator [] (unsigned int index);

        /**
         * operator [], non-const.
         * @param index sample index [0...count()-1]
         * @return reference to the requested sample (read only)
         */
        const sample_t & operator [] (unsigned int index) const;

        /**
         * Resizes the array
         * @param size new number of samples
         * @return true if succeeded, false if failed
         */
        bool resize(unsigned int size);

        /**
         * Returns the number of samples.
         * @return samples [0...N]
         */
        unsigned int size() const;

        /**
         * Returns whether the array is empty.
         * The same as (size() == 0).
         * @return true if empty, false if not
         */
        inline bool isEmpty() const { return (size() == 0); }

    private:

        class SampleStorage: public QSharedData {
        public:

            /** default constructor */
            SampleStorage();

            /** copy constructor */
            SampleStorage(const SampleStorage &other);

            /** destructor */
            virtual ~SampleStorage();

            /**
             * Resizes the array
             * @param size new number of samples
             */
            void resize(unsigned int size);

        public:
            /** size in samples */
            unsigned int m_size;

            /** pointer to the area with the samples (allocated) */
            sample_t *m_data;
        };

        QSharedDataPointer<SampleStorage> m_storage;
    };
}

#endif /* SAMPLE_ARRAY_H */

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