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),
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
)),
34 AudioParameters::AudioParameters(Format format
, ChannelLayout channel_layout
,
36 int sample_rate
, int bits_per_sample
,
37 int frames_per_buffer
)
39 channel_layout_(channel_layout
),
40 sample_rate_(sample_rate
),
41 bits_per_sample_(bits_per_sample
),
42 frames_per_buffer_(frames_per_buffer
),
43 channels_(ChannelLayoutToChannelCount(channel_layout
)),
44 input_channels_(input_channels
) {
47 void AudioParameters::Reset(Format format
, ChannelLayout channel_layout
,
48 int channels
, int input_channels
,
49 int sample_rate
, int bits_per_sample
,
50 int frames_per_buffer
) {
51 if (channel_layout
!= CHANNEL_LAYOUT_DISCRETE
)
52 DCHECK_EQ(channels
, ChannelLayoutToChannelCount(channel_layout
));
55 channel_layout_
= channel_layout
;
57 input_channels_
= input_channels
;
58 sample_rate_
= sample_rate
;
59 bits_per_sample_
= bits_per_sample
;
60 frames_per_buffer_
= frames_per_buffer
;
63 bool AudioParameters::IsValid() const {
64 return (format_
>= AUDIO_PCM_LINEAR
) &&
65 (format_
< AUDIO_LAST_FORMAT
) &&
67 (channels_
<= media::limits::kMaxChannels
) &&
68 (channel_layout_
> CHANNEL_LAYOUT_UNSUPPORTED
) &&
69 (channel_layout_
< CHANNEL_LAYOUT_MAX
) &&
70 (input_channels_
>= 0) &&
71 (input_channels_
<= media::limits::kMaxChannels
) &&
72 (sample_rate_
>= media::limits::kMinSampleRate
) &&
73 (sample_rate_
<= media::limits::kMaxSampleRate
) &&
74 (bits_per_sample_
> 0) &&
75 (bits_per_sample_
<= media::limits::kMaxBitsPerSample
) &&
76 (frames_per_buffer_
> 0) &&
77 (frames_per_buffer_
<= media::limits::kMaxSamplesPerPacket
);
80 int AudioParameters::GetBytesPerBuffer() const {
81 return frames_per_buffer_
* GetBytesPerFrame();
84 int AudioParameters::GetBytesPerSecond() const {
85 return sample_rate_
* GetBytesPerFrame();
88 int AudioParameters::GetBytesPerFrame() const {
89 return channels_
* bits_per_sample_
/ 8;
92 void AudioParameters::SetDiscreteChannels(int channels
) {
93 channel_layout_
= CHANNEL_LAYOUT_DISCRETE
;