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.
4
5 #include "media/audio/audio_parameters.h"
6
7 #include "base/logging.h"
8 #include "media/base/limits.h"
9
10 namespace media {
11
AudioParameters()12 AudioParameters::AudioParameters()
13 : format_(AUDIO_PCM_LINEAR),
14 channel_layout_(CHANNEL_LAYOUT_NONE),
15 sample_rate_(0),
16 bits_per_sample_(0),
17 frames_per_buffer_(0),
18 channels_(0),
19 effects_(NO_EFFECTS) {
20 }
21
AudioParameters(Format format,ChannelLayout channel_layout,int sample_rate,int bits_per_sample,int frames_per_buffer)22 AudioParameters::AudioParameters(Format format, ChannelLayout channel_layout,
23 int sample_rate, int bits_per_sample,
24 int frames_per_buffer)
25 : format_(format),
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) {
32 }
33
AudioParameters(Format format,ChannelLayout channel_layout,int sample_rate,int bits_per_sample,int frames_per_buffer,int effects)34 AudioParameters::AudioParameters(Format format, ChannelLayout channel_layout,
35 int sample_rate, int bits_per_sample,
36 int frames_per_buffer, int effects)
37 : format_(format),
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)),
43 effects_(effects) {
44 }
45
AudioParameters(Format format,ChannelLayout channel_layout,int channels,int sample_rate,int bits_per_sample,int frames_per_buffer,int effects)46 AudioParameters::AudioParameters(Format format, ChannelLayout channel_layout,
47 int channels, int sample_rate,
48 int bits_per_sample, int frames_per_buffer,
49 int effects)
50 : format_(format),
51 channel_layout_(channel_layout),
52 sample_rate_(sample_rate),
53 bits_per_sample_(bits_per_sample),
54 frames_per_buffer_(frames_per_buffer),
55 channels_(channels),
56 effects_(effects) {
57 if (channel_layout != CHANNEL_LAYOUT_DISCRETE)
58 DCHECK_EQ(channels, ChannelLayoutToChannelCount(channel_layout));
59 }
60
Reset(Format format,ChannelLayout channel_layout,int channels,int sample_rate,int bits_per_sample,int frames_per_buffer)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));
66
67 format_ = format;
68 channel_layout_ = channel_layout;
69 channels_ = channels;
70 sample_rate_ = sample_rate;
71 bits_per_sample_ = bits_per_sample;
72 frames_per_buffer_ = frames_per_buffer;
73 }
74
IsValid() const75 bool AudioParameters::IsValid() const {
76 return (format_ >= AUDIO_PCM_LINEAR) &&
77 (format_ < AUDIO_LAST_FORMAT) &&
78 (channels_ > 0) &&
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);
88 }
89
GetBytesPerBuffer() const90 int AudioParameters::GetBytesPerBuffer() const {
91 return frames_per_buffer_ * GetBytesPerFrame();
92 }
93
GetBytesPerSecond() const94 int AudioParameters::GetBytesPerSecond() const {
95 return sample_rate_ * GetBytesPerFrame();
96 }
97
GetBytesPerFrame() const98 int AudioParameters::GetBytesPerFrame() const {
99 return channels_ * bits_per_sample_ / 8;
100 }
101
GetBufferDuration() const102 base::TimeDelta AudioParameters::GetBufferDuration() const {
103 return base::TimeDelta::FromMicroseconds(
104 frames_per_buffer_ * base::Time::kMicrosecondsPerSecond /
105 static_cast<float>(sample_rate_));
106 }
107
Equals(const AudioParameters & other) const108 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();
116 }
117
118 } // namespace media
119