audio_parameters.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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
12namespace media {
13
14struct 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.
22struct MEDIA_EXPORT AudioInputBuffer {
23  AudioInputBufferParameters params;
24  int8 audio[1];
25};
26
27class 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 private:
83  Format format_;                 // Format of the stream.
84  ChannelLayout channel_layout_;  // Order of surround sound channels.
85  int sample_rate_;               // Sampling frequency/rate.
86  int bits_per_sample_;           // Number of bits per sample.
87  int frames_per_buffer_;         // Number of frames in a buffer.
88
89  int channels_;                  // Number of channels. Value set based on
90                                  // |channel_layout|.
91  int input_channels_;            // Optional number of input channels.
92                                  // Normally 0, but can be set to specify
93                                  // synchronized I/O.
94};
95
96// Comparison is useful when AudioParameters is used with std structures.
97inline bool operator<(const AudioParameters& a, const AudioParameters& b) {
98  if (a.format() != b.format())
99    return a.format() < b.format();
100  if (a.channels() != b.channels())
101    return a.channels() < b.channels();
102  if (a.input_channels() != b.input_channels())
103    return a.input_channels() < b.input_channels();
104  if (a.sample_rate() != b.sample_rate())
105    return a.sample_rate() < b.sample_rate();
106  if (a.bits_per_sample() != b.bits_per_sample())
107    return a.bits_per_sample() < b.bits_per_sample();
108  return a.frames_per_buffer() < b.frames_per_buffer();
109}
110
111}  // namespace media
112
113#endif  // MEDIA_AUDIO_AUDIO_PARAMETERS_H_
114