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 #include "media/audio/audio_parameters.h"
7 #include "base/logging.h"
8 #include "media/base/limits.h"
12 AudioParameters::AudioParameters()
13 : format_(AUDIO_PCM_LINEAR
),
14 channel_layout_(CHANNEL_LAYOUT_NONE
),
17 frames_per_buffer_(0),
19 effects_(NO_EFFECTS
) {
22 AudioParameters::AudioParameters(Format format
, ChannelLayout channel_layout
,
23 int sample_rate
, int bits_per_sample
,
24 int frames_per_buffer
)
26 channel_layout_(channel_layout
),
27 sample_rate_(sample_rate
),
28 bits_per_sample_(bits_per_sample
),
29 frames_per_buffer_(frames_per_buffer
),
30 channels_(ChannelLayoutToChannelCount(channel_layout
)),
31 effects_(NO_EFFECTS
) {
34 AudioParameters::AudioParameters(Format format
, ChannelLayout channel_layout
,
35 int sample_rate
, int bits_per_sample
,
36 int frames_per_buffer
, int effects
)
38 channel_layout_(channel_layout
),
39 sample_rate_(sample_rate
),
40 bits_per_sample_(bits_per_sample
),
41 frames_per_buffer_(frames_per_buffer
),
42 channels_(ChannelLayoutToChannelCount(channel_layout
)),
46 AudioParameters::AudioParameters(Format format
, ChannelLayout channel_layout
,
47 int channels
, int sample_rate
,
48 int bits_per_sample
, int frames_per_buffer
,
51 channel_layout_(channel_layout
),
52 sample_rate_(sample_rate
),
53 bits_per_sample_(bits_per_sample
),
54 frames_per_buffer_(frames_per_buffer
),
57 if (channel_layout
!= CHANNEL_LAYOUT_DISCRETE
)
58 DCHECK_EQ(channels
, ChannelLayoutToChannelCount(channel_layout
));
61 void AudioParameters::Reset(Format format
, ChannelLayout channel_layout
,
62 int channels
, int sample_rate
,
63 int bits_per_sample
, int frames_per_buffer
) {
64 if (channel_layout
!= CHANNEL_LAYOUT_DISCRETE
)
65 DCHECK_EQ(channels
, ChannelLayoutToChannelCount(channel_layout
));
68 channel_layout_
= channel_layout
;
70 sample_rate_
= sample_rate
;
71 bits_per_sample_
= bits_per_sample
;
72 frames_per_buffer_
= frames_per_buffer
;
75 bool AudioParameters::IsValid() const {
76 return (format_
>= AUDIO_PCM_LINEAR
) &&
77 (format_
< AUDIO_LAST_FORMAT
) &&
79 (channels_
<= media::limits::kMaxChannels
) &&
80 (channel_layout_
> CHANNEL_LAYOUT_UNSUPPORTED
) &&
81 (channel_layout_
<= CHANNEL_LAYOUT_MAX
) &&
82 (sample_rate_
>= media::limits::kMinSampleRate
) &&
83 (sample_rate_
<= media::limits::kMaxSampleRate
) &&
84 (bits_per_sample_
> 0) &&
85 (bits_per_sample_
<= media::limits::kMaxBitsPerSample
) &&
86 (frames_per_buffer_
> 0) &&
87 (frames_per_buffer_
<= media::limits::kMaxSamplesPerPacket
);
90 std::string
AudioParameters::AsHumanReadableString() const {
92 s
<< "format: " << format()
93 << " channels: " << channels()
94 << " channel_layout: " << channel_layout()
95 << " sample_rate: " << sample_rate()
96 << " bits_per_sample: " << bits_per_sample()
97 << " frames_per_buffer: " << frames_per_buffer();
101 int AudioParameters::GetBytesPerBuffer() const {
102 return frames_per_buffer_
* GetBytesPerFrame();
105 int AudioParameters::GetBytesPerSecond() const {
106 return sample_rate_
* GetBytesPerFrame();
109 int AudioParameters::GetBytesPerFrame() const {
110 return channels_
* bits_per_sample_
/ 8;
113 base::TimeDelta
AudioParameters::GetBufferDuration() const {
114 return base::TimeDelta::FromMicroseconds(static_cast<int64
>(
115 frames_per_buffer_
* base::Time::kMicrosecondsPerSecond
/
116 static_cast<float>(sample_rate_
)));
119 bool AudioParameters::Equals(const AudioParameters
& other
) const {
120 return format_
== other
.format() &&
121 sample_rate_
== other
.sample_rate() &&
122 channel_layout_
== other
.channel_layout() &&
123 channels_
== other
.channels() &&
124 bits_per_sample_
== other
.bits_per_sample() &&
125 frames_per_buffer_
== other
.frames_per_buffer() &&
126 effects_
== other
.effects();