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