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 : AudioParameters(AUDIO_PCM_LINEAR
, CHANNEL_LAYOUT_NONE
, 0, 0, 0) {}
15 AudioParameters::AudioParameters(Format format
,
16 ChannelLayout channel_layout
,
19 int frames_per_buffer
) {
20 Reset(format
, channel_layout
, sample_rate
, bits_per_sample
,
24 AudioParameters::~AudioParameters() {}
26 AudioParameters::AudioParameters(const AudioParameters
&) = default;
27 AudioParameters
& AudioParameters::operator=(const AudioParameters
&) = default;
29 void AudioParameters::Reset(Format format
,
30 ChannelLayout channel_layout
,
33 int frames_per_buffer
) {
35 channel_layout_
= channel_layout
;
36 channels_
= ChannelLayoutToChannelCount(channel_layout
);
37 sample_rate_
= sample_rate
;
38 bits_per_sample_
= bits_per_sample
;
39 frames_per_buffer_
= frames_per_buffer
;
40 effects_
= NO_EFFECTS
;
41 mic_positions_
.clear();
44 bool AudioParameters::IsValid() const {
45 return (channels_
> 0) && (channels_
<= media::limits::kMaxChannels
) &&
46 (channel_layout_
> CHANNEL_LAYOUT_UNSUPPORTED
) &&
47 (sample_rate_
>= media::limits::kMinSampleRate
) &&
48 (sample_rate_
<= media::limits::kMaxSampleRate
) &&
49 (bits_per_sample_
> 0) &&
50 (bits_per_sample_
<= media::limits::kMaxBitsPerSample
) &&
51 (frames_per_buffer_
> 0) &&
52 (frames_per_buffer_
<= media::limits::kMaxSamplesPerPacket
) &&
53 (channel_layout_
== CHANNEL_LAYOUT_DISCRETE
||
54 channels_
== ChannelLayoutToChannelCount(channel_layout_
));
57 std::string
AudioParameters::AsHumanReadableString() const {
59 s
<< "format: " << format() << " channel_layout: " << channel_layout()
60 << " channels: " << channels() << " sample_rate: " << sample_rate()
61 << " bits_per_sample: " << bits_per_sample()
62 << " frames_per_buffer: " << frames_per_buffer()
63 << " effects: " << effects()
64 << " mic_positions: " << PointsToString(mic_positions_
);
68 int AudioParameters::GetBytesPerBuffer() const {
69 return frames_per_buffer_
* GetBytesPerFrame();
72 int AudioParameters::GetBytesPerSecond() const {
73 return sample_rate_
* GetBytesPerFrame();
76 int AudioParameters::GetBytesPerFrame() const {
77 return channels_
* bits_per_sample_
/ 8;
80 base::TimeDelta
AudioParameters::GetBufferDuration() const {
81 return base::TimeDelta::FromMicroseconds(static_cast<int64
>(
82 frames_per_buffer_
* base::Time::kMicrosecondsPerSecond
/
83 static_cast<float>(sample_rate_
)));
86 bool AudioParameters::Equals(const AudioParameters
& other
) const {
87 return format_
== other
.format() && sample_rate_
== other
.sample_rate() &&
88 channel_layout_
== other
.channel_layout() &&
89 channels_
== other
.channels() &&
90 bits_per_sample_
== other
.bits_per_sample() &&
91 frames_per_buffer_
== other
.frames_per_buffer() &&
92 effects_
== other
.effects() && mic_positions_
== other
.mic_positions_
;