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
),
59 void AudioParameters::Reset(Format format
, ChannelLayout channel_layout
,
60 int channels
, int sample_rate
,
61 int bits_per_sample
, int frames_per_buffer
) {
63 channel_layout_
= channel_layout
;
65 sample_rate_
= sample_rate
;
66 bits_per_sample_
= bits_per_sample
;
67 frames_per_buffer_
= frames_per_buffer
;
70 bool AudioParameters::IsValid() const {
71 return (format_
>= AUDIO_PCM_LINEAR
) &&
72 (format_
< AUDIO_LAST_FORMAT
) &&
74 (channels_
<= media::limits::kMaxChannels
) &&
75 (channel_layout_
> CHANNEL_LAYOUT_UNSUPPORTED
) &&
76 (channel_layout_
<= CHANNEL_LAYOUT_MAX
) &&
77 (sample_rate_
>= media::limits::kMinSampleRate
) &&
78 (sample_rate_
<= media::limits::kMaxSampleRate
) &&
79 (bits_per_sample_
> 0) &&
80 (bits_per_sample_
<= media::limits::kMaxBitsPerSample
) &&
81 (frames_per_buffer_
> 0) &&
82 (frames_per_buffer_
<= media::limits::kMaxSamplesPerPacket
) &&
83 (channel_layout_
== CHANNEL_LAYOUT_DISCRETE
||
84 channels_
== ChannelLayoutToChannelCount(channel_layout_
));
87 std::string
AudioParameters::AsHumanReadableString() const {
89 s
<< "format: " << format()
90 << " channels: " << channels()
91 << " channel_layout: " << channel_layout()
92 << " sample_rate: " << sample_rate()
93 << " bits_per_sample: " << bits_per_sample()
94 << " frames_per_buffer: " << frames_per_buffer();
98 int AudioParameters::GetBytesPerBuffer() const {
99 return frames_per_buffer_
* GetBytesPerFrame();
102 int AudioParameters::GetBytesPerSecond() const {
103 return sample_rate_
* GetBytesPerFrame();
106 int AudioParameters::GetBytesPerFrame() const {
107 return channels_
* bits_per_sample_
/ 8;
110 base::TimeDelta
AudioParameters::GetBufferDuration() const {
111 return base::TimeDelta::FromMicroseconds(static_cast<int64
>(
112 frames_per_buffer_
* base::Time::kMicrosecondsPerSecond
/
113 static_cast<float>(sample_rate_
)));
116 bool AudioParameters::Equals(const AudioParameters
& other
) const {
117 return format_
== other
.format() &&
118 sample_rate_
== other
.sample_rate() &&
119 channel_layout_
== other
.channel_layout() &&
120 channels_
== other
.channels() &&
121 bits_per_sample_
== other
.bits_per_sample() &&
122 frames_per_buffer_
== other
.frames_per_buffer() &&
123 effects_
== other
.effects();