Roll src/third_party/WebKit fc1d527:f73ea91 (svn 188133:188166)
[chromium-blink-merge.git] / media / audio / audio_parameters.h
blob9267232acf9a5705f35004766450021178b1e1c3
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 <string>
10 #include "base/basictypes.h"
11 #include "base/time/time.h"
12 #include "media/base/channel_layout.h"
13 #include "media/base/media_export.h"
15 namespace media {
17 struct MEDIA_EXPORT AudioInputBufferParameters {
18 double volume;
19 uint32 size;
20 bool key_pressed;
23 // Use a struct-in-struct approach to ensure that we can calculate the required
24 // size as sizeof(AudioInputBufferParameters) + #(bytes in audio buffer) without
25 // using packing.
26 struct MEDIA_EXPORT AudioInputBuffer {
27 AudioInputBufferParameters params;
28 int8 audio[1];
31 class MEDIA_EXPORT AudioParameters {
32 public:
33 // TODO(miu): Rename this enum to something that correctly reflects its
34 // semantics, such as "TransportScheme."
35 enum Format {
36 AUDIO_PCM_LINEAR = 0, // PCM is 'raw' amplitude samples.
37 AUDIO_PCM_LOW_LATENCY, // Linear PCM, low latency requested.
38 AUDIO_FAKE, // Creates a fake AudioOutputStream object.
39 AUDIO_LAST_FORMAT // Only used for validation of format.
42 enum {
43 // Telephone quality sample rate, mostly for speech-only audio.
44 kTelephoneSampleRate = 8000,
45 // CD sampling rate is 44.1 KHz or conveniently 2x2x3x3x5x5x7x7.
46 kAudioCDSampleRate = 44100,
49 // Bitmasks to determine whether certain platform (typically hardware) audio
50 // effects should be enabled.
51 enum PlatformEffectsMask {
52 NO_EFFECTS = 0x0,
53 ECHO_CANCELLER = 0x1,
54 DUCKING = 0x2, // Enables ducking if the OS supports it.
55 KEYBOARD_MIC = 0x4,
56 HOTWORD = 0x8,
59 AudioParameters();
60 AudioParameters(Format format, ChannelLayout channel_layout,
61 int sample_rate, int bits_per_sample,
62 int frames_per_buffer);
63 AudioParameters(Format format, ChannelLayout channel_layout,
64 int sample_rate, int bits_per_sample,
65 int frames_per_buffer, int effects);
66 AudioParameters(Format format, ChannelLayout channel_layout,
67 int channels, int sample_rate, int bits_per_sample,
68 int frames_per_buffer, int effects);
70 void Reset(Format format, ChannelLayout channel_layout,
71 int channels, int sample_rate, int bits_per_sample,
72 int frames_per_buffer);
74 // Checks that all values are in the expected range. All limits are specified
75 // in media::Limits.
76 bool IsValid() const;
78 // Returns a human-readable string describing |*this|. For debugging & test
79 // output only.
80 std::string AsHumanReadableString() const;
82 // Returns size of audio buffer in bytes.
83 int GetBytesPerBuffer() const;
85 // Returns the number of bytes representing one second of audio.
86 int GetBytesPerSecond() const;
88 // Returns the number of bytes representing a frame of audio.
89 int GetBytesPerFrame() const;
91 // Returns the duration of this buffer as calculated from frames_per_buffer()
92 // and sample_rate().
93 base::TimeDelta GetBufferDuration() const;
95 // Comparison with other AudioParams.
96 bool Equals(const AudioParameters& other) const;
98 Format format() const { return format_; }
99 ChannelLayout channel_layout() const { return channel_layout_; }
100 int sample_rate() const { return sample_rate_; }
101 int bits_per_sample() const { return bits_per_sample_; }
102 int frames_per_buffer() const { return frames_per_buffer_; }
103 int channels() const { return channels_; }
104 int effects() const { return effects_; }
106 private:
107 // These members are mutable to support entire struct assignment. They should
108 // not be mutated individually.
109 Format format_; // Format of the stream.
110 ChannelLayout channel_layout_; // Order of surround sound channels.
111 int sample_rate_; // Sampling frequency/rate.
112 int bits_per_sample_; // Number of bits per sample.
113 int frames_per_buffer_; // Number of frames in a buffer.
115 int channels_; // Number of channels. Value set based on
116 // |channel_layout|.
117 int effects_; // Bitmask using PlatformEffectsMask.
120 // Comparison is useful when AudioParameters is used with std structures.
121 inline bool operator<(const AudioParameters& a, const AudioParameters& b) {
122 if (a.format() != b.format())
123 return a.format() < b.format();
124 if (a.channels() != b.channels())
125 return a.channels() < b.channels();
126 if (a.sample_rate() != b.sample_rate())
127 return a.sample_rate() < b.sample_rate();
128 if (a.bits_per_sample() != b.bits_per_sample())
129 return a.bits_per_sample() < b.bits_per_sample();
130 return a.frames_per_buffer() < b.frames_per_buffer();
133 } // namespace media
135 #endif // MEDIA_AUDIO_AUDIO_PARAMETERS_H_