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.
5 #ifndef MEDIA_AUDIO_AUDIO_PARAMETERS_H_
6 #define MEDIA_AUDIO_AUDIO_PARAMETERS_H_
10 #include "base/basictypes.h"
11 #include "base/time/time.h"
12 #include "media/base/channel_layout.h"
13 #include "media/base/media_export.h"
17 struct MEDIA_EXPORT AudioInputBufferParameters
{
23 // Use a struct-in-struct approach to ensure that we can calculate the required
24 // size as sizeof(AudioInputBufferParameters) + #(bytes in audio buffer) without
26 struct MEDIA_EXPORT AudioInputBuffer
{
27 AudioInputBufferParameters params
;
31 class MEDIA_EXPORT AudioParameters
{
33 // TODO(miu): Rename this enum to something that correctly reflects its
34 // semantics, such as "TransportScheme."
36 AUDIO_PCM_LINEAR
= 0, // PCM is 'raw' amplitude samples.
37 AUDIO_PCM_LOW_LATENCY
, // Linear PCM, low latency requested.
38 AUDIO_FAKE
, // Creates a fake AudioOutputStream object.
39 AUDIO_LAST_FORMAT
// Only used for validation of format.
43 // Telephone quality sample rate, mostly for speech-only audio.
44 kTelephoneSampleRate
= 8000,
45 // CD sampling rate is 44.1 KHz or conveniently 2x2x3x3x5x5x7x7.
46 kAudioCDSampleRate
= 44100,
49 // Bitmasks to determine whether certain platform (typically hardware) audio
50 // effects should be enabled.
51 enum PlatformEffectsMask
{
54 DUCKING
= 0x2, // Enables ducking if the OS supports it.
60 AudioParameters(Format format
, ChannelLayout channel_layout
,
61 int sample_rate
, int bits_per_sample
,
62 int frames_per_buffer
);
63 AudioParameters(Format format
, ChannelLayout channel_layout
,
64 int sample_rate
, int bits_per_sample
,
65 int frames_per_buffer
, int effects
);
66 AudioParameters(Format format
, ChannelLayout channel_layout
,
67 int channels
, int sample_rate
, int bits_per_sample
,
68 int frames_per_buffer
, int effects
);
70 void Reset(Format format
, ChannelLayout channel_layout
,
71 int channels
, int sample_rate
, int bits_per_sample
,
72 int frames_per_buffer
);
74 // Checks that all values are in the expected range. All limits are specified
78 // Returns a human-readable string describing |*this|. For debugging & test
80 std::string
AsHumanReadableString() const;
82 // Returns size of audio buffer in bytes.
83 int GetBytesPerBuffer() const;
85 // Returns the number of bytes representing one second of audio.
86 int GetBytesPerSecond() const;
88 // Returns the number of bytes representing a frame of audio.
89 int GetBytesPerFrame() const;
91 // Returns the duration of this buffer as calculated from frames_per_buffer()
93 base::TimeDelta
GetBufferDuration() const;
95 // Comparison with other AudioParams.
96 bool Equals(const AudioParameters
& other
) const;
98 Format
format() const { return format_
; }
99 ChannelLayout
channel_layout() const { return channel_layout_
; }
100 int sample_rate() const { return sample_rate_
; }
101 int bits_per_sample() const { return bits_per_sample_
; }
102 int frames_per_buffer() const { return frames_per_buffer_
; }
103 int channels() const { return channels_
; }
104 int effects() const { return effects_
; }
107 // These members are mutable to support entire struct assignment. They should
108 // not be mutated individually.
109 Format format_
; // Format of the stream.
110 ChannelLayout channel_layout_
; // Order of surround sound channels.
111 int sample_rate_
; // Sampling frequency/rate.
112 int bits_per_sample_
; // Number of bits per sample.
113 int frames_per_buffer_
; // Number of frames in a buffer.
115 int channels_
; // Number of channels. Value set based on
117 int effects_
; // Bitmask using PlatformEffectsMask.
120 // Comparison is useful when AudioParameters is used with std structures.
121 inline bool operator<(const AudioParameters
& a
, const AudioParameters
& b
) {
122 if (a
.format() != b
.format())
123 return a
.format() < b
.format();
124 if (a
.channels() != b
.channels())
125 return a
.channels() < b
.channels();
126 if (a
.sample_rate() != b
.sample_rate())
127 return a
.sample_rate() < b
.sample_rate();
128 if (a
.bits_per_sample() != b
.bits_per_sample())
129 return a
.bits_per_sample() < b
.bits_per_sample();
130 return a
.frames_per_buffer() < b
.frames_per_buffer();
135 #endif // MEDIA_AUDIO_AUDIO_PARAMETERS_H_