std::shared_ptr
[dcpomatic.git] / src / lib / audio_buffers.h
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/lib/audio_buffers.h
22  *  @brief AudioBuffers class.
23  */
24
25
26 #ifndef DCPOMATIC_AUDIO_BUFFERS_H
27 #define DCPOMATIC_AUDIO_BUFFERS_H
28
29
30 #include <stdint.h>
31 #include <memory>
32
33
34 /** @class AudioBuffers
35  *  @brief A class to hold multi-channel audio data in float format.
36  *
37  *  The use of int32_t for frame counts in this class is due to the
38  *  round-up to the next power-of-2 code in ensure_size(); if that
39  *  were changed the frame count could use any integer type.
40  */
41 class AudioBuffers
42 {
43 public:
44         AudioBuffers (int channels, int32_t frames);
45         AudioBuffers (AudioBuffers const &);
46         explicit AudioBuffers (std::shared_ptr<const AudioBuffers>);
47         AudioBuffers (std::shared_ptr<const AudioBuffers> other, int32_t frames_to_copy, int32_t read_offset);
48         ~AudioBuffers ();
49
50         AudioBuffers & operator= (AudioBuffers const &);
51
52         std::shared_ptr<AudioBuffers> clone () const;
53         std::shared_ptr<AudioBuffers> channel (int) const;
54
55         void ensure_size (int32_t);
56
57         float** data () const {
58                 return _data;
59         }
60
61         float* data (int) const;
62
63         int channels () const {
64                 return _channels;
65         }
66
67         int frames () const {
68                 return _frames;
69         }
70
71         void set_frames (int32_t f);
72
73         void make_silent ();
74         void make_silent (int c);
75         void make_silent (int32_t from, int32_t frames);
76
77         void apply_gain (float);
78
79         void copy_from (AudioBuffers const * from, int32_t frames_to_copy, int32_t read_offset, int32_t write_offset);
80         void copy_channel_from (AudioBuffers const * from, int from_channel, int to_channel);
81         void move (int32_t frames, int32_t from, int32_t to);
82         void accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel, float gain = 1);
83         void accumulate_frames (AudioBuffers const * from, int32_t frames, int32_t read_offset, int32_t write_offset);
84         void append (std::shared_ptr<const AudioBuffers> other);
85         void trim_start (int32_t frames);
86
87 private:
88         void allocate (int channels, int32_t frames);
89         void deallocate ();
90
91         /** Number of channels */
92         int _channels;
93         /** Number of frames (where a frame is one sample across all channels) */
94         int32_t _frames;
95         /** Number of frames that _data can hold */
96         int32_t _allocated_frames;
97         /** Audio data (so that, e.g. _data[2][6] is channel 2, sample 6) */
98         float** _data;
99 };
100
101 #endif