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
, ChannelLayout channel_layout
,
84 int sample_rate
, int bits_per_sample
,
85 int frames_per_buffer
);
86 AudioParameters(Format format
, ChannelLayout channel_layout
,
87 int sample_rate
, int bits_per_sample
,
88 int frames_per_buffer
, int effects
);
89 AudioParameters(Format format
, ChannelLayout channel_layout
,
90 int channels
, int sample_rate
, int bits_per_sample
,
91 int frames_per_buffer
, int effects
);
93 void Reset(Format format
, ChannelLayout channel_layout
,
94 int channels
, int sample_rate
, int bits_per_sample
,
95 int frames_per_buffer
);
97 // Checks that all values are in the expected range. All limits are specified
101 // Returns a human-readable string describing |*this|. For debugging & test
103 std::string
AsHumanReadableString() const;
105 // Returns size of audio buffer in bytes.
106 int GetBytesPerBuffer() const;
108 // Returns the number of bytes representing one second of audio.
109 int GetBytesPerSecond() const;
111 // Returns the number of bytes representing a frame of audio.
112 int GetBytesPerFrame() const;
114 // Returns the duration of this buffer as calculated from frames_per_buffer()
115 // and sample_rate().
116 base::TimeDelta
GetBufferDuration() const;
118 // Comparison with other AudioParams.
119 bool Equals(const AudioParameters
& other
) const;
121 Format
format() const { return format_
; }
122 ChannelLayout
channel_layout() const { return channel_layout_
; }
123 int sample_rate() const { return sample_rate_
; }
124 int bits_per_sample() const { return bits_per_sample_
; }
125 int frames_per_buffer() const { return frames_per_buffer_
; }
126 int channels() const { return channels_
; }
127 int effects() const { return effects_
; }
130 // These members are mutable to support entire struct assignment. They should
131 // not be mutated individually.
132 Format format_
; // Format of the stream.
133 ChannelLayout channel_layout_
; // Order of surround sound channels.
134 int sample_rate_
; // Sampling frequency/rate.
135 int bits_per_sample_
; // Number of bits per sample.
136 int frames_per_buffer_
; // Number of frames in a buffer.
138 int channels_
; // Number of channels. Value set based on
140 int effects_
; // Bitmask using PlatformEffectsMask.
143 // Comparison is useful when AudioParameters is used with std structures.
144 inline bool operator<(const AudioParameters
& a
, const AudioParameters
& b
) {
145 if (a
.format() != b
.format())
146 return a
.format() < b
.format();
147 if (a
.channels() != b
.channels())
148 return a
.channels() < b
.channels();
149 if (a
.sample_rate() != b
.sample_rate())
150 return a
.sample_rate() < b
.sample_rate();
151 if (a
.bits_per_sample() != b
.bits_per_sample())
152 return a
.bits_per_sample() < b
.bits_per_sample();
153 return a
.frames_per_buffer() < b
.frames_per_buffer();
158 #endif // MEDIA_AUDIO_AUDIO_PARAMETERS_H_