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_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/time/time.h"
14 #include "media/base/audio_bus.h"
15 #include "media/base/channel_layout.h"
16 #include "media/base/media_export.h"
20 // Use a struct-in-struct approach to ensure that we can calculate the required
21 // size as sizeof(AudioInputBufferParameters) + #(bytes in audio buffer) without
22 // using packing. Also align AudioInputBufferParameters instead of in
23 // AudioInputBuffer to be able to calculate size like so. Use a macro for the
24 // alignment value that's the same as AudioBus::kChannelAlignment, since MSVC
25 // doesn't accept the latter to be used.
28 #pragma warning(disable: 4324) // Disable warning for added padding.
30 #define PARAMETERS_ALIGNMENT 16
31 COMPILE_ASSERT(AudioBus::kChannelAlignment
== PARAMETERS_ALIGNMENT
,
32 AudioInputBufferParameters_alignment_not_same_as_AudioBus
);
33 struct MEDIA_EXPORT
ALIGNAS(PARAMETERS_ALIGNMENT
) AudioInputBufferParameters
{
36 uint32_t hardware_delay_bytes
;
40 #undef PARAMETERS_ALIGNMENT
46 sizeof(AudioInputBufferParameters
) % AudioBus::kChannelAlignment
== 0,
47 AudioInputBufferParameters_not_aligned
);
49 struct MEDIA_EXPORT AudioInputBuffer
{
50 AudioInputBufferParameters params
;
54 class MEDIA_EXPORT AudioParameters
{
56 // TODO(miu): Rename this enum to something that correctly reflects its
57 // semantics, such as "TransportScheme."
59 AUDIO_PCM_LINEAR
= 0, // PCM is 'raw' amplitude samples.
60 AUDIO_PCM_LOW_LATENCY
, // Linear PCM, low latency requested.
61 AUDIO_FAKE
, // Creates a fake AudioOutputStream object.
62 AUDIO_FORMAT_LAST
= AUDIO_FAKE
, // Only used for validation of format.
66 // Telephone quality sample rate, mostly for speech-only audio.
67 kTelephoneSampleRate
= 8000,
68 // CD sampling rate is 44.1 KHz or conveniently 2x2x3x3x5x5x7x7.
69 kAudioCDSampleRate
= 44100,
72 // Bitmasks to determine whether certain platform (typically hardware) audio
73 // effects should be enabled.
74 enum PlatformEffectsMask
{
77 DUCKING
= 0x2, // Enables ducking if the OS supports it.
83 AudioParameters(Format format
,
84 ChannelLayout channel_layout
,
87 int frames_per_buffer
);
89 // Re-initializes all members.
90 void Reset(Format format
,
91 ChannelLayout channel_layout
,
94 int frames_per_buffer
);
96 // Checks that all values are in the expected range. All limits are specified
100 // Returns a human-readable string describing |*this|. For debugging & test
102 std::string
AsHumanReadableString() const;
104 // Returns size of audio buffer in bytes.
105 int GetBytesPerBuffer() const;
107 // Returns the number of bytes representing one second of audio.
108 int GetBytesPerSecond() const;
110 // Returns the number of bytes representing a frame of audio.
111 int GetBytesPerFrame() const;
113 // Returns the duration of this buffer as calculated from frames_per_buffer()
114 // and sample_rate().
115 base::TimeDelta
GetBufferDuration() const;
117 // Comparison with other AudioParams.
118 bool Equals(const AudioParameters
& other
) const;
120 void set_format(Format format
) { format_
= format
; }
121 Format
format() const { return format_
; }
123 // A setter for channel_layout_ is intentionally excluded.
124 ChannelLayout
channel_layout() const { return channel_layout_
; }
126 // The number of channels is usually computed from channel_layout_. Setting
127 // this explictly is only required with CHANNEL_LAYOUT_DISCRETE.
128 void set_channels_for_discrete(int channels
) {
129 DCHECK(channel_layout_
== CHANNEL_LAYOUT_DISCRETE
||
130 channels
== ChannelLayoutToChannelCount(channel_layout_
));
131 channels_
= channels
;
133 int channels() const { return channels_
; }
135 void set_sample_rate(int sample_rate
) { sample_rate_
= sample_rate
; }
136 int sample_rate() const { return sample_rate_
; }
138 void set_bits_per_sample(int bits_per_sample
) {
139 bits_per_sample_
= bits_per_sample
;
141 int bits_per_sample() const { return bits_per_sample_
; }
143 void set_frames_per_buffer(int frames_per_buffer
) {
144 frames_per_buffer_
= frames_per_buffer
;
146 int frames_per_buffer() const { return frames_per_buffer_
; }
148 void set_effects(int effects
) { effects_
= effects
; }
149 int effects() const { return effects_
; }
151 AudioParameters(const AudioParameters
&) = default;
152 AudioParameters
& operator=(const AudioParameters
&) = default;
155 Format format_
; // Format of the stream.
156 ChannelLayout channel_layout_
; // Order of surround sound channels.
157 int channels_
; // Number of channels. Value set based on
159 int sample_rate_
; // Sampling frequency/rate.
160 int bits_per_sample_
; // Number of bits per sample.
161 int frames_per_buffer_
; // Number of frames in a buffer.
162 int effects_
; // Bitmask using PlatformEffectsMask.
165 // Comparison is useful when AudioParameters is used with std structures.
166 inline bool operator<(const AudioParameters
& a
, const AudioParameters
& b
) {
167 if (a
.format() != b
.format())
168 return a
.format() < b
.format();
169 if (a
.channels() != b
.channels())
170 return a
.channels() < b
.channels();
171 if (a
.sample_rate() != b
.sample_rate())
172 return a
.sample_rate() < b
.sample_rate();
173 if (a
.bits_per_sample() != b
.bits_per_sample())
174 return a
.bits_per_sample() < b
.bits_per_sample();
175 return a
.frames_per_buffer() < b
.frames_per_buffer();
180 #endif // MEDIA_AUDIO_AUDIO_PARAMETERS_H_