63e4fac9d5089421374dcdb66033226df65c5e11
[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 #ifndef DCPOMATIC_AUDIO_BUFFERS_H
26 #define DCPOMATIC_AUDIO_BUFFERS_H
27
28 #include <boost/shared_ptr.hpp>
29 #include <stdint.h>
30
31 /** @class AudioBuffers
32  *  @brief A class to hold multi-channel audio data in float format.
33  *
34  *  The use of int32_t for frame counts in this class is due to the
35  *  round-up to the next power-of-2 code in ensure_size(); if that
36  *  were changed the frame count could use any integer type.
37  */
38 class AudioBuffers
39 {
40 public:
41         AudioBuffers (int channels, int32_t frames);
42         AudioBuffers (AudioBuffers const &);
43         explicit AudioBuffers (boost::shared_ptr<const AudioBuffers>);
44         AudioBuffers (boost::shared_ptr<const AudioBuffers> other, int32_t frames_to_copy, int32_t read_offset);
45         ~AudioBuffers ();
46
47         AudioBuffers & operator= (AudioBuffers const &);
48
49         boost::shared_ptr<AudioBuffers> clone () const;
50         boost::shared_ptr<AudioBuffers> channel (int) const;
51
52         void ensure_size (int32_t);
53
54         float** data () const {
55                 return _data;
56         }
57
58         float* data (int) const;
59
60         int channels () const {
61                 return _channels;
62         }
63
64         int frames () const {
65                 return _frames;
66         }
67
68         void set_frames (int32_t f);
69
70         void make_silent ();
71         void make_silent (int c);
72         void make_silent (int32_t from, int32_t frames);
73
74         void apply_gain (float);
75
76         void copy_from (AudioBuffers const * from, int32_t frames_to_copy, int32_t read_offset, int32_t write_offset);
77         void copy_channel_from (AudioBuffers const * from, int from_channel, int to_channel);
78         void move (int32_t frames, int32_t from, int32_t to);
79         void accumulate_channel (AudioBuffers const * from, int from_channel, int to_channel, float gain = 1);
80         void accumulate_frames (AudioBuffers const * from, int32_t frames, int32_t read_offset, int32_t write_offset);
81         void append (boost::shared_ptr<const AudioBuffers> other);
82         void trim_start (int32_t frames);
83
84 private:
85         void allocate (int channels, int32_t frames);
86         void deallocate ();
87
88         /** Number of channels */
89         int _channels;
90         /** Number of frames (where a frame is one sample across all channels) */
91         int32_t _frames;
92         /** Number of frames that _data can hold */
93         int32_t _allocated_frames;
94         /** Audio data (so that, e.g. _data[2][6] is channel 2, sample 6) */
95         float** _data;
96 };
97
98 #endif