Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / base / audio_bus.h
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef MEDIA_BASE_AUDIO_BUS_H_
6 #define MEDIA_BASE_AUDIO_BUS_H_
7
8 #include <vector>
9
10 #include "base/memory/aligned_memory.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "media/base/media_export.h"
13
14 namespace media {
15 class AudioParameters;
16
17 // Scoped container for "busing" audio channel data around.  Each channel is
18 // stored in planar format and guaranteed to be aligned by kChannelAlignment.
19 // AudioBus objects can be created normally or via wrapping.  Normally, AudioBus
20 // will dice up a contiguous memory block for channel data.  When wrapped,
21 // AudioBus instead routes requests for channel data to the wrapped object.
22 class MEDIA_EXPORT AudioBus {
23  public:
24   // Guaranteed alignment of each channel's data; use 16-byte alignment for easy
25   // SSE optimizations.
26   enum { kChannelAlignment = 16 };
27
28   // Creates a new AudioBus and allocates |channels| of length |frames|.  Uses
29   // channels() and frames_per_buffer() from AudioParameters if given.
30   static scoped_ptr<AudioBus> Create(int channels, int frames);
31   static scoped_ptr<AudioBus> Create(const AudioParameters& params);
32
33   // Creates a new AudioBus with the given number of channels, but zero length.
34   // It's expected to be used with SetChannelData() and set_frames() to
35   // wrap externally allocated memory.
36   static scoped_ptr<AudioBus> CreateWrapper(int channels);
37
38   // Creates a new AudioBus from an existing channel vector.  Does not transfer
39   // ownership of |channel_data| to AudioBus; i.e., |channel_data| must outlive
40   // the returned AudioBus.  Each channel must be aligned by kChannelAlignment.
41   static scoped_ptr<AudioBus> WrapVector(
42       int frames, const std::vector<float*>& channel_data);
43
44   // Creates a new AudioBus by wrapping an existing block of memory.  Block must
45   // be at least CalculateMemorySize() bytes in size.  |data| must outlive the
46   // returned AudioBus.  |data| must be aligned by kChannelAlignment.
47   static scoped_ptr<AudioBus> WrapMemory(int channels, int frames, void* data);
48   static scoped_ptr<AudioBus> WrapMemory(const AudioParameters& params,
49                                          void* data);
50   static int CalculateMemorySize(const AudioParameters& params);
51
52   // Calculates the required size for an AudioBus given the number of channels
53   // and frames.
54   static int CalculateMemorySize(int channels, int frames);
55
56   // Helper methods for converting an AudioBus from and to interleaved integer
57   // data.  Expects interleaving to be [ch0, ch1, ..., chN, ch0, ch1, ...] with
58   // |bytes_per_sample| per value.  Values are scaled and bias corrected during
59   // conversion.  ToInterleaved() will also clip values to format range.
60   // Handles uint8, int16, and int32 currently.  FromInterleaved() will zero out
61   // any unfilled frames when |frames| is less than frames().
62   void FromInterleaved(const void* source, int frames, int bytes_per_sample);
63   void ToInterleaved(int frames, int bytes_per_sample, void* dest) const;
64   void ToInterleavedPartial(int start_frame, int frames, int bytes_per_sample,
65                             void* dest) const;
66
67   // Similar to FromInterleaved() above, but meant for streaming sources.  Does
68   // not zero out remaining frames, the caller is responsible for doing so using
69   // ZeroFramesPartial().  Frames are deinterleaved from the start of |source|
70   // to channel(x)[start_frame].
71   void FromInterleavedPartial(const void* source, int start_frame, int frames,
72                               int bytes_per_sample);
73
74   // Helper method for copying channel data from one AudioBus to another.  Both
75   // AudioBus object must have the same frames() and channels().
76   void CopyTo(AudioBus* dest) const;
77
78   // Helper method to copy frames from one AudioBus to another. Both AudioBus
79   // objects must have the same number of channels(). |source_start_frame| is
80   // the starting offset. |dest_start_frame| is the starting offset in |dest|.
81   // |frame_count| is the number of frames to copy.
82   void CopyPartialFramesTo(int source_start_frame,
83                            int frame_count,
84                            int dest_start_frame,
85                            AudioBus* dest) const;
86
87   // Returns a raw pointer to the requested channel.  Pointer is guaranteed to
88   // have a 16-byte alignment.  Warning: Do not rely on having sane (i.e. not
89   // inf, nan, or between [-1.0, 1.0]) values in the channel data.
90   float* channel(int channel) { return channel_data_[channel]; }
91   const float* channel(int channel) const { return channel_data_[channel]; }
92   void SetChannelData(int channel, float* data);
93
94   int channels() const { return static_cast<int>(channel_data_.size()); }
95   int frames() const { return frames_; }
96   void set_frames(int frames);
97
98   // Helper method for zeroing out all channels of audio data.
99   void Zero();
100   void ZeroFrames(int frames);
101   void ZeroFramesPartial(int start_frame, int frames);
102
103   // Scale internal channel values by |volume| >= 0.  If an invalid value
104   // is provided, no adjustment is done.
105   void Scale(float volume);
106
107  private:
108   friend struct base::DefaultDeleter<AudioBus>;
109   ~AudioBus();
110
111   AudioBus(int channels, int frames);
112   AudioBus(int channels, int frames, float* data);
113   AudioBus(int frames, const std::vector<float*>& channel_data);
114   explicit AudioBus(int channels);
115
116   // Helper method for building |channel_data_| from a block of memory.  |data|
117   // must be at least BlockSize() bytes in size.
118   void BuildChannelData(int channels, int aligned_frame, float* data);
119
120   // Contiguous block of channel memory.
121   scoped_ptr<float, base::AlignedFreeDeleter> data_;
122
123   std::vector<float*> channel_data_;
124   int frames_;
125
126   // Protect SetChannelData() and set_frames() for use by CreateWrapper().
127   bool can_set_channel_data_;
128
129   DISALLOW_COPY_AND_ASSIGN(AudioBus);
130 };
131
132 }  // namespace media
133
134 #endif  // MEDIA_BASE_AUDIO_BUS_H_