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 #ifndef MEDIA_AUDIO_AUDIO_PARAMETERS_H_
      6 #define MEDIA_AUDIO_AUDIO_PARAMETERS_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "media/base/channel_layout.h"
     10 #include "media/base/media_export.h"
     11 
     12 namespace media {
     13 
     14 struct MEDIA_EXPORT AudioInputBufferParameters {
     15   double volume;
     16   uint32 size;
     17 };
     18 
     19 // Use a struct-in-struct approach to ensure that we can calculate the required
     20 // size as sizeof(AudioInputBufferParameters) + #(bytes in audio buffer) without
     21 // using packing.
     22 struct MEDIA_EXPORT AudioInputBuffer {
     23   AudioInputBufferParameters params;
     24   int8 audio[1];
     25 };
     26 
     27 class MEDIA_EXPORT AudioParameters {
     28  public:
     29   // TODO(miu): Rename this enum to something that correctly reflects its
     30   // semantics, such as "TransportScheme."
     31   enum Format {
     32     AUDIO_PCM_LINEAR = 0,     // PCM is 'raw' amplitude samples.
     33     AUDIO_PCM_LOW_LATENCY,    // Linear PCM, low latency requested.
     34     AUDIO_FAKE,               // Creates a fake AudioOutputStream object.
     35     AUDIO_LAST_FORMAT         // Only used for validation of format.
     36   };
     37 
     38   enum {
     39     // Telephone quality sample rate, mostly for speech-only audio.
     40     kTelephoneSampleRate = 8000,
     41     // CD sampling rate is 44.1 KHz or conveniently 2x2x3x3x5x5x7x7.
     42     kAudioCDSampleRate = 44100,
     43   };
     44 
     45   AudioParameters();
     46   AudioParameters(Format format, ChannelLayout channel_layout,
     47                   int sample_rate, int bits_per_sample,
     48                   int frames_per_buffer);
     49   AudioParameters(Format format, ChannelLayout channel_layout,
     50                   int input_channels,
     51                   int sample_rate, int bits_per_sample,
     52                   int frames_per_buffer);
     53   void Reset(Format format, ChannelLayout channel_layout,
     54              int channels, int input_channels,
     55              int sample_rate, int bits_per_sample,
     56              int frames_per_buffer);
     57 
     58   // Checks that all values are in the expected range. All limits are specified
     59   // in media::Limits.
     60   bool IsValid() const;
     61 
     62   // Returns size of audio buffer in bytes.
     63   int GetBytesPerBuffer() const;
     64 
     65   // Returns the number of bytes representing one second of audio.
     66   int GetBytesPerSecond() const;
     67 
     68   // Returns the number of bytes representing a frame of audio.
     69   int GetBytesPerFrame() const;
     70 
     71   Format format() const { return format_; }
     72   ChannelLayout channel_layout() const { return channel_layout_; }
     73   int sample_rate() const { return sample_rate_; }
     74   int bits_per_sample() const { return bits_per_sample_; }
     75   int frames_per_buffer() const { return frames_per_buffer_; }
     76   int channels() const { return channels_; }
     77   int input_channels() const { return input_channels_; }
     78 
     79   // Set to CHANNEL_LAYOUT_DISCRETE with given number of channels.
     80   void SetDiscreteChannels(int channels);
     81 
     82   // Comparison with other AudioParams.
     83   bool operator==(const AudioParameters& other) const {
     84     return format_ == other.format() &&
     85            sample_rate_ == other.sample_rate() &&
     86            channel_layout_ == other.channel_layout() &&
     87            channels_ == other.channels() &&
     88            input_channels_ == other.input_channels() &&
     89            bits_per_sample_ == other.bits_per_sample() &&
     90            frames_per_buffer_ == other.frames_per_buffer();
     91   }
     92 
     93  private:
     94   Format format_;                 // Format of the stream.
     95   ChannelLayout channel_layout_;  // Order of surround sound channels.
     96   int sample_rate_;               // Sampling frequency/rate.
     97   int bits_per_sample_;           // Number of bits per sample.
     98   int frames_per_buffer_;         // Number of frames in a buffer.
     99 
    100   int channels_;                  // Number of channels. Value set based on
    101                                   // |channel_layout|.
    102   int input_channels_;            // Optional number of input channels.
    103                                   // Normally 0, but can be set to specify
    104                                   // synchronized I/O.
    105 };
    106 
    107 // Comparison is useful when AudioParameters is used with std structures.
    108 inline bool operator<(const AudioParameters& a, const AudioParameters& b) {
    109   if (a.format() != b.format())
    110     return a.format() < b.format();
    111   if (a.channels() != b.channels())
    112     return a.channels() < b.channels();
    113   if (a.input_channels() != b.input_channels())
    114     return a.input_channels() < b.input_channels();
    115   if (a.sample_rate() != b.sample_rate())
    116     return a.sample_rate() < b.sample_rate();
    117   if (a.bits_per_sample() != b.bits_per_sample())
    118     return a.bits_per_sample() < b.bits_per_sample();
    119   return a.frames_per_buffer() < b.frames_per_buffer();
    120 }
    121 
    122 }  // namespace media
    123 
    124 #endif  // MEDIA_AUDIO_AUDIO_PARAMETERS_H_
    125