Home | History | Annotate | Download | only in audio
      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 
     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       input_channels_(0),
     20       effects_(NO_EFFECTS) {
     21 }
     22 
     23 AudioParameters::AudioParameters(Format format, ChannelLayout channel_layout,
     24                                  int sample_rate, int bits_per_sample,
     25                                  int frames_per_buffer)
     26     : format_(format),
     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)),
     32       input_channels_(0),
     33       effects_(NO_EFFECTS) {
     34 }
     35 
     36 AudioParameters::AudioParameters(Format format, ChannelLayout channel_layout,
     37                                  int input_channels,
     38                                  int sample_rate, int bits_per_sample,
     39                                  int frames_per_buffer, int effects)
     40     : format_(format),
     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),
     47       effects_(effects) {
     48 }
     49 
     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)
     54     : format_(format),
     55       channel_layout_(channel_layout),
     56       sample_rate_(sample_rate),
     57       bits_per_sample_(bits_per_sample),
     58       frames_per_buffer_(frames_per_buffer),
     59       channels_(channels),
     60       input_channels_(input_channels),
     61       effects_(effects) {
     62   if (channel_layout != CHANNEL_LAYOUT_DISCRETE)
     63     DCHECK_EQ(channels, ChannelLayoutToChannelCount(channel_layout));
     64 }
     65 
     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));
     72 
     73   format_ = format;
     74   channel_layout_ = channel_layout;
     75   channels_ = channels;
     76   input_channels_ = input_channels;
     77   sample_rate_ = sample_rate;
     78   bits_per_sample_ = bits_per_sample;
     79   frames_per_buffer_ = frames_per_buffer;
     80 }
     81 
     82 bool AudioParameters::IsValid() const {
     83   return (format_ >= AUDIO_PCM_LINEAR) &&
     84          (format_ < AUDIO_LAST_FORMAT) &&
     85          (channels_ > 0) &&
     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);
     97 }
     98 
     99 int AudioParameters::GetBytesPerBuffer() const {
    100   return frames_per_buffer_ * GetBytesPerFrame();
    101 }
    102 
    103 int AudioParameters::GetBytesPerSecond() const {
    104   return sample_rate_ * GetBytesPerFrame();
    105 }
    106 
    107 int AudioParameters::GetBytesPerFrame() const {
    108   return channels_ * bits_per_sample_ / 8;
    109 }
    110 
    111 base::TimeDelta AudioParameters::GetBufferDuration() const {
    112   return base::TimeDelta::FromMicroseconds(
    113       frames_per_buffer_ * base::Time::kMicrosecondsPerSecond /
    114       static_cast<float>(sample_rate_));
    115 }
    116 
    117 }  // namespace media
    118