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 int AudioParameters::GetBytesPerBuffer() const {
91 return frames_per_buffer_
* GetBytesPerFrame();
94 int AudioParameters::GetBytesPerSecond() const {
95 return sample_rate_
* GetBytesPerFrame();
98 int AudioParameters::GetBytesPerFrame() const {
99 return channels_
* bits_per_sample_
/ 8;
102 base::TimeDelta
AudioParameters::GetBufferDuration() const {
103 return base::TimeDelta::FromMicroseconds(
104 frames_per_buffer_
* base::Time::kMicrosecondsPerSecond
/
105 static_cast<float>(sample_rate_
));
108 bool AudioParameters::Equals(const AudioParameters
& other
) const {
109 return format_
== other
.format() &&
110 sample_rate_
== other
.sample_rate() &&
111 channel_layout_
== other
.channel_layout() &&
112 channels_
== other
.channels() &&
113 bits_per_sample_
== other
.bits_per_sample() &&
114 frames_per_buffer_
== other
.frames_per_buffer() &&
115 effects_
== other
.effects();