Add a virtual beamforming audio device on ChromeOS.
[chromium-blink-merge.git] / media / audio / audio_parameters.h
blob61ca8121f9e98e20bcae5e7511579b81cb773815
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.
5 #ifndef MEDIA_AUDIO_AUDIO_PARAMETERS_H_
6 #define MEDIA_AUDIO_AUDIO_PARAMETERS_H_
8 #include <stdint.h>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/time/time.h"
14 #include "media/audio/point.h"
15 #include "media/base/audio_bus.h"
16 #include "media/base/channel_layout.h"
17 #include "media/base/media_export.h"
19 namespace media {
21 // Use a struct-in-struct approach to ensure that we can calculate the required
22 // size as sizeof(AudioInputBufferParameters) + #(bytes in audio buffer) without
23 // using packing. Also align AudioInputBufferParameters instead of in
24 // AudioInputBuffer to be able to calculate size like so. Use a macro for the
25 // alignment value that's the same as AudioBus::kChannelAlignment, since MSVC
26 // doesn't accept the latter to be used.
27 #if defined(OS_WIN)
28 #pragma warning(push)
29 #pragma warning(disable: 4324) // Disable warning for added padding.
30 #endif
31 #define PARAMETERS_ALIGNMENT 16
32 COMPILE_ASSERT(AudioBus::kChannelAlignment == PARAMETERS_ALIGNMENT,
33 AudioInputBufferParameters_alignment_not_same_as_AudioBus);
34 struct MEDIA_EXPORT ALIGNAS(PARAMETERS_ALIGNMENT) AudioInputBufferParameters {
35 double volume;
36 uint32 size;
37 uint32_t hardware_delay_bytes;
38 uint32_t id;
39 bool key_pressed;
41 #undef PARAMETERS_ALIGNMENT
42 #if defined(OS_WIN)
43 #pragma warning(pop)
44 #endif
46 COMPILE_ASSERT(
47 sizeof(AudioInputBufferParameters) % AudioBus::kChannelAlignment == 0,
48 AudioInputBufferParameters_not_aligned);
50 struct MEDIA_EXPORT AudioInputBuffer {
51 AudioInputBufferParameters params;
52 int8 audio[1];
55 class MEDIA_EXPORT AudioParameters {
56 public:
57 // TODO(miu): Rename this enum to something that correctly reflects its
58 // semantics, such as "TransportScheme."
59 enum Format {
60 AUDIO_PCM_LINEAR = 0, // PCM is 'raw' amplitude samples.
61 AUDIO_PCM_LOW_LATENCY, // Linear PCM, low latency requested.
62 AUDIO_FAKE, // Creates a fake AudioOutputStream object.
63 AUDIO_FORMAT_LAST = AUDIO_FAKE, // Only used for validation of format.
66 enum {
67 // Telephone quality sample rate, mostly for speech-only audio.
68 kTelephoneSampleRate = 8000,
69 // CD sampling rate is 44.1 KHz or conveniently 2x2x3x3x5x5x7x7.
70 kAudioCDSampleRate = 44100,
73 // Bitmasks to determine whether certain platform (typically hardware) audio
74 // effects should be enabled.
75 enum PlatformEffectsMask {
76 NO_EFFECTS = 0x0,
77 ECHO_CANCELLER = 0x1,
78 DUCKING = 0x2, // Enables ducking if the OS supports it.
79 KEYBOARD_MIC = 0x4,
80 HOTWORD = 0x8,
83 AudioParameters();
84 AudioParameters(Format format,
85 ChannelLayout channel_layout,
86 int sample_rate,
87 int bits_per_sample,
88 int frames_per_buffer);
90 ~AudioParameters();
92 // Re-initializes all members.
93 void Reset(Format format,
94 ChannelLayout channel_layout,
95 int sample_rate,
96 int bits_per_sample,
97 int frames_per_buffer);
99 // Checks that all values are in the expected range. All limits are specified
100 // in media::Limits.
101 bool IsValid() const;
103 // Returns a human-readable string describing |*this|. For debugging & test
104 // output only.
105 std::string AsHumanReadableString() const;
107 // Returns size of audio buffer in bytes.
108 int GetBytesPerBuffer() const;
110 // Returns the number of bytes representing one second of audio.
111 int GetBytesPerSecond() const;
113 // Returns the number of bytes representing a frame of audio.
114 int GetBytesPerFrame() const;
116 // Returns the duration of this buffer as calculated from frames_per_buffer()
117 // and sample_rate().
118 base::TimeDelta GetBufferDuration() const;
120 // Comparison with other AudioParams.
121 bool Equals(const AudioParameters& other) const;
123 void set_format(Format format) { format_ = format; }
124 Format format() const { return format_; }
126 // A setter for channel_layout_ is intentionally excluded.
127 ChannelLayout channel_layout() const { return channel_layout_; }
129 // The number of channels is usually computed from channel_layout_. Setting
130 // this explictly is only required with CHANNEL_LAYOUT_DISCRETE.
131 void set_channels_for_discrete(int channels) {
132 DCHECK(channel_layout_ == CHANNEL_LAYOUT_DISCRETE ||
133 channels == ChannelLayoutToChannelCount(channel_layout_));
134 channels_ = channels;
136 int channels() const { return channels_; }
138 void set_sample_rate(int sample_rate) { sample_rate_ = sample_rate; }
139 int sample_rate() const { return sample_rate_; }
141 void set_bits_per_sample(int bits_per_sample) {
142 bits_per_sample_ = bits_per_sample;
144 int bits_per_sample() const { return bits_per_sample_; }
146 void set_frames_per_buffer(int frames_per_buffer) {
147 frames_per_buffer_ = frames_per_buffer;
149 int frames_per_buffer() const { return frames_per_buffer_; }
151 void set_effects(int effects) { effects_ = effects; }
152 int effects() const { return effects_; }
154 void set_mic_positions(const std::vector<Point>& mic_positions) {
155 mic_positions_ = mic_positions;
157 const std::vector<Point>& mic_positions() const { return mic_positions_; }
159 AudioParameters(const AudioParameters&);
160 AudioParameters& operator=(const AudioParameters&);
162 private:
163 Format format_; // Format of the stream.
164 ChannelLayout channel_layout_; // Order of surround sound channels.
165 int channels_; // Number of channels. Value set based on
166 // |channel_layout|.
167 int sample_rate_; // Sampling frequency/rate.
168 int bits_per_sample_; // Number of bits per sample.
169 int frames_per_buffer_; // Number of frames in a buffer.
170 int effects_; // Bitmask using PlatformEffectsMask.
172 // Microphone positions using Cartesian coordinates:
173 // x: the horizontal dimension, with positive to the right from the camera's
174 // perspective.
175 // y: the depth dimension, with positive forward from the camera's
176 // perspective.
177 // z: the vertical dimension, with positive upwards.
179 // Usually, the center of the microphone array will be treated as the origin
180 // (often the position of the camera).
182 // An empty vector indicates unknown positions.
183 std::vector<Point> mic_positions_;
186 // Comparison is useful when AudioParameters is used with std structures.
187 inline bool operator<(const AudioParameters& a, const AudioParameters& b) {
188 if (a.format() != b.format())
189 return a.format() < b.format();
190 if (a.channels() != b.channels())
191 return a.channels() < b.channels();
192 if (a.sample_rate() != b.sample_rate())
193 return a.sample_rate() < b.sample_rate();
194 if (a.bits_per_sample() != b.bits_per_sample())
195 return a.bits_per_sample() < b.bits_per_sample();
196 return a.frames_per_buffer() < b.frames_per_buffer();
199 } // namespace media
201 #endif // MEDIA_AUDIO_AUDIO_PARAMETERS_H_