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),
20 effects_(NO_EFFECTS
) {
23 AudioParameters::AudioParameters(Format format
, ChannelLayout channel_layout
,
24 int sample_rate
, int bits_per_sample
,
25 int frames_per_buffer
)
27 channel_layout_(channel_layout
),
28 sample_rate_(sample_rate
),
29 bits_per_sample_(bits_per_sample
),
30 frames_per_buffer_(frames_per_buffer
),
31 channels_(ChannelLayoutToChannelCount(channel_layout
)),
33 effects_(NO_EFFECTS
) {
36 AudioParameters::AudioParameters(Format format
, ChannelLayout channel_layout
,
38 int sample_rate
, int bits_per_sample
,
39 int frames_per_buffer
, int effects
)
41 channel_layout_(channel_layout
),
42 sample_rate_(sample_rate
),
43 bits_per_sample_(bits_per_sample
),
44 frames_per_buffer_(frames_per_buffer
),
45 channels_(ChannelLayoutToChannelCount(channel_layout
)),
46 input_channels_(input_channels
),
50 AudioParameters::AudioParameters(Format format
, ChannelLayout channel_layout
,
51 int channels
, int input_channels
,
52 int sample_rate
, int bits_per_sample
,
53 int frames_per_buffer
, int effects
)
55 channel_layout_(channel_layout
),
56 sample_rate_(sample_rate
),
57 bits_per_sample_(bits_per_sample
),
58 frames_per_buffer_(frames_per_buffer
),
60 input_channels_(input_channels
),
62 if (channel_layout
!= CHANNEL_LAYOUT_DISCRETE
)
63 DCHECK_EQ(channels
, ChannelLayoutToChannelCount(channel_layout
));
66 void AudioParameters::Reset(Format format
, ChannelLayout channel_layout
,
67 int channels
, int input_channels
,
68 int sample_rate
, int bits_per_sample
,
69 int frames_per_buffer
) {
70 if (channel_layout
!= CHANNEL_LAYOUT_DISCRETE
)
71 DCHECK_EQ(channels
, ChannelLayoutToChannelCount(channel_layout
));
74 channel_layout_
= channel_layout
;
76 input_channels_
= input_channels
;
77 sample_rate_
= sample_rate
;
78 bits_per_sample_
= bits_per_sample
;
79 frames_per_buffer_
= frames_per_buffer
;
82 bool AudioParameters::IsValid() const {
83 return (format_
>= AUDIO_PCM_LINEAR
) &&
84 (format_
< AUDIO_LAST_FORMAT
) &&
86 (channels_
<= media::limits::kMaxChannels
) &&
87 (channel_layout_
> CHANNEL_LAYOUT_UNSUPPORTED
) &&
88 (channel_layout_
<= CHANNEL_LAYOUT_MAX
) &&
89 (input_channels_
>= 0) &&
90 (input_channels_
<= media::limits::kMaxChannels
) &&
91 (sample_rate_
>= media::limits::kMinSampleRate
) &&
92 (sample_rate_
<= media::limits::kMaxSampleRate
) &&
93 (bits_per_sample_
> 0) &&
94 (bits_per_sample_
<= media::limits::kMaxBitsPerSample
) &&
95 (frames_per_buffer_
> 0) &&
96 (frames_per_buffer_
<= media::limits::kMaxSamplesPerPacket
);
99 int AudioParameters::GetBytesPerBuffer() const {
100 return frames_per_buffer_
* GetBytesPerFrame();
103 int AudioParameters::GetBytesPerSecond() const {
104 return sample_rate_
* GetBytesPerFrame();
107 int AudioParameters::GetBytesPerFrame() const {
108 return channels_
* bits_per_sample_
/ 8;
111 base::TimeDelta
AudioParameters::GetBufferDuration() const {
112 return base::TimeDelta::FromMicroseconds(
113 frames_per_buffer_
* base::Time::kMicrosecondsPerSecond
/
114 static_cast<float>(sample_rate_
));